@libraz/libsonare 1.7.2 → 1.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 (303) hide show
  1. package/NOTICE +178 -0
  2. package/README.md +26 -203
  3. package/dist/_chain_config.d.ts +14 -0
  4. package/dist/_chain_config.d.ts.map +1 -0
  5. package/dist/_effects_common.d.ts +6 -0
  6. package/dist/_effects_common.d.ts.map +1 -0
  7. package/dist/_feature_validation.d.ts +8 -0
  8. package/dist/_feature_validation.d.ts.map +1 -0
  9. package/dist/_fft_options.d.ts +24 -0
  10. package/dist/_fft_options.d.ts.map +1 -0
  11. package/dist/align_take.d.ts +50 -0
  12. package/dist/align_take.d.ts.map +1 -0
  13. package/dist/analysis.d.ts +29 -5840
  14. package/dist/analysis.d.ts.map +1 -0
  15. package/dist/analysis.js +873 -722
  16. package/dist/analysis.js.map +1 -1
  17. package/dist/analysis_helpers.d.ts +9 -0
  18. package/dist/analysis_helpers.d.ts.map +1 -0
  19. package/dist/audio.d.ts +163 -0
  20. package/dist/audio.d.ts.map +1 -0
  21. package/dist/clip_page_streamer.d.ts +133 -0
  22. package/dist/clip_page_streamer.d.ts.map +1 -0
  23. package/dist/codes.d.ts +44 -0
  24. package/dist/codes.d.ts.map +1 -0
  25. package/dist/effects_mastering.d.ts +23 -0
  26. package/dist/effects_mastering.d.ts.map +1 -0
  27. package/dist/effects_note_ops.d.ts +477 -0
  28. package/dist/effects_note_ops.d.ts.map +1 -0
  29. package/dist/effects_percussive.d.ts +185 -0
  30. package/dist/effects_percussive.d.ts.map +1 -0
  31. package/dist/effects_separation.d.ts +65 -0
  32. package/dist/effects_separation.d.ts.map +1 -0
  33. package/dist/effects_spectral.d.ts +28 -0
  34. package/dist/effects_spectral.d.ts.map +1 -0
  35. package/dist/effects_timepitch.d.ts +129 -0
  36. package/dist/effects_timepitch.d.ts.map +1 -0
  37. package/dist/effects_voice_change.d.ts +53 -0
  38. package/dist/effects_voice_change.d.ts.map +1 -0
  39. package/dist/errors.d.ts +51 -0
  40. package/dist/errors.d.ts.map +1 -0
  41. package/dist/feature_core.d.ts +341 -0
  42. package/dist/feature_core.d.ts.map +1 -0
  43. package/dist/feature_decompose.d.ts +278 -0
  44. package/dist/feature_decompose.d.ts.map +1 -0
  45. package/dist/feature_inverse.d.ts +128 -0
  46. package/dist/feature_inverse.d.ts.map +1 -0
  47. package/dist/feature_loudness.d.ts +66 -0
  48. package/dist/feature_loudness.d.ts.map +1 -0
  49. package/dist/feature_music.d.ts +307 -0
  50. package/dist/feature_music.d.ts.map +1 -0
  51. package/dist/feature_pitch.d.ts +108 -0
  52. package/dist/feature_pitch.d.ts.map +1 -0
  53. package/dist/feature_resample.d.ts +16 -0
  54. package/dist/feature_resample.d.ts.map +1 -0
  55. package/dist/feature_spectral.d.ts +137 -0
  56. package/dist/feature_spectral.d.ts.map +1 -0
  57. package/dist/feature_spectrogram.d.ts +198 -0
  58. package/dist/feature_spectrogram.d.ts.map +1 -0
  59. package/dist/features.d.ts +10 -0
  60. package/dist/features.d.ts.map +1 -0
  61. package/dist/hrtf/default.shrf +0 -0
  62. package/dist/index.d.ts +74 -7431
  63. package/dist/index.d.ts.map +1 -0
  64. package/dist/index.js +3758 -1362
  65. package/dist/index.js.map +1 -1
  66. package/dist/instrument_types.d.ts +517 -0
  67. package/dist/instrument_types.d.ts.map +1 -0
  68. package/dist/live_audio.d.ts +35 -0
  69. package/dist/live_audio.d.ts.map +1 -0
  70. package/dist/mastering_chain.d.ts +213 -0
  71. package/dist/mastering_chain.d.ts.map +1 -0
  72. package/dist/mastering_core.d.ts +457 -0
  73. package/dist/mastering_core.d.ts.map +1 -0
  74. package/dist/mastering_dynamics.d.ts +80 -0
  75. package/dist/mastering_dynamics.d.ts.map +1 -0
  76. package/dist/metering.d.ts +287 -0
  77. package/dist/metering.d.ts.map +1 -0
  78. package/dist/mixer.d.ts +464 -0
  79. package/dist/mixer.d.ts.map +1 -0
  80. package/dist/mixing_assistant.d.ts +62 -0
  81. package/dist/mixing_assistant.d.ts.map +1 -0
  82. package/dist/mixing_oneshot.d.ts +40 -0
  83. package/dist/mixing_oneshot.d.ts.map +1 -0
  84. package/dist/module_state.d.ts +15 -0
  85. package/dist/module_state.d.ts.map +1 -0
  86. package/dist/opfs_clip_pages.d.ts +28 -0
  87. package/dist/opfs_clip_pages.d.ts.map +1 -0
  88. package/dist/playback_renderer.d.ts +128 -0
  89. package/dist/playback_renderer.d.ts.map +1 -0
  90. package/dist/polyphony.d.ts +202 -0
  91. package/dist/polyphony.d.ts.map +1 -0
  92. package/dist/project.d.ts +8 -0
  93. package/dist/project.d.ts.map +1 -0
  94. package/dist/project_class.d.ts +562 -0
  95. package/dist/project_class.d.ts.map +1 -0
  96. package/dist/project_internal.d.ts +194 -0
  97. package/dist/project_internal.d.ts.map +1 -0
  98. package/dist/project_synth.d.ts +74 -0
  99. package/dist/project_synth.d.ts.map +1 -0
  100. package/dist/project_types.d.ts +652 -0
  101. package/dist/project_types.d.ts.map +1 -0
  102. package/dist/public_types.d.ts +185 -0
  103. package/dist/public_types.d.ts.map +1 -0
  104. package/dist/public_types_acoustic.d.ts +215 -0
  105. package/dist/public_types_acoustic.d.ts.map +1 -0
  106. package/dist/public_types_mastering.d.ts +510 -0
  107. package/dist/public_types_mastering.d.ts.map +1 -0
  108. package/dist/public_types_mixing.d.ts +436 -0
  109. package/dist/public_types_mixing.d.ts.map +1 -0
  110. package/dist/public_types_music.d.ts +619 -0
  111. package/dist/public_types_music.d.ts.map +1 -0
  112. package/dist/public_types_playback.d.ts +164 -0
  113. package/dist/public_types_playback.d.ts.map +1 -0
  114. package/dist/public_types_realtime.d.ts +174 -0
  115. package/dist/public_types_realtime.d.ts.map +1 -0
  116. package/dist/public_types_repair.d.ts +424 -0
  117. package/dist/public_types_repair.d.ts.map +1 -0
  118. package/dist/public_types_spectral.d.ts +697 -0
  119. package/dist/public_types_spectral.d.ts.map +1 -0
  120. package/dist/quick_analysis.d.ts +445 -0
  121. package/dist/quick_analysis.d.ts.map +1 -0
  122. package/dist/realtime_engine.d.ts +850 -0
  123. package/dist/realtime_engine.d.ts.map +1 -0
  124. package/dist/realtime_voice_changer.d.ts +158 -0
  125. package/dist/realtime_voice_changer.d.ts.map +1 -0
  126. package/dist/repair_dereverb.d.ts +187 -0
  127. package/dist/repair_dereverb.d.ts.map +1 -0
  128. package/dist/repair_impulsive.d.ts +186 -0
  129. package/dist/repair_impulsive.d.ts.map +1 -0
  130. package/dist/repair_noise.d.ts +239 -0
  131. package/dist/repair_noise.d.ts.map +1 -0
  132. package/dist/repair_trim.d.ts +123 -0
  133. package/dist/repair_trim.d.ts.map +1 -0
  134. package/dist/sample_bank.d.ts +84 -0
  135. package/dist/sample_bank.d.ts.map +1 -0
  136. package/dist/scale.d.ts +10 -0
  137. package/dist/scale.d.ts.map +1 -0
  138. package/dist/schemas/mixer-scene.schema.json +393 -0
  139. package/dist/schemas/playback-renderer-config.schema.json +392 -0
  140. package/dist/sonare-analysis.d.ts +8 -0
  141. package/dist/sonare-analysis.js +2 -2
  142. package/dist/sonare-analysis.wasm +0 -0
  143. package/dist/sonare.d.ts +3909 -0
  144. package/dist/sonare.js +2 -2
  145. package/dist/sonare.wasm +0 -0
  146. package/dist/stream_analyzer.d.ts +163 -0
  147. package/dist/stream_analyzer.d.ts.map +1 -0
  148. package/dist/stream_types.d.ts +214 -0
  149. package/dist/stream_types.d.ts.map +1 -0
  150. package/dist/streaming_mixing.d.ts +6 -0
  151. package/dist/streaming_mixing.d.ts.map +1 -0
  152. package/dist/streaming_processors.d.ts +335 -0
  153. package/dist/streaming_processors.d.ts.map +1 -0
  154. package/dist/transcribe.d.ts +77 -0
  155. package/dist/transcribe.d.ts.map +1 -0
  156. package/dist/validation.d.ts +140 -0
  157. package/dist/validation.d.ts.map +1 -0
  158. package/dist/web_midi.d.ts +77 -0
  159. package/dist/web_midi.d.ts.map +1 -0
  160. package/dist/worker.d.ts +5 -48
  161. package/dist/worker.d.ts.map +1 -0
  162. package/dist/worker.js +94 -41
  163. package/dist/worker.js.map +1 -1
  164. package/dist/worker_client.d.ts +96 -0
  165. package/dist/worker_client.d.ts.map +1 -0
  166. package/dist/worker_protocol.d.ts +43 -0
  167. package/dist/worker_protocol.d.ts.map +1 -0
  168. package/dist/worklet/audio_types.d.ts +21 -0
  169. package/dist/worklet/audio_types.d.ts.map +1 -0
  170. package/dist/worklet/engine-automation.d.ts +29 -0
  171. package/dist/worklet/engine-automation.d.ts.map +1 -0
  172. package/dist/worklet/engine-capture-facade.d.ts +35 -0
  173. package/dist/worklet/engine-capture-facade.d.ts.map +1 -0
  174. package/dist/worklet/engine-clips.d.ts +23 -0
  175. package/dist/worklet/engine-clips.d.ts.map +1 -0
  176. package/dist/worklet/engine-markers.d.ts +40 -0
  177. package/dist/worklet/engine-markers.d.ts.map +1 -0
  178. package/dist/worklet/engine-mixer-facade.d.ts +128 -0
  179. package/dist/worklet/engine-mixer-facade.d.ts.map +1 -0
  180. package/dist/worklet/engine-node.d.ts +81 -0
  181. package/dist/worklet/engine-node.d.ts.map +1 -0
  182. package/dist/worklet/engine-offline.d.ts +81 -0
  183. package/dist/worklet/engine-offline.d.ts.map +1 -0
  184. package/dist/worklet/engine-options.d.ts +12 -0
  185. package/dist/worklet/engine-options.d.ts.map +1 -0
  186. package/dist/worklet/engine-parameter-facade.d.ts +106 -0
  187. package/dist/worklet/engine-parameter-facade.d.ts.map +1 -0
  188. package/dist/worklet/engine-processor.d.ts +68 -0
  189. package/dist/worklet/engine-processor.d.ts.map +1 -0
  190. package/dist/worklet/engine-register.d.ts +2 -0
  191. package/dist/worklet/engine-register.d.ts.map +1 -0
  192. package/dist/worklet/engine-strips.d.ts +73 -0
  193. package/dist/worklet/engine-strips.d.ts.map +1 -0
  194. package/dist/worklet/engine-sync.d.ts +37 -0
  195. package/dist/worklet/engine-sync.d.ts.map +1 -0
  196. package/dist/worklet/engine-tempo-facade.d.ts +48 -0
  197. package/dist/worklet/engine-tempo-facade.d.ts.map +1 -0
  198. package/dist/worklet/engine.d.ts +397 -0
  199. package/dist/worklet/engine.d.ts.map +1 -0
  200. package/dist/worklet/guards.d.ts +47 -0
  201. package/dist/worklet/guards.d.ts.map +1 -0
  202. package/dist/worklet/messages.d.ts +710 -0
  203. package/dist/worklet/messages.d.ts.map +1 -0
  204. package/dist/worklet/mixer-processor.d.ts +46 -0
  205. package/dist/worklet/mixer-processor.d.ts.map +1 -0
  206. package/dist/worklet/playback-processor.d.ts +62 -0
  207. package/dist/worklet/playback-processor.d.ts.map +1 -0
  208. package/dist/worklet/protocol.d.ts +323 -0
  209. package/dist/worklet/protocol.d.ts.map +1 -0
  210. package/dist/worklet/voice-changer-processor.d.ts +41 -0
  211. package/dist/worklet/voice-changer-processor.d.ts.map +1 -0
  212. package/dist/worklet.d.ts +16 -2515
  213. package/dist/worklet.d.ts.map +1 -0
  214. package/dist/worklet.js +2763 -501
  215. package/dist/worklet.js.map +1 -1
  216. package/package.json +23 -12
  217. package/src/_effects_common.ts +17 -0
  218. package/src/_feature_validation.ts +34 -0
  219. package/src/_fft_options.ts +39 -0
  220. package/src/align_take.ts +64 -0
  221. package/src/analysis.ts +56 -3
  222. package/src/analysis_helpers.ts +7 -0
  223. package/src/audio.ts +106 -3
  224. package/src/codes.ts +39 -2
  225. package/src/effects_mastering.ts +97 -22
  226. package/src/effects_note_ops.ts +635 -0
  227. package/src/effects_percussive.ts +217 -0
  228. package/src/effects_separation.ts +150 -0
  229. package/src/effects_spectral.ts +60 -0
  230. package/src/effects_timepitch.ts +377 -0
  231. package/src/errors.ts +23 -1
  232. package/src/feature_core.ts +127 -2
  233. package/src/feature_decompose.ts +633 -0
  234. package/src/feature_inverse.ts +454 -0
  235. package/src/feature_loudness.ts +125 -0
  236. package/src/feature_music.ts +107 -14
  237. package/src/feature_pitch.ts +96 -1
  238. package/src/feature_spectral.ts +16 -611
  239. package/src/feature_spectrogram.ts +63 -450
  240. package/src/features.ts +36 -22
  241. package/src/index.ts +282 -30
  242. package/src/instrument_types.ts +645 -0
  243. package/src/live_audio.ts +27 -1
  244. package/src/mastering_chain.ts +184 -0
  245. package/src/mastering_core.ts +346 -32
  246. package/src/mastering_dynamics.ts +22 -11
  247. package/src/metering.ts +67 -24
  248. package/src/mixer.ts +212 -3
  249. package/src/mixing_assistant.ts +138 -0
  250. package/src/mixing_oneshot.ts +10 -5
  251. package/src/module_state.ts +24 -2
  252. package/src/playback_renderer.ts +252 -0
  253. package/src/polyphony.ts +279 -0
  254. package/src/project.ts +61 -24
  255. package/src/project_class.ts +450 -27
  256. package/src/project_internal.ts +149 -42
  257. package/src/project_synth.ts +67 -1
  258. package/src/project_types.ts +268 -270
  259. package/src/public_types.ts +122 -3
  260. package/src/public_types_acoustic.ts +112 -3
  261. package/src/public_types_mastering.ts +254 -73
  262. package/src/public_types_mixing.ts +363 -1
  263. package/src/public_types_music.ts +312 -2
  264. package/src/public_types_playback.ts +195 -0
  265. package/src/public_types_realtime.ts +39 -7
  266. package/src/public_types_repair.ts +446 -0
  267. package/src/public_types_spectral.ts +487 -1
  268. package/src/quick_analysis.ts +203 -26
  269. package/src/realtime_engine.ts +711 -28
  270. package/src/realtime_voice_changer.ts +55 -1
  271. package/src/repair_dereverb.ts +299 -0
  272. package/src/repair_impulsive.ts +395 -0
  273. package/src/repair_noise.ts +425 -0
  274. package/src/repair_trim.ts +226 -0
  275. package/src/sample_bank.ts +113 -0
  276. package/src/sonare.js.d.ts +1122 -30
  277. package/src/stream_analyzer.ts +36 -4
  278. package/src/stream_types.ts +37 -0
  279. package/src/streaming_mixing.ts +1 -1
  280. package/src/streaming_processors.ts +194 -10
  281. package/src/transcribe.ts +89 -0
  282. package/src/validation.ts +285 -11
  283. package/src/web_midi.ts +1 -6
  284. package/src/worker.ts +18 -2
  285. package/src/worklet/audio_types.ts +37 -0
  286. package/src/worklet/engine-mixer-facade.ts +444 -10
  287. package/src/worklet/engine-node.ts +78 -26
  288. package/src/worklet/engine-offline.ts +14 -8
  289. package/src/worklet/engine-parameter-facade.ts +21 -0
  290. package/src/worklet/engine-processor.ts +272 -83
  291. package/src/worklet/engine-register.ts +32 -18
  292. package/src/worklet/engine-strips.ts +280 -9
  293. package/src/worklet/engine-sync.ts +14 -6
  294. package/src/worklet/engine.ts +365 -31
  295. package/src/worklet/guards.ts +140 -44
  296. package/src/worklet/messages.ts +227 -2
  297. package/src/worklet/mixer-processor.ts +109 -47
  298. package/src/worklet/playback-processor.ts +300 -0
  299. package/src/worklet/protocol.ts +53 -3
  300. package/src/worklet/voice-changer-processor.ts +17 -11
  301. package/src/worklet.ts +17 -0
  302. package/src/effects_transform.ts +0 -718
  303. package/src/mastering_repair.ts +0 -273
