Can't create buffers in array in JS (v8)

Roald Baudoux's icon

Hello,I am trying to create arrays of buffers within JS (v8).I have buffers named "start" and "end" in my patcher, both containing a sound, and I want to make some copies of these buffers (only in JS, not in the patch) . Before performing the copies I am trying to dimension the copies according to the original buffers.This code doesn't function. channelcount and framecount performed on any of the new buffers both return -1. I suspect the issue is the way I create the buffers or the array itself. What's wrong?

let hybrid_number = 5; // number of copies
let hybrid_start_buffers = []; // to contain copies of "start"
let hybrid_end_buffers = []; // to contain copies of "end"

// create buffers and copy original samples
function create_hybrid_buffers() {
	let start_buffer = new Buffer("start"); 
	let start_dur_samps = start_buffer.framecount()
	let start_chans = start_buffer.channelcount()
	let end_buffer = new Buffer("end"); 
	let end_dur_samps = end_buffer.framecount()
	let end_chans = end_buffer.channelcount()

// creation of new buffers
for (let i = 0; i < hybrid_number; i++) {
	let my_start_buffer = new Buffer();
	hybrid_start_buffers.push(my_start_buffer);
	let my_end_buffer = new Buffer();
	hybrid_end_buffers.push(my_end_buffer);
	my_start_buffer.send("sizeinsamps", start_dur_samps, start_chans);
	my_end_buffer.send("sizeinsamps", end_dur_samps, end_chans);
}

TFL's icon

I might be mistaken, but it seems that the js Buffer object can only bind to existing [buffer~] objects in the patch. As said in the doc: "The Buffer object in JavaScript is a companion to the buffer~ object in Max", and when you create a new Buffer(), you need to provide a name (the one of the max object buffer).

It sadly doesn't work like Dict and I don't know why there is this limitation, but that's probably it.

So you'll probably need to create those [buffer~] objects in the Max patch (could be programmatically directly in the js code) before accessing them through their Buffer js companion.

Roald Baudoux's icon

This confirms what I feared but allright! Thank you TFL.