@audio/shift-pvoc 1.0.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.
- package/atom.js +62 -0
- package/host.js +168 -0
- package/index.js +2 -2
- package/package.json +9 -4
package/atom.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// atom manifest — wraps the phase-vocoder pitch shift per @audio/atom CONTRACT.
|
|
2
|
+
// The kernel streams (stftStream writer), but write(chunk) returns variable-length
|
|
3
|
+
// bursts (frame/hop bookkeeping is not 1:1 with input chunking) — a per-channel FIFO
|
|
4
|
+
// absorbs that into the fixed equal-frames-in/out shape §process requires, at a fixed
|
|
5
|
+
// extra delay. Measured end-to-end through this manifest (tone-burst envelope
|
|
6
|
+
// cross-correlation at ratio 1, blocks 128–4096): 2048 samples = 1× frameSize =
|
|
7
|
+
// 4× hopSize, block-size-invariant, confirmed independently by input/output
|
|
8
|
+
// sample-count bookkeeping (steady-state deficit 2048).
|
|
9
|
+
//
|
|
10
|
+
// `semitones` is live: the writer is constructed with a function ratio (the kernel
|
|
11
|
+
// samples it per analysis frame via makeFrameRatio), reading a cell this manifest
|
|
12
|
+
// updates from the per-block params — no reconstruction on change. The function
|
|
13
|
+
// form also defeats the ratio-1 identity shortcut, keeping latency constant.
|
|
14
|
+
|
|
15
|
+
import vocoderFn from './index.js'
|
|
16
|
+
|
|
17
|
+
const FRAME = 2048, HOP = 512
|
|
18
|
+
const LATENCY = 2048
|
|
19
|
+
|
|
20
|
+
function makeFifo() { return { buf: new Float32Array(1 << 14), len: 0 } }
|
|
21
|
+
function fifoPush(f, chunk) {
|
|
22
|
+
if (!chunk.length) return
|
|
23
|
+
let need = f.len + chunk.length
|
|
24
|
+
if (need > f.buf.length) {
|
|
25
|
+
let nb = new Float32Array(Math.max(need * 2, f.buf.length * 2))
|
|
26
|
+
nb.set(f.buf.subarray(0, f.len)); f.buf = nb
|
|
27
|
+
}
|
|
28
|
+
f.buf.set(chunk, f.len); f.len += chunk.length
|
|
29
|
+
}
|
|
30
|
+
function fifoPull(f, out) {
|
|
31
|
+
let n = out.length
|
|
32
|
+
if (f.len >= n) { out.set(f.buf.subarray(0, n)); f.buf.copyWithin(0, n, f.len); f.len -= n }
|
|
33
|
+
else { out.set(f.buf.subarray(0, f.len)); out.fill(0, f.len); f.len = 0 }
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export const vocoder = (ctx) => {
|
|
37
|
+
const cell = { r: 2 ** (ctx.params.semitones[0] / 12) }
|
|
38
|
+
const ratio = () => cell.r
|
|
39
|
+
const chans = []
|
|
40
|
+
for (let c = 0, N = ctx.maxChannels ?? 8; c < N; c++) {
|
|
41
|
+
chans.push({
|
|
42
|
+
write: vocoderFn({ ratio, frameSize: FRAME, hopSize: HOP, sampleRate: ctx.sampleRate, fs: ctx.sampleRate }),
|
|
43
|
+
fifo: makeFifo()
|
|
44
|
+
})
|
|
45
|
+
}
|
|
46
|
+
return (inputs, outputs, params) => {
|
|
47
|
+
const inp = inputs[0], out = outputs[0]
|
|
48
|
+
if (!inp || !inp.length) return
|
|
49
|
+
cell.r = 2 ** (params.semitones[0] / 12)
|
|
50
|
+
for (let c = 0; c < inp.length; c++) {
|
|
51
|
+
const ch = chans[c]
|
|
52
|
+
fifoPush(ch.fifo, ch.write(inp[c]))
|
|
53
|
+
fifoPull(ch.fifo, out[c])
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
vocoder.channels = 'any'
|
|
58
|
+
vocoder.latency = LATENCY
|
|
59
|
+
vocoder.tail = 0
|
|
60
|
+
vocoder.params = {
|
|
61
|
+
semitones: { type: 'number', min: -24, max: 24, default: 0, smoothing: 0.01 },
|
|
62
|
+
}
|
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 '
|
|
2
|
-
import { makeFrameRatio, wrapPhase, scatterGated } from '@audio/
|
|
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,19 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@audio/shift-pvoc",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "Canonical phase vocoder (Bernsee/SMB) pitch shift",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
7
7
|
"main": "index.js",
|
|
8
8
|
"exports": {
|
|
9
9
|
".": "./index.js",
|
|
10
|
+
"./atom": "./atom.js",
|
|
10
11
|
"./package.json": "./package.json"
|
|
11
12
|
},
|
|
12
13
|
"files": [
|
|
13
|
-
"index.js"
|
|
14
|
+
"index.js",
|
|
15
|
+
"atom.js",
|
|
16
|
+
"host.js"
|
|
14
17
|
],
|
|
15
18
|
"dependencies": {
|
|
16
|
-
"@audio/
|
|
19
|
+
"@audio/spectral-pvoc": "^1.0.0",
|
|
20
|
+
"fourier-transform": "^2.3.0"
|
|
17
21
|
},
|
|
18
22
|
"publishConfig": {
|
|
19
23
|
"access": "public"
|
|
@@ -36,5 +40,6 @@
|
|
|
36
40
|
],
|
|
37
41
|
"engines": {
|
|
38
42
|
"node": ">=18"
|
|
39
|
-
}
|
|
43
|
+
},
|
|
44
|
+
"atom": "./atom.js"
|
|
40
45
|
}
|