@audio/shift-hybrid 1.1.0 → 1.2.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.
- package/audio.d.ts +3 -3
- package/audio.js +12 -3
- package/host.js +34 -7
- package/index.js +31 -19
- package/package.json +5 -2
package/audio.d.ts
CHANGED
|
@@ -10,7 +10,7 @@ type Process = (inputs: Float32Array[][], outputs: Float32Array[][], params: Liv
|
|
|
10
10
|
|
|
11
11
|
/** Chainable-host options for 'pitchHybrid' */
|
|
12
12
|
export interface PitchHybridOptions {
|
|
13
|
-
/** -24..24 (default 0) */
|
|
13
|
+
/** -24..24 st (default 0) */
|
|
14
14
|
"semitones"?: Auto
|
|
15
15
|
at?: number | string
|
|
16
16
|
duration?: number | string
|
|
@@ -20,9 +20,9 @@ export declare const pitchHybrid: {
|
|
|
20
20
|
(ctx: Ctx): Process
|
|
21
21
|
channels: "any"
|
|
22
22
|
streaming: false
|
|
23
|
-
tail:
|
|
23
|
+
tail: (at: { sampleRate: number, params: Live, buffers: {} }) => number
|
|
24
24
|
params: {
|
|
25
|
-
/** -24..24 (default 0) */
|
|
25
|
+
/** -24..24 st (default 0) */
|
|
26
26
|
"semitones": { type: "number", default: 0 }
|
|
27
27
|
}
|
|
28
28
|
}
|
package/audio.js
CHANGED
|
@@ -8,12 +8,21 @@ export const pitchHybrid = (ctx) => {
|
|
|
8
8
|
return (inputs, outputs, params) => {
|
|
9
9
|
const inp = inputs[0], out = outputs[0]
|
|
10
10
|
if (!inp || !inp.length) return
|
|
11
|
-
|
|
11
|
+
const y = shift(inp, { semitones: params.semitones[0], sampleRate: ctx.sampleRate, fs: ctx.sampleRate })
|
|
12
|
+
for (let c = 0; c < inp.length; c++) out[c].set(y[c])
|
|
12
13
|
}
|
|
13
14
|
}
|
|
14
15
|
pitchHybrid.channels = 'any'
|
|
15
16
|
pitchHybrid.streaming = false
|
|
16
|
-
|
|
17
|
+
// The longer of its engines' rings: the peak-locked vocoder's frame of N = 2048, which writes over the N frames it read,
|
|
18
|
+
// N − 1 past the take; WSOLA's (@audio/shift-wsola at its defaults: DELTA + N(1/ratio − 1) shifted down, DELTA = N/4
|
|
19
|
+
// shifted up, the resampler's 8 taps and a frame's rounding beside), moved up to LAG = 1.5·DELTA later by the blend's
|
|
20
|
+
// alignment; none at a ratio of 1. Lag and trust are measured over the take, so a host's padding moves nothing over it
|
|
21
|
+
const N = 2048, DELTA = N >> 2, LAG = Math.round(1.5 * DELTA)
|
|
22
|
+
pitchHybrid.tail = ({ sampleRate, params }) => {
|
|
23
|
+
let st = params.semitones[0], r = st ? Math.pow(2, st / 12) : 1
|
|
24
|
+
return r === 1 ? 0 : Math.max(N - 1, DELTA + Math.max(8, (N + 8) / r - N) + 4 + LAG) / sampleRate
|
|
25
|
+
}
|
|
17
26
|
pitchHybrid.params = {
|
|
18
|
-
semitones: { type: 'number', min: -24, max: 24, default: 0 },
|
|
27
|
+
semitones: { type: 'number', min: -24, max: 24, default: 0, unit: 'st' },
|
|
19
28
|
}
|
package/host.js
CHANGED
|
@@ -38,7 +38,10 @@ function createChannelWriter(factory) {
|
|
|
38
38
|
return (chunk) => {
|
|
39
39
|
if (chunk === undefined) {
|
|
40
40
|
if (!writers) return new Float32Array(0)
|
|
41
|
-
|
|
41
|
+
if (mode !== 'channels') return writers[0]()
|
|
42
|
+
// The channels of a stream are one take: a whole-signal shift takes its length over all
|
|
43
|
+
let take = writers[0].take ? Math.max(...writers.map((w) => w.take())) : undefined
|
|
44
|
+
return writers.map((w) => w(undefined, take))
|
|
42
45
|
}
|
|
43
46
|
|
|
44
47
|
if (isChannelArray(chunk)) {
|
|
@@ -67,14 +70,17 @@ export function makePitchShift(batch, stream) {
|
|
|
67
70
|
if (isVariable(opts)) return false
|
|
68
71
|
return resolvePitchParams(opts).ratio === 1
|
|
69
72
|
}
|
|
70
|
-
|
|
73
|
+
// `take`: the take's length, where the caller shifts one channel of a take with more
|
|
74
|
+
return function shift(data, opts, take) {
|
|
71
75
|
if (data instanceof Float32Array) {
|
|
72
76
|
if (isIdentity(opts)) return new Float32Array(data)
|
|
73
|
-
return batch(data, opts)
|
|
77
|
+
return batch(data, opts, take)
|
|
74
78
|
}
|
|
75
79
|
if (isChannelArray(data)) {
|
|
76
80
|
if (isIdentity(opts)) return data.map((c) => new Float32Array(c))
|
|
77
|
-
|
|
81
|
+
// The channels are one take: its length is the longest of theirs, the same for every one
|
|
82
|
+
let m = Math.max(...data.map(takeLength))
|
|
83
|
+
return data.map((c) => batch(c, opts, m))
|
|
78
84
|
}
|
|
79
85
|
opts = normalizeOptionsInput(data)
|
|
80
86
|
if (isIdentity(opts)) return createChannelWriter(() => passThroughWriter())
|
|
@@ -82,11 +88,12 @@ export function makePitchShift(batch, stream) {
|
|
|
82
88
|
}
|
|
83
89
|
}
|
|
84
90
|
|
|
85
|
-
// Streaming adapter for whole-signal-look-ahead algorithms: buffer, emit on flush.
|
|
91
|
+
// Streaming adapter for whole-signal-look-ahead algorithms: buffer, emit on flush. `take()`
|
|
92
|
+
// is the buffered take's length, for the channels of a stream to shift over one.
|
|
86
93
|
export function bufferedStream(batch, opts) {
|
|
87
94
|
let parts = []
|
|
88
95
|
let flushed = false
|
|
89
|
-
|
|
96
|
+
let write = (chunk, take) => {
|
|
90
97
|
if (chunk === undefined) {
|
|
91
98
|
if (flushed) return new Float32Array(0)
|
|
92
99
|
flushed = true
|
|
@@ -95,10 +102,30 @@ export function bufferedStream(batch, opts) {
|
|
|
95
102
|
let all = new Float32Array(total)
|
|
96
103
|
let o = 0
|
|
97
104
|
for (let p of parts) { all.set(p, o); o += p.length }
|
|
98
|
-
return batch(all, opts)
|
|
105
|
+
return batch(all, opts, take)
|
|
99
106
|
}
|
|
100
107
|
if (flushed) throw new Error('pitchShift: stream already flushed')
|
|
101
108
|
parts.push(new Float32Array(chunk))
|
|
102
109
|
return new Float32Array(0)
|
|
103
110
|
}
|
|
111
|
+
write.take = () => {
|
|
112
|
+
let m = 0
|
|
113
|
+
for (let p of parts) m += p.length
|
|
114
|
+
for (let k = parts.length - 1; k >= 0; k--) {
|
|
115
|
+
let i = takeLength(parts[k])
|
|
116
|
+
if (i) return m - parts[k].length + i
|
|
117
|
+
m -= parts[k].length
|
|
118
|
+
}
|
|
119
|
+
return 0
|
|
120
|
+
}
|
|
121
|
+
return write
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// The take's length: its frames up to the last that is not 0. What follows is silence, a
|
|
125
|
+
// host's (one that renders a tail appends it) or the take's own: a whole-signal shift
|
|
126
|
+
// renders on through it, and no level, fit or statistic it takes counts it.
|
|
127
|
+
export function takeLength(data) {
|
|
128
|
+
let m = data.length
|
|
129
|
+
while (m > 0 && data[m - 1] === 0) m--
|
|
130
|
+
return m
|
|
104
131
|
}
|
package/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { stft } from 'fourier-transform/stft'
|
|
2
2
|
import phaseLock from '@audio/shift-pvoc-lock'
|
|
3
3
|
import wsola from '@audio/shift-wsola'
|
|
4
|
-
import { makePitchShift, resolvePitchParams, bufferedStream } from './host.js'
|
|
4
|
+
import { makePitchShift, resolvePitchParams, bufferedStream, takeLength } from './host.js'
|
|
5
5
|
|
|
6
6
|
// Hybrid pitch shifter. Runs two canonical engines in parallel and crossfades between them
|
|
7
7
|
// sample-by-sample, driven by a per-sample transient confidence signal:
|
|
@@ -27,9 +27,9 @@ import { makePitchShift, resolvePitchParams, bufferedStream } from './host.js'
|
|
|
27
27
|
// not: a smooth tremolo/vibrato swell tops out at a low, predictable nFlux (measured ≈0.17 at
|
|
28
28
|
// this frameSize/hop for a full-scale 5 Hz/60%-depth AM sine) while genuine onsets reach
|
|
29
29
|
// several times that, so `threshold × max(0.3, std)` separates them without per-material tuning.
|
|
30
|
-
function transientConfidence(data, opts) {
|
|
30
|
+
function transientConfidence(data, m, opts) {
|
|
31
31
|
let N = 1024, hop = N >> 2, half = N >> 1
|
|
32
|
-
if (
|
|
32
|
+
if (m < 64) return new Float32Array(data.length)
|
|
33
33
|
let frames = stft(data, { frameSize: N, hopSize: hop })
|
|
34
34
|
if (frames.length < 2) return new Float32Array(data.length)
|
|
35
35
|
|
|
@@ -118,9 +118,8 @@ function activeRanges(conf, floor, margin, n) {
|
|
|
118
118
|
// WSOLA's late one, suppressing the true attack and injecting a phantom echo where WSOLA's
|
|
119
119
|
// delayed copy lands. Estimate that delay as a single global lag via cross-correlation over
|
|
120
120
|
// `ranges` and compensate before blending. Returns a sample count; positive means `td` lags `pv`.
|
|
121
|
-
function estimateLag(pv, td, ranges, maxLag) {
|
|
121
|
+
function estimateLag(pv, td, ranges, maxLag, n) {
|
|
122
122
|
if (!ranges.length) return 0
|
|
123
|
-
let n = pv.length
|
|
124
123
|
let bestLag = 0, bestScore = -Infinity
|
|
125
124
|
for (let lag = -maxLag; lag <= maxLag; lag++) {
|
|
126
125
|
let dot = 0, na = 0, nb = 0
|
|
@@ -139,13 +138,13 @@ function estimateLag(pv, td, ranges, maxLag) {
|
|
|
139
138
|
|
|
140
139
|
// Shift `td` earlier by `lag` samples (a single constant compensation, applied whole-signal)
|
|
141
140
|
// so its reconstruction of a transient lines up with phaseLock's before blending.
|
|
142
|
-
|
|
141
|
+
// `td` runs past the n frames wanted by as much as the lag reaches, so each reads its own.
|
|
142
|
+
function alignTd(td, lag, n) {
|
|
143
143
|
if (!lag) return td
|
|
144
|
-
let n = td.length
|
|
145
144
|
let out = new Float32Array(n)
|
|
146
145
|
for (let i = 0; i < n; i++) {
|
|
147
146
|
let j = i + lag
|
|
148
|
-
out[i] = j < 0 ? 0 : td[Math.min(
|
|
147
|
+
out[i] = j < 0 ? 0 : td[Math.min(td.length - 1, j)]
|
|
149
148
|
}
|
|
150
149
|
return out
|
|
151
150
|
}
|
|
@@ -157,24 +156,25 @@ function alignTd(td, lag) {
|
|
|
157
156
|
// silence/steady padding would understate genuine agreement). Below R=0.5 the two reconstructions
|
|
158
157
|
// no longer share a majority of their variation, so the alignment is disagreement, not delay —
|
|
159
158
|
// trust is 0 and the blend falls back to phaseLock exactly, never a worse-than-either compromise.
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
159
|
+
// Measured over the first `e` of the take's `m` frames; where the take blends WSOLA in but
|
|
160
|
+
// only past `e`, nothing measured trusts it, and the blend there is phaseLock's alone.
|
|
161
|
+
function alignmentTrust(pv, tdAligned, conf, floor, e, m) {
|
|
162
|
+
let dot = 0, na = 0, nb = 0, count = 0, blended = 0
|
|
163
|
+
for (let i = 0; i < m; i++) {
|
|
163
164
|
if (conf[i] <= floor) continue
|
|
165
|
+
blended++
|
|
166
|
+
if (i >= e) continue
|
|
164
167
|
let a = pv[i], b = tdAligned[i]
|
|
165
168
|
dot += a * b; na += a * a; nb += b * b; count++
|
|
166
169
|
}
|
|
170
|
+
if (count < 10 && blended >= 10) return 0
|
|
167
171
|
if (count < 10 || na < 1e-9 || nb < 1e-9) return 1 // too little evidence to distrust
|
|
168
172
|
let r = dot / Math.sqrt(na * nb)
|
|
169
173
|
return r > 0.5 ? r * r : 0
|
|
170
174
|
}
|
|
171
175
|
|
|
172
|
-
function hybridBatch(data, opts) {
|
|
176
|
+
function hybridBatch(data, opts, take = takeLength(data)) {
|
|
173
177
|
resolvePitchParams(opts) // validate early — wsola rejects variable ratio, catch it here with a clear message
|
|
174
|
-
let pv = phaseLock(data, opts)
|
|
175
|
-
let td = wsola(data, opts)
|
|
176
|
-
let conf = transientConfidence(data, opts)
|
|
177
|
-
|
|
178
178
|
// Mirrors wsola's own deriveOpts so the search bound tracks whatever tolerance it's
|
|
179
179
|
// actually configured with; ×1.5 covers the observed delay (which runs somewhat above
|
|
180
180
|
// the nudge bound itself), capped so a caller-supplied extreme tolerance can't blow up
|
|
@@ -183,9 +183,21 @@ function hybridBatch(data, opts) {
|
|
|
183
183
|
let delta = opts?.tolerance ?? (frameSize >> 2)
|
|
184
184
|
let maxLag = Math.min(4096, Math.max(64, Math.round(delta * 1.5)))
|
|
185
185
|
let confFloor = 0.05
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
186
|
+
// The take ends at its last sound; what follows is silence, which a host that renders the
|
|
187
|
+
// tail appends. WSOLA runs on the take followed by silence, as far as the lag reads past
|
|
188
|
+
// the call's end. The lag and the trust are measures of the whole take: taken short of its
|
|
189
|
+
// last N + 2δ + maxLag frames, where WSOLA's ending lies (its last grains, their search and
|
|
190
|
+
// correlation, read up to maxLag later), so the blend over the take follows neither the
|
|
191
|
+
// silence after it nor how WSOLA ends it.
|
|
192
|
+
let n = data.length, m = Math.min(take, n), e = Math.max(0, m - (frameSize + 2 * delta + maxLag))
|
|
193
|
+
let pv = phaseLock(data, opts)
|
|
194
|
+
let src = new Float32Array(n + maxLag)
|
|
195
|
+
src.set(data.subarray(0, m))
|
|
196
|
+
let td = wsola(src, opts, m)
|
|
197
|
+
let conf = transientConfidence(data, m, opts)
|
|
198
|
+
let ranges = activeRanges(conf, confFloor, maxLag, e)
|
|
199
|
+
let tdAligned = alignTd(td, estimateLag(pv, td, ranges, maxLag, e), n)
|
|
200
|
+
let trust = alignmentTrust(pv, tdAligned, conf, confFloor, e, m)
|
|
189
201
|
|
|
190
202
|
// Linear (equal-gain) crossfade, not constant-power: pv/tdAligned are two reconstructions
|
|
191
203
|
// of the *same* source, not independent signals, so they're correlated rather than
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@audio/shift-hybrid",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Hybrid pitch shift — runs phase-lock + WSOLA in parallel, crossfades by transient confidence",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -13,6 +13,9 @@
|
|
|
13
13
|
},
|
|
14
14
|
"./package.json": "./package.json"
|
|
15
15
|
},
|
|
16
|
+
"scripts": {
|
|
17
|
+
"prepublishOnly": "node ../../../compile/tools/tested.js"
|
|
18
|
+
},
|
|
16
19
|
"files": [
|
|
17
20
|
"index.js",
|
|
18
21
|
"audio.d.ts",
|
|
@@ -22,7 +25,7 @@
|
|
|
22
25
|
],
|
|
23
26
|
"dependencies": {
|
|
24
27
|
"@audio/shift-pvoc-lock": "^1.0.0",
|
|
25
|
-
"@audio/shift-wsola": "^1.
|
|
28
|
+
"@audio/shift-wsola": "^1.2.0",
|
|
26
29
|
"fourier-transform": "^2.3.0"
|
|
27
30
|
},
|
|
28
31
|
"publishConfig": {
|