@audio/shift-pvoc 1.1.4 → 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 CHANGED
@@ -10,7 +10,7 @@ type Process = (inputs: Float32Array[][], outputs: Float32Array[][], params: Liv
10
10
 
11
11
  /** Chainable-host options for 'vocoder' */
12
12
  export interface VocoderOptions {
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 vocoder: {
20
20
  (ctx: Ctx): Process
21
21
  channels: "any"
22
22
  latency: 2048
23
- tail: 0
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
@@ -1,11 +1,9 @@
1
1
  // audio manifest — wraps the phase-vocoder pitch shift per @audio/compile CONTRACT.
2
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).
3
+ // bursts: fourier-transform/stft holds a sample back at most FRAME samples, with output
4
+ // sample j aligned to input sample j. A per-channel FIFO primed with FRAME zeros turns
5
+ // the bursts into the fixed equal-frames-in/out shape §process requires: it never runs
6
+ // dry, so the delay is exactly FRAME under any block size (pinned in test.js).
9
7
  //
10
8
  // `semitones` is live: the writer is constructed with a function ratio (the kernel
11
9
  // samples it per analysis frame via makeFrameRatio), reading a cell this manifest
@@ -15,22 +13,20 @@
15
13
  import vocoderFn from './index.js'
16
14
 
17
15
  const FRAME = 2048, HOP = 512
18
- const LATENCY = 2048
16
+ const LATENCY = FRAME
19
17
 
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
18
+ // A FIFO on an array made for the most a call brings, primed with zeros; loops and copyWithin, as a subarray view is an
19
+ // allocation a call
20
+ function makeFifo(max) { return { buf: new Float32Array(LATENCY + max), len: LATENCY } }
21
+ function fifoPush(f, src, n) {
22
+ for (let i = 0; i < n; i++) f.buf[f.len + i] = src[i]
23
+ f.len += n
29
24
  }
30
25
  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 }
26
+ let n = Math.min(out.length, f.len), b = f.buf
27
+ for (let i = 0; i < n; i++) out[i] = b[i]
28
+ out.fill(0, n)
29
+ b.copyWithin(0, n, f.len); f.len -= n
34
30
  }
35
31
 
36
32
  export const vocoder = (ctx) => {
@@ -40,7 +36,7 @@ export const vocoder = (ctx) => {
40
36
  for (let c = 0, N = ctx.maxChannels ?? 8; c < N; c++) {
41
37
  chans.push({
42
38
  write: vocoderFn({ ratio, frameSize: FRAME, hopSize: HOP, sampleRate: ctx.sampleRate, fs: ctx.sampleRate }),
43
- fifo: makeFifo()
39
+ fifo: makeFifo(ctx.maxBlockSize + 1) // a write gives a block and a sample at most
44
40
  })
45
41
  }
46
42
  return (inputs, outputs, params) => {
@@ -49,14 +45,17 @@ export const vocoder = (ctx) => {
49
45
  cell.r = 2 ** (params.semitones[0] / 12)
50
46
  for (let c = 0; c < inp.length; c++) {
51
47
  const ch = chans[c]
52
- fifoPush(ch.fifo, ch.write(inp[c]))
48
+ const y = ch.write(inp[c])
49
+ fifoPush(ch.fifo, y, y.length)
53
50
  fifoPull(ch.fifo, out[c])
54
51
  }
55
52
  }
56
53
  }
57
54
  vocoder.channels = 'any'
58
55
  vocoder.latency = LATENCY
59
- vocoder.tail = 0
56
+ // a frame that read the input's last sample spreads what it carries over all its FRAME samples, and the frames after read
57
+ // silence and give silence: the output ends FRAME − 1 samples past the latency
58
+ vocoder.tail = ({ sampleRate }) => (FRAME - 1) / sampleRate
60
59
  vocoder.params = {
61
- semitones: { type: 'number', min: -24, max: 24, default: 0, smoothing: 0.01 },
60
+ semitones: { type: 'number', min: -24, max: 24, default: 0, unit: 'st', smoothing: 0.01 },
62
61
  }
package/index.js CHANGED
@@ -10,13 +10,15 @@ import { makeFrameRatio, wrapPhase, scatterGated } from '@audio/spectral-pvoc'
10
10
  function process(mag, phase, state, ctx) {
11
11
  if (!state.fr) state.fr = makeFrameRatio(ctx.ratioFn || ctx.ratio || 1)
12
12
  let { half, hop } = ctx
13
- let ratio = state.fr.at(ctx.frameStart, ctx.sampleRate)
13
+ let ratio = state.fr.at(ctx.frameStart + ctx.N / 2, ctx.sampleRate)
14
14
  if (!state.prev) {
15
15
  state.prev = new Float64Array(half + 1)
16
16
  state.syn = new Float64Array(half + 1)
17
17
  state.newMag = new Float64Array(half + 1)
18
18
  state.newFreq = new Float64Array(half + 1)
19
19
  state.peakMag = new Float64Array(half + 1)
20
+ // what every frame returns, made once: a literal a frame is an allocation a frame
21
+ state.out = { mag: state.newMag, phase: state.syn }
20
22
  state.first = true
21
23
  }
22
24
 
@@ -31,7 +33,7 @@ function process(mag, phase, state, ctx) {
31
33
  for (let k = 0; k <= half; k++) syn[k] = wrapPhase(syn[k] + newFreq[k] * hop)
32
34
 
33
35
  state.first = false
34
- return { mag: newMag, phase: syn }
36
+ return state.out
35
37
  }
36
38
 
37
39
  // Batch and stream share scatterGated's per-frame energy renormalization, so both
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@audio/shift-pvoc",
3
- "version": "1.1.4",
3
+ "version": "1.2.0",
4
4
  "description": "Canonical phase vocoder (Bernsee/SMB) pitch shift",
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.js",
@@ -22,7 +25,7 @@
22
25
  ],
23
26
  "dependencies": {
24
27
  "@audio/spectral-pvoc": "^1.0.0",
25
- "fourier-transform": "^2.3.0"
28
+ "fourier-transform": "^2.4.2"
26
29
  },
27
30
  "publishConfig": {
28
31
  "access": "public"