@audio/stretch-transient 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 +25 -0
- package/README.md +26 -0
- package/package.json +47 -0
- package/transient.d.ts +11 -0
- package/transient.js +76 -0
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,26 @@
|
|
|
1
|
+
# @audio/stretch-transient
|
|
2
|
+
|
|
3
|
+
Transient-aware phase-locked vocoder (Röbel, 2003). Measures spectral flux between frames; on a sharp onset resets to the original analysis phase instead of propagating it, preserving attack sharpness on drums and plucks. Implies phase locking.
|
|
4
|
+
|
|
5
|
+
```js
|
|
6
|
+
import transient from '@audio/stretch-transient'
|
|
7
|
+
|
|
8
|
+
transient(data, { factor: 2 })
|
|
9
|
+
transient(data, { factor: 1.5, transientThreshold: 2.0 }) // less sensitive
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
| Param | Default | |
|
|
13
|
+
|---|---|---|
|
|
14
|
+
| `factor` | `1` | Time stretch ratio |
|
|
15
|
+
| `frameSize` | `2048` | FFT size (power of 2) |
|
|
16
|
+
| `hopSize` | `frameSize/4` | Hop between frames |
|
|
17
|
+
| `transientThreshold` | `1.5` | Spectral flux threshold (higher = fewer resets) |
|
|
18
|
+
|
|
19
|
+
**Use when:** the right default for most music — percussion, mixed sources.<br>
|
|
20
|
+
**Not for:** voice/speech (use [`@audio/stretch-psola`](../stretch-psola)) or extreme stretch (use [`@audio/stretch-paulstretch`](../stretch-paulstretch)).
|
|
21
|
+
|
|
22
|
+
Part of [`@audio/stretch`](../..).
|
|
23
|
+
|
|
24
|
+
## License
|
|
25
|
+
|
|
26
|
+
[MIT](./LICENSE) · [ॐ](https://github.com/krishnized/license/)
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@audio/stretch-transient",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Transient-aware phase-locked vocoder (Röbel 2003) — preserves attacks on drums and plucks",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"main": "transient.js",
|
|
8
|
+
"types": "transient.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": "./transient.js",
|
|
11
|
+
"./package.json": "./package.json"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"transient.js",
|
|
15
|
+
"transient.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
|
+
"transient",
|
|
29
|
+
"onset",
|
|
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-transient",
|
|
38
|
+
"repository": {
|
|
39
|
+
"type": "git",
|
|
40
|
+
"url": "git+https://github.com/audiojs/stretch.git",
|
|
41
|
+
"directory": "packages/stretch-transient"
|
|
42
|
+
},
|
|
43
|
+
"bugs": "https://github.com/audiojs/stretch/issues",
|
|
44
|
+
"engines": {
|
|
45
|
+
"node": ">=18"
|
|
46
|
+
}
|
|
47
|
+
}
|
package/transient.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { StreamWriter, StretchOpts } from '@audio/stretch-core'
|
|
2
|
+
|
|
3
|
+
export interface TransientOpts extends StretchOpts {
|
|
4
|
+
transientThreshold?: number
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
declare const transient: {
|
|
8
|
+
(data: Float32Array, opts?: TransientOpts): Float32Array
|
|
9
|
+
(opts?: TransientOpts): StreamWriter
|
|
10
|
+
}
|
|
11
|
+
export default transient
|
package/transient.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// Transient-aware phase-locked vocoder (Röbel, 2003). Measures spectral flux
|
|
2
|
+
// between frames; on a sharp onset, resets to the original analysis phase
|
|
3
|
+
// instead of propagating it — preserving attack sharpness on drums and plucks.
|
|
4
|
+
// Implies phase locking.
|
|
5
|
+
|
|
6
|
+
import { stftBatch, stftStream } from 'fourier-transform/stft'
|
|
7
|
+
import { writer, wrapPhase, lockPhase, stretchOpts } from '@audio/stretch-core'
|
|
8
|
+
|
|
9
|
+
function updateFluxStats(state, value, alpha) {
|
|
10
|
+
if (state.fluxMean == null) { state.fluxMean = value; state.fluxVar = 0; return }
|
|
11
|
+
let delta = value - state.fluxMean
|
|
12
|
+
state.fluxMean += alpha * delta
|
|
13
|
+
state.fluxVar = (1 - alpha) * (state.fluxVar + alpha * delta * delta)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function makeProcess(threshold) {
|
|
17
|
+
return function (mag, phase, state, ctx) {
|
|
18
|
+
let { half, anaHop, synHop, freqPerBin } = ctx
|
|
19
|
+
|
|
20
|
+
if (!state.prev) {
|
|
21
|
+
state.prev = new Float64Array(half + 1)
|
|
22
|
+
state.synPrev = new Float64Array(half + 1)
|
|
23
|
+
state.prevMag = new Float64Array(half + 1)
|
|
24
|
+
state.p = new Float64Array(half + 1)
|
|
25
|
+
state.frames = 0
|
|
26
|
+
state.cooldown = 0
|
|
27
|
+
state.first = true
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
let isTransient = false
|
|
31
|
+
if (!state.first) {
|
|
32
|
+
let flux = 0, energy = 0
|
|
33
|
+
for (let k = 0; k <= half; k++) {
|
|
34
|
+
let weight = 0.5 + 0.5 * k / Math.max(1, half)
|
|
35
|
+
let d = Math.log1p(mag[k]) - Math.log1p(state.prevMag[k])
|
|
36
|
+
if (d > 0) flux += d
|
|
37
|
+
energy += weight * Math.log1p(mag[k])
|
|
38
|
+
}
|
|
39
|
+
let normFlux = energy > 1e-10 ? flux / energy : 0
|
|
40
|
+
let mean = state.fluxMean ?? normFlux
|
|
41
|
+
let std = Math.sqrt(state.fluxVar ?? 0)
|
|
42
|
+
// Std floor 0.07: steady polyphonic beating measures ≤ ~0.07 normFlux (p90),
|
|
43
|
+
// genuine onsets ≥ ~0.19 — the floor keeps chord beats from firing resets.
|
|
44
|
+
isTransient = state.frames > 4 && state.cooldown === 0 &&
|
|
45
|
+
normFlux > mean + threshold * Math.max(0.07, std) && normFlux > mean * 1.35
|
|
46
|
+
updateFluxStats(state, normFlux, isTransient ? 0.3 : 0.12)
|
|
47
|
+
state.cooldown = isTransient ? 1 : Math.max(0, state.cooldown - 1)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
let p = state.p
|
|
51
|
+
if (state.first || isTransient) {
|
|
52
|
+
p.set(phase)
|
|
53
|
+
state.first = false
|
|
54
|
+
} else {
|
|
55
|
+
for (let k = 0; k <= half; k++) {
|
|
56
|
+
let dp = wrapPhase(phase[k] - state.prev[k] - k * freqPerBin * anaHop)
|
|
57
|
+
p[k] = state.synPrev[k] + (k * freqPerBin + dp / anaHop) * synHop
|
|
58
|
+
}
|
|
59
|
+
lockPhase(phase, p, mag, half)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
state.prev.set(phase)
|
|
63
|
+
state.synPrev.set(p)
|
|
64
|
+
state.prevMag.set(mag)
|
|
65
|
+
state.frames++
|
|
66
|
+
return { mag, phase: p }
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export default function transient(data, opts) {
|
|
71
|
+
let threshold = (data instanceof Float32Array ? opts?.transientThreshold : data?.transientThreshold) ?? 1.5
|
|
72
|
+
let process = makeProcess(threshold)
|
|
73
|
+
if (!(data instanceof Float32Array)) return writer(stftStream(process, stretchOpts(data)))
|
|
74
|
+
if ((opts?.factor ?? 1) === 1) return new Float32Array(data)
|
|
75
|
+
return stftBatch(data, process, stretchOpts(opts))
|
|
76
|
+
}
|