spessasynth_lib 3.20.34 → 3.20.35

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -176,7 +176,7 @@ export class WorkletVolumeEnvelope
176
176
  {
177
177
  env.attenuation = env.attenuationTarget;
178
178
  }
179
- const sustainDb = Math.min(DB_SILENCE, env.sustainDbRelative + env.attenuation);
179
+ const sustainDb = Math.min(DB_SILENCE, env.sustainDbRelative);
180
180
 
181
181
  // calculate durations
182
182
  env.attackDuration = timecentsToSamples(voice.modulatedGenerators[generatorTypes.attackVolEnv]);
@@ -186,7 +186,7 @@ export class WorkletVolumeEnvelope
186
186
  // (changing from attenuation to sustain instead of -100dB)
187
187
  const fullChange = voice.modulatedGenerators[generatorTypes.decayVolEnv];
188
188
  const keyNumAddition = (60 - voice.targetKey) * voice.modulatedGenerators[generatorTypes.keyNumToVolEnvDecay];
189
- const fraction = (sustainDb - env.attenuation) / DB_SILENCE;
189
+ const fraction = sustainDb / DB_SILENCE;
190
190
  env.decayDuration = timecentsToSamples(fullChange + keyNumAddition) * fraction;
191
191
 
192
192
  env.releaseDuration = timecentsToSamples(voice.modulatedGenerators[generatorTypes.releaseVolEnv]);
@@ -206,7 +206,7 @@ export class WorkletVolumeEnvelope
206
206
  // if this is the first recalculation and the voice has no attack or delay time, set current db to peak
207
207
  if(env.state === 0 && env.attackEnd === 0)
208
208
  {
209
- env.currentAttenuationDb = env.attenuationTarget;
209
+ // env.currentAttenuationDb = env.attenuationTarget;
210
210
  env.state = 2;
211
211
  }
212
212
 
@@ -214,9 +214,9 @@ export class WorkletVolumeEnvelope
214
214
  if(voice.isInRelease)
215
215
  {
216
216
  // no interpolation this time: force update to actual attenuation and calculate release start from there
217
- env.attenuation = Math.min(DB_SILENCE, env.attenuationTarget);
218
- const sustainDb = Math.max(0, Math.min(DB_SILENCE, env.sustainDbRelative + env.attenuation));
219
- const fraction = (sustainDb - env.attenuation) / DB_SILENCE;
217
+ //env.attenuation = Math.min(DB_SILENCE, env.attenuationTarget);
218
+ const sustainDb = Math.max(0, Math.min(DB_SILENCE, env.sustainDbRelative));
219
+ const fraction = sustainDb / DB_SILENCE;
220
220
  env.decayDuration = timecentsToSamples(fullChange + keyNumAddition) * fraction;
221
221
 
222
222
  switch (env.state)
@@ -232,26 +232,21 @@ export class WorkletVolumeEnvelope
232
232
  // attack is linear (in gain) so we need to do get db from that
233
233
  let elapsed = 1 - ((env.attackEnd - env.releaseStartTimeSamples) / env.attackDuration);
234
234
  // calculate the gain that the attack would have
235
- let attackGain = elapsed * decibelAttenuationToGain(env.attenuation);
236
-
237
235
  // turn that into db
238
- env.releaseStartDb = 20 * Math.log10(attackGain) * -1;
236
+ env.releaseStartDb = 20 * Math.log10(elapsed) * -1;
239
237
  break;
240
238
 
241
239
  case 2:
242
- env.releaseStartDb = env.attenuation;
240
+ env.releaseStartDb = 0;
243
241
  break;
244
242
 
245
243
  case 3:
246
- env.releaseStartDb = (1 - (env.decayEnd - env.releaseStartTimeSamples) / env.decayDuration) * (sustainDb - env.attenuation) + env.attenuation;
244
+ env.releaseStartDb = (1 - (env.decayEnd - env.releaseStartTimeSamples) / env.decayDuration) * sustainDb;
247
245
  break;
248
246
 
249
247
  case 4:
250
248
  env.releaseStartDb = sustainDb;
251
249
  break;
252
-
253
- default:
254
- env.releaseStartDb = env.currentAttenuationDb;
255
250
  }
256
251
  env.releaseStartDb = Math.max(0, Math.min(env.releaseStartDb, DB_SILENCE));
257
252
  if(env.releaseStartDb >= PERCEIVED_DB_SILENCE)
@@ -268,6 +263,7 @@ export class WorkletVolumeEnvelope
268
263
  * @param audioBuffer {Float32Array} the audio buffer to modify
269
264
  * @param centibelOffset {number} the centibel offset of volume, for modLFOtoVolume
270
265
  * @param smoothingFactor {number} the adjusted smoothing factor for the envelope
