@audio/synth-osc 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.
Files changed (3) hide show
  1. package/audio-module.js +30 -0
  2. package/osc.js +26 -0
  3. package/package.json +35 -0
@@ -0,0 +1,30 @@
1
+ // audio-module manifest — oscillator as a generator: no input bus, phase in closure.
2
+
3
+ import { sine, square, sawtooth, triangle } from 'periodic-function'
4
+
5
+ const WAVES = { sine, square, sawtooth, triangle }
6
+
7
+ export const osc = (ctx) => {
8
+ let t = 0
9
+ return (inputs, outputs, params) => {
10
+ const out = outputs[0]
11
+ if (!out || !out.length) return
12
+ const f = params.freq[0] * 2 ** (params.detune[0] / 1200)
13
+ const dt = f / ctx.sampleRate
14
+ const fn = WAVES[params.type]
15
+ const g = params.gain[0]
16
+ const o0 = out[0]
17
+ for (let i = 0; i < o0.length; i++) {
18
+ o0[i] = g * fn(t % 1)
19
+ t += dt
20
+ }
21
+ for (let c = 1; c < out.length; c++) out[c].set(o0)
22
+ }
23
+ }
24
+ osc.channels = { inputs: [], outputs: 1 }
25
+ osc.params = {
26
+ freq: { type: 'number', min: 20, max: 20000, default: 440, curve: 'log', unit: 'Hz' },
27
+ detune: { type: 'number', min: -1200, max: 1200, default: 0, unit: 'cents' },
28
+ gain: { type: 'number', min: 0, max: 1, default: 0.8, smoothing: 0.01 },
29
+ type: { type: 'enum', values: ['sine', 'square', 'sawtooth', 'triangle'], default: 'sine' },
30
+ }
package/osc.js ADDED
@@ -0,0 +1,26 @@
1
+ // Oscillator — classic waveforms rendered from periodic-function (scijs), with detune.
2
+
3
+ import { sine, square, sawtooth, triangle } from 'periodic-function'
4
+
5
+ const WAVES = { sine, square, sawtooth, triangle }
6
+
7
+ /**
8
+ * @param {number} freq — Hz
9
+ * @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 }
11
+ * @returns {Float32Array}
12
+ */
13
+ export default function osc (freq, { duration = 1, fs = 44100, type = 'sine', amp = 0.8, detune = 0, phase = 0, wave } = {}) {
14
+ let fn = wave || WAVES[type]
15
+ if (!fn) throw new RangeError(`osc: unknown type "${type}"`)
16
+ let f = freq * 2 ** (detune / 1200)
17
+ let n = Math.round(duration * fs)
18
+ let d = new Float32Array(n)
19
+ let t = phase
20
+ let dt = f / fs
21
+ for (let i = 0; i < n; i++) {
22
+ d[i] = amp * fn(t % 1)
23
+ t += dt
24
+ }
25
+ return d
26
+ }
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@audio/synth-osc",
3
+ "version": "1.0.0",
4
+ "description": "Oscillator \u2014 sine/square/sawtooth/triangle with detune (wraps periodic-function)",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "main": "osc.js",
8
+ "exports": {
9
+ ".": "./osc.js",
10
+ "./package.json": "./package.json",
11
+ "./audio-module": "./audio-module.js"
12
+ },
13
+ "files": [
14
+ "osc.js",
15
+ "audio-module.js"
16
+ ],
17
+ "dependencies": {
18
+ "periodic-function": "^2.1.0"
19
+ },
20
+ "keywords": [
21
+ "audio",
22
+ "dsp",
23
+ "synth",
24
+ "oscillator"
25
+ ],
26
+ "license": "MIT",
27
+ "author": "Dmitry Iv. <dfcreative@gmail.com>",
28
+ "engines": {
29
+ "node": ">=18"
30
+ },
31
+ "publishConfig": {
32
+ "access": "public"
33
+ },
34
+ "audio-module": "./audio-module.js"
35
+ }