@audio/shift-pvoc 1.1.5 → 1.2.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/audio.d.ts CHANGED
@@ -1,16 +1,18 @@
1
1
  // Generated from the audio.js manifest (params metadata is the source of truth).
2
- // Regenerate: node tools/dts.js in @audio/compile. Do not edit by hand.
2
+ // Regenerate: node tools/dts.js in @audio/plugin. Do not edit by hand.
3
3
 
4
4
  /** Automatable number — scalar, `t => value` fn, or breakpoint curve {t, v} */
5
5
  type Auto = number | ((t: number) => number) | { t: number[], v: number[] }
6
6
  /** Per-block param values as delivered by hosts (numbers arrive as 1-length Float32Array) */
7
7
  type Live = Record<string, Float32Array | string | boolean>
8
- type Ctx = { sampleRate: number, maxBlockSize: number, maxChannels: number, currentTime: number, duration?: number, events?: readonly any[], emit?: (name: string, ...args: any[]) => void, [k: string]: unknown }
8
+ type Transport = Readonly<{ playing: boolean, bpm: number, beat: number, barStart: number, timeSig: [number, number] }>
9
+ /** What a host hands a processor it builds: the contract's Context (CONTRACT.d.ts, 1.0) */
10
+ type Ctx = { readonly sampleRate: number, readonly maxBlockSize: number, readonly maxChannels: number, readonly layouts?: { readonly inputs: readonly (string | undefined)[], readonly outputs: readonly (string | undefined)[] }, readonly params: Live, readonly buffers: Readonly<Record<string, Float32Array[] | undefined>>, readonly events?: readonly { time: number, kind: 'on' | 'off', midi: number, velocity: number, id: number }[], emit(name: string, value: number): void, readonly transport: Transport }
9
11
  type Process = (inputs: Float32Array[][], outputs: Float32Array[][], params: Live) => void
10
12
 
11
13
  /** Chainable-host options for 'vocoder' */
12
14
  export interface VocoderOptions {
13
- /** -24..24 (default 0) */
15
+ /** -24..24 st (default 0) */
14
16
  "semitones"?: Auto
15
17
  at?: number | string
16
18
  duration?: number | string
@@ -20,9 +22,9 @@ export declare const vocoder: {
20
22
  (ctx: Ctx): Process
21
23
  channels: "any"
22
24
  latency: 2048
23
- tail: 0
25
+ tail: (at: { sampleRate: number, params: Live, buffers?: {}, transport: Transport }) => number
24
26
  params: {
25
- /** -24..24 (default 0) */
26
- "semitones": { type: "number", default: 0 }
27
+ /** -24..24 st (default 0) */
28
+ "semitones": { type: "number", min: -24, max: 24, default: 0, unit: "st", smoothing: 0.01 }
27
29
  }
28
30
  }
package/audio.js CHANGED
@@ -15,20 +15,18 @@ import vocoderFn from './index.js'
15
15
  const FRAME = 2048, HOP = 512
16
16
  const LATENCY = FRAME
17
17
 
18
- function makeFifo() { return { buf: new Float32Array(1 << 14), len: LATENCY } } // primed with zeros
19
- function fifoPush(f, chunk) {
20
- if (!chunk.length) return
21
- let need = f.len + chunk.length
22
- if (need > f.buf.length) {
23
- let nb = new Float32Array(Math.max(need * 2, f.buf.length * 2))
24
- nb.set(f.buf.subarray(0, f.len)); f.buf = nb
25
- }
26
- 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
27
24
  }
28
25
  function fifoPull(f, out) {
29
- let n = out.length
30
- if (f.len >= n) { out.set(f.buf.subarray(0, n)); f.buf.copyWithin(0, n, f.len); f.len -= n }
31
- 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
32
30
  }
33
31
 
34
32
  export const vocoder = (ctx) => {
@@ -38,7 +36,7 @@ export const vocoder = (ctx) => {
38
36
  for (let c = 0, N = ctx.maxChannels ?? 8; c < N; c++) {
39
37
  chans.push({
40
38
  write: vocoderFn({ ratio, frameSize: FRAME, hopSize: HOP, sampleRate: ctx.sampleRate, fs: ctx.sampleRate }),
41
- fifo: makeFifo()
39
+ fifo: makeFifo(ctx.maxBlockSize + 1) // a write gives a block and a sample at most
42
40
  })
43
41
  }
44
42
  return (inputs, outputs, params) => {
@@ -47,14 +45,17 @@ export const vocoder = (ctx) => {
47
45
  cell.r = 2 ** (params.semitones[0] / 12)
48
46
  for (let c = 0; c < inp.length; c++) {
49
47
  const ch = chans[c]
50
- fifoPush(ch.fifo, ch.write(inp[c]))
48
+ const y = ch.write(inp[c])
49
+ fifoPush(ch.fifo, y, y.length)
51
50
  fifoPull(ch.fifo, out[c])
52
51
  }
53
52
  }
54
53
  }
55
54
  vocoder.channels = 'any'
56
55
  vocoder.latency = LATENCY
57
- 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
58
59
  vocoder.params = {
59
- 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 },
60
61
  }
package/index.js CHANGED
@@ -17,6 +17,8 @@ function process(mag, phase, state, ctx) {
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.5",
3
+ "version": "1.2.1",
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",