@audio/stretch-pvoc 1.0.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/LICENSE ADDED
@@ -0,0 +1,25 @@
1
+ MIT License
2
+
3
+ Copyright (c) Dmitry Iv
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
23
+ ---
24
+
25
+ This work is offered under the Krishnized License (https://github.com/krishnized/license).
package/README.md ADDED
@@ -0,0 +1,23 @@
1
+ # @audio/stretch-pvoc
2
+
3
+ Plain phase vocoder. Each FFT bin's phase advances at its instantaneous frequency. Magnitudes preserved, but inter-harmonic phase coherence is lost — complex signals get a diffuse, "underwater" character. Educational baseline.
4
+
5
+ ```js
6
+ import pvoc from '@audio/stretch-pvoc'
7
+ let out = pvoc(data, { factor: 2 })
8
+ ```
9
+
10
+ | Param | Default | |
11
+ |---|---|---|
12
+ | `factor` | `1` | Time stretch ratio |
13
+ | `frameSize` | `2048` | FFT size (power of 2) |
14
+ | `hopSize` | `frameSize/4` | Hop between frames |
15
+
16
+ **Use when:** learning the phase-vocoder algorithm, simple tonal signals.<br>
17
+ **Not for:** general music — use [`@audio/stretch-pvoc-lock`](../stretch-pvoc-lock) or [`@audio/stretch-transient`](../stretch-transient).
18
+
19
+ Part of [`@audio/stretch`](../..).
20
+
21
+ ## License
22
+
23
+ [MIT](./LICENSE) · [ॐ](https://github.com/krishnized/license/)
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@audio/stretch-pvoc",
3
+ "version": "1.0.0",
4
+ "description": "Phase vocoder time stretching — educational baseline without phase locking",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "main": "pvoc.js",
8
+ "types": "pvoc.d.ts",
9
+ "exports": {
10
+ ".": "./pvoc.js",
11
+ "./package.json": "./package.json"
12
+ },
13
+ "files": [
14
+ "pvoc.js",
15
+ "pvoc.d.ts",
16
+ "README.md",
17
+ "LICENSE"
18
+ ],
19
+ "dependencies": {
20
+ "@audio/stretch-core": "^1.0.0",
21
+ "fourier-transform": "^2.3.0"
22
+ },
23
+ "keywords": [
24
+ "audio",
25
+ "dsp",
26
+ "time-stretch",
27
+ "phase-vocoder",
28
+ "pvoc",
29
+ "stft"
30
+ ],
31
+ "publishConfig": {
32
+ "access": "public"
33
+ },
34
+ "license": "MIT",
35
+ "author": "Dmitry Iv. <dfcreative@gmail.com>",
36
+ "homepage": "https://github.com/audiojs/stretch/tree/main/packages/stretch-pvoc",
37
+ "repository": {
38
+ "type": "git",
39
+ "url": "git+https://github.com/audiojs/stretch.git",
40
+ "directory": "packages/stretch-pvoc"
41
+ },
42
+ "bugs": "https://github.com/audiojs/stretch/issues",
43
+ "engines": {
44
+ "node": ">=18"
45
+ }
46
+ }
package/pvoc.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ import type { StreamWriter, StretchOpts } from '@audio/stretch-core'
2
+
3
+ export type PvocOpts = StretchOpts
4
+
5
+ declare const pvoc: {
6
+ (data: Float32Array, opts?: PvocOpts): Float32Array
7
+ (opts?: PvocOpts): StreamWriter
8
+ }
9
+ export default pvoc
package/pvoc.js ADDED
@@ -0,0 +1,47 @@
1
+ // Plain phase vocoder. Each bin's phase advances at its instantaneous frequency,
2
+ // independently of the rest. Magnitudes preserved but incoherent inter-harmonic
3
+ // phase relationships give complex signals a diffuse, "underwater" quality.
4
+ //
5
+ // For harmonic coherence use @audio/stretch-pvoc-lock.
6
+ // For transient preservation use @audio/stretch-transient.
7
+
8
+ import { stftBatch, stftStream } from 'fourier-transform/stft'
9
+ import { writer, wrapPhase, stretchOpts } from '@audio/stretch-core'
10
+
11
+ function process(mag, phase, state, ctx) {
12
+ let { half, anaHop, synHop, freqPerBin, frameStart } = ctx
13
+
14
+ if (!state.prev) {
15
+ state.prev = new Float64Array(half + 1)
16
+ state.sum = new Float64Array(half + 1)
17
+ state.p = new Float64Array(half + 1)
18
+ state.first = true
19
+ }
20
+
21
+ // stftBatch pre-pads with N zeros; propagating phase through those partial
22
+ // frames drifts the accumulator. Pass them through untouched (preserves the
23
+ // onset), then anchor once the analysis frame sits fully inside the signal.
24
+ if (state.first) {
25
+ if (frameStart < 0) return { mag, phase }
26
+ state.sum.set(phase)
27
+ state.prev.set(phase)
28
+ state.first = false
29
+ return { mag, phase }
30
+ }
31
+
32
+ let p = state.p
33
+ for (let k = 0; k <= half; k++) {
34
+ let dp = wrapPhase(phase[k] - state.prev[k] - k * freqPerBin * anaHop)
35
+ state.sum[k] += (k * freqPerBin + dp / anaHop) * synHop
36
+ p[k] = state.sum[k]
37
+ }
38
+
39
+ state.prev.set(phase)
40
+ return { mag, phase: p }
41
+ }
42
+
43
+ export default function pvoc(data, opts) {
44
+ if (!(data instanceof Float32Array)) return writer(stftStream(process, stretchOpts(data)))
45
+ if ((opts?.factor ?? 1) === 1) return new Float32Array(data)
46
+ return stftBatch(data, process, stretchOpts(opts))
47
+ }