@audio/shift-pvoc 1.1.0 → 1.1.1

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 (3) hide show
  1. package/host.js +168 -0
  2. package/index.js +2 -2
  3. package/package.json +5 -3
package/host.js ADDED
@@ -0,0 +1,168 @@
1
+ import { stftBatch as ftStftBatch, stftStream as ftStftStream } from 'fourier-transform/stft'
2
+
3
+ // Pitch-shift host plumbing (inlined family convention): input normalization,
4
+ // ratio semantics, identity fast-path, channel/stream writers.
5
+
6
+ function isChannelArray(data) {
7
+ return Array.isArray(data) && data.every((c) => c instanceof Float32Array)
8
+ }
9
+
10
+ function normalizeOptionsInput(data) {
11
+ if (data === undefined || data === null || typeof data === 'object') return data
12
+ throw new TypeError('pitchShift: options must be an object')
13
+ }
14
+
15
+ export function resolvePitchParams(opts) {
16
+ let semitones = opts?.semitones ?? 0
17
+ if (!Number.isFinite(semitones)) throw new TypeError('pitchShift: `semitones` must be a finite number')
18
+ let raw = opts?.ratio
19
+ if (typeof raw === 'function' || raw instanceof Float32Array) {
20
+ throw new TypeError('pitchShift: variable `ratio` (function or Float32Array) is supported by vocoder, phaseLock, transient, formant, paulstretch, sms, hpss, and sample')
21
+ }
22
+ let ratio = raw ?? (semitones ? Math.pow(2, semitones / 12) : 1)
23
+ if (!Number.isFinite(ratio) || ratio <= 0) throw new TypeError('pitchShift: `ratio` must be a finite number > 0')
24
+ return { ratio, semitones }
25
+ }
26
+
27
+ function passThroughWriter() {
28
+ let flushed = false
29
+ return (chunk) => {
30
+ if (chunk === undefined) { flushed = true; return new Float32Array(0) }
31
+ if (flushed) throw new Error('pitchShift: stream already flushed')
32
+ return new Float32Array(chunk)
33
+ }
34
+ }
35
+
36
+ function createChannelWriter(factory) {
37
+ let mode = null
38
+ let writers = null
39
+
40
+ return (chunk) => {
41
+ if (chunk === undefined) {
42
+ if (!writers) return new Float32Array(0)
43
+ return mode === 'channels' ? writers.map((w) => w()) : writers[0]()
44
+ }
45
+
46
+ if (isChannelArray(chunk)) {
47
+ if (!writers) { mode = 'channels'; writers = chunk.map(() => factory()) }
48
+ if (mode !== 'channels') throw new TypeError('pitchShift: cannot mix mono and multi-channel writes')
49
+ if (writers.length !== chunk.length) throw new TypeError('pitchShift: streaming channel count must stay constant')
50
+ return chunk.map((c, i) => writers[i](c))
51
+ }
52
+
53
+ if (!(chunk instanceof Float32Array)) {
54
+ throw new TypeError('pitchShift: streaming input must be Float32Array or array of Float32Array channels')
55
+ }
56
+
57
+ if (!writers) { mode = 'mono'; writers = [factory()] }
58
+ if (mode !== 'mono') throw new TypeError('pitchShift: cannot mix mono and multi-channel writes')
59
+ return writers[0](chunk)
60
+ }
61
+ }
62
+
63
+ export function makePitchShift(batch, stream) {
64
+ let isVariable = (opts) => {
65
+ let raw = opts?.ratio
66
+ return typeof raw === 'function' || raw instanceof Float32Array
67
+ }
68
+ let isIdentity = (opts) => {
69
+ if (isVariable(opts)) return false
70
+ return resolvePitchParams(opts).ratio === 1
71
+ }
72
+ return function shift(data, opts) {
73
+ if (data instanceof Float32Array) {
74
+ if (isIdentity(opts)) return new Float32Array(data)
75
+ return batch(data, opts)
76
+ }
77
+ if (isChannelArray(data)) {
78
+ if (isIdentity(opts)) return data.map((c) => new Float32Array(c))
79
+ return data.map((c) => batch(c, opts))
80
+ }
81
+ opts = normalizeOptionsInput(data)
82
+ if (isIdentity(opts)) return createChannelWriter(() => passThroughWriter())
83
+ return createChannelWriter(() => stream(opts))
84
+ }
85
+ }
86
+
87
+ // Variable-ratio resolver: scalar at t=0 + ratioFn(t) for time-varying pitch.
88
+ export function resolveRatio(opts) {
89
+ let raw = opts?.ratio
90
+ if (typeof raw === 'function') {
91
+ let r0 = raw(0)
92
+ if (!Number.isFinite(r0) || r0 <= 0) throw new TypeError('pitchShift: `ratio(0)` must be a finite number > 0')
93
+ return { ratio: r0, ratioFn: raw }
94
+ }
95
+ if (raw instanceof Float32Array) {
96
+ if (raw.length === 0) throw new TypeError('pitchShift: `ratio` Float32Array must be non-empty')
97
+ let arr = raw
98
+ let last = arr.length - 1
99
+ let sr = opts?.sampleRate || 44100
100
+ let durOpt = opts?.ratioDuration
101
+ if (durOpt !== undefined && durOpt !== null && (!Number.isFinite(durOpt) || durOpt <= 0)) {
102
+ throw new TypeError('pitchShift: `ratioDuration` must be a finite number > 0')
103
+ }
104
+ let dur = durOpt ?? (arr.length / sr)
105
+ let fn = (t) => {
106
+ let pos = (t / dur) * last
107
+ if (pos <= 0) return arr[0]
108
+ if (pos >= last) return arr[last]
109
+ let i0 = Math.floor(pos)
110
+ let frac = pos - i0
111
+ return (1 - frac) * arr[i0] + frac * arr[i0 + 1]
112
+ }
113
+ return { ratio: arr[0], ratioFn: fn }
114
+ }
115
+ let { ratio } = resolvePitchParams(opts)
116
+ return { ratio, ratioFn: null }
117
+ }
118
+
119
+ // Rescale out in place so its RMS matches ref's — bounded to the 0.1..10x ballpark
120
+ // (outside it the output is legitimately silent or the algorithm is broken).
121
+ export function matchGain(out, ref) {
122
+ let no = out.length, nr = ref.length
123
+ let so = 0, sr = 0
124
+ for (let i = 0; i < no; i++) so += out[i] * out[i]
125
+ for (let i = 0; i < nr; i++) sr += ref[i] * ref[i]
126
+ if (so <= 1e-12 || sr <= 1e-12) return out
127
+ let rmsO = Math.sqrt(so / no)
128
+ let rmsR = Math.sqrt(sr / nr)
129
+ let ratio = rmsO / rmsR
130
+ if (ratio < 0.1 || ratio > 10) return out
131
+ let g = rmsR / rmsO
132
+ for (let i = 0; i < no; i++) out[i] *= g
133
+ return out
134
+ }
135
+
136
+ // STFT wrappers exposing ratio/ratioFn at ctx top (pitch-shift convention) over
137
+ // fourier-transform/stft, plus the makeStftShift factory.
138
+ function wrapProcess(process) {
139
+ return function (mag, phase, state, ctx) {
140
+ if (ctx.ratio === undefined) ctx.ratio = ctx.opts?.ratio
141
+ if (ctx.ratioFn === undefined) ctx.ratioFn = ctx.opts?.ratioFn ?? null
142
+ return process(mag, phase, state, ctx)
143
+ }
144
+ }
145
+
146
+ export function stftBatch(data, process, opts) {
147
+ return ftStftBatch(data, wrapProcess(process), opts)
148
+ }
149
+
150
+ export function stftStream(process, opts) {
151
+ let s = ftStftStream(wrapProcess(process), opts)
152
+ return { write: (chunk) => s.write(chunk), flush: () => s.flush() }
153
+ }
154
+
155
+ // Wraps a per-frame STFT process callback into batch+stream pitch-shift entry points.
156
+ export function makeStftShift(process, { deriveOpts = () => ({}), post = matchGain } = {}) {
157
+ function batch(data, opts) {
158
+ let { ratio, ratioFn } = resolveRatio(opts)
159
+ let out = stftBatch(data, process, { ...opts, ratio, ratioFn, ...deriveOpts(opts) })
160
+ return post(out, data)
161
+ }
162
+ function stream(opts) {
163
+ let { ratio, ratioFn } = resolveRatio(opts)
164
+ let s = stftStream(process, { ...opts, ratio, ratioFn, ...deriveOpts(opts) })
165
+ return (chunk) => chunk === undefined ? s.flush() : s.write(chunk)
166
+ }
167
+ return makePitchShift(batch, stream)
168
+ }
package/index.js CHANGED
@@ -1,5 +1,5 @@
1
- import { makeStftShift } from '@audio/shift-core/stft'
2
- import { makeFrameRatio, wrapPhase, scatterGated } from '@audio/shift-core'
1
+ import { makeStftShift } from './host.js'
2
+ import { makeFrameRatio, wrapPhase, scatterGated } from '@audio/spectral-pvoc'
3
3
 
4
4
  // Canonical phase vocoder pitch shift (Bernsee / SMB method), peak-gated bin scatter.
5
5
  // Per frame: compute true instantaneous frequency at each analysis bin eligible under the
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@audio/shift-pvoc",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "Canonical phase vocoder (Bernsee/SMB) pitch shift",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -12,10 +12,12 @@
12
12
  },
13
13
  "files": [
14
14
  "index.js",
15
- "atom.js"
15
+ "atom.js",
16
+ "host.js"
16
17
  ],
17
18
  "dependencies": {
18
- "@audio/shift-core": "^1.0.0"
19
+ "@audio/spectral-pvoc": "^1.0.0",
20
+ "fourier-transform": "^2.3.0"
19
21
  },
20
22
  "publishConfig": {
21
23
  "access": "public"