@@ -0,0 +1,635 @@
1
+ /**
2
+ * Note-level editing: segmenting audio and a pitch track into editable notes,
3
+ * rendering an edited set back, and the split/merge operations over it.
4
+ */
5
+
6
+ import { toVoicedFloat32 } from './_effects_common';
7
+ import { getSonareModule } from './module_state';
8
+ import type {
9
+ NoteExtractorOptions,
10
+ NoteMoveOptions,
11
+ NoteObject,
12
+ NoteObjectInput,
13
+ NoteSetEntry,
14
+ NoteStretchOptions,
15
+ NoteTarget,
16
+ NoteTargetAssignResult,
17
+ NoteTargetUnmatchedPolicy,
18
+ PitchDecompositionResult,
19
+ VoicedFlags,
20
+ } from './public_types';
21
+ import type { ValidateOptions } from './validation';
22
+ import { assertSampleRate, assertSamples } from './validation';
23
+
24
+ function requireModule() {
25
+ return getSonareModule();
26
+ }
27
+
28
+ // Shared input checks of the entry points that take audio plus a whole F0 track,
29
+ // returning the voicing flags in the Float32Array form the embind layer reads. A
30
+ // companion array of the wrong length is a RangeError here because the native
31
+ // side can only report it as a flat InvalidParameter.
32
+ function assertNoteTrack(
33
+ fnName: string,
34
+ request: ExtractNotesRequest | NoteSetRequest,
35
+ ): Float32Array | undefined {
36
+ assertSamples(fnName, request.samples, request.validate !== false);
37
+ assertSampleRate(fnName, request.sampleRate);
38
+ if (request.voiced && request.voiced.length !== request.f0Hz.length) {
39
+ throw new RangeError(`${fnName}: voiced length must match f0Hz length`);
40
+ }
41
+ if (request.voicedProb && request.voicedProb.length !== request.f0Hz.length) {
42
+ throw new RangeError(`${fnName}: voicedProb length must match f0Hz length`);
43
+ }
44
+ return request.voiced ? toVoicedFloat32(request.voiced) : undefined;
45
+ }
46
+
47
+ export interface NoteStretchRequest extends NoteStretchOptions, ValidateOptions {
48
+ samples: Float32Array;
49
+ sampleRate?: number;
50
+ }
51
+
52
+ export interface NoteMoveRequest extends NoteMoveOptions, ValidateOptions {
53
+ samples: Float32Array;
54
+ sampleRate?: number;
55
+ }
56
+
57
+ /** Canonical request form for {@link extractNotes}. */
58
+ export interface ExtractNotesRequest extends NoteExtractorOptions, ValidateOptions {
59
+ samples: Float32Array;
60
+ /**
61
+ * Sample rate in Hz. Required: `minNoteMs` and the per-frame RMS windows are
62
+ * converted to samples with this rate, so a wrong/omitted value silently
63
+ * segments differently.
64
+ */
65
+ sampleRate: number;
66
+ /**
67
+ * Per-frame F0 in Hz. A frame carrying no pitch is spelled as zero, a
68
+ * negative value or a non-finite one, and all three read the same: that
69
+ * frame contributes no measurement. A {@link pitchPyin} track can be passed
70
+ * straight through — its `fillNa` is a choice about the contour you want,
71
+ * not a requirement of this call.
72
+ */
73
+ f0Hz: Float32Array;
74
+ /** F0 frames per second. */
75
+ frameRate: number;
76
+ /** Per-frame voiced flags (truthy = voiced). Takes precedence over `voicedProb`. */
77
+ voiced?: VoicedFlags;
78
+ /** Per-frame voicing probability in `[0, 1]`; read only when `voiced` is omitted. */
79
+ voicedProb?: Float32Array;
80
+ }
81
+
82
+ /** Canonical request form for {@link renderNotes}. */
83
+ export interface RenderNotesRequest extends ValidateOptions {
84
+ samples: Float32Array;
85
+ /**
86
+ * Sample rate in Hz. Required: `fadeMs` is converted to samples with this
87
+ * rate, so a wrong/omitted value changes the cross-fade length.
88
+ */
89
+ sampleRate: number;
90
+ /** The notes to render, with their edits. Source spans must not overlap. */
91
+ notes: readonly NoteObjectInput[];
92
+ /**
93
+ * Equal-power cross-fade at each edited note's edges. Default 5 ms; a hard cut
94
+ * is deliberately not selectable, because the seam it leaves is a click.
95
+ */
96
+ fadeMs?: number;
97
+ /**
98
+ * The F0 track the notes were extracted from. Required only by
99
+ * `vibratoDepthChange` and `driftChange`, which act on the note's own pitch
100
+ * curve; every other edit ignores it. The curve is not carried on a
101
+ * {@link NoteObject} for the same reason it is not returned by
102
+ * {@link extractNotes} — it is this array sliced by
103
+ * `[frameStart, frameEnd)`, which the caller already holds.
104
+ */
105
+ f0Hz?: Float32Array;
106
+ /** F0 frames per second. Required when `f0Hz` is given. */
107
+ frameRate?: number;
108
+ /**
109
+ * Boundary between the drift and the vibrato that `vibratoDepthChange` and
110
+ * `driftChange` act on, in Hz. Default 3 Hz.
111
+ *
112
+ * Pass whatever {@link decomposeNotePitch} was called with. A host that draws
113
+ * the vibrato at one cutoff and edits it at another edits a curve it never
114
+ * showed anyone.
115
+ */
116
+ vibratoCutoffHz?: number;
117
+ }
118
+
119
+ /** Canonical request form for {@link decomposeNotePitch}. */
120
+ export interface DecomposeNotePitchRequest {
121
+ /**
122
+ * The note's slice of the F0 track — `f0Hz.subarray(frameStart, frameEnd)`.
123
+ * Every value must be finite and non-negative; zero denotes an unvoiced frame.
124
+ */
125
+ f0Hz: Float32Array;
126
+ /** F0 frames per second. */
127
+ frameRate: number;
128
+ /**
129
+ * The note's `medianHz`. A note with no pitch is spelled 0, so a negative
130
+ * value is rejected rather than read as a second way of saying that.
131
+ */
132
+ medianHz: number;
133
+ /** Where drift ends and vibrato begins, in Hz. Default 3 Hz. */
134
+ vibratoCutoffHz?: number;
135
+ }
136
+
137
+ /** The audio, track and note set the two note-set reshaping calls share. */
138
+ export interface NoteSetRequest extends NoteExtractorOptions, ValidateOptions {
139
+ /**
140
+ * The audio the set was extracted from. Every note is re-measured against it,
141
+ * so a different buffer re-derives the whole set against something else.
142
+ */
143
+ samples: Float32Array;
144
+ sampleRate: number;
145
+ /**
146
+ * Per-frame F0 in Hz. A frame carrying no pitch is spelled as zero, a
147
+ * negative value or a non-finite one, and all three read the same: that
148
+ * frame contributes no measurement. A {@link pitchPyin} track can be passed
149
+ * straight through — its `fillNa` is a choice about the contour you want,
150
+ * not a requirement of this call.
151
+ */
152
+ f0Hz: Float32Array;
153
+ /** F0 frames per second. */
154
+ frameRate: number;
155
+ /** Per-frame voiced flags (truthy = voiced). Takes precedence over `voicedProb`. */
156
+ voiced?: VoicedFlags;
157
+ /** Per-frame voicing probability in `[0, 1]`; read only when `voiced` is omitted. */
158
+ voicedProb?: Float32Array;
159
+ /** The current note set. Each note's `[frameStart, frameEnd)` must be non-empty and inside the track. */
160
+ notes: readonly NoteSetEntry[];
161
+ }
162
+
163
+ /** Canonical request form for {@link splitNote}. */
164
+ export interface SplitNoteRequest extends NoteSetRequest {
165
+ /** Index of the note to split. */
166
+ index: number;
167
+ /** Track frame to cut at, strictly inside that note's own span. */
168
+ frame: number;
169
+ }
170
+
171
+ /** Canonical request form for {@link mergeNotes}. */
172
+ export interface MergeNotesRequest extends NoteSetRequest {
173
+ /** Index of the first note to join; must be `< last`. */
174
+ first: number;
175
+ /** Index of the last note to join, inclusive. */
176
+ last: number;
177
+ }
178
+
179
+ /** Canonical request form for {@link noteTargetsFromSmf}. */
180
+ export interface NoteTargetsFromSmfRequest {
181
+ /** The Standard MIDI File, in memory. */
182
+ data: Uint8Array;
183
+ /**
184
+ * Which MIDI-bearing track to read, NOT an index into the file's own tracks: a
185
+ * track holding only meta events — a conductor track carrying the tempo map is
186
+ * the usual one — is not counted. A file whose first track is a conductor track
187
+ * therefore has its melody at 0, which is also the default.
188
+ */
189
+ trackIndex?: number;
190
+ }
191
+
192
+ /** Canonical request form for {@link assignNoteTargets}. */
193
+ export interface AssignNoteTargetsRequest {
194
+ /**
195
+ * The notes to assign targets to. Not modified; the result carries a new array.
196
+ * Only `onsetSample`, `offsetSample` and `medianHz` are read.
197
+ */
198
+ notes: readonly NoteObject[];
199
+ /** Converts each note's sample span to seconds, so the targets line up. Must be > 0. */
200
+ sampleRate: number;
201
+ /** The reference melody. An empty array assigns nothing and applies the policy. */
202
+ targets: readonly NoteTarget[];
203
+ /** What to do with a note that has a pitch and no target. Default `'leave'`. */
204
+ unmatchedPolicy?: NoteTargetUnmatchedPolicy;
205
+ /**
206
+ * Fraction of the note that must overlap a target for it to count, in `[0, 1]`.
207
+ * Default 0.5. `0` is its own meaning — any overlap at all counts — not a
208
+ * request for the default.
209
+ */
210
+ minOverlapRatio?: number;
211
+ /**
212
+ * Where the assigned shift saturates, in semitones. Default 12. `0` is its own
213
+ * meaning, as above: every correction saturates to nothing and the take is left
214
+ * as recorded. Must not be negative.
215
+ */
216
+ maxCorrectionSemitones?: number;
217
+ }
218
+
219
+ /**
220
+ * Time-stretch a note region between two sample offsets without changing pitch.
221
+ *
222
+ * @param samples - Audio samples (mono, float32)
223
+ * @param sampleRate - Sample rate in Hz
224
+ * @param onsetSample - Note onset position in samples
225
+ * @param offsetSample - Note offset position in samples
226
+ * @param stretchRatio - Stretch ratio (0.5 = half duration, 2.0 = double duration)
227
+ * @returns Audio with the note region stretched
228
+ */
229
+ export function noteStretch(request: NoteStretchRequest): Float32Array;
230
+ export function noteStretch(
231
+ samples: Float32Array,
232
+ sampleRate?: number,
233
+ options?: NoteStretchOptions & ValidateOptions,
234
+ ): Float32Array;
235
+ export function noteStretch(
236
+ samples: Float32Array | NoteStretchRequest,
237
+ sampleRate = 22050,
238
+ options: NoteStretchOptions & ValidateOptions = {},
239
+ ): Float32Array {
240
+ const request = samples instanceof Float32Array ? { samples, sampleRate, ...options } : samples;
241
+ assertSamples('noteStretch', request.samples, request.validate !== false);
242
+ return requireModule().noteStretch(
243
+ request.samples,
244
+ request.sampleRate ?? 22050,
245
+ request.onsetSample ?? 0,
246
+ request.offsetSample ?? request.samples.length,
247
+ request.stretchRatio ?? 1.0,
248
+ );
249
+ }
250
+
251
+ /** Move a note region to a new onset without changing its duration. */
252
+ export function noteMove(request: NoteMoveRequest): Float32Array;
253
+ export function noteMove(
254
+ samples: Float32Array,
255
+ sampleRate?: number,
256
+ options?: NoteMoveOptions & ValidateOptions,
257
+ ): Float32Array;
258
+ export function noteMove(
259
+ samples: Float32Array | NoteMoveRequest,
260
+ sampleRate = 22050,
261
+ options: NoteMoveOptions & ValidateOptions = {},
262
+ ): Float32Array {
263
+ const request = samples instanceof Float32Array ? { samples, sampleRate, ...options } : samples;
264
+ assertSamples('noteMove', request.samples, request.validate !== false);
265
+ return requireModule().noteMove(
266
+ request.samples,
267
+ request.sampleRate ?? 22050,
268
+ request.onsetSample ?? 0,
269
+ request.offsetSample ?? request.samples.length,
270
+ request.targetOnsetSample ?? 0,
271
+ );
272
+ }
273
+
274
+ /**
275
+ * Extract editable note objects from audio and a caller-supplied F0 track.
276
+ *
277
+ * The track is segmented into monophonic notes; each note carries its span in
278
+ * source samples, its span in the track's own frames, its median pitch, two
279
+ * measured quality figures, its per-frame amplitude (RMS) curve, and an identity
280
+ * {@link NoteEdit}. Edit the notes and hand them to {@link renderNotes} to apply
281
+ * the result — the source audio is never mutated, and a set whose edits are all
282
+ * identity renders back to the input bit for bit.
283
+ *
284
+ * The per-note F0 curve is deliberately not returned: it is the caller's own
285
+ * `f0Hz` sliced by `[frameStart, frameEnd)`. The amplitude curve is measured
286
+ * here, so it is, one entry per F0 frame over that note's span.
287
+ *
288
+ * Voicing comes from `voiced` (truthy = voiced). `voicedProb` is read only when
289
+ * `voiced` is absent, and then a frame counts as voiced at or above
290
+ * `voicedThreshold` (default 0.5). At least one of the two is required. Because
291
+ * `voicedProb` from pYIN rises with F0 for a fixed frame length, prefer passing
292
+ * a {@link PitchResult}'s `voicedFlag` through `voiced`.
293
+ *
294
+ * @param request - Audio, F0 track, frame cadence and segmenter options
295
+ * @returns One {@link NoteObject} per segmented note, in time order; an empty
296
+ * array when the track segments to nothing
297
+ * @throws RangeError when `voiced` / `voicedProb` do not match `f0Hz` in length,
298
+ * or the samples/sample rate fail the shared input checks
299
+ * @throws SonareError (`InvalidParameter`) on an empty `f0Hz`, a non-positive
300
+ * `frameRate`, a negative or non-finite `f0Hz` value, a `voicedProb` outside
301
+ * `[0, 1]`, or a negative option value
302
+ *
303
+ * @example
304
+ * ```ts
305
+ * // pitchPyin's default leaves unvoiced frames NaN, which reads here as a
306
+ * // frame carrying no pitch, so fillNa is a choice rather than a requirement.
307
+ * const pitch = pitchPyin({ samples, sampleRate });
308
+ * const notes = extractNotes({
309
+ * samples,
310
+ * sampleRate,
311
+ * f0Hz: pitch.f0,
312
+ * voiced: pitch.voicedFlag,
313
+ * frameRate: sampleRate / 512,
314
+ * minNoteMs: 40,
315
+ * });
316
+ * // Lift the second note by a semitone and mute the third.
317
+ * notes[1].edit.pitchShiftSemitones = 1;
318
+ * notes[2].edit.muted = true;
319
+ * const edited = renderNotes({ samples, sampleRate, notes });
320
+ * ```
321
+ */
322
+ export function extractNotes(request: ExtractNotesRequest): NoteObject[] {
323
+ const voicedF32 = assertNoteTrack('extractNotes', request);
324
+ return requireModule().extractNotes(
325
+ request.samples,
326
+ request.sampleRate,
327
+ request.f0Hz,
328
+ request.voicedProb,
329
+ voicedF32,
330
+ request.frameRate,
331
+ request,
332
+ );
333
+ }
334
+
335
+ /**
336
+ * Render edited note objects back over their source audio.
337
+ *
338
+ * Only a note whose edit is non-identity is resynthesized; the source passes
339
+ * through everywhere else, so a set of untouched {@link extractNotes} output
340
+ * reproduces the input exactly. The output has the input's length: an edit that
341
+ * pushes a note past either end is truncated there.
342
+ *
343
+ * Per note the order is: pitch curve, time stretch, pitch shift, formant warp,
344
+ * amplitude envelope, then gain.
345
+ *
346
+ * Each note's `onsetSample`, `offsetSample` and `edit` are read, plus its
347
+ * `frameStart`, `frameEnd` and `medianHz` when the request carries an `f0Hz`
348
+ * track for a curve edit to act on; so an extracted note can be passed back
349
+ * as-is, or a note can be built by hand from those fields alone. Overlap is
350
+ * checked on the source spans only — where `timeOffsetSamples` lands a note is
351
+ * not, and a note lengthened past its own span writes into its neighbours'
352
+ * samples, so two moved or stretched notes may be written over each other.
353
+ *
354
+ * @param request - Source audio, the notes to render, the cross-fade length, and
355
+ * the F0 track a vibrato or drift edit reads
356
+ * @returns The rendered audio, the same length and sample rate as the input
357
+ * @throws RangeError when the samples or sample rate fail the shared input checks
358
+ * @throws SonareError (`InvalidParameter`) on a note whose span is empty,
359
+ * reversed or missing, overlapping source spans, a non-finite or non-positive
360
+ * edit field, a negative or non-finite envelope value, a negative `fadeMs` or
361
+ * `vibratoCutoffHz`, a frame span outside `f0Hz`, or a `vibratoDepthChange` /
362
+ * `driftChange` on a note with no usable pitch curve to apply it to
363
+ *
364
+ * @example
365
+ * ```ts
366
+ * const notes = extractNotes({ samples, sampleRate, f0Hz, voiced, frameRate });
367
+ *
368
+ * // Silence the third note and leave the rest untouched.
369
+ * const muted = notes.map((note, index) =>
370
+ * index === 2 ? { ...note, edit: { ...note.edit, muted: true } } : note,
371
+ * );
372
+ * const rendered = renderNotes({ samples, sampleRate, notes: muted, fadeMs: 10 });
373
+ *
374
+ * // Flatten the first note's vibrato, which needs the track it was measured on.
375
+ * const flattened = notes.map((note, index) =>
376
+ * index === 0 ? { ...note, edit: { ...note.edit, vibratoDepthChange: -1 } } : note,
377
+ * );
378
+ * const steady = renderNotes({
379
+ * samples,
380
+ * sampleRate,
381
+ * notes: flattened,
382
+ * f0Hz,
383
+ * frameRate: sampleRate / 512,
384
+ * });
385
+ * ```
386
+ */
387
+ export function renderNotes(request: RenderNotesRequest): Float32Array {
388
+ assertSamples('renderNotes', request.samples, request.validate !== false);
389
+ assertSampleRate('renderNotes', request.sampleRate);
390
+ return requireModule().renderNotes(request.samples, request.sampleRate, request.notes, request);
391
+ }
392
+
393
+ /**
394
+ * Split one note's pitch curve into a centre, a slow drift and a vibrato.
395
+ *
396
+ * A performed note's pitch is one curve carrying three things at once: the note
397
+ * that was aimed at, a slow wander around it, and a periodic oscillation on top.
398
+ * Editing any of them on its own needs them separated first, and the only thing
399
+ * that decides where drift ends and vibrato begins is `vibratoCutoffHz`. Hand
400
+ * the same cutoff to {@link renderNotes}, or it edits a curve nobody was shown.
401
+ *
402
+ * Frames whose F0 is unusable carry no measurement, so the curve is held at the
403
+ * nearest usable neighbour across them. Both curves therefore have an entry
404
+ * everywhere; a host marking the held ones reads them off `f0Hz`, which is exact.
405
+ *
406
+ * A note with no usable pitch is reported as a zero `centreHz` and two empty
407
+ * curves rather than as an error — that is a measurement which came up empty,
408
+ * not a bad argument.
409
+ *
410
+ * @param request - The note's slice of the F0 track, its cadence, its centre, and
411
+ * the cutoff
412
+ * @returns The centre and the two curves, each one entry per frame of `f0Hz`
413
+ * @throws SonareError (`InvalidParameter`) on an empty `f0Hz`, a negative or
414
+ * non-finite `f0Hz` value, a non-positive `frameRate`, a negative `medianHz`,
415
+ * or a negative `vibratoCutoffHz`
416
+ *
417
+ * @example
418
+ * ```ts
419
+ * const note = notes[0];
420
+ * const { centreHz, driftCents, vibratoCents } = decomposeNotePitch({
421
+ * f0Hz: f0Hz.subarray(note.frameStart, note.frameEnd),
422
+ * frameRate: sampleRate / 512,
423
+ * medianHz: note.medianHz,
424
+ * });
425
+ * ```
426
+ */
427
+ export function decomposeNotePitch(request: DecomposeNotePitchRequest): PitchDecompositionResult {
428
+ return requireModule().decomposeNotePitch(
429
+ request.f0Hz,
430
+ request.frameRate,
431
+ request.medianHz,
432
+ request.vibratoCutoffHz ?? 0,
433
+ );
434
+ }
435
+
436
+ /**
437
+ * Split one note of a set in two at a track frame.
438
+ *
439
+ * Both halves are re-derived from the audio and the track the way
440
+ * {@link extractNotes} derives its own, rather than by patching the fields of
441
+ * the note they replace. Both inherit the source note's edit, and its amplitude
442
+ * envelope is cut at the same proportion so each half keeps its own part of it —
443
+ * a note whose edit is the identity therefore still renders bit for bit after
444
+ * being split.
445
+ *
446
+ * Every note in the set, not just the two halves, has its spans, curves, medians
447
+ * and stability re-derived from `samples` and the track, because a
448
+ * {@link NoteSetEntry} carries no curves for this call to copy through. The
449
+ * frame bounds are therefore what a note is identified by here, and the audio
450
+ * and track must be the ones the set was extracted from or the whole set is
451
+ * re-measured against something else.
452
+ *
453
+ * @param request - The source, the current note set, and where to cut
454
+ * @returns The whole new note set, one note longer than the one handed in
455
+ * @throws RangeError when `voiced` / `voicedProb` do not match `f0Hz` in length,
456
+ * or the samples/sample rate fail the shared input checks
457
+ * @throws SonareError (`InvalidParameter`) on an out-of-range `index`, a `frame`
458
+ * that is not strictly inside that note's own span, a note whose frame span is
459
+ * empty or runs past the track, or the track arguments {@link extractNotes}
460
+ * itself rejects
461
+ *
462
+ * @example
463
+ * ```ts
464
+ * const notes = extractNotes({ samples, sampleRate, f0Hz, voiced, frameRate });
465
+ * const halves = splitNote({
466
+ * samples,
467
+ * sampleRate,
468
+ * f0Hz,
469
+ * voiced,
470
+ * frameRate,
471
+ * notes,
472
+ * index: 1,
473
+ * frame: Math.floor((notes[1].frameStart + notes[1].frameEnd) / 2),
474
+ * });
475
+ * ```
476
+ */
477
+ export function splitNote(request: SplitNoteRequest): NoteObject[] {
478
+ const voicedF32 = assertNoteTrack('splitNote', request);
479
+ return requireModule().splitNote(
480
+ request.samples,
481
+ request.sampleRate,
482
+ request.f0Hz,
483
+ request.voicedProb,
484
+ voicedF32,
485
+ request.frameRate,
486
+ request.notes,
487
+ request.index,
488
+ request.frame,
489
+ request,
490
+ );
491
+ }
492
+
493
+ /**
494
+ * Join a run of notes into one.
495
+ *
496
+ * The result spans from `notes[first]`'s onset to `notes[last]`'s offset,
497
+ * including whatever the segmenter cut out between them, and its measured fields
498
+ * are derived over that whole span — the pitch and amplitude of an unvoiced gap
499
+ * live in the track and the audio, not in either neighbour.
500
+ *
501
+ * It takes `notes[first]`'s edit, envelope included. Notes carrying different
502
+ * edits have no single correct answer here, so the rule is stated rather than
503
+ * guessed at; a host that cares sets the edit afterwards. Every note in the set
504
+ * is re-derived from `samples` and the track, exactly as {@link splitNote}
505
+ * describes.
506
+ *
507
+ * @param request - The source, the current note set, and the run to join
508
+ * @returns The whole new note set, `last - first` notes shorter than the one
509
+ * handed in
510
+ * @throws RangeError when `voiced` / `voicedProb` do not match `f0Hz` in length,
511
+ * or the samples/sample rate fail the shared input checks
512
+ * @throws SonareError (`InvalidParameter`) unless `first < last < notes.length`,
513
+ * on a note whose frame span is empty or runs past the track, or on the track
514
+ * arguments {@link extractNotes} itself rejects
515
+ *
516
+ * @example
517
+ * ```ts
518
+ * // Undo a split by rejoining the two halves it produced.
519
+ * const rejoined = mergeNotes({
520
+ * samples,
521
+ * sampleRate,
522
+ * f0Hz,
523
+ * voiced,
524
+ * frameRate,
525
+ * notes: halves,
526
+ * first: 1,
527
+ * last: 2,
528
+ * });
529
+ * ```
530
+ */
531
+ export function mergeNotes(request: MergeNotesRequest): NoteObject[] {
532
+ const voicedF32 = assertNoteTrack('mergeNotes', request);
533
+ return requireModule().mergeNotes(
534
+ request.samples,
535
+ request.sampleRate,
536
+ request.f0Hz,
537
+ request.voicedProb,
538
+ voicedF32,
539
+ request.frameRate,
540
+ request.notes,
541
+ request.first,
542
+ request.last,
543
+ request,
544
+ );
545
+ }
546
+
547
+ /**
548
+ * Read one track of an in-memory Standard MIDI File as a reference melody.
549
+ *
550
+ * Each note-on is paired with the next note-off of the same note number on the
551
+ * same channel, and the pair becomes one {@link NoteTarget} at the note's own
552
+ * pitch. Times come from the file's tempo map, so a tempo change or a ramp inside
553
+ * it is followed rather than the initial tempo being scaled.
554
+ *
555
+ * A note-on the track never closes is dropped: it has no end, and the track's end
556
+ * is not a substitute for one — with events after it the note would span the whole
557
+ * remainder and, being the longest overlap everywhere, take the assignment away
558
+ * from every note that follows. Zero-length notes are skipped for the mirror
559
+ * reason: they overlap nothing, so they could never be assigned.
560
+ *
561
+ * @param request - The file's bytes and which MIDI-bearing track to read
562
+ * @returns One target per closed note, sorted by `startSec`; an empty array for a
563
+ * track with no closed note, which is a measurement that came up empty rather
564
+ * than an error
565
+ * @throws SonareError (`InvalidFormat`) when the bytes are not a readable SMF
566
+ * @throws SonareError (`InvalidParameter`) when `trackIndex` names no
567
+ * MIDI-bearing track
568
+ *
569
+ * @example
570
+ * ```ts
571
+ * // A project's own export writes the tempo map as a conductor track, which
572
+ * // carries no MIDI, so the melody is at index 0 rather than 1.
573
+ * const targets = noteTargetsFromSmf({ data: project.exportSmf() });
574
+ * ```
575
+ */
576
+ export function noteTargetsFromSmf(request: NoteTargetsFromSmfRequest): NoteTarget[] {
577
+ const module = requireModule();
578
+ if (typeof module.noteTargetsFromSmf !== 'function') {
579
+ throw new Error('libsonare was built without arrangement support');
580
+ }
581
+ return module.noteTargetsFromSmf(request.data, request.trackIndex ?? 0);
582
+ }
583
+
584
+ /**
585
+ * Write each note's `edit.pitchShiftSemitones` from the reference target it
586
+ * overlaps, which is what makes a take follow a written melody instead of a
587
+ * single stated interval.
588
+ *
589
+ * A note is matched to the target it overlaps longest, provided that overlap is
590
+ * at least `minOverlapRatio` of the note's own span; an exact tie goes to the
591
+ * target that starts first, so the answer does not depend on the order the
592
+ * targets arrived in. The shift is `targetMidi` minus the note's own `medianHz`
593
+ * as a MIDI number, saturated at `maxCorrectionSemitones` — a reference an octave
594
+ * out is a wrong reference, and a rejected call would tell the caller less than a
595
+ * bounded correction does.
596
+ *
597
+ * A note whose `medianHz` is not finite and positive is never assigned and never
598
+ * edited, whatever `unmatchedPolicy` says. Such a note has no measured pitch to
599
+ * correct from, so `'nearest'` would compute a shift from a pitch that does not
600
+ * exist; the policy governs notes that have a pitch and no target, which is a
601
+ * different thing from having no pitch.
602
+ *
603
+ * The notes handed in are not modified. Only `edit.pitchShiftSemitones` and
604
+ * `edit.muted` are written on the returned copies; every other field of a note —
605
+ * its spans, its metrics, its amplitude curve and its other edits — comes back
606
+ * exactly as it went in.
607
+ *
608
+ * @param request - The notes, their sample rate, the reference melody, and the
609
+ * three tunable knobs
610
+ * @returns The new note set and how many notes received a target
611
+ * @throws SonareError (`InvalidParameter`) on a non-positive `sampleRate`, an
612
+ * unknown `unmatchedPolicy`, a `minOverlapRatio` outside `[0, 1]`, a negative
613
+ * `maxCorrectionSemitones`, a note missing `onsetSample` / `offsetSample`, or a
614
+ * target whose `startSec`, `endSec` or `targetMidi` is not finite
615
+ *
616
+ * @example
617
+ * ```ts
618
+ * const notes = extractNotes({ samples, sampleRate, f0Hz, voiced, frameRate });
619
+ * const { notes: retuned, assignedCount } = assignNoteTargets({
620
+ * notes,
621
+ * sampleRate,
622
+ * targets: noteTargetsFromSmf({ data: smf }),
623
+ * unmatchedPolicy: 'mute',
624
+ * });
625
+ * const corrected = renderNotes({ samples, sampleRate, notes: retuned });
626
+ * ```
627
+ */
628
+ export function assignNoteTargets(request: AssignNoteTargetsRequest): NoteTargetAssignResult {
629
+ return requireModule().assignNoteTargets(
630
+ request.notes,
631
+ request.sampleRate,
632
+ request.targets,
633
+ request,
634
+ );
635
+ }