Something went wrong. Try again.
Monorepo for Aesthetic.Computer aesthetic.computer
Something went wrong. Try again.
JavaScript
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970/** * Max/MSP JavaScript for batching audio samples * Receives packed [L, R] sample pairs, batches them, and outputs as arrays * * Inlets: * 0: list of [left, right] sample pair * * Outlets: * 0: executejavascript message with batched samples */
// Configurationvar BATCH_SIZE = 64; // Sample pairs per batch (~64ms at 1kHz metro)
// Statevar batchL = [];var batchR = [];
// Declare inlets/outletsinlets = 1;outlets = 1;
function list(l, r) { // Receive packed [L, R] pair batchL.push(l); batchR.push(r); // When batch is full, send it if (batchL.length >= BATCH_SIZE) { sendBatch(); }}
function sendBatch() { if (batchL.length === 0) return; // Take samples from buffers var left = batchL.splice(0, BATCH_SIZE); var right = batchR.splice(0, BATCH_SIZE); // Format as JSON array strings (compact) var leftStr = "[" + left.join(",") + "]"; var rightStr = "[" + right.join(",") + "]"; // Output executejavascript command outlet(0, "executejavascript", "window.acSampleBatch&&window.acSampleBatch(" + leftStr + "," + rightStr + ")");}
// Reset buffersfunction reset() { batchL = []; batchR = [];}
// Change batch sizefunction batchsize(n) { if (n > 0 && n <= 4096) { BATCH_SIZE = n; reset(); post("Batch size set to " + n + "\n"); }}
// Flush any remaining samplesfunction bang() { if (batchL.length > 0) { sendBatch(); }}