@audio/synth-osc 1.0.4 → 1.1.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/README.md +13 -2
- package/audio.js +5 -6
- package/blep.js +59 -0
- package/osc.d.ts +5 -2
- package/osc.js +11 -10
- package/package.json +3 -5
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @audio/synth-osc [](https://www.npmjs.com/package/@audio/synth-osc) [](https://github.com/krishnized/license)
|
|
2
2
|
|
|
3
|
-
Oscillator —
|
|
3
|
+
Oscillator — band-limited sine/square/saw/triangle, detune, custom wave fn
|
|
4
4
|
|
|
5
5
|
```
|
|
6
6
|
npm install @audio/synth-osc
|
|
@@ -10,7 +10,18 @@ npm install @audio/synth-osc
|
|
|
10
10
|
import osc from '@audio/synth-osc'
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
-
Classic waveform oscillator
|
|
13
|
+
Classic waveform oscillator with cents detune and phase offset, in [`periodic-function`](https://www.npmjs.com/package/periodic-function)'s phase conventions (saw falling, square high on the first half, triangle falling from its peak). Pitched — `(freq, opts)`, the family generator contract.
|
|
14
|
+
|
|
15
|
+
Square, saw and triangle are band-limited: at each jump in value (saw, square) or slope (triangle) within ±32 samples, the waveform takes the residual of a band-limited step (BLEP; Brandt 2001, Välimäki & Huovilainen 2007) or its integral (BLAMP; Esqueda et al. 2016), a 64-tap Kaiser-windowed sinc (β 10, cutoff 0.47·fs), linear phase, no latency. Measured from 55 Hz to 10 kHz at 44.1 and 48 kHz (2¹⁸-point Blackman-Harris spectra):
|
|
16
|
+
|
|
17
|
+
| | naive (1.0.x) | band-limited |
|
|
18
|
+
|---|---|---|
|
|
19
|
+
| strongest alias below 16 kHz, dB re fundamental (saw) | −54 (55 Hz) to −10 (10 kHz) | under −94 (the analysis floor) |
|
|
20
|
+
| strongest alias anywhere | −52 to −9 | under −75 |
|
|
21
|
+
| all aliased power re harmonics | −29 to −5 | −76 to −109 |
|
|
22
|
+
| harmonics vs the ideal, to 16 kHz | exact, plus aliases | within 0.01 dB; −0.8 dB at 0.45·fs |
|
|
23
|
+
|
|
24
|
+
`wave(type, t, dt)` gives one sample of it at phase `t` advancing `dt = f/fs` per sample, for oscillators of your own (synth-sfx sweeps its pitch through it).
|
|
14
25
|
|
|
15
26
|
```js
|
|
16
27
|
osc(440, { type: 'sawtooth', detune: -5 }) // → Float32Array
|
package/audio.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
// atom manifest — oscillator as a generator: no input bus, phase in closure.
|
|
1
|
+
// atom manifest — oscillator as a generator: no input bus, phase in closure, band-limited (blep.js).
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
const WAVES = { sine, square, sawtooth, triangle }
|
|
3
|
+
import { wave } from './blep.js'
|
|
6
4
|
|
|
7
5
|
export const osc = (ctx) => {
|
|
8
6
|
let t = 0
|
|
@@ -11,12 +9,13 @@ export const osc = (ctx) => {
|
|
|
11
9
|
if (!out || !out.length) return
|
|
12
10
|
const f = params.freq[0] * 2 ** (params.detune[0] / 1200)
|
|
13
11
|
const dt = f / ctx.sampleRate
|
|
14
|
-
const
|
|
12
|
+
const type = params.type
|
|
15
13
|
const g = params.gain[0]
|
|
16
14
|
const o0 = out[0]
|
|
17
15
|
for (let i = 0; i < o0.length; i++) {
|
|
18
|
-
o0[i] = g *
|
|
16
|
+
o0[i] = g * wave(type, t, dt)
|
|
19
17
|
t += dt
|
|
18
|
+
t -= Math.floor(t)
|
|
20
19
|
}
|
|
21
20
|
for (let c = 1; c < out.length; c++) out[c].set(o0)
|
|
22
21
|
}
|
package/blep.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// Band-limited classic waveforms. Each is the naive shape (periodic-function's phase conventions: saw falling
|
|
2
|
+
// 1 → −1, square high on [0, ½), triangle falling from 1 at t = 0) plus, at every discontinuity within ±32
|
|
3
|
+
// samples, the band-limited step's residual (BLEP, Brandt 2001, "Hard sync without aliasing"; Välimäki &
|
|
4
|
+
// Huovilainen 2007, "Antialiasing oscillators in subtractive synthesis") for a jump in value, or its integral
|
|
5
|
+
// (BLAMP, Esqueda, Välimäki & Bilbao 2016) for a jump in slope. The step is a 64-tap Kaiser-windowed sinc
|
|
6
|
+
// (β = 10, cutoff 0.47·fs), linear phase; the discontinuities' positions follow from the phase and its
|
|
7
|
+
// increment, so no state is kept and nothing is delayed. Measured (2^18-point Blackman-Harris spectra, 55 Hz to
|
|
8
|
+
// 10 kHz, 44.1 and 48 kHz): aliases below 16 kHz under −94 dB re the fundamental, the analysis floor, and under
|
|
9
|
+
// −75 dB anywhere; harmonics within 0.01 dB of the ideal to 16 kHz, 0.8 dB low at 0.45·fs.
|
|
10
|
+
|
|
11
|
+
const K = 32, O = 128, FC = 0.47, BETA = 10
|
|
12
|
+
|
|
13
|
+
let H, R // ∫h and ∫(∫h − step), tabulated O per sample over (−K, K)
|
|
14
|
+
function tables () {
|
|
15
|
+
let L = 2 * K * O + 1, h = new Float64Array(L)
|
|
16
|
+
let I0 = x => { let s = 1, t = 1; for (let k = 1; k < 60; k++) { t *= (x / 2 / k) ** 2; s += t } return s }
|
|
17
|
+
for (let i = 0; i < L; i++) {
|
|
18
|
+
let x = i / O - K, u = x / K
|
|
19
|
+
h[i] = (x ? Math.sin(2 * Math.PI * FC * x) / (Math.PI * x) : 2 * FC) * I0(BETA * Math.sqrt(Math.max(0, 1 - u * u))) / I0(BETA)
|
|
20
|
+
}
|
|
21
|
+
let sum = 0
|
|
22
|
+
for (let i = 1; i < L; i++) sum += (h[i - 1] + h[i]) / 2
|
|
23
|
+
H = new Float64Array(L); R = new Float64Array(L)
|
|
24
|
+
for (let i = 1; i < L; i++) H[i] = H[i - 1] + (h[i - 1] + h[i]) / 2 / sum
|
|
25
|
+
// R integrates r = H − step; the cell ending on the step takes r's left limit there
|
|
26
|
+
for (let i = 1, c = K * O; i < L; i++) {
|
|
27
|
+
let r0 = H[i - 1] - (i - 1 >= c ? 1 : 0), r1 = H[i] - (i > c ? 1 : 0)
|
|
28
|
+
R[i] = R[i - 1] + (r0 + r1) / 2 / O
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const lookup = (T, x) => { let p = (x + K) * O, i = p | 0; return i >= T.length - 1 ? T[T.length - 1] : T[i] + (T[i + 1] - T[i]) * (p - i) }
|
|
33
|
+
|
|
34
|
+
// residuals x samples after a unit step (x < 0: before it) and after a unit change of slope per sample
|
|
35
|
+
const step = x => x <= -K || x >= K ? 0 : lookup(H, x) - (x >= 0 ? 1 : 0)
|
|
36
|
+
const ramp = x => x <= -K || x >= K ? 0 : lookup(R, x)
|
|
37
|
+
|
|
38
|
+
// sum of f over the discontinuities at phase `at`, past and coming, within K samples of phase t
|
|
39
|
+
function around (t, dt, at, f) {
|
|
40
|
+
let p = t - at, s = 0
|
|
41
|
+
if (p < 0) p += 1
|
|
42
|
+
for (let x = p / dt; x < K; x += 1 / dt) s += f(x)
|
|
43
|
+
for (let x = (p - 1) / dt; x > -K; x -= 1 / dt) s += f(x)
|
|
44
|
+
return s
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Band-limited waveform value at phase t ∈ [0, 1) advancing dt = f/fs per sample.
|
|
49
|
+
* @param {'sine'|'sawtooth'|'square'|'triangle'} type
|
|
50
|
+
*/
|
|
51
|
+
export function wave (type, t, dt) {
|
|
52
|
+
if (type === 'sine') return Math.sin(2 * Math.PI * t)
|
|
53
|
+
if (!H) tables()
|
|
54
|
+
let bl = dt > 0
|
|
55
|
+
if (type === 'sawtooth') return 1 - 2 * t + (bl ? 2 * around(t, dt, 0, step) : 0)
|
|
56
|
+
if (type === 'square') return (t < 0.5 ? 1 : -1) + (bl ? 2 * (around(t, dt, 0, step) - around(t, dt, 0.5, step)) : 0)
|
|
57
|
+
if (type === 'triangle') return (t < 0.5 ? 1 - 4 * t : 4 * t - 3) + (bl ? 8 * dt * (around(t, dt, 0.5, ramp) - around(t, dt, 0, ramp)) : 0)
|
|
58
|
+
throw new RangeError(`wave: unknown type "${type}"`)
|
|
59
|
+
}
|
package/osc.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/** Classic waveform oscillator
|
|
1
|
+
/** Classic waveform oscillator, band-limited, with detune + phase offset. */
|
|
2
2
|
export interface OscOptions {
|
|
3
3
|
/** seconds, default 1 */
|
|
4
4
|
duration?: number
|
|
@@ -12,9 +12,12 @@ export interface OscOptions {
|
|
|
12
12
|
detune?: number
|
|
13
13
|
/** starting phase 0..1, default 0 */
|
|
14
14
|
phase?: number
|
|
15
|
-
/** custom waveform fn, overrides type */
|
|
15
|
+
/** custom waveform fn, overrides type (rendered as given, not band-limited) */
|
|
16
16
|
wave?: (t: number) => number
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
/** freq: Hz (positional) */
|
|
20
20
|
export default function osc(freq: number, options?: OscOptions): Float32Array
|
|
21
|
+
|
|
22
|
+
/** One sample of a band-limited waveform at phase t ∈ [0, 1) advancing dt = f/fs per sample (BLEP / BLAMP). */
|
|
23
|
+
export function wave(type: 'sine' | 'square' | 'sawtooth' | 'triangle', t: number, dt: number): number
|
package/osc.js
CHANGED
|
@@ -1,26 +1,27 @@
|
|
|
1
|
-
// Oscillator — classic waveforms
|
|
1
|
+
// Oscillator — classic waveforms, band-limited (blep.js), with detune.
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { wave } from './blep.js'
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
export { wave }
|
|
6
|
+
|
|
7
|
+
const TYPES = ['sine', 'square', 'sawtooth', 'triangle']
|
|
6
8
|
|
|
7
9
|
/**
|
|
8
10
|
* @param {number} freq — Hz
|
|
9
11
|
* @param {object} opts — { duration=1 (s), fs=44100, type='sine'|'square'|'sawtooth'|'triangle',
|
|
10
|
-
* amp=0.8, detune=0 (cents), phase=0 (0..1), wave: custom t=>v }
|
|
12
|
+
* amp=0.8, detune=0 (cents), phase=0 (0..1), wave: custom t=>v (rendered as given, not band-limited) }
|
|
11
13
|
* @returns {Float32Array}
|
|
12
14
|
*/
|
|
13
|
-
export default function osc (freq, { duration = 1, fs = 44100, type = 'sine', amp = 0.8, detune = 0, phase = 0, wave } = {}) {
|
|
14
|
-
|
|
15
|
-
if (!fn) throw new RangeError(`osc: unknown type "${type}"`)
|
|
15
|
+
export default function osc (freq, { duration = 1, fs = 44100, type = 'sine', amp = 0.8, detune = 0, phase = 0, wave: fn } = {}) {
|
|
16
|
+
if (!fn && !TYPES.includes(type)) throw new RangeError(`osc: unknown type "${type}"`)
|
|
16
17
|
let f = freq * 2 ** (detune / 1200)
|
|
17
18
|
let n = Math.round(duration * fs)
|
|
18
19
|
let d = new Float32Array(n)
|
|
19
|
-
let t = phase
|
|
20
20
|
let dt = f / fs
|
|
21
21
|
for (let i = 0; i < n; i++) {
|
|
22
|
-
|
|
23
|
-
t +=
|
|
22
|
+
let t = (phase + i * dt) % 1
|
|
23
|
+
if (t < 0) t += 1
|
|
24
|
+
d[i] = amp * (fn ? fn(t) : wave(type, t, dt))
|
|
24
25
|
}
|
|
25
26
|
return d
|
|
26
27
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@audio/synth-osc",
|
|
3
|
-
"version": "1.0
|
|
4
|
-
"description": "Oscillator — sine/square/sawtooth/triangle with detune
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "Oscillator — band-limited sine/square/sawtooth/triangle with detune",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
7
7
|
"main": "osc.js",
|
|
@@ -15,14 +15,12 @@
|
|
|
15
15
|
},
|
|
16
16
|
"files": [
|
|
17
17
|
"osc.js",
|
|
18
|
+
"blep.js",
|
|
18
19
|
"osc.d.ts",
|
|
19
20
|
"audio.js",
|
|
20
21
|
"audio.d.ts"
|
|
21
22
|
],
|
|
22
23
|
"types": "osc.d.ts",
|
|
23
|
-
"dependencies": {
|
|
24
|
-
"periodic-function": "^2.1.0"
|
|
25
|
-
},
|
|
26
24
|
"keywords": [
|
|
27
25
|
"audio",
|
|
28
26
|
"dsp",
|