266
+ * @description essentially we use approach of 100dB is silence, 0dB is peak, and always add attenuation to that (which is interpolated)
271
267
  */
272
268
  static apply(voice, audioBuffer, centibelOffset, smoothingFactor)
273
269
  {
@@ -279,9 +275,6 @@ export class WorkletVolumeEnvelope
279
275
  // RELEASE PHASE
280
276
  if(voice.isInRelease)
281
277
  {
282
- // release needs a more aggressive smoothing factor
283
- // as the instant notes don't end instantly when they should
284
- const releaseSmoothingFactor = smoothingFactor * 10;
285
278
  let elapsedRelease = env.currentSampleTime - env.releaseStartTimeSamples;
286
279
  if(elapsedRelease >= env.releaseDuration)
287
280
  {
@@ -295,9 +288,10 @@ export class WorkletVolumeEnvelope
295
288
  let dbDifference = DB_SILENCE - env.releaseStartDb;
296
289
  for (let i = 0; i < audioBuffer.length; i++)
297
290
  {
291
+ // attenuation interpolation
292
+ env.attenuation += (env.attenuationTarget - env.attenuation) * attenuationSmoothing;
298
293
  let db = (elapsedRelease / env.releaseDuration) * dbDifference + env.releaseStartDb;
299
- let gain = decibelAttenuationToGain(db + decibelOffset);
300
- env.currentReleaseGain += (gain - env.currentReleaseGain) * releaseSmoothingFactor;
294
+ env.currentReleaseGain = decibelAttenuationToGain(db + decibelOffset + env.attenuation);
301
295
  audioBuffer[i] *= env.currentReleaseGain;
302
296
  env.currentSampleTime++;
303
297
  elapsedRelease++;
@@ -340,7 +334,7 @@ export class WorkletVolumeEnvelope
340
334
  let linearAttenuation = 1 - (env.attackEnd - env.currentSampleTime) / env.attackDuration; // 0 to 1
341
335
  audioBuffer[filledBuffer] *= linearAttenuation * decibelAttenuationToGain(env.attenuation + decibelOffset);
342
336
  // set current attenuation to peak as its invalid during this phase
343
- env.currentAttenuationDb = env.attenuation;
337
+ env.currentAttenuationDb = 0;
344
338
 
345
339
  env.currentSampleTime++;
346
340
  if(++filledBuffer >= audioBuffer.length)
@@ -359,7 +353,7 @@ export class WorkletVolumeEnvelope
359
353
  env.attenuation += (env.attenuationTarget - env.attenuation) * attenuationSmoothing;
360
354
 
361
355
  audioBuffer[filledBuffer] *= decibelAttenuationToGain(env.attenuation + decibelOffset);
362
- env.currentAttenuationDb = env.attenuation;
356
+ env.currentAttenuationDb = 0;
363
357
 
364
358
  env.currentSampleTime++;
365
359
  if(++filledBuffer >= audioBuffer.length)
@@ -377,9 +371,8 @@ export class WorkletVolumeEnvelope
377
371
  // attenuation interpolation
378
372
  env.attenuation += (env.attenuationTarget - env.attenuation) * attenuationSmoothing;
379
373
 
380
- const sustainDb = Math.min(DB_SILENCE, env.sustainDbRelative + env.attenuation);
381
- env.currentAttenuationDb = (1 - (env.decayEnd - env.currentSampleTime) / env.decayDuration) * (sustainDb - env.attenuation) + env.attenuation;
382
- audioBuffer[filledBuffer] *= decibelAttenuationToGain(env.currentAttenuationDb + decibelOffset);
374
+ env.currentAttenuationDb = (1 - (env.decayEnd - env.currentSampleTime) / env.decayDuration) * env.sustainDbRelative;
375
+ audioBuffer[filledBuffer] *= decibelAttenuationToGain(env.currentAttenuationDb + decibelOffset + env.attenuation);
383
376
 
384
377
  env.currentSampleTime++;
385
378
  if(++filledBuffer >= audioBuffer.length)
@@ -400,10 +393,9 @@ export class WorkletVolumeEnvelope
400
393
  {
401
394
  // attenuation interpolation
402
395
  env.attenuation += (env.attenuationTarget - env.attenuation) * attenuationSmoothing;
403
- const sustainDb = Math.min(DB_SILENCE, env.sustainDbRelative + env.attenuation);
404
396
 
405
- audioBuffer[filledBuffer] *= decibelAttenuationToGain(sustainDb + decibelOffset);
406
- env.currentAttenuationDb = sustainDb;
397
+ audioBuffer[filledBuffer] *= decibelAttenuationToGain(env.sustainDbRelative + decibelOffset + env.attenuation);
398
+ env.currentAttenuationDb = env.sustainDbRelative;
407
399
  env.currentSampleTime++;
408
400
  if(++filledBuffer >= audioBuffer.length)
409
401
  {