Skip to content

Commit

Permalink
Testing more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
julianstorer committed Mar 11, 2024
1 parent 699737e commit 2802f2b
Show file tree
Hide file tree
Showing 50 changed files with 8,698 additions and 270 deletions.
47 changes: 38 additions & 9 deletions assets/example_patches/808/cmaj_808.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,26 +312,55 @@ class TR808

/** Copies frames from the output stream "out" into a destination array.
*
* @param {Array} destChannelArrays - An array of arrays (one per channel) into
* which the samples will be copied
* @param {Array} destChannelArrays - An array of arrays (one per channel) into
* which the samples will be copied
* @param {number} maxNumFramesToRead - The maximum number of frames to copy
* @param {number} destChannel - The channel to start writing from
*/
getOutputFrames_out (destChannelArrays, maxNumFramesToRead, destChannel)
getOutputFrames_out (destChannelArrays, maxNumFramesToRead)
{
let source = 1631264;
let numDestChans = destChannelArrays.length;

if (maxNumFramesToRead > 512)
maxNumFramesToRead = 512;

const channelsToCopy = Math.min (1, destChannelArrays.length - destChannel);
if (numDestChans < 1)
{
for (let frame = 0; frame < maxNumFramesToRead; ++frame)
{
for (let channel = 0; channel < numDestChans; ++channel)
destChannelArrays[channel][frame] = this.memoryDataView.getFloat32 (source + 4 * channel, true);

source += 4;
}
}
else if (numDestChans > 1)
{
for (let frame = 0; frame < maxNumFramesToRead; ++frame)
{
let lastSample;

for (let frame = 0; frame < maxNumFramesToRead; ++frame)
for (let channel = 0; channel < 1; ++channel)
{
lastSample = this.memoryDataView.getFloat32 (source + 4 * channel, true);
destChannelArrays[channel][frame] = lastSample;
}

for (let channel = 1; channel < numDestChans; ++channel)
destChannelArrays[channel][frame] = lastSample;

source += 4;
}
}
else
{
for (let channel = 0; channel < channelsToCopy; ++channel)
destChannelArrays[destChannel + channel][frame] = this.memoryDataView.getFloat32 (source + 4 * channel, true);
for (let frame = 0; frame < maxNumFramesToRead; ++frame)
{
for (let channel = 0; channel < 1; ++channel)
destChannelArrays[channel][frame] = this.memoryDataView.getFloat32 (source + 4 * channel, true);

source += 4;
source += 4;
}
}
}

Expand Down
36 changes: 12 additions & 24 deletions assets/example_patches/808/cmaj_api/cmaj_audio_worklet_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,27 +104,14 @@ function registerWorkletProcessor (workletName, WrapperClass)
if (endpoints.length === 0)
return () => {};

var handlers = [];
var targetChannels = [];

var channelCount = 0;

for (const endpoint of endpoints)
{
const handleFrames = wrapper[`${wrapperMethodNamePrefix}_${endpoint.endpointID}`]?.bind (wrapper);
if (! handleFrames)
return () => {};

handlers.push (handleFrames);
targetChannels.push (channelCount);
channelCount += endpoint.numAudioChannels;
}
// N.B. we just take the first for now (and do the same when creating the node).
// we can do better, and should probably align with something similar to what the patch player does
const first = endpoints[0];
const handleFrames = wrapper[`${wrapperMethodNamePrefix}_${first.endpointID}`]?.bind (wrapper);
if (! handleFrames)
return () => {};

return (channels, blockSize) =>
{
for (var i = 0; i < handlers.length; i++)
handlers[i] (channels, blockSize, targetChannels[i]);
}
return (channels, blockSize) => handleFrames (channels, blockSize);
}

function makeInputStreamEndpointHandler (wrapper)
Expand Down Expand Up @@ -527,11 +514,12 @@ export class AudioWorkletPatchConnection extends PatchConnection
const audioInputEndpoints = this.inputEndpoints.filter (({ purpose }) => purpose === "audio in");
const audioOutputEndpoints = this.outputEndpoints.filter (({ purpose }) => purpose === "audio out");

