@audio/stretch-pvoc-lock 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-lock
2
+
3
+ Phase-locked vocoder (Laroche & Dolson, 1999). After propagating phases, locks non-peak bins to their nearest spectral peak's rotation — restores harmonic phase coherence and eliminates the phasiness of plain phase vocoder.
4
+
5
+ ```js
6
+ import pvocLock from '@audio/stretch-pvoc-lock'
7
+ let out = pvocLock(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:** general music — tonal/ambient material.<br>
17
+ **Not for:** percussive material where attacks must stay sharp — use [`@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,47 @@
1
+ {
2
+ "name": "@audio/stretch-pvoc-lock",
3
+ "version": "1.0.0",
4
+ "description": "Phase-locked vocoder (Laroche & Dolson 1999) — eliminates phasiness of plain pvoc",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "main": "pvoc-lock.js",
8
+ "types": "pvoc-lock.d.ts",
9
+ "exports": {
10
+ ".": "./pvoc-lock.js",
11
+ "./package.json": "./package.json"
12
+ },
13
+ "files": [
14
+ "pvoc-lock.js",
15
+ "pvoc-lock.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
+ "phase-locking",
30
+ "stft"
31
+ ],
32
+ "publishConfig": {
33
+ "access": "public"
34
+ },
35
+ "license": "MIT",
36
+ "author": "Dmitry Iv. <dfcreative@gmail.com>",
37
+ "homepage": "https://github.com/audiojs/stretch/tree/main/packages/stretch-pvoc-lock",
38
+ "repository": {
39
+ "type": "git",
40
+ "url": "git+https://github.com/audiojs/stretch.git",
41
+ "directory": "packages/stretch-pvoc-lock"
42
+ },
43
+ "bugs": "https://github.com/audiojs/stretch/issues",
44
+ "engines": {
45
+ "node": ">=18"
46
+ }
47
+ }
package/pvoc-lock.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ import type { StreamWriter, StretchOpts } from '@audio/stretch-core'
2
+
3
+ export type PvocLockOpts = StretchOpts
4
+
5
+ declare const pvocLock: {
6
+ (data: Float32Array, opts?: PvocLockOpts): Float32Array
7
+ (opts?: PvocLockOpts): StreamWriter
8
+ }
9
+ export default pvocLock
package/pvoc-lock.js ADDED
@@ -0,0 +1,44 @@
1
+ // Phase-locked vocoder (Laroche & Dolson, 1999). After propagating phases,
2
+ // locks non-peak bins to their nearest spectral peak's rotation — restores
3
+ // harmonic phase coherence, eliminating the "phasiness" of plain pvoc.
4
+ //
5
+ // For attack preservation on percussion, use @audio/stretch-transient.
6
+
7
+ import { stftBatch, stftStream } from 'fourier-transform/stft'
8
+ import { writer, wrapPhase, lockPhase, stretchOpts } from '@audio/stretch-core'
9
+
10
+ function process(mag, phase, state, ctx) {
11
+ let { half, anaHop, synHop, freqPerBin } = ctx
12
+
13
+ if (!state.prev) {
14
+ state.prev = new Float64Array(half + 1)
15
+ state.synPrev = new Float64Array(half + 1)
16
+ state.p = new Float64Array(half + 1)
17
+ state.first = true
18
+ }
19
+
20
+ // Unlike plain pvoc, pre-pad partial frames are processed, not passed through:
21
+ // per-frame peak locking re-coheres whatever the early propagation does, and
22
+ // warming the phase state on pad frames measures ~0.2 dB better LSD at onset.
23
+ let p = state.p
24
+ if (state.first) {
25
+ p.set(phase)
26
+ state.first = false
27
+ } else {
28
+ for (let k = 0; k <= half; k++) {
29
+ let dp = wrapPhase(phase[k] - state.prev[k] - k * freqPerBin * anaHop)
30
+ p[k] = state.synPrev[k] + (k * freqPerBin + dp / anaHop) * synHop
31
+ }
32
+ lockPhase(phase, p, mag, half)
33
+ }
34
+
35
+ state.prev.set(phase)
36
+ state.synPrev.set(p)
37
+ return { mag, phase: p }
38
+ }
39
+
40
+ export default function pvocLock(data, opts) {
41
+ if (!(data instanceof Float32Array)) return writer(stftStream(process, stretchOpts(data)))
42
+ if ((opts?.factor ?? 1) === 1) return new Float32Array(data)
43
+ return stftBatch(data, process, stretchOpts(opts))
44
+ }