From a608b083f322fc50a9e2a91227ab91bf78ea7007 Mon Sep 17 00:00:00 2001 From: Kieran McAuliffe Date: Wed, 25 Sep 2024 15:59:27 +0200 Subject: [PATCH] working sine interpolation --- lfogui.js | 18 ++++++++++------- modulators.js | 56 ++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 62 insertions(+), 12 deletions(-) diff --git a/lfogui.js b/lfogui.js index 7dc65bc..9b34d80 100644 --- a/lfogui.js +++ b/lfogui.js @@ -66,10 +66,14 @@ function MasterLfoHandler(){ const [minArr, setMinArr] = React.useState(Array(MAXLFOS).fill('0')); const [maxArr, setMaxArr] = React.useState(Array(MAXLFOS).fill('1')); - const [phaseArr, setPhaseArr] = React.useState(Array(MAXLFOS).fill('0')); + const [initPhaseArr, setInitPhaseArr] = React.useState(Array(MAXLFOS).fill('0')); + const [lastPhaseArr, setLastPhaseArr] = React.useState(Array(MAXLFOS).fill(0)); + const [cachedNoiseValueArr, setCachedNoiseValueArr] = React.useState(Array(MAXLFOS).fill([0, 0])); - const allModArrays = [modVisibleArr, modTypeArr, modInstanceNumArr, shapeArr, noiseTypeArr, djParamArr, freqArr, minArr, maxArr, phaseArr]; - const allModSetters = [setModVisibleArr, setModTypeArr, setModInstanceNumArr, setShapeArr, setNoiseTypeArr, setDjParamArr, setFreqArr, setMinArr, setMaxArr, setPhaseArr]; + + + const allModArrays = [modVisibleArr, modTypeArr, modInstanceNumArr, shapeArr, noiseTypeArr, djParamArr, freqArr, minArr, maxArr, initPhaseArr]; + const allModSetters = [setModVisibleArr, setModTypeArr, setModInstanceNumArr, setShapeArr, setNoiseTypeArr, setDjParamArr, setFreqArr, setMinArr, setMaxArr, setInitPhaseArr]; const modBlankVals = [true, '1', SHAPETYPES[0], MODPARAMOPTIONS[0], '1hz', '0', '1', '0']; @@ -202,8 +206,8 @@ function MasterLfoHandler(){ function handleTick(event) { let time = (Date.now() - firstUpdateTime) / 1000; - - operateModulators(modVisibleArr, modInstanceNumArr, djParamArr, modCenterVals, freqArr, minArr, maxArr, shapeArr, phaseArr, userDefinedWave, time, beatsInMeasure, ticks); + let noiseData = {lastPhaseArr, setLastPhaseArr, cachedNoiseValueArr, setCachedNoiseValueArr}; + operateModulators(modVisibleArr, modTypeArr, modInstanceNumArr, djParamArr, modCenterVals, freqArr, minArr, maxArr, shapeArr, initPhaseArr, noiseData, userDefinedWave, time, beatsInMeasure, ticks); } function handleTimeSig(event) { @@ -284,8 +288,8 @@ function MasterLfoHandler(){ max: maxArr[i], setMax: CreateParamChanger(maxArr, setMaxArr, i), - phase: phaseArr[i], - setPhase: CreateParamChanger(phaseArr, setPhaseArr, i), + phase: initPhaseArr[i], + setPhase: CreateParamChanger(initPhaseArr, setInitPhaseArr, i), visible: modVisibleArr[i], linked: CheckLinked(modInstanceNumArr[i], djParamArr[i], enumInstanceNumArr, enumDjParamArr), diff --git a/modulators.js b/modulators.js index 0f33b22..1190080 100644 --- a/modulators.js +++ b/modulators.js @@ -78,7 +78,7 @@ function indexWave(type, phase, userDefinedWave){ } } -function operateModulators(visibleArr, instanceNumArr, paramNames, centers, freqs, mins, maxs, waveTypes, phaseArr, userDefinedWave, currTime, beatsInMeasure, ticks){ +function operateModulators(visibleArr, typeArr, instanceNumArr, paramNames, centers, freqs, mins, maxs, waveTypes, phaseArr, noiseData, userDefinedWave, currTime, beatsInMeasure, ticks){ for (let i=0; i phase){ // occurs if the phase reset to 0 or at the very start + + noiseData.cachedNoiseValueArr[index][0] = noiseData.cachedNoiseValueArr[index][1]; + if (noiseData.cachedNoiseValueArr[index][0] == 0) + noiseData.cachedNoiseValueArr[index][0] = center; + + noiseData.cachedNoiseValueArr[index][1] = Math.random(); + noiseData.setCachedNoiseValueArr(noiseData.cachedNoiseValueArr); + } + noiseData.lastPhaseArr[index] = phase; + noiseData.setLastPhaseArr(noiseData.lastPhaseArr); + + let sinePhase = (Math.sin(Math.PI + Math.PI * phase) + 1) / 2 + + let unscaled = (noiseData.cachedNoiseValueArr[index][1] - noiseData.cachedNoiseValueArr[index][0]) * sinePhase + noiseData.cachedNoiseValueArr[index][0]; + syncDisplay(inst, name, unscaled); return unscaled * amp + center + parseFloat(min); + } // actual returns the period for musical timing, to avoid floating point errors