soundroo 0.7.0 → 0.8.0

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.
Files changed (217) hide show
  1. package/AGENTS.md +3 -1
  2. package/README.md +11 -3
  3. package/THIRD_PARTY_NOTICES.md +1 -1
  4. package/assets/reverb/hall.wav +0 -0
  5. package/assets/samples/SOURCES.md +233 -333
  6. package/assets/samples/samples.json +51856 -363
  7. package/bin/soundroo.js +25 -3
  8. package/docs/composing.md +24 -7
  9. package/docs/headless.md +19 -6
  10. package/package.json +1 -1
  11. package/src/arrangement.js +2 -1
  12. package/src/audio/convolution.js +121 -0
  13. package/src/audio/library.js +194 -0
  14. package/src/audio/midi-import.js +174 -0
  15. package/src/audio/midi.js +27 -4
  16. package/src/audio/sampler.js +208 -52
  17. package/src/audio/synth.js +129 -6
  18. package/src/catalog.js +6 -6
  19. package/src/cloud/payload.js +2 -1
  20. package/src/headless.js +37 -9
  21. package/src/identity.js +14 -4
  22. package/src/project.js +20 -7
  23. package/src/render.js +121 -46
  24. package/src/samples-install.js +103 -0
  25. package/src/score.js +72 -15
  26. package/src/timeline.js +50 -0
  27. package/src/version.js +1 -1
  28. package/assets/samples/brass/39-1-1.wav +0 -0
  29. package/assets/samples/brass/41-1-1.wav +0 -0
  30. package/assets/samples/brass/45-1-1.wav +0 -0
  31. package/assets/samples/brass/46-1-1.wav +0 -0
  32. package/assets/samples/brass/50-1-1.wav +0 -0
  33. package/assets/samples/brass/53-1-1.wav +0 -0
  34. package/assets/samples/brass/57-1-1.wav +0 -0
  35. package/assets/samples/brass/60-1-1.wav +0 -0
  36. package/assets/samples/brass/63-1-1.wav +0 -0
  37. package/assets/samples/brass/67-1-1.wav +0 -0
  38. package/assets/samples/brass/70-1-1.wav +0 -0
  39. package/assets/samples/brass/74-1-1.wav +0 -0
  40. package/assets/samples/brass/77-1-1.wav +0 -0
  41. package/assets/samples/brass/81-1-1.wav +0 -0
  42. package/assets/samples/closed-hat/42-1-1.wav +0 -0
  43. package/assets/samples/closed-hat/42-1-2.wav +0 -0
  44. package/assets/samples/closed-hat/42-2-1.wav +0 -0
  45. package/assets/samples/closed-hat/42-2-2.wav +0 -0
  46. package/assets/samples/closed-hat/42-3-1.wav +0 -0
  47. package/assets/samples/closed-hat/42-3-2.wav +0 -0
  48. package/assets/samples/crash/49-1-1.wav +0 -0
  49. package/assets/samples/crash/49-2-1.wav +0 -0
  50. package/assets/samples/electric-bass/24-1-1.wav +0 -0
  51. package/assets/samples/electric-bass/24-2-1.wav +0 -0
  52. package/assets/samples/electric-bass/26-1-1.wav +0 -0
  53. package/assets/samples/electric-bass/26-2-1.wav +0 -0
  54. package/assets/samples/electric-bass/29-1-1.wav +0 -0
  55. package/assets/samples/electric-bass/29-2-1.wav +0 -0
  56. package/assets/samples/electric-bass/32-1-1.wav +0 -0
  57. package/assets/samples/electric-bass/32-2-1.wav +0 -0
  58. package/assets/samples/electric-bass/35-1-1.wav +0 -0
  59. package/assets/samples/electric-bass/35-2-1.wav +0 -0
  60. package/assets/samples/electric-bass/38-1-1.wav +0 -0
  61. package/assets/samples/electric-bass/38-2-1.wav +0 -0
  62. package/assets/samples/electric-bass/41-1-1.wav +0 -0
  63. package/assets/samples/electric-bass/41-2-1.wav +0 -0
  64. package/assets/samples/electric-bass/44-1-1.wav +0 -0
  65. package/assets/samples/electric-bass/44-2-1.wav +0 -0
  66. package/assets/samples/electric-bass/47-1-1.wav +0 -0
  67. package/assets/samples/electric-bass/47-2-1.wav +0 -0
  68. package/assets/samples/electric-bass/50-1-1.wav +0 -0
  69. package/assets/samples/electric-bass/50-2-1.wav +0 -0
  70. package/assets/samples/electric-bass/53-1-1.wav +0 -0
  71. package/assets/samples/electric-bass/53-2-1.wav +0 -0
  72. package/assets/samples/electric-bass/56-1-1.wav +0 -0
  73. package/assets/samples/electric-bass/56-2-1.wav +0 -0
  74. package/assets/samples/electric-bass/59-1-1.wav +0 -0
  75. package/assets/samples/electric-bass/59-2-1.wav +0 -0
  76. package/assets/samples/electric-bass/62-1-1.wav +0 -0
  77. package/assets/samples/electric-bass/62-2-1.wav +0 -0
  78. package/assets/samples/guitar-clean/40-1-1.wav +0 -0
  79. package/assets/samples/guitar-clean/41-1-1.wav +0 -0
  80. package/assets/samples/guitar-clean/45-1-1.wav +0 -0
  81. package/assets/samples/guitar-clean/48-1-1.wav +0 -0
  82. package/assets/samples/guitar-clean/50-1-1.wav +0 -0
  83. package/assets/samples/guitar-clean/52-1-1.wav +0 -0
  84. package/assets/samples/guitar-clean/55-1-1.wav +0 -0
  85. package/assets/samples/guitar-clean/59-1-1.wav +0 -0
  86. package/assets/samples/guitar-clean/61-1-1.wav +0 -0
  87. package/assets/samples/guitar-clean/64-1-1.wav +0 -0
  88. package/assets/samples/guitar-clean/67-1-1.wav +0 -0
  89. package/assets/samples/guitar-clean/71-1-1.wav +0 -0
  90. package/assets/samples/guitar-clean/72-1-1.wav +0 -0
  91. package/assets/samples/guitar-clean/74-1-1.wav +0 -0
  92. package/assets/samples/guitar-clean/77-1-1.wav +0 -0
  93. package/assets/samples/guitar-clean/80-1-1.wav +0 -0
  94. package/assets/samples/guitar-clean/82-1-1.wav +0 -0
  95. package/assets/samples/guitar-clean/85-1-1.wav +0 -0
  96. package/assets/samples/guitar-crunch/40-1-1.wav +0 -0
  97. package/assets/samples/guitar-crunch/41-1-1.wav +0 -0
  98. package/assets/samples/guitar-crunch/45-1-1.wav +0 -0
  99. package/assets/samples/guitar-crunch/48-1-1.wav +0 -0
  100. package/assets/samples/guitar-crunch/50-1-1.wav +0 -0
  101. package/assets/samples/guitar-crunch/52-1-1.wav +0 -0
  102. package/assets/samples/guitar-crunch/55-1-1.wav +0 -0
  103. package/assets/samples/guitar-crunch/59-1-1.wav +0 -0
  104. package/assets/samples/guitar-crunch/61-1-1.wav +0 -0
  105. package/assets/samples/guitar-crunch/64-1-1.wav +0 -0
  106. package/assets/samples/guitar-crunch/67-1-1.wav +0 -0
  107. package/assets/samples/guitar-crunch/71-1-1.wav +0 -0
  108. package/assets/samples/guitar-crunch/72-1-1.wav +0 -0
  109. package/assets/samples/guitar-crunch/74-1-1.wav +0 -0
  110. package/assets/samples/guitar-crunch/77-1-1.wav +0 -0
  111. package/assets/samples/guitar-crunch/80-1-1.wav +0 -0
  112. package/assets/samples/guitar-crunch/82-1-1.wav +0 -0
  113. package/assets/samples/guitar-crunch/85-1-1.wav +0 -0
  114. package/assets/samples/open-hat/46-1-1.wav +0 -0
  115. package/assets/samples/open-hat/46-2-1.wav +0 -0
  116. package/assets/samples/piano/102-1-1.wav +0 -0
  117. package/assets/samples/piano/102-2-1.wav +0 -0
  118. package/assets/samples/piano/105-1-1.wav +0 -0
  119. package/assets/samples/piano/105-2-1.wav +0 -0
  120. package/assets/samples/piano/108-1-1.wav +0 -0
  121. package/assets/samples/piano/108-2-1.wav +0 -0
  122. package/assets/samples/piano/21-1-1.wav +0 -0
  123. package/assets/samples/piano/21-2-1.wav +0 -0
  124. package/assets/samples/piano/24-1-1.wav +0 -0
  125. package/assets/samples/piano/24-2-1.wav +0 -0
  126. package/assets/samples/piano/27-1-1.wav +0 -0
  127. package/assets/samples/piano/27-2-1.wav +0 -0
  128. package/assets/samples/piano/30-1-1.wav +0 -0
  129. package/assets/samples/piano/30-2-1.wav +0 -0
  130. package/assets/samples/piano/33-1-1.wav +0 -0
  131. package/assets/samples/piano/33-2-1.wav +0 -0
  132. package/assets/samples/piano/36-1-1.wav +0 -0
  133. package/assets/samples/piano/36-2-1.wav +0 -0
  134. package/assets/samples/piano/39-1-1.wav +0 -0
  135. package/assets/samples/piano/39-2-1.wav +0 -0
  136. package/assets/samples/piano/42-1-1.wav +0 -0
  137. package/assets/samples/piano/42-2-1.wav +0 -0
  138. package/assets/samples/piano/45-1-1.wav +0 -0
  139. package/assets/samples/piano/45-2-1.wav +0 -0
  140. package/assets/samples/piano/48-1-1.wav +0 -0
  141. package/assets/samples/piano/48-2-1.wav +0 -0
  142. package/assets/samples/piano/51-1-1.wav +0 -0
  143. package/assets/samples/piano/51-2-1.wav +0 -0
  144. package/assets/samples/piano/54-1-1.wav +0 -0
  145. package/assets/samples/piano/54-2-1.wav +0 -0
  146. package/assets/samples/piano/57-1-1.wav +0 -0
  147. package/assets/samples/piano/57-2-1.wav +0 -0
  148. package/assets/samples/piano/60-1-1.wav +0 -0
  149. package/assets/samples/piano/60-2-1.wav +0 -0
  150. package/assets/samples/piano/63-1-1.wav +0 -0
  151. package/assets/samples/piano/63-2-1.wav +0 -0
  152. package/assets/samples/piano/66-1-1.wav +0 -0
  153. package/assets/samples/piano/66-2-1.wav +0 -0
  154. package/assets/samples/piano/69-1-1.wav +0 -0
  155. package/assets/samples/piano/69-2-1.wav +0 -0
  156. package/assets/samples/piano/72-1-1.wav +0 -0
  157. package/assets/samples/piano/72-2-1.wav +0 -0
  158. package/assets/samples/piano/75-1-1.wav +0 -0
  159. package/assets/samples/piano/75-2-1.wav +0 -0
  160. package/assets/samples/piano/78-1-1.wav +0 -0
  161. package/assets/samples/piano/78-2-1.wav +0 -0
  162. package/assets/samples/piano/81-1-1.wav +0 -0
  163. package/assets/samples/piano/81-2-1.wav +0 -0
  164. package/assets/samples/piano/84-1-1.wav +0 -0
  165. package/assets/samples/piano/84-2-1.wav +0 -0
  166. package/assets/samples/piano/87-1-1.wav +0 -0
  167. package/assets/samples/piano/87-2-1.wav +0 -0
  168. package/assets/samples/piano/90-1-1.wav +0 -0
  169. package/assets/samples/piano/90-2-1.wav +0 -0
  170. package/assets/samples/piano/93-1-1.wav +0 -0
  171. package/assets/samples/piano/93-2-1.wav +0 -0
  172. package/assets/samples/piano/96-1-1.wav +0 -0
  173. package/assets/samples/piano/96-2-1.wav +0 -0
  174. package/assets/samples/piano/99-1-1.wav +0 -0
  175. package/assets/samples/piano/99-2-1.wav +0 -0
  176. package/assets/samples/ride/51-1-1.wav +0 -0
  177. package/assets/samples/ride/51-2-1.wav +0 -0
  178. package/assets/samples/rock-kick/36-1-1.wav +0 -0
  179. package/assets/samples/rock-kick/36-1-2.wav +0 -0
  180. package/assets/samples/rock-kick/36-2-1.wav +0 -0
  181. package/assets/samples/rock-kick/36-2-2.wav +0 -0
  182. package/assets/samples/rock-kick/36-3-1.wav +0 -0
  183. package/assets/samples/rock-kick/36-3-2.wav +0 -0
  184. package/assets/samples/rock-kick/36-4-1.wav +0 -0
  185. package/assets/samples/rock-kick/36-4-2.wav +0 -0
  186. package/assets/samples/rock-snare/38-1-1.wav +0 -0
  187. package/assets/samples/rock-snare/38-1-2.wav +0 -0
  188. package/assets/samples/rock-snare/38-2-1.wav +0 -0
  189. package/assets/samples/rock-snare/38-2-2.wav +0 -0
  190. package/assets/samples/rock-snare/38-3-1.wav +0 -0
  191. package/assets/samples/rock-snare/38-3-2.wav +0 -0
  192. package/assets/samples/rock-snare/38-4-1.wav +0 -0
  193. package/assets/samples/rock-snare/38-4-2.wav +0 -0
  194. package/assets/samples/strings/36-1-1.wav +0 -0
  195. package/assets/samples/strings/40-1-1.wav +0 -0
  196. package/assets/samples/strings/43-1-1.wav +0 -0
  197. package/assets/samples/strings/47-1-1.wav +0 -0
  198. package/assets/samples/strings/50-1-1.wav +0 -0
  199. package/assets/samples/strings/53-1-1.wav +0 -0
  200. package/assets/samples/strings/55-1-1.wav +0 -0
  201. package/assets/samples/strings/59-1-1.wav +0 -0
  202. package/assets/samples/strings/62-1-1.wav +0 -0
  203. package/assets/samples/strings/65-1-1.wav +0 -0
  204. package/assets/samples/strings/69-1-1.wav +0 -0
  205. package/assets/samples/strings/72-1-1.wav +0 -0
  206. package/assets/samples/strings/76-1-1.wav +0 -0
  207. package/assets/samples/strings/79-1-1.wav +0 -0
  208. package/assets/samples/strings/83-1-1.wav +0 -0
  209. package/assets/samples/strings/86-1-1.wav +0 -0
  210. package/assets/samples/tom/41-1-1.wav +0 -0
  211. package/assets/samples/tom/41-2-1.wav +0 -0
  212. package/assets/samples/tom/45-1-1.wav +0 -0
  213. package/assets/samples/tom/45-2-1.wav +0 -0
  214. package/assets/samples/tom/48-1-1.wav +0 -0
  215. package/assets/samples/tom/48-2-1.wav +0 -0
  216. package/assets/samples/tom/50-1-1.wav +0 -0
  217. package/assets/samples/tom/50-2-1.wav +0 -0
