Skip to content

Commit

Permalink
Refactored stereo delay
Browse files Browse the repository at this point in the history
  • Loading branch information
steeelydan committed Oct 5, 2023
1 parent 5111d9b commit ad59940
Show file tree
Hide file tree
Showing 6 changed files with 253 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ myVar__S2 = myArr__D?ä__DENY_COMPILATION__2;
expect(result.errors[0].type).to.equal('TypeError');
});


it('error if property access performed on wrong type: with just one dimension', () => {
const compiler = new Js2EelCompiler();

Expand Down Expand Up @@ -369,6 +368,55 @@ buf__size = 2;
myVar2__S2 = buf__B1[1];
`)
);
expect(result.errors.length).to.equal(0);
});

it('Array access with eachChannel() ch param as position accessor', () => {
const compiler = new Js2EelCompiler();

const result =
compiler.compile(`config({ description: 'member_expression_computed', inChannels: 2, outChannels: 2 });
const buf = new EelArray(2, 2);
onSample(() => {
eachChannel((_sample, channel) => {
const myConst = buf[0][channel];
});
});
`);

expect(result.success).to.equal(true);
expect(testEelSrc(result.src)).to.equal(
testEelSrc(`/* Compiled with JS2EEL v0.9.1 */
desc:member_expression_computed
in_pin:In 0
in_pin:In 1
out_pin:In 0
out_pin:In 1
@sample
/* Channel 0 */
CH__0 = 0;
myConst__S4 = buf__D0__0;
/* Channel 1 */
CH__1 = 1;
myConst__S5 = buf__D0__1;
`)
);
expect(result.errors.length).to.equal(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,36 @@ out_pin:In 1
outChannels: 2
});
const someObj = { one: function myFunc() {} };
`);

expect(result.success).to.equal(false);
expect(testEelSrc(result.src)).to.equal(
testEelSrc(`/* Compiled with JS2EEL v0.7.0 */
desc:object_expression
in_pin:In 0
in_pin:In 1
out_pin:In 0
out_pin:In 1
`)
);
expect(result.errors.length).to.equal(1);
expect(result.errors[0].type).to.equal('TypeError');
});