var inputChannelCount = 0;
var outputChannelCount = 0;
// N.B. we just take the first for now (and do the same in the processor too).
// we can do better, and should probably align with something similar to what the patch player does
const pickFirstEndpointChannelCount = (endpoints) => endpoints.length ? endpoints[0].numAudioChannels : 0;

audioInputEndpoints.forEach ((endpoint) => { inputChannelCount = inputChannelCount + endpoint.numAudioChannels; });
audioOutputEndpoints.forEach ((endpoint) => { outputChannelCount = outputChannelCount + endpoint.numAudioChannels; });
const inputChannelCount = pickFirstEndpointChannelCount (audioInputEndpoints);
const outputChannelCount = pickFirstEndpointChannelCount (audioOutputEndpoints);

const hasInput = inputChannelCount > 0;
const hasOutput = outputChannelCount > 0;
Expand Down
47 changes: 38 additions & 9 deletions assets/example_patches/ElectricPiano/cmaj_Electric_Piano.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -104,27 +104,14 @@ function registerWorkletProcessor (workletName, WrapperClass)
if (endpoints.length === 0)
return () => {};

var handlers = [];
var targetChannels = [];

var channelCount = 0;

for (const endpoint of endpoints)
{
const handleFrames = wrapper[`${wrapperMethodNamePrefix}_${endpoint.endpointID}`]?.bind (wrapper);
if (! handleFrames)
return () => {};

handlers.push (handleFrames);
targetChannels.push (channelCount);
channelCount += endpoint.numAudioChannels;
}
// N.B. we just take the first for now (and do the same when creating the node).
// we can do better, and should probably align with something similar to what the patch player does
const first = endpoints[0];
const handleFrames = wrapper[`${wrapperMethodNamePrefix}_${first.endpointID}`]?.bind (wrapper);
if (! handleFrames)
return () => {};

return (channels, blockSize) =>
{
for (var i = 0; i < handlers.length; i++)
handlers[i] (channels, blockSize, targetChannels[i]);
}
return (channels, blockSize) => handleFrames (channels, blockSize);
}

function makeInputStreamEndpointHandler (wrapper)
Expand Down Expand Up @@ -527,11 +514,12 @@ export class AudioWorkletPatchConnection extends PatchConnection
const audioInputEndpoints = this.inputEndpoints.filter (({ purpose }) => purpose === "audio in");
const audioOutputEndpoints = this.outputEndpoints.filter (({ purpose }) => purpose === "audio out");

var inputChannelCount = 0;
var outputChannelCount = 0;
// N.B. we just take the first for now (and do the same in the processor too).
// we can do better, and should probably align with something similar to what the patch player does
const pickFirstEndpointChannelCount = (endpoints) => endpoints.length ? endpoints[0].numAudioChannels : 0;

audioInputEndpoints.forEach ((endpoint) => { inputChannelCount = inputChannelCount + endpoint.numAudioChannels; });
audioOutputEndpoints.forEach ((endpoint) => { outputChannelCount = outputChannelCount + endpoint.numAudioChannels; });
const inputChannelCount = pickFirstEndpointChannelCount (audioInputEndpoints);
const outputChannelCount = pickFirstEndpointChannelCount (audioOutputEndpoints);

const hasInput = inputChannelCount > 0;
const hasOutput = outputChannelCount > 0;
Expand Down
47 changes: 38 additions & 9 deletions assets/example_patches/HelloWorld/cmaj_Hello_World.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,26 +197,55 @@ class HelloWorld