@@ -115,6 +115,49 @@ export class Env {
115
115
 
116
116
  // --- oscillator helpers -----------------------------------------------------------
117
117
 
118
+ // Recordings are pitched with Kaiser-windowed sinc interpolation: flat to
119
+ // 16 kHz and free of the treble loss and aliasing a short polynomial adds.
120
+ // A kernel holds `taps` weights for each of `phases` fractional positions.
121
+ // Played at its own speed or slower, a recording uses 16 taps; played faster
122
+ // (higher notes, or a lower output rate), the kernel's cutoff falls with the
123
+ // speed and it grows longer, so nothing above the output's Nyquist folds back.
124
+ export const SINC_TAPS = 16;
125
+ const SINC_PHASES = 2048;
126
+ const MAX_TAPS = 128;
127
+ function makeKernel(taps, phases, cutoff, beta = 8) {
128
+ const half = taps / 2;
129
+ const bessel = x => { let sum = 1, term = 1; for (let k = 1; k < 32; k++) { term *= (x / (2 * k)) ** 2; sum += term; } return sum; };
130
+ const table = new Float32Array((phases + 1) * taps);
131
+ for (let p = 0; p <= phases; p++) {
132
+ const f = p / phases, weights = [];
133
+ for (let j = 0; j < taps; j++) {
134
+ const x = j - half + 1 - f;
135
+ const sinc = x === 0 ? 2 * cutoff : Math.sin(2 * Math.PI * cutoff * x) / (Math.PI * x);
136
+ weights.push(Math.abs(x) >= half ? 0 : sinc * bessel(beta * Math.sqrt(1 - (x / half) ** 2)) / bessel(beta));
137
+ }
138
+ const sum = weights.reduce((a, b) => a + b, 0);
139
+ weights.forEach((w, j) => { table[p * taps + j] = w / sum; });
140
+ }
141
+ return { table, taps, phases };
142
+ }
143
+ const SINC_KERNEL = makeKernel(SINC_TAPS, SINC_PHASES, 0.46);
144
+ const SINC = SINC_KERNEL.table;
145
+ const kernels = new Map();
146
+ /** The interpolation kernel for reading a recording `step` source frames per output frame. */
147
+ export function kernelFor(step) {
148
+ if (!(step > 1 + 1e-9)) return SINC_KERNEL;
149
+ const speed = Math.ceil(step * 32) / 32;
150
+ if (!kernels.has(speed)) kernels.set(speed, makeKernel(Math.min(MAX_TAPS, 2 * Math.ceil(8 * speed)), 1024, 0.46 / speed));
151
+ return kernels.get(speed);
152
+ }
153
+
154
+ // 4-point, 3rd-order Hermite interpolation between d[j] and d[j + 1].
155
+ function hermite(d, jm, j, f) {
156
+ const xm1 = d[jm], x0 = d[j], x1 = d[j + 1], x2 = d[j + 2];
157
+ const c1 = 0.5 * (x1 - xm1), c2 = xm1 - 2.5 * x0 + 2 * x1 - 0.5 * x2, c3 = 0.5 * (x2 - xm1) + 1.5 * (x0 - x1);
158
+ return ((c3 * f + c2) * f + c1) * f + x0;
159
+ }
160
+
118
161
  function polyblep(t, dt) {
119
162
  if (t < dt) {
120
163
  t /= dt;
@@ -127,7 +170,7 @@ function polyblep(t, dt) {
127
170
  return 0;
128
171
  }
129
172
 
130
- export const WAVES = { sine: 0, saw: 1, tri: 2, square: 3, noise: 4, pink: 5, sample: 6, string: 7 };
173
+ export const WAVES = { sine: 0, saw: 1, tri: 2, square: 3, noise: 4, pink: 5, sample: 6, string: 7, stream: 8 };
131
174
 
132
175
  // --- biquad (RBJ cookbook) ------------------------------------------------------------
133
176
 
@@ -198,6 +241,8 @@ export class Voice {
198
241
  this.filter = null;
199
242
  this.pan = 0;
200
243
  this.panEnv = null;
244
+ // A stereo voice plays a recording's two channels into a stereo lane.
245
+ this.stereo = false;
201
246
  }
202
247
  layer(spec) {
203
248
  this.layers.push(spec);
@@ -356,6 +401,7 @@ export class Lane {
356
401
  this.panLfo = spec.panLfo || null;
357
402
  this.send = spec.reverb || 0;
358
403
  this.filter = spec.filter ? makeFilter(spec.filter.type, spec.filter.freq, spec.filter.q ?? 0.707) : null;
404
+ this.filterR = spec.filter && this.stereo ? makeFilter(spec.filter.type, spec.filter.freq, spec.filter.q ?? 0.707) : null;
359
405
  this.filterLfo = spec.filter && spec.filter.lfo ? spec.filter.lfo : null;
360
406
  this.overdrive = spec.overdrive ?? null;
361
407
  this.drivePrevious = 0;
@@ -384,17 +430,28 @@ export class Lane {
384
430
 
385
431
  // Mixes the block into the synth's dry buffers and the reverb input.
386
432
  mix(i0, n, dryL, dryR, send) {
387
- if (!this.dirty && !this.pumpTimes && (!this.filter || (Math.abs(this.filter.z1) < 1e-12 && Math.abs(this.filter.z2) < 1e-12))) return;
433
+ const quiet = f => !f || (Math.abs(f.z1) < 1e-12 && Math.abs(f.z2) < 1e-12);
434
+ if (!this.dirty && !this.pumpTimes && quiet(this.filter) && quiet(this.filterR)) return;
388
435
  const sr = this.synth.sr;
389
436
  const tMid = (i0 + n / 2) / sr;
390
437
  const g = this.gain;
391
438
  if (this.stereo) {
392
439
  const L = this.L;
393
440
  const R = this.R;
441
+ if (this.filter) {
442
+ for (const [f, buffer] of [[this.filter, L], [this.filterR, R]]) {
443
+ if (f.freq !== f.lastF) { biquadCoefs(f.type, f.freq, f.q, sr, f); f.lastF = f.freq; }
444
+ biquadRun(f, buffer, 0, n);
445
+ }
446
+ }
447
+ // Panning a stereo recording moves its whole picture: toward the right,
448
+ // the left channel blends into the right one rather than disappearing.
449
+ const p = Math.max(-1, Math.min(1, this.panBase));
450
+ const ll = g * (p > 0 ? 1 - p : 1), lr = g * (p < 0 ? -p : 0), rr = g * (p < 0 ? 1 + p : 1), rl = g * (p > 0 ? p : 0);
394
451
  const s = this.send;
395
452
  for (let k = 0; k < n; k++) {
396
- const l = L[k] * g;
397
- const r = R[k] * g;
453
+ const l = L[k] * ll + R[k] * lr;
454
+ const r = R[k] * rr + L[k] * rl;
398
455
  dryL[k] += l;
399
456
  dryR[k] += r;
400
457
  if (s) send[k] += (l + r) * 0.5 * s;
@@ -473,6 +530,7 @@ export class Synth {
473
530
  }
474
531
  this.send = new Float32Array(BLOCK);
475
532
  this.acc = new Float32Array(BLOCK);
533
+ this.accR = new Float32Array(BLOCK);
476
534
  this.tmp = new Float32Array(BLOCK);
477
535
  this.amp = new Float32Array(BLOCK);
478
536
  this.fq = new Float32Array(BLOCK);
@@ -530,6 +588,10 @@ export class Synth {
530
588
  const f = v.filter;
531
589
  Object.assign(f, { b0: 1, b1: 0, b2: 0, a1: 0, a2: 0, z1: 0, z2: 0, lastF: -1 });
532
590
  if (f.freqEnv) f.freqEnv.begin(sr, v.start);
591
+ if (v.stereo) {
592
+ v.filterR = { ...f, z1: 0, z2: 0, lastF: -1 };
593
+ if (f.freqEnv) v.filterR.freqEnv = null;
594
+ }
533
595
  }
534
596
  if (v.panEnv) v.panEnv.begin(sr, v.start);
535
597
  }
@@ -541,9 +603,11 @@ export class Synth {
541
603
  if (cnt <= 0) return;
542
604
  const sr = this.sr;
543
605
  const acc = this.acc;
606
+ const accR = this.accR;
544
607
  const amp = this.amp;
545
608
  const fq = this.fq;
546
609
  acc.fill(0, 0, cnt);
610
+ if (v.stereo) accR.fill(0, 0, cnt);
547
611
  for (const L of v.layers) {
548
612
  L.amp.fill(amp, cnt);
549
613
  if (L.freqEnv) L.freqEnv.fill(fq, cnt);
@@ -683,6 +747,48 @@ export class Synth {
683
747
  L.pos = pos;
684
748
  break;
685
749
  }
750
+ case 8: { // recording read from its pack while it plays, windowed-sinc pitching, optional loop, mono or stereo
751
+ const source = L.source, step = L.step * mul, loop = L.loop, last = source.frames;
752
+ const kernel = L.kernel ??= kernelFor(step), taps = kernel.taps, half = taps / 2;
753
+ let pos = L.pos;
754
+ source.ensure(Math.floor(pos) - half, Math.ceil(pos + step * cnt) + half + 1);
755
+ const base = source.base, left = source.data[0], right = source.data[1] ?? left;
756
+ // Stereo voices fill both accumulators; a mono lane takes the average of the channels.
757
+ const both = v.stereo, two = right !== left, exact = step === 1 && pos === Math.floor(pos);
758
+ const offset = 1 - half - base;
759
+ if (kernel === SINC_KERNEL) {
760
+ const S = SINC;
761
+ for (let k = 0; k < cnt; k++) {
762
+ if (loop !== null && loop !== undefined && pos >= loop[1]) pos -= loop[1] - loop[0];
763
+ const i = pos | 0;
764
+ if (i >= last) break;
765
+ const a = amp[k];
766
+ let l, r;
767
+ if (exact) { l = left[i - base]; r = right[i - base]; } else {
768
+ const w = ((pos - i) * SINC_PHASES + 0.5 | 0) * SINC_TAPS, f = i + offset;
769
+ l = S[w + 0] * left[f + 0] + S[w + 1] * left[f + 1] + S[w + 2] * left[f + 2] + S[w + 3] * left[f + 3] + S[w + 4] * left[f + 4] + S[w + 5] * left[f + 5] + S[w + 6] * left[f + 6] + S[w + 7] * left[f + 7] + S[w + 8] * left[f + 8] + S[w + 9] * left[f + 9] + S[w + 10] * left[f + 10] + S[w + 11] * left[f + 11] + S[w + 12] * left[f + 12] + S[w + 13] * left[f + 13] + S[w + 14] * left[f + 14] + S[w + 15] * left[f + 15];
770
+ r = two ? S[w + 0] * right[f + 0] + S[w + 1] * right[f + 1] + S[w + 2] * right[f + 2] + S[w + 3] * right[f + 3] + S[w + 4] * right[f + 4] + S[w + 5] * right[f + 5] + S[w + 6] * right[f + 6] + S[w + 7] * right[f + 7] + S[w + 8] * right[f + 8] + S[w + 9] * right[f + 9] + S[w + 10] * right[f + 10] + S[w + 11] * right[f + 11] + S[w + 12] * right[f + 12] + S[w + 13] * right[f + 13] + S[w + 14] * right[f + 14] + S[w + 15] * right[f + 15] : l;
771
+ }
772
+ if (both) { acc[k] += l * a; accR[k] += r * a; } else acc[k] += 0.5 * (l + r) * a;
773
+ pos += step;
774
+ }
775
+ } else {
776
+ const S = kernel.table, phases = kernel.phases;
777
+ for (let k = 0; k < cnt; k++) {
778
+ if (loop !== null && loop !== undefined && pos >= loop[1]) pos -= loop[1] - loop[0];
779
+ const i = pos | 0;
780
+ if (i >= last) break;
781
+ const a = amp[k], w = ((pos - i) * phases + 0.5 | 0) * taps, f = i + offset;
782
+ let l = 0, r = 0;
783
+ if (two) for (let t = 0; t < taps; t++) { const c = S[w + t]; l += c * left[f + t]; r += c * right[f + t]; }
784
+ else { for (let t = 0; t < taps; t++) l += S[w + t] * left[f + t]; r = l; }
785
+ if (both) { acc[k] += l * a; accR[k] += r * a; } else acc[k] += 0.5 * (l + r) * a;
786
+ pos += step;
787
+ }
788
+ }
789
+ L.pos = pos;
790
+ break;
791
+ }
686
792
  case 7: { // fractional-delay Karplus–Strong plucked string
687
793
  const buffer = L.string, length = buffer.length;
688
794
  let index = L.stringIndex, previous = L.stringPrevious;
@@ -725,13 +831,26 @@ export class Synth {
725
831
  f.lastF = f.freq;
726
832
  }
727
833
  biquadRun(f, acc, 0, cnt);
834
+ if (v.stereo) {
835
+ const g = v.filterR;
836
+ if (g.lastF !== g.freq) { biquadCoefs(g.type, g.freq, g.q, sr, g); g.lastF = g.freq; }
837
+ biquadRun(g, accR, 0, cnt);
838
+ }
728
839
  }
729
840
  }
730
841
 
731
842
  const lane = v.lane;
732
843
  lane.dirty = true;
733
844
  const off = a - i0;
734
- if (lane.stereo) {
845
+ if (v.stereo) {
846
+ if (lane.stereo) {
847
+ const L = lane.L, R = lane.R;
848
+ for (let k = 0; k < cnt; k++) { L[off + k] += acc[k]; R[off + k] += accR[k]; }
849
+ } else {
850
+ const m = lane.mono;
851
+ for (let k = 0; k < cnt; k++) m[off + k] += 0.5 * (acc[k] + accR[k]);
852
+ }
853
+ } else if (lane.stereo) {
735
854
  const pan = this.pan;
736
855
  if (v.panEnv) v.panEnv.fill(pan, cnt);
737
856
  const L = lane.L;
@@ -763,7 +882,11 @@ export class Synth {
763
882
  }
764
883
  for (const lane of this.lanes) lane.clear(n);
765
884
  for (const v of this.active) this._renderVoice(v, i0, n);
766
- if (this.active.length) this.active = this.active.filter((v) => v.end > end);
885
+ if (this.active.length) {
886
+ // A finished voice lets go of the recording window it was reading.
887
+ for (const v of this.active) if (v.end <= end) for (const layer of v.layers) layer.source?.release();
888
+ this.active = this.active.filter((v) => v.end > end);
889
+ }
767
890
 
768
891
  const send = this.send;
769
892
  send.fill(0, 0, n);
package/src/catalog.js CHANGED
@@ -8,7 +8,7 @@ const attack = control(0.001, 3, 0.02, 'Attack time in seconds.');
8
8
  const detune = control(0, 30, 8, 'Oscillator spread in cents.');
9
9
  const definitions = [
10
10
  ['electric-piano', 'Electric piano', 'FM tine with a soft body and attack bark.', 4, { brightness }],
11
- ['piano', 'Grand piano', 'Recorded grand piano with soft and strong layers.', 0, { brightness: { ...brightness, default: 12000 }, release }],
11
+ ['piano', 'Grand piano', 'Recorded concert grand (Yamaha C5) in stereo with sixteen loudness layers per note, natural decay, damper and pedal sounds. Use the sustain pedal (score version 3) for legato and ringing chords.', 0, { brightness: { ...brightness, max: 20000, default: 20000, description: 'Low-pass cutoff in Hz; 20000 leaves the recording unfiltered.' }, release: { ...release, description: 'Release time in seconds of the synthesized piano; the recorded piano uses its dampers.' } }],
12
12
  ['pad', 'Warm pad', 'Filtered detuned saws with a slow swell.', 89, { brightness: { ...brightness, default: 1800 }, attack: { ...attack, default: 0.7 }, release: { ...release, default: 1.6 }, detune }],
13
13
  ['pluck', 'Plucked synth', 'A bright attack through a closing resonant filter.', 81, { brightness, release: { ...release, default: 0.3 } }],
14
14
  ['bass', 'Round bass', 'Sine and triangle with a firm low end.', 38, {}],
@@ -18,8 +18,8 @@ const definitions = [
18
18
  ['fm-lead', 'FM lead', 'Glass-like two-operator FM with a falling modulation index.', 80, { brightness, attack, release }],
19
19
  ['acid-bass', 'Acid bass', 'Resonant saw pluck with a closing filter.', 38, { brightness: { ...brightness, default: 1800 }, release: { ...release, default: 0.2 } }],
20
20
  ['organ', 'Organ', 'Sustained additive drawbar-like harmonics.', 16, { brightness, attack: { ...attack, default: 0.006 }, release: { ...release, default: 0.12 } }],
21
- ['strings', 'String section', 'Recorded violin, viola and cello sections with vibrato; long notes hold steadily.', 48, { brightness: { ...brightness, default: 9000 }, attack: { ...attack, default: 0.08 }, release: { ...release, default: 0.8 }, detune: { ...detune, default: 10, description: 'Doubles the section with a second take this many cents apart; 0 plays one take.' } }],
22
- ['brass', 'Brass section', 'Recorded trumpet (from C4 up) and trombone (below), doubled into a small section. Good for stabs and swells.', 61, { brightness: { ...brightness, default: 9000 }, attack: { ...attack, default: 0.01 }, release: { ...release, default: 0.2 }, detune: { ...detune, default: 10, description: 'Doubles the section with a second take this many cents apart; 0 plays one take.' } }],
21
+ ['strings', 'String section', 'Recorded violin, viola and cello sections with vibrato; long notes hold steadily.', 48, { brightness: { ...brightness, max: 20000, default: 20000, description: 'Low-pass cutoff in Hz; 20000 leaves the recording unfiltered.' }, attack: { ...attack, default: 0.08 }, release: { ...release, default: 0.8 }, detune: { ...detune, default: 10, description: 'Doubles the section with a second take this many cents apart; 0 plays one take.' } }],
22
+ ['brass', 'Brass section', 'Recorded trumpet (from C4 up) and trombone (below), doubled into a small section. Good for stabs and swells.', 61, { brightness: { ...brightness, max: 20000, default: 20000, description: 'Low-pass cutoff in Hz; 20000 leaves the recording unfiltered.' }, attack: { ...attack, default: 0.01 }, release: { ...release, default: 0.2 }, detune: { ...detune, default: 10, description: 'Doubles the section with a second take this many cents apart; 0 plays one take.' } }],
23
23
  ['kick', 'Electronic kick', 'Drum-machine kick: a sine pitch drop. For a real drum kit use rock-kick.', 0, {}],
24
24
  ['snare', 'Electronic snare', 'Drum-machine snare: filtered noise and a short tone. For a real drum kit use rock-snare.', 0, {}],
25
25
  ['clap', 'Clap', 'A cluster of noise strikes.', 0, {}],
@@ -27,9 +27,9 @@ const definitions = [
27
27
  ['shaker', 'Shaker', 'Soft high-pass noise grains.', 0, {}],
28
28
  ['noise', 'Noise texture', 'A held, filtered pink-noise envelope.', 0, { brightness: { ...brightness, default: 1600 }, attack: { ...attack, default: 0.4 }, release: { ...release, default: 0.8 } }],
29
29
  ['sample', 'Speech / sample', 'Local WAV playback, spoken textures and vocoder effects; not natural singing.', 0, {}],
30
- ['guitar-clean', 'Clean electric guitar', 'Recorded clean electric guitar, one note per pitch; play chords as several notes.', 27, { brightness: { ...brightness, default: 9000 }, release: { ...release, default: 1.2 }, palmMute: control(0, 1, 0, 'Shorten and damp the string for palm-muted articulation.') }],
30
+ ['guitar-clean', 'Clean electric guitar', 'Recorded clean electric guitar, one note per pitch; play chords as several notes.', 27, { brightness: { ...brightness, max: 20000, default: 20000, description: 'Low-pass cutoff in Hz; 20000 leaves the recording unfiltered.' }, release: { ...release, default: 1.2 }, palmMute: control(0, 1, 0, 'Shorten and damp the string for palm-muted articulation.') }],
31
31
  ['guitar-crunch', 'Crunch guitar', 'Recorded guitar through overdrive and a cabinet filter. Power chords (root and fifth, often an octave too) sound biggest.', 29, { brightness: { ...brightness, default: 5500 }, release: { ...release, default: 0.65 }, palmMute: control(0, 1, 0.2, 'Shorten and damp the string.'), drive: control(0, 1, 0.7, 'Overdrive amount before the part fader.') }],
32
- ['electric-bass', 'Electric bass', 'Recorded fingered electric bass with soft and strong layers.', 33, { brightness: { ...brightness, default: 5000 }, release: { ...release, default: 0.25 }, palmMute: control(0, 1, 0, 'Shorten and damp the string.') }],
32
+ ['electric-bass', 'Electric bass', 'Recorded fingered electric bass with five loudness layers and string release sounds.', 33, { brightness: { ...brightness, max: 20000, default: 20000, description: 'Low-pass cutoff in Hz; 20000 leaves the recording unfiltered.' }, release: { ...release, default: 0.25 }, palmMute: control(0, 1, 0, 'Shorten and damp the string.') }],
33
33
  ['rock-kick', 'Acoustic kick', 'Recorded kick drum of the acoustic kit (rock-kick, rock-snare, closed-hat, open-hat, tom, ride, crash).', 0, {}],
34
34
  ['rock-snare', 'Acoustic snare', 'Recorded snare drum of the acoustic kit.', 0, {}],
35
35
  ['closed-hat', 'Closed hi-hat', 'Recorded closed hi-hat of the acoustic kit.', 0, {}],
@@ -45,7 +45,7 @@ export const SAMPLE_EFFECTS = ['dry', 'tape', 'granular', 'vocoder', 'chop'];
45
45
  // English voices of the optional local Kokoro-82M speech model.
46
46
  export const SPEECH_VOICES = ['af_heart', 'af_alloy', 'af_aoede', 'af_bella', 'af_jessica', 'af_kore', 'af_nicole', 'af_nova', 'af_river', 'af_sarah', 'af_sky', 'am_adam', 'am_echo', 'am_eric', 'am_fenrir', 'am_liam', 'am_michael', 'am_onyx', 'am_puck', 'am_santa', 'bf_alice', 'bf_emma', 'bf_isabella', 'bf_lily', 'bm_daniel', 'bm_fable', 'bm_george', 'bm_lewis'];
47
47
  export const DEFAULT_SPEECH_VOICE = 'af_heart';
48
- export const SCORE_LIMITS = Object.freeze({ maxSeconds: 600, maxParts: 64, maxEvents: 100000, maxPatterns: 128, maxClips: 4096, maxNotationCharacters: 16384, maxSpeechRecipes: 16, maxSpeechCharacters: 200 });
48
+ export const SCORE_LIMITS = Object.freeze({ maxSeconds: 600, maxParts: 64, maxEvents: 100000, maxPatterns: 128, maxClips: 4096, maxNotationCharacters: 16384, maxSpeechRecipes: 16, maxSpeechCharacters: 200, maxTempoChanges: 1024, maxPedalSpans: 4096 });
49
49
  const instruments = definitions.map(([id, name, description, program, controls]) => ({ id, name, description, program, drums: ['kick', 'snare', 'clap', 'hi-hat', 'shaker', 'rock-kick', 'rock-snare', 'closed-hat', 'tom', 'crash', 'ride', 'open-hat'].includes(id), controls }));
50
50
  export function getCatalog() {
51
51
  return structuredClone({ version: 1, instruments, sampleAssets: SAMPLE_ASSETS.map(id => `builtin:${id}`), sampleEffects: SAMPLE_EFFECTS, speechVoices: SPEECH_VOICES, limits: SCORE_LIMITS, mix: { volume: { min: 0, max: 2, default: 1 }, pan: { min: -1, max: 1, default: 0 }, mute: false, solo: false }, master: { volume: { min: 0, max: 2, default: 0.8 }, reverb: { min: 0, max: 1, default: 0.2 } } });
@@ -8,6 +8,7 @@ import crypto from 'node:crypto';
8
8
  import { libraryPath, readJson, readScore, readMix, readCurrent, trackPath, sourceIdentity, VERSION } from '../project.js';
9
9
  import * as engine from '../headless.js';
10
10
  import { canonicalJson } from '../identity.js';
11
+ import { makeTimeline } from '../timeline.js';
11
12
  import { readSharing, serviceState, LIBRARY_UID } from './state.js';
12
13
  import { ShareError } from './errors.js';
13
14
 
@@ -67,7 +68,7 @@ export function inspectTrack(directory, id) {
67
68
  const check = engine.validate(score);
68
69
  if (!check.valid) { problem('invalid_score', 'The score has problems. Fix them and render again.', { issues: check.issues.slice(0, 12) }); return out; }
69
70
  const compiled = engine.compile(score);
70
- out.duration = compiled.beats * 60 / compiled.bpm;
71
+ out.duration = makeTimeline(compiled).duration;
71
72
  const local = (Array.isArray(score.parts) ? score.parts : []).filter(part => typeof part?.sample?.asset === 'string' && !part.sample.asset.startsWith('builtin:'));
72
73
  for (const part of local) {
73
74
  problem('local_audio', `Part “${part.name ?? part.id}” plays the local recording ${part.sample.asset}. The service renders only built-in voice phrases and spoken text recipes. Replace the asset with "speech": {"text": "…", "voice": "af_heart"} or remove the part, render again, then share.`, { part: part.id, asset: part.sample.asset });
package/src/headless.js CHANGED
@@ -17,9 +17,11 @@ import { coverSvg, SLEEVE_STYLES } from './artwork.js';
17
17
  import { getCatalog, SCORE_LIMITS, SPEECH_VOICES } from './catalog.js';
18
18
  import { engineHash, engineManifest, sourceIdentity, musicFingerprint } from './identity.js';
19
19
  import { SPEECH_MODEL, speechRecipes as recipesOf, speechFile, synthesizeSpeech, defaultSpeechDir } from './speech.js';
20
+ import { samplesStatus, scorePacks, installPacks, prepareSamples as prepareScoreSamples } from './samples-install.js';
21
+ import { defaultSamplesDir, MissingSamplesError } from './audio/library.js';
20
22
  import { VERSION } from './version.js';
21
23
 
22
- export { engineHash, engineManifest };
24
+ export { engineHash, engineManifest, defaultSamplesDir, MissingSamplesError };
23
25
  export const FINGERPRINT_VERSION = 'fp1';
24
26
  export const LIMITS = Object.freeze({ ...SCORE_LIMITS, ...RENDER_LIMITS });
25
27
  const programs = new Map(getCatalog().instruments.map(preset => [preset.id, preset.program]));
@@ -29,7 +31,7 @@ export function engineInfo() {
29
31
  return { version: VERSION, hash: engineHash(), scoreVersions: [...SCORE_VERSIONS], sounds: [...SOUNDS], fingerprint: FINGERPRINT_VERSION, limits: { ...LIMITS }, speech: { model: SPEECH_MODEL.id, dtype: SPEECH_MODEL.dtype, weights: SPEECH_MODEL.weights, sha256: SPEECH_MODEL.sha256, voices: [...SPEECH_VOICES] } };
30
32
  }
31
33
 
32
- /** {valid, issues: [{path, message}]} for score version 1 or 2. Never throws. */
34
+ /** {valid, issues: [{path, message}]} for score version 1, 2 or 3. Never throws. */
33
35
  export function validate(score) {
34
36
  return validateScore(score);
35
37
  }
@@ -44,10 +46,13 @@ export function compile(score) {
44
46
  /** Render a score to 16-bit stereo WAV bytes, exactly as the CLI and studio do.
45
47
  * Options: mix, sampleRate (44100), from/to seconds, stems, projectDir for local
46
48
  * assets/ WAVs, speech {cacheDir, synthesize, modelDir, offline, log},
47
- * progress, signal. Returns {revision, duration, sampleRate, wav, report,
48
- * parts, stems?: {partId: wav}}. Invalid scores throw ScoreValidationError;
49
- * cancelled renders throw AbortError; audio failing signal checks throws an
50
- * error carrying .report. */
49
+ * samplesDir (where the recorded instruments' packs are installed; see
50
+ * prepareSamples), sound ('recorded' or 'synthesized'), progress, signal.
51
+ * Returns {revision, duration, sampleRate, wav, report, parts, stems?:
52
+ * {partId: wav}}. Invalid scores throw ScoreValidationError; cancelled renders
53
+ * throw AbortError; recorded instruments whose packs are not installed throw
54
+ * MissingSamplesError (code samples_missing); audio failing signal checks
55
+ * throws an error carrying .report. Renders never download. */
51
56
  export async function render(score, { mix = {}, sampleRate = 44100, projectDir, ...options } = {}) {
52
57
  const result = await renderScore(score, { ...options, mix, sampleRate, projectDir });
53
58
  const revision = sourceIdentity(score, { mix, sampleRate, projectDir });
@@ -78,11 +83,34 @@ const PERCUSSION = { kick: 36, snare: 38, clap: 39, 'hi-hat': 42, shaker: 70, 'r
78
83
  // GM toms occupy a sparse range; other pitches would select cymbals or effects.
79
84
  const tomPitch = pitch => [41, 43, 45, 47, 48, 50].reduce((a, b) => Math.abs(b - pitch) < Math.abs(a - pitch) ? b : a);
80
85
  /** Standard MIDI file bytes (format 1, 480 ticks per beat): a conductor track
81
- * with tempo and section markers, then one track per instrument with its GM
82
- * program; drums on channel 10. Sample and noise parts are left out. */
86
+ * with tempo (changes too; ramps in sixteenth-note steps) and section markers,
87
+ * then one track per instrument with its GM program and sustain pedal
88
+ * (controller 64); drums on channel 10. Sample and noise parts are left out. */
83
89
  export function midi(score) {
84
90
  const expanded = compileScore(score);
85
- return encodeMidi({ title: expanded.title, bpm: expanded.bpm, sections: expanded.sections, tracks: expanded.parts.filter(p => !['sample', 'noise'].includes(p.instrument)).map(p => ({ name: p.name, program: programs.get(p.instrument), drums: p.instrument in PERCUSSION, notes: p.notes.map(n => ({ ...n, pitch: p.instrument === 'tom' ? tomPitch(n.pitch) : PERCUSSION[p.instrument] ?? n.pitch })) })) });
91
+ return encodeMidi({ title: expanded.title, bpm: expanded.bpm, tempo: expanded.tempo, sections: expanded.sections, tracks: expanded.parts.filter(p => !['sample', 'noise'].includes(p.instrument)).map(p => ({ name: p.name, program: programs.get(p.instrument), drums: p.instrument in PERCUSSION, pedal: p.pedal, notes: p.notes.map(n => ({ ...n, pitch: p.instrument === 'tom' ? tomPitch(n.pitch) : PERCUSSION[p.instrument] ?? n.pitch })) })) });
92
+ }
93
+
94
+ /** The sample packs of the recorded instruments: [{name, file, size,
95
+ * installed, instruments}]. Option: samplesDir. */
96
+ export function samples(options) {
97
+ return samplesStatus(options);
98
+ }
99
+
100
+ /** The packs a score's recorded instruments play from, by name. */
101
+ export function samplePacks(score) {
102
+ return scorePacks(score);
103
+ }
104
+
105
+ /** Downloads the packs a score needs that are not installed yet, and checks
106
+ * them (see samples-install.js). Options: samplesDir, log, signal. */
107
+ export async function prepareSamples(score, options) {
108
+ return prepareScoreSamples(score, options);
109
+ }
110
+
111
+ /** Downloads the named packs (all packs when names is omitted). */
112
+ export async function installSamples(names, options) {
113
+ return installPacks(names ?? samplesStatus(options).map(pack => pack.name), options);
86
114
  }
87
115
 
88
116
  /** The original 800×800 SVG sleeve the studio shows. Pass the track's position
package/src/identity.js CHANGED
@@ -19,8 +19,9 @@ import { getCatalog } from './catalog.js';
19
19
  import { audibleNote, audibleSample } from './render.js';
20
20
 
21
21
  const ROOT = path.dirname(path.dirname(fileURLToPath(import.meta.url)));
22
- const ENGINE_FILES = ['src/identity.js', 'src/catalog.js', 'src/headless.js', 'src/notation.js', 'src/render.js', 'src/score.js', 'src/speech.js'];
23
- const ENGINE_FOLDERS = [['src/audio', '.js'], ['assets/voices', '.wav'], ['assets/samples', '.wav'], ['assets/samples', '.json']];
22
+ const ENGINE_FILES = ['src/identity.js', 'src/catalog.js', 'src/headless.js', 'src/notation.js', 'src/render.js', 'src/score.js', 'src/speech.js', 'src/timeline.js'];
23
+ // The sample manifest carries the size and SHA-256 of every sample pack, so it binds the engine to the exact recordings.
24
+ const ENGINE_FOLDERS = [['src/audio', '.js'], ['assets/voices', '.wav'], ['assets/samples', '.json'], ['assets/reverb', '.wav']];
24
25
  const presets = new Map(getCatalog().instruments.map(preset => [preset.id, preset]));
25
26
  const sha256 = data => crypto.createHash('sha256').update(data).digest('hex');
26
27
 
@@ -65,13 +66,22 @@ const byValues = (a, b) => {
65
66
  for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return a[i] === null ? -1 : b[i] === null ? 1 : a[i] - b[i];
66
67
  return 0;
67
68
  };
69
+ // fp1 counts an unset tone control at its default. Engine 0.8.0 raised the brightness defaults of the
70
+ // recorded instruments (they now play unfiltered); fingerprints keep the defaults fp1 was defined with, so
71
+ // an unchanged score keeps its fingerprint across engines.
72
+ const FINGERPRINT_DEFAULTS = { piano: { brightness: 12000 }, strings: { brightness: 9000 }, brass: { brightness: 9000 }, 'guitar-clean': { brightness: 9000 }, 'electric-bass': { brightness: 5000 } };
73
+
68
74
  export function musicFingerprint(score) {
69
75
  const expanded = compileScore(score);
76
+ // Tempo changes and the sustain pedal (score version 3) are part of the music; scores without them keep
77
+ // the fingerprint they had before version 3 existed.
70
78
  const parts = expanded.parts.filter(part => part.volume > 0 && part.notes.length).map(part => canonicalJson({
71
79
  instrument: part.instrument,
72
- tone: round(Object.fromEntries(Object.entries(presets.get(part.instrument).controls).map(([name, spec]) => [name, part.tone[name] ?? spec.default]))),
80
+ tone: round(Object.fromEntries(Object.entries(presets.get(part.instrument).controls).map(([name, spec]) => [name, part.tone[name] ?? FINGERPRINT_DEFAULTS[part.instrument]?.[name] ?? spec.default]))),
73
81
  sample: part.sample ? round(audibleSample(part.sample)) : null,
74
82
  notes: part.notes.map(note => round(Object.values(audibleNote(part.instrument, note)))).sort(byValues),
83
+ ...(part.pedal?.length ? { pedal: round(part.pedal.map(span => [span.beat, span.beats])) } : {}),
75
84
  })).sort();
76
- return 'fp1:' + sha256(`{"bpm":${round(expanded.bpm)},"parts":[${parts.join(',')}]}`);
85
+ const tempo = expanded.tempo?.length ? `,"tempo":${canonicalJson(round(expanded.tempo.map(change => [change.beat, change.bpm, change.ramp])))}` : '';
86
+ return 'fp1:' + sha256(`{"bpm":${round(expanded.bpm)}${tempo},"parts":[${parts.join(',')}]}`);
77
87
  }
package/src/project.js CHANGED
@@ -3,6 +3,7 @@ import path from 'node:path';
3
3
  import crypto from 'node:crypto';
4
4
  import { fileURLToPath } from 'node:url';
5
5
  import * as engine from './headless.js';
6
+ import { makeTimeline } from './timeline.js';
6
7
 
7
8
  export { VERSION } from './version.js';
8
9
  export const ROOT = path.dirname(path.dirname(fileURLToPath(import.meta.url)));
@@ -130,7 +131,7 @@ export function saveMix(directory, id, mix) {
130
131
  export function saveFeedback(directory, id, { text, time = 0 }) {
131
132
  const score = readScore(directory, id);
132
133
  if (typeof text !== 'string' || !text.trim() || text.length > 4000) throw new Error('Write a note between 1 and 4,000 characters.');
133
- const duration = readCurrent(directory, id)?.duration ?? score.beats * 60 / score.bpm + 10;
134
+ const duration = readCurrent(directory, id)?.duration ?? scoreTimeline(score).duration + 10;
134
135
  if (!Number.isFinite(time) || time < 0 || time > duration + 1) throw new Error('Choose a time inside the track.');
135
136
  const entry = { id: crypto.randomBytes(8).toString('hex'), text: text.trim(), time, date: new Date().toISOString() };
136
137
  const entries = readJson(trackPath(directory, id, 'feedback.json'), []); entries.push(entry);
@@ -158,25 +159,35 @@ export function readRequests(directory) {
158
159
  return fs.readdirSync(dir).filter(n => n.endsWith('.json')).sort().reverse().map(n => ({ ...readJson(path.join(dir, n)), path: path.join(dir, n) }));
159
160
  }
160
161
 
162
+ // Beats to seconds for a stored score; a score that does not compile plays at its bpm.
163
+ function scoreTimeline(score) {
164
+ try { return makeTimeline(engine.compile(score)); } catch { return makeTimeline({ bpm: score.bpm > 0 ? score.bpm : 120, beats: score.beats > 0 ? score.beats : 0 }); }
165
+ }
166
+
161
167
  export function trackInfo(directory, id, { detail = false } = {}) {
162
168
  const score = readScore(directory, id), mix = readMix(directory, id), current = readCurrent(directory, id);
163
169
  const state = readJson(trackPath(directory, id, 'state.json'), {});
164
170
  const versions = readVersions(directory, id).map(v => ({ ...v, audioUrl: `/api/tracks/${id}/versions/${v.id}/audio` }));
165
171
  const report = current ? readJson(trackPath(directory, id, 'renders', current.id + '.report.json'), {}) : {};
166
- const seconds = 60 / score.bpm;
172
+ const timeline = scoreTimeline(score);
167
173
  const info = {
168
174
  id, title: score.title, description: score.description ?? '', genre: score.genre ?? '', mood: score.mood ?? '', bpm: score.bpm, key: score.key ?? '',
169
- duration: current?.duration ?? score.beats * seconds, rendered: Boolean(current && fs.existsSync(trackPath(directory, id, 'renders', current.id + '.wav'))),
175
+ duration: current?.duration ?? timeline.duration, rendered: Boolean(current && fs.existsSync(trackPath(directory, id, 'renders', current.id + '.wav'))),
170
176
  favorite: Boolean(state.favorite), origin: score.origin ?? 'personal', audioUrl: `/api/tracks/${id}/audio`, artworkUrl: `/api/tracks/${id}/artwork`,
171
177
  waveform: report.waveform ?? [], revision: current?.id ?? null, versions,
172
- sections: (score.sections ?? []).map(s => ({ id: s.id, name: s.name, start: s.beat * seconds, end: (s.beat + s.beats) * seconds })),
178
+ sections: (score.sections ?? []).map(s => ({ id: s.id, name: s.name, start: timeline.seconds(s.beat), end: timeline.seconds(s.beat + s.beats) })),
173
179
  parts: (score.parts ?? []).map((p, i) => ({ id: p.id, name: p.name ?? p.id, instrument: p.instrument, volume: mix.parts?.[p.id]?.volume ?? p.volume ?? 1, pan: mix.parts?.[p.id]?.pan ?? p.pan ?? 0, mute: mix.parts?.[p.id]?.mute ?? false, solo: mix.parts?.[p.id]?.solo ?? false, color: p.color ?? ['#ef946b', '#a9bce8', '#b9d0ac', '#b69ccf', '#e2ce87'][i % 5] })),
174
180
  };
175
181
  if (detail) {
176
182
  Object.assign(info, { score, mix: { ...mix, master: { volume: score.master?.volume ?? 0.8, ...mix.master } }, report, feedback: readJson(trackPath(directory, id, 'feedback.json'), []) });
177
- // The studio draws version 2 pattern strings from their compiled notes.
178
- if (score.version === 2) {
179
- try { const expanded = engine.compile(score); info.arrangement = { bpm: expanded.bpm, parts: expanded.parts.map(({ id: part, name, notes }) => ({ id: part, name, notes })) }; } catch { /* invalid scores show parts without notes */ }
183
+ // The studio draws version 2 pattern strings from their compiled notes, and version 3 notes at their
184
+ // times in seconds, which tempo changes move.
185
+ if (score.version === 2 || score.version === 3) {
186
+ try {
187
+ const expanded = engine.compile(score);
188
+ const placed = notes => timeline.constant ? notes : notes.map(n => ({ pitch: n.pitch, time: timeline.seconds(n.beat), length: timeline.seconds(n.beat + n.duration) - timeline.seconds(n.beat), velocity: n.velocity }));
189
+ info.arrangement = { bpm: expanded.bpm, parts: expanded.parts.map(({ id: part, name, notes }) => ({ id: part, name, notes: placed(notes) })) };
190
+ } catch { /* invalid scores show parts without notes */ }
180
191
  }
181
192
  }
182
193
  return info;
@@ -202,6 +213,8 @@ export async function renderTrack(directory, id, { sampleRate = 44100, stems = f
202
213
  const cached = fs.existsSync(wavFile) && fs.existsSync(reportFile) && (!stems || fs.existsSync(stemsDir));
203
214
  let report;
204
215
  if (!cached) {
216
+ // The recorded instruments' packs are downloaded the first time a track needs them.
217
+ if (sound === 'recorded') await engine.prepareSamples(score, { log });
205
218
  const result = await engine.render(score, { sampleRate, mix, projectDir: libraryPath(directory), stems, progress, sound, speech: { log } });
206
219
  report = result.report;
207
220
  const temporary = wavFile + '.tmp'; fs.writeFileSync(temporary, result.wav, { mode: 0o600 }); fs.renameSync(temporary, wavFile);