it('literal value of wrong type', () => {
const compiler = new Js2EelCompiler();

const result = compiler.compile(`config({
description: 'object_expression',
inChannels: 2,
outChannels: 2
});
const someObj = { one: 'somestring' };
`);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,40 @@ export const objectExpression = (
return null;
}

if (
property.value.type !== 'Literal' ||
typeof property.value.value !== 'number' ||
property.value.value === undefined
) {
instance.error(
'TypeError',
`Object property value must be number literal. ${property.value.type} not allowed`,
property.value
);
switch (property.value.type) {
case 'Literal': {
if (
typeof property.value.value !== 'number' ||
property.value.value === undefined
) {
instance.error(
'TypeError',
`Literal object property value must be number. ${typeof property.value
.value} not allowed`,
property.value
);

return null;
}
return null;
}

object[property.key.name] = property.value.value;
eelSrc += `${objectIdentifier.name}__${suffixScopeByScopeSuffix(
property.key.name,
instance.getCurrentScopeSuffix()
)} = ${property.value.value};\n`;

object[property.key.name] = property.value.value;
eelSrc += `${objectIdentifier.name}__${suffixScopeByScopeSuffix(
property.key.name,
instance.getCurrentScopeSuffix()
)} = ${property.value.value};\n`;
break;
}
default: {
instance.error(
'TypeError',
`Object property value is wrong type. ${property.value.type} not allowed`,
property.value
);

return null;
}
}
}

return { objectRepresentation: object, eelSrc: eelSrc };
Expand Down
66 changes: 32 additions & 34 deletions compiler/src/js2eel/test/examplePlugins/04_stereoDelay.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ const JS_STEREO_DELAY_SRC = fs.readFileSync(
'utf-8'
);

const EEL_STEREO_DELAY_SRC_EXPECTED = `/* Compiled with JS2EEL v0.0.15 */
const EEL_STEREO_DELAY_SRC_EXPECTED = `/* Compiled with JS2EEL v0.9.1 */
desc:stereo_delay
slider1:lengthMsL=120 < 0, 2000, 1 >Delay L / Mono (ms)
slider2:lengthMsR=120 < 0, 2000, 1 >Delay R (ms)
slider3:feedbackPercent=0 < 0, 100, 0.1 >Feedback (%)
slider4:mixDb=-6 < -120, 6, 0.01 >Mix (dB)
slider2:lengthMsL=120 < 0, 2000, 1 >Delay L / Mono (ms)
slider3:lengthMsR=120 < 0, 2000, 1 >Delay R (ms)
slider4:feedbackPercent=0 < 0, 100, 0.1 >Feedback (%)
slider5:mixDb=-6 < -120, 6, 0.01 >Mix (dB)
slider1:type=0 < 0, 3, 1 {Mono, Stereo, Ping Pong} >Type
in_pin:In 0
in_pin:In 1
Expand All @@ -26,50 +28,46 @@ out_pin:In 1
@init
numSamples__L = 0;
numSamples__R = 0;
bufferPos__L = 0;
bufferPos__R = 0;
buffer__B0 = 0 * 400000;
buffer__B1 = 1 * 400000;
buffer__size = 400000;
@slider
numSamples__D0__0 = lengthMsL * srate / (1000);
numSamples__D0__1 = lengthMsR * srate / (1000);
feedback = feedbackPercent / (100);
numSamples__L = lengthMsL * srate / (1000);
numSamples__R = lengthMsR * srate / (1000);
(type == 0 || type == 2) ? (
lengthMsR = lengthMsL;
);
mix = 2 ^ (mixDb / (6));
@sample
/* Channel 0 */
CH__0 = 0;
bufferValue__S6 = buffer__B0[bufferPos__D0__0];
delayVal__S6 = min((spl0 + bufferValue__S6 * feedback), 1);
currentBufPos__S6 = bufferPos__D0__0;
buffer__B0[currentBufPos__S6] = delayVal__S6;
bufferPos__D0__0 = (currentBufPos__S6 + 1);
bufferPos__D0__0 >= numSamples__D0__0 ? (
bufferPos__D0__0 = 0;
bufferValueL__S5 = buffer__B0[bufferPos__L];
bufferValueR__S5 = buffer__B1[bufferPos__R];
type == 2 ? (
buffer__B1[bufferPos__R] = min((spl0 + bufferValueL__S5 * feedback), 1);
buffer__B0[bufferPos__L] = min((spl1 + bufferValueR__S5 * feedback), 1);
) : (buffer__B0[bufferPos__L] = min((spl0 + bufferValueL__S5 * feedback), 1);
buffer__B1[bufferPos__R] = min((spl1 + bufferValueR__S5 * feedback), 1);
);
spl0 = (spl0 + bufferValue__S6 * mix);
/* Channel 1 */
CH__1 = 1;
bufferValue__S8 = buffer__B1[bufferPos__D0__1];
delayVal__S8 = min((spl1 + bufferValue__S8 * feedback), 1);
currentBufPos__S8 = bufferPos__D0__1;
buffer__B1[currentBufPos__S8] = delayVal__S8;
bufferPos__D0__1 = (currentBufPos__S8 + 1);
bufferPos__D0__1 >= numSamples__D0__1 ? (
bufferPos__D0__1 = 0;
bufferPos__L += 1;
bufferPos__R += 1;
bufferPos__L >= numSamples__L ? (
bufferPos__L = 0;
);
spl1 = (spl1 + bufferValue__S8 * mix);
bufferPos__R >= numSamples__R ? (
bufferPos__R = 0;
);
spl0 = (spl0 + bufferValueL__S5 * mix);
spl1 = (spl1 + bufferValueR__S5 * mix);
`;
Expand Down
77 changes: 55 additions & 22 deletions examples/04_stereo_delay.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
config({ description: 'stereo_delay', inChannels: 2, outChannels: 2 });

let type;
let lengthMsL;
let lengthMsR;
let mixDb;
Expand All @@ -8,35 +9,67 @@ let feedbackPercent;
let mix;

const buffer = new EelBuffer(2, 400000);
const numSamples = new EelArray(1, 2);
const bufferPos = new EelArray(1, 2);

slider(1, lengthMsL, 120, 0, 2000, 1, 'Delay L / Mono (ms)');
slider(2, lengthMsR, 120, 0, 2000, 1, 'Delay R (ms)');
slider(3, feedbackPercent, 0, 0, 100, 0.1, 'Feedback (%)');
slider(4, mixDb, -6, -120, 6, 0.01, 'Mix (dB)');
const numSamples = {
L: 0,
R: 0
};
const bufferPos = {
L: 0,
R: 0
};

onSlider(() => {
numSamples[0][0] = (lengthMsL * srate) / 1000;
numSamples[0][1] = (lengthMsR * srate) / 1000;
selectBox(
1,
type,
'mono',
[
{ name: 'mono', label: 'Mono' },
{ name: 'stereo', label: 'Stereo' },
{ name: 'pingpong', label: 'Ping Pong' }
],
'Type'
);
slider(2, lengthMsL, 120, 0, 2000, 1, 'Delay L / Mono (ms)');
slider(3, lengthMsR, 120, 0, 2000, 1, 'Delay R (ms)');
slider(4, feedbackPercent, 0, 0, 100, 0.1, 'Feedback (%)');
slider(5, mixDb, -6, -120, 6, 0.01, 'Mix (dB)');

onSlider(() => {
feedback = feedbackPercent / 100;

numSamples.L = (lengthMsL * srate) / 1000;
numSamples.R = (lengthMsR * srate) / 1000;

if (type === 'mono' || type === 'pingpong') {
lengthMsR = lengthMsL;
}

mix = Math.pow(2, mixDb / 6);
});

onSample(() => {
eachChannel((sample, ch) => {
const bufferValue = buffer[ch][bufferPos[0][ch]];
const delayVal = min(sample + bufferValue * feedback, 1);
const currentBufPos = bufferPos[0][ch];
buffer[ch][currentBufPos] = delayVal;
bufferPos[0][ch] = currentBufPos + 1;

if (bufferPos[0][ch] >= numSamples[0][ch]) {
bufferPos[0][ch] = 0;
}

sample = sample + bufferValue * mix;
});
const bufferValueL = buffer[0][bufferPos.L];
const bufferValueR = buffer[1][bufferPos.R];

if (type === 'pingpong') {
buffer[1][bufferPos.R] = min(spl0 + bufferValueL * feedback, 1);
buffer[0][bufferPos.L] = min(spl1 + bufferValueR * feedback, 1);
} else {
buffer[0][bufferPos.L] = min(spl0 + bufferValueL * feedback, 1);
buffer[1][bufferPos.R] = min(spl1 + bufferValueR * feedback, 1);
}

bufferPos.L += 1;
bufferPos.R += 1;

if (bufferPos.L >= numSamples.L) {
bufferPos.L = 0;
}
if (bufferPos.R >= numSamples.R) {
bufferPos.R = 0;
}

spl0 = spl0 + bufferValueL * mix;
spl1 = spl1 + bufferValueR * mix;
});
Loading

0 comments on commit ad59940

Please sign in to comment.