/** Copies frames from the output stream "out" into a destination array.
*
* @param {Array} destChannelArrays - An array of arrays (one per channel) into
* which the samples will be copied
* @param {Array} destChannelArrays - An array of arrays (one per channel) into
* which the samples will be copied
* @param {number} maxNumFramesToRead - The maximum number of frames to copy
* @param {number} destChannel - The channel to start writing from
*/
getOutputFrames_out (destChannelArrays, maxNumFramesToRead, destChannel)
getOutputFrames_out (destChannelArrays, maxNumFramesToRead)
{
let source = 71744;
let numDestChans = destChannelArrays.length;

if (maxNumFramesToRead > 512)
maxNumFramesToRead = 512;

const channelsToCopy = Math.min (1, destChannelArrays.length - destChannel);
if (numDestChans < 1)
{
for (let frame = 0; frame < maxNumFramesToRead; ++frame)
{
for (let channel = 0; channel < numDestChans; ++channel)
destChannelArrays[channel][frame] = this.memoryDataView.getFloat32 (source + 4 * channel, true);

source += 4;
}
}
else if (numDestChans > 1)
{
for (let frame = 0; frame < maxNumFramesToRead; ++frame)
{
let lastSample;

for (let frame = 0; frame < maxNumFramesToRead; ++frame)
for (let channel = 0; channel < 1; ++channel)
{
lastSample = this.memoryDataView.getFloat32 (source + 4 * channel, true);
destChannelArrays[channel][frame] = lastSample;
}

for (let channel = 1; channel < numDestChans; ++channel)
destChannelArrays[channel][frame] = lastSample;

source += 4;
}
}
else
{
for (let channel = 0; channel < channelsToCopy; ++channel)
destChannelArrays[destChannel + channel][frame] = this.memoryDataView.getFloat32 (source + 4 * channel, true);
for (let frame = 0; frame < maxNumFramesToRead; ++frame)
{
for (let channel = 0; channel < 1; ++channel)
destChannelArrays[channel][frame] = this.memoryDataView.getFloat32 (source + 4 * channel, true);

source += 4;
source += 4;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,27 +104,14 @@ function registerWorkletProcessor (workletName, WrapperClass)
if (endpoints.length === 0)
return () => {};

var handlers = [];
var targetChannels = [];

var channelCount = 0;

for (const endpoint of endpoints)
{
const handleFrames = wrapper[`${wrapperMethodNamePrefix}_${endpoint.endpointID}`]?.bind (wrapper);
if (! handleFrames)
return () => {};

handlers.push (handleFrames);
targetChannels.push (channelCount);
channelCount += endpoint.numAudioChannels;
}
// N.B. we just take the first for now (and do the same when creating the node).
// we can do better, and should probably align with something similar to what the patch player does
const first = endpoints[0];
const handleFrames = wrapper[`${wrapperMethodNamePrefix}_${first.endpointID}`]?.bind (wrapper);
if (! handleFrames)
return () => {};

return (channels, blockSize) =>
{
for (var i = 0; i < handlers.length; i++)
handlers[i] (channels, blockSize, targetChannels[i]);
}
return (channels, blockSize) => handleFrames (channels, blockSize);
}

function makeInputStreamEndpointHandler (wrapper)
Expand Down Expand Up @@ -527,11 +514,12 @@ export class AudioWorkletPatchConnection extends PatchConnection
const audioInputEndpoints = this.inputEndpoints.filter (({ purpose }) => purpose === "audio in");
const audioOutputEndpoints = this.outputEndpoints.filter (({ purpose }) => purpose === "audio out");

var inputChannelCount = 0;
var outputChannelCount = 0;
// N.B. we just take the first for now (and do the same in the processor too).
// we can do better, and should probably align with something similar to what the patch player does
const pickFirstEndpointChannelCount = (endpoints) => endpoints.length ? endpoints[0].numAudioChannels : 0;

audioInputEndpoints.forEach ((endpoint) => { inputChannelCount = inputChannelCount + endpoint.numAudioChannels; });
audioOutputEndpoints.forEach ((endpoint) => { outputChannelCount = outputChannelCount + endpoint.numAudioChannels; });
const inputChannelCount = pickFirstEndpointChannelCount (audioInputEndpoints);
const outputChannelCount = pickFirstEndpointChannelCount (audioOutputEndpoints);

const hasInput = inputChannelCount > 0;
const hasOutput = outputChannelCount > 0;
Expand Down
Loading

0 comments on commit 2802f2b

Please sign in to comment.