Skip to content

Commit bc2684d

Browse files
committed
Pass all the necessary references to generated synth functions so that they do not need to rely on global scope naming conventions.
1 parent b50dfed commit bc2684d

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

synth/synth.ts

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6092,7 +6092,9 @@ export class Synth {
60926092

60936093
//console.log(synthSource.join("\n"));
60946094

6095-
Synth.fmSynthFunctionCache[fingerprint] = new Function("synth", "bufferIndex", "runLength", "tone", "instrument", synthSource.join("\n"));
6095+
const wrappedFmSynth: string = "return (synth, bufferIndex, runLength, tone, instrument) => {" + synthSource.join("\n") + "}";
6096+
6097+
Synth.fmSynthFunctionCache[fingerprint] = new Function("Config", "Synth", wrappedFmSynth)(Config, Synth);
60966098
}
60976099
return Synth.fmSynthFunctionCache[fingerprint];
60986100
} else if (instrument.type == InstrumentType.chip) {
@@ -6285,11 +6287,9 @@ export class Synth {
62856287
const voiceCount: number = instrumentState.unison!.voices;
62866288
let pickedStringFunction: Function = Synth.pickedStringFunctionCache[voiceCount];
62876289
if (pickedStringFunction == undefined) {
6288-
let pickedStringSource: string = "";
6290+
let pickedStringSource: string = "return (synth, bufferIndex, runLength, tone, instrumentState) => {";
62896291

62906292
pickedStringSource += `
6291-
const Config = beepbox.Config;
6292-
const Synth = beepbox.Synth;
62936293
const data = synth.tempMonoInstrumentSampleBuffer;
62946294
62956295
let pickedString# = tone.pickedStrings[#];
@@ -6409,7 +6409,8 @@ export class Synth {
64096409
64106410
synth.sanitizeFilters(filters);
64116411
tone.initialNoteFilterInput1 = initialFilterInput1;
6412-
tone.initialNoteFilterInput2 = initialFilterInput2;`
6412+
tone.initialNoteFilterInput2 = initialFilterInput2;
6413+
}`
64136414

64146415
// Duplicate lines containing "#" for each voice and replace the "#" with the voice index.
64156416
pickedStringSource = pickedStringSource.replace(/^.*\#.*$/mg, line => {
@@ -6421,7 +6422,7 @@ export class Synth {
64216422
});
64226423

64236424
//console.log(pickedStringSource);
6424-
pickedStringFunction = new Function("synth", "bufferIndex", "runLength", "tone", "instrumentState", pickedStringSource);
6425+
pickedStringFunction = new Function("Config", "Synth", pickedStringSource)(Config, Synth);
64256426
Synth.pickedStringFunctionCache[voiceCount] = pickedStringFunction;
64266427
}
64276428

@@ -6447,12 +6448,11 @@ export class Synth {
64476448

64486449
let effectsFunction: Function = Synth.effectsFunctionCache[signature];
64496450
if (effectsFunction == undefined) {
6450-
let effectsSource: string = "";
6451+
let effectsSource: string = "return (synth, outputDataL, outputDataR, bufferIndex, runLength, instrumentState) => {";
64516452

64526453
const usesDelays: boolean = usesChorus || usesReverb || usesEcho;
64536454

64546455
effectsSource += `
6455-
const Config = beepbox.Config;
64566456
const tempMonoInstrumentSampleBuffer = synth.tempMonoInstrumentSampleBuffer;
64576457
64586458
let mixVolume = +instrumentState.mixVolume;
@@ -6528,7 +6528,7 @@ export class Synth {
65286528
const filterCount = instrumentState.eqFilterCount|0;
65296529
let initialFilterInput1 = +instrumentState.initialEqFilterInput1;
65306530
let initialFilterInput2 = +instrumentState.initialEqFilterInput2;
6531-
const applyFilters = beepbox.Synth.applyFilters;`
6531+
const applyFilters = Synth.applyFilters;`
65326532
}
65336533

65346534
// The eq filter volume is also used to fade out the instrument state, so always include it.
@@ -6932,7 +6932,7 @@ export class Synth {
69326932
if (usesPanning) {
69336933
effectsSource += `
69346934
6935-
beepbox.Synth.sanitizeDelayLine(panningDelayLine, panningDelayPos, panningMask);
6935+
Synth.sanitizeDelayLine(panningDelayLine, panningDelayPos, panningMask);
69366936
instrumentState.panningDelayPos = panningDelayPos;
69376937
instrumentState.panningVolumeL = panningVolumeL;
69386938
instrumentState.panningVolumeR = panningVolumeR;
@@ -6943,8 +6943,8 @@ export class Synth {
69436943
if (usesChorus) {
69446944
effectsSource += `
69456945
6946-
beepbox.Synth.sanitizeDelayLine(chorusDelayLineL, chorusDelayPos, chorusMask);
6947-
beepbox.Synth.sanitizeDelayLine(chorusDelayLineR, chorusDelayPos, chorusMask);
6946+
Synth.sanitizeDelayLine(chorusDelayLineL, chorusDelayPos, chorusMask);
6947+
Synth.sanitizeDelayLine(chorusDelayLineR, chorusDelayPos, chorusMask);
69486948
instrumentState.chorusPhase = chorusPhase;
69496949
instrumentState.chorusDelayPos = chorusDelayPos;
69506950
instrumentState.chorusVoiceMult = chorusVoiceMult;
@@ -6954,8 +6954,8 @@ export class Synth {
69546954
if (usesEcho) {
69556955
effectsSource += `
69566956
6957-
beepbox.Synth.sanitizeDelayLine(echoDelayLineL, echoDelayPos, echoMask);
6958-
beepbox.Synth.sanitizeDelayLine(echoDelayLineR, echoDelayPos, echoMask);
6957+
Synth.sanitizeDelayLine(echoDelayLineL, echoDelayPos, echoMask);
6958+
Synth.sanitizeDelayLine(echoDelayLineR, echoDelayPos, echoMask);
69596959
instrumentState.echoDelayPos = echoDelayPos;
69606960
instrumentState.echoMult = echoMult;
69616961
instrumentState.echoDelayOffsetRatio = echoDelayOffsetRatio;
@@ -6973,10 +6973,10 @@ export class Synth {
69736973
if (usesReverb) {
69746974
effectsSource += `
69756975
6976-
beepbox.Synth.sanitizeDelayLine(reverbDelayLine, reverbDelayPos , reverbMask);
6977-
beepbox.Synth.sanitizeDelayLine(reverbDelayLine, reverbDelayPos + 3041, reverbMask);
6978-
beepbox.Synth.sanitizeDelayLine(reverbDelayLine, reverbDelayPos + 6426, reverbMask);
6979-
beepbox.Synth.sanitizeDelayLine(reverbDelayLine, reverbDelayPos + 10907, reverbMask);
6976+
Synth.sanitizeDelayLine(reverbDelayLine, reverbDelayPos , reverbMask);
6977+
Synth.sanitizeDelayLine(reverbDelayLine, reverbDelayPos + 3041, reverbMask);
6978+
Synth.sanitizeDelayLine(reverbDelayLine, reverbDelayPos + 6426, reverbMask);
6979+
Synth.sanitizeDelayLine(reverbDelayLine, reverbDelayPos + 10907, reverbMask);
69806980
instrumentState.reverbDelayPos = reverbDelayPos;
69816981
instrumentState.reverbMult = reverb;
69826982
@@ -6998,8 +6998,10 @@ export class Synth {
69986998
instrumentState.reverbShelfPrevInput3 = reverbShelfPrevInput3;`
69996999
}
70007000

7001+
effectsSource += "}";
7002+
70017003
//console.log(effectsSource);
7002-
effectsFunction = new Function("synth", "outputDataL", "outputDataR", "bufferIndex", "runLength", "instrumentState", effectsSource);
7004+
effectsFunction = new Function("Config", "Synth", effectsSource)(Config, Synth);
70037005
Synth.effectsFunctionCache[signature] = effectsFunction;
70047006
}
70057007

@@ -7075,7 +7077,7 @@ export class Synth {
70757077

70767078
private static fmSourceTemplate: string[] = (`
70777079
const data = synth.tempMonoInstrumentSampleBuffer;
7078-
const sineWave = beepbox.Config.sineWave;
7080+
const sineWave = Config.sineWave;
70797081
70807082
// I'm adding 1000 to the phase to ensure that it's never negative even when modulated by other waves because negative numbers don't work with the modulus operator very well.
70817083
let operator#Phase = +((tone.phases[#] % 1) + 1000) * ` + Config.sineWaveLength + `;
@@ -7093,7 +7095,7 @@ export class Synth {
70937095
const filterCount = tone.noteFilterCount|0;
70947096
let initialFilterInput1 = +tone.initialNoteFilterInput1;
70957097
let initialFilterInput2 = +tone.initialNoteFilterInput2;
7096-
const applyFilters = beepbox.Synth.applyFilters;
7098+
const applyFilters = Synth.applyFilters;
70977099
70987100
const stopIndex = bufferIndex + runLength;
70997101
for (let sampleIndex = bufferIndex; sampleIndex < stopIndex; sampleIndex++) {

0 commit comments

Comments
 (0)