spessasynth_core 4.0.24 → 4.0.25

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.
package/dist/index.d.ts CHANGED
@@ -3212,6 +3212,12 @@ declare class BasicMIDI {
3212
3212
  * @returns The time in seconds.
3213
3213
  */
3214
3214
  midiTicksToSeconds(ticks: number): number;
3215
+ /**
3216
+ * Converts seconds to time in MIDI ticks.
3217
+ * @param seconds The time in seconds.
3218
+ * @returns The time in MIDI ticks.
3219
+ */
3220
+ secondsToMIDITicks(seconds: number): number;
3215
3221
  /**
3216
3222
  * Gets the used programs and keys for this MIDI file with a given sound bank.
3217
3223
  * @param soundbank the sound bank.
package/dist/index.js CHANGED
@@ -2999,6 +2999,41 @@ var BasicMIDI2 = class _BasicMIDI {
2999
2999
  }
3000
3000
  return totalSeconds;
3001
3001
  }
3002
+ /**
3003
+ * Converts seconds to time in MIDI ticks.
3004
+ * @param seconds The time in seconds.
3005
+ * @returns The time in MIDI ticks.
3006
+ */
3007
+ secondsToMIDITicks(seconds) {
3008
+ seconds = Math.max(seconds, 0);
3009
+ if (seconds === 0) return 0;
3010
+ if (this.tempoChanges.length < 1) {
3011
+ throw new Error(
3012
+ "There are no tempo changes in the sequence. At least one is needed."
3013
+ );
3014
+ }
3015
+ if (this.tempoChanges[this.tempoChanges.length - 1].ticks !== 0) {
3016
+ throw new Error(
3017
+ `The last tempo change is not at 0 ticks. Got ${this.tempoChanges[this.tempoChanges.length - 1].ticks} ticks.`
3018
+ );
3019
+ }
3020
+ let remainingSeconds = seconds;
3021
+ let totalTicks = 0;
3022
+ for (let i = this.tempoChanges.length - 1; i >= 0; i--) {
3023
+ const currentTempo = this.tempoChanges[i];
3024
+ const next = this.tempoChanges[i - 1];
3025
+ const ticksToNextTempo = next ? next.ticks - currentTempo.ticks : Infinity;
3026
+ const oneTickToSeconds = 60 / (currentTempo.tempo * this.timeDivision);
3027
+ const secondsToNextTempo = ticksToNextTempo * oneTickToSeconds;
3028
+ if (remainingSeconds <= secondsToNextTempo) {
3029
+ totalTicks += Math.round(remainingSeconds / oneTickToSeconds);
3030
+ return totalTicks;
3031
+ }
3032
+ totalTicks += ticksToNextTempo;
3033
+ remainingSeconds -= secondsToNextTempo;
3034
+ }
3035
+ return totalTicks;
3036
+ }
3002
3037
  /**
3003
3038
  * Gets the used programs and keys for this MIDI file with a given sound bank.
3004
3039
  * @param soundbank the sound bank.
@@ -11324,45 +11359,89 @@ function controllerChange(controllerNumber, controllerValue, sendEvent = true) {
11324
11359
  }
11325
11360
 
11326
11361
  // src/synthesizer/audio_engine/engine_methods/portamento_time.ts
11327
- var portamentoLookup = {
11328
- 0: 0,
11329
- 1: 6e-3,
11330
- 2: 0.023,
11331
- 4: 0.05,
11332
- 8: 0.11,
11333
- 16: 0.25,
11334
- 32: 0.5,
11335
- 64: 2.06,
11336
- 80: 4.2,
11337
- 96: 8.4,
11338
- 112: 19.5,
11339
- 116: 26.7,
11340
- 120: 40,
11341
- 124: 80,
11342
- 127: 480
11343
- };
11344
- function portaTimeToRate(value) {
11345
- let portaTime = 0;
11346
- if (portamentoLookup[value] !== void 0) {
11347
- portaTime = portamentoLookup[value];
11348
- }
11349
- let lower = null;
11350
- let upper = null;
11351
- for (const k of Object.keys(portamentoLookup)) {
11352
- const key = parseInt(k);
11353
- if (key < value && (lower === null || key > lower)) {
11354
- lower = key;
11355
- }
11356
- if (key > value && (upper === null || key < upper)) {
11357
- upper = key;
11358
- }
11359
- }
11360
- if (lower !== null && upper !== null) {
11361
- const lowerTime = portamentoLookup[lower];
11362
- const upperTime = portamentoLookup[upper];
11363
- portaTime = lowerTime + (value - lower) * (upperTime - lowerTime) / (upper - lower);
11362
+ var PORTA_DIVISION_CONSTANT = 40;
11363
+ function portaTimeToRate(cc) {
11364
+ if (cc < 1) {
11365
+ return 0;
11366
+ } else {
11367
+ const x0 = [1, 2, 4, 8, 16, 32, 64, 80, 96, 112, 120, 124];
11368
+ const ih = [
11369
+ 1,
11370
+ 0.5,
11371
+ 0.25,
11372
+ 0.125,
11373
+ 0.0625,
11374
+ 0.03125,
11375
+ 0.0625,
11376
+ 0.0625,
11377
+ 0.0625,
11378
+ 0.125,
11379
+ 0.25,
11380
+ 1 / 3
11381
+ ];
11382
+ const a = [
11383
+ -0.16653127382501215,
11384
+ 0.11863875218299408,
11385
+ 0.029479047361245264,
11386
+ -0.005442312089231738,
11387
+ 0.1451520875973037,
11388
+ -0.005056281449558275,
11389
+ -0.005095486882876532,
11390
+ 0.03334009551111544,
11391
+ -0.09361368678020432,
11392
+ 0.14132569702451822,
11393
+ -0.15805565301011382,
11394
+ -0.09918856955881927
11395
+ ];
11396
+ const b = [
11397
+ 0.028212773333433472,
11398
+ -0.3388502064992847,
11399
+ -0.15839529890929713,
11400
+ -0.12398131766775483,
11401
+ -0.2874848552685111,
11402
+ 0.012254866302537692,
11403
+ 0.005957797193345771,
11404
+ -0.03745899330347374,
11405
+ 0.12911781869810196,
11406
+ -0.15867193224162568,
11407
+ 0.504406322732748,
11408
+ 0.3786845131875458
11409
+ ];
11410
+ const c = [
11411
+ 0.7218950861255283,
11412
+ 0.5574536226347168,
11413
+ 0.47133893237025826,
11414
+ 0.48597095327079914,
11415
+ 0.44336276333518854,
11416
+ 0.6076986311801551,
11417
+ 0.30851975971827794,
11418
+ 0.30514889345633955,
11419
+ 0.3302511933827384,
11420
+ 0.153822885219165,
11421
+ 0.1302280559047337,
11422
+ 0.49865530675491687
11423
+ ];
11424
+ const d = [
11425
+ -2.2218487496163566,
11426
+ -1.6382721639824072,
11427
+ -1.3010299956639813,
11428
+ -0.958607314841775,
11429
+ -0.6020599913279624,
11430
+ -0.3010299956639812,
11431
+ 0.31386722036915343,
11432
+ 0.6232492903979004,
11433
+ 0.9242792860618817,
11434
+ 1.290034611362518,
11435
+ 1.4265112613645752,
11436
+ 1.9030899869919435
11437
+ ];
11438
+ const thresholds = [2, 4, 8, 16, 32, 64, 80, 96, 112, 120, 124];
11439
+ const s = thresholds.findLastIndex((t2) => t2 < cc) + 1;
11440
+ const t = (cc - x0[s]) * ih[s];
11441
+ return Math.exp(
11442
+ 2.302585092994046 * (((a[s] * t + b[s]) * t + c[s]) * t + d[s])
11443
+ ) / PORTA_DIVISION_CONSTANT;
11364
11444
  }
11365
- return portaTime / 40;
11366
11445
  }
11367
11446
  function portamentoTimeToSeconds(time, distance) {
11368
11447
  return portaTimeToRate(time) * distance;