@audio/shift-hybrid 1.0.0 → 1.0.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 +104 -0
  2. package/index.js +1 -1
  3. package/package.json +3 -3
package/host.js ADDED
@@ -0,0 +1,104 @@
1
+ // Pitch-shift host plumbing (inlined family convention): input normalization,
2
+ // ratio semantics, identity fast-path, channel/stream writers.
3
+
4
+ function isChannelArray(data) {
5
+ return Array.isArray(data) && data.every((c) => c instanceof Float32Array)
6
+ }
7
+
8
+ function normalizeOptionsInput(data) {
9
+ if (data === undefined || data === null || typeof data === 'object') return data
10
+ throw new TypeError('pitchShift: options must be an object')
11
+ }
12
+
13
+ export function resolvePitchParams(opts) {
14
+ let semitones = opts?.semitones ?? 0
15
+ if (!Number.isFinite(semitones)) throw new TypeError('pitchShift: `semitones` must be a finite number')
16
+ let raw = opts?.ratio
17
+ if (typeof raw === 'function' || raw instanceof Float32Array) {
18
+ throw new TypeError('pitchShift: variable `ratio` (function or Float32Array) is supported by vocoder, phaseLock, transient, formant, paulstretch, sms, hpss, and sample')
19
+ }
20
+ let ratio = raw ?? (semitones ? Math.pow(2, semitones / 12) : 1)
21
+ if (!Number.isFinite(ratio) || ratio <= 0) throw new TypeError('pitchShift: `ratio` must be a finite number > 0')
22
+ return { ratio, semitones }
23
+ }
24
+
25
+ function passThroughWriter() {
26
+ let flushed = false
27
+ return (chunk) => {
28
+ if (chunk === undefined) { flushed = true; return new Float32Array(0) }
29
+ if (flushed) throw new Error('pitchShift: stream already flushed')
30
+ return new Float32Array(chunk)
31
+ }
32
+ }
33
+
34
+ function createChannelWriter(factory) {
35
+ let mode = null
36
+ let writers = null
37
+
38
+ return (chunk) => {
39
+ if (chunk === undefined) {
40
+ if (!writers) return new Float32Array(0)
41
+ return mode === 'channels' ? writers.map((w) => w()) : writers[0]()
42
+ }
43
+
44
+ if (isChannelArray(chunk)) {
45
+ if (!writers) { mode = 'channels'; writers = chunk.map(() => factory()) }
46
+ if (mode !== 'channels') throw new TypeError('pitchShift: cannot mix mono and multi-channel writes')
47
+ if (writers.length !== chunk.length) throw new TypeError('pitchShift: streaming channel count must stay constant')
48
+ return chunk.map((c, i) => writers[i](c))
49
+ }
50
+
51
+ if (!(chunk instanceof Float32Array)) {
52
+ throw new TypeError('pitchShift: streaming input must be Float32Array or array of Float32Array channels')
53
+ }
54
+
55
+ if (!writers) { mode = 'mono'; writers = [factory()] }
56
+ if (mode !== 'mono') throw new TypeError('pitchShift: cannot mix mono and multi-channel writes')
57
+ return writers[0](chunk)
58
+ }
59
+ }
60
+
61
+ export function makePitchShift(batch, stream) {
62
+ let isVariable = (opts) => {
63
+ let raw = opts?.ratio
64
+ return typeof raw === 'function' || raw instanceof Float32Array
65
+ }
66
+ let isIdentity = (opts) => {
67
+ if (isVariable(opts)) return false
68
+ return resolvePitchParams(opts).ratio === 1
69
+ }
70
+ return function shift(data, opts) {
71
+ if (data instanceof Float32Array) {
72
+ if (isIdentity(opts)) return new Float32Array(data)
73
+ return batch(data, opts)
74
+ }
75
+ if (isChannelArray(data)) {
76
+ if (isIdentity(opts)) return data.map((c) => new Float32Array(c))
77
+ return data.map((c) => batch(c, opts))
78
+ }
79
+ opts = normalizeOptionsInput(data)
80
+ if (isIdentity(opts)) return createChannelWriter(() => passThroughWriter())
81
+ return createChannelWriter(() => stream(opts))
82
+ }
83
+ }
84
+
85
+ // Streaming adapter for whole-signal-look-ahead algorithms: buffer, emit on flush.
86
+ export function bufferedStream(batch, opts) {
87
+ let parts = []
88
+ let flushed = false
89
+ return (chunk) => {
90
+ if (chunk === undefined) {
91
+ if (flushed) return new Float32Array(0)
92
+ flushed = true
93
+ let total = 0
94
+ for (let p of parts) total += p.length
95
+ let all = new Float32Array(total)
96
+ let o = 0
97
+ for (let p of parts) { all.set(p, o); o += p.length }
98
+ return batch(all, opts)
99
+ }
100
+ if (flushed) throw new Error('pitchShift: stream already flushed')
101
+ parts.push(new Float32Array(chunk))
102
+ return new Float32Array(0)
103
+ }
104
+ }
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 '@audio/shift-core'
4
+ import { makePitchShift, resolvePitchParams, bufferedStream } 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:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@audio/shift-hybrid",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Hybrid pitch shift — runs phase-lock + WSOLA in parallel, crossfades by transient confidence",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -10,10 +10,10 @@
10
10
  "./package.json": "./package.json"
11
11
  },
12
12
  "files": [
13
- "index.js"
13
+ "index.js",
14
+ "host.js"
14
15
  ],
15
16
  "dependencies": {
16
- "@audio/shift-core": "^1.0.0",
17
17
  "@audio/shift-pvoc-lock": "^1.0.0",
18
18
  "@audio/shift-wsola": "^1.0.0",
19
19
  "fourier-transform": "^2.3.0"