@sxnnyside/somnia 0.0.0-stage → 0.6.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 +50 -2
- package/dist/index.cjs +283 -0
- package/dist/index.d.ts +188 -0
- package/dist/index.js +283 -0
- package/package.json +39 -4
package/README.md
CHANGED
|
@@ -1,3 +1,51 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @sxnnyside/somnia
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Deterministic zero-allocation procedural audio generator and ambient synthesizer engine for TypeScript, Node.js, and the Web Audio API.
|
|
4
|
+
|
|
5
|
+
### Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
bun add @sxnnyside/somnia # npm i @sxnnyside/somnia
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
### Usage
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { SomniaEngine, ScaleType } from "@sxnnyside/somnia";
|
|
15
|
+
|
|
16
|
+
const engine = new SomniaEngine(424242);
|
|
17
|
+
const note = engine.nextEvent(ScaleType.Dorian, 0, 120);
|
|
18
|
+
|
|
19
|
+
if (note.isRest) {
|
|
20
|
+
console.log("REST");
|
|
21
|
+
} else {
|
|
22
|
+
console.log(`${note.label} | ${note.frequency} Hz | ${note.durationMs} ms`);
|
|
23
|
+
}
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
### API
|
|
27
|
+
|
|
28
|
+
Full API reference is documented with TSDoc annotations. See [API.md](https://github.com/core-red-project/somnia/blob/main/API.md) for stability guarantees.
|
|
29
|
+
|
|
30
|
+
### Browser (Web Audio API)
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
import { SomniaEngine, ScaleType, playNote } from "@sxnnyside/somnia";
|
|
34
|
+
|
|
35
|
+
const ctx = new AudioContext();
|
|
36
|
+
const engine = new SomniaEngine(1337);
|
|
37
|
+
|
|
38
|
+
function playNext() {
|
|
39
|
+
const note = engine.nextEvent(ScaleType.Lydian, 0, 90);
|
|
40
|
+
playNote(ctx, note);
|
|
41
|
+
setTimeout(playNext, note.durationMs);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
document.getElementById("start-btn")?.addEventListener("click", () => {
|
|
45
|
+
ctx.resume().then(playNext);
|
|
46
|
+
});
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### License
|
|
50
|
+
|
|
51
|
+
Mozilla Public License 2.0 (MPL-2.0)
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
var ScaleType;
|
|
3
|
+
((ScaleType) => {
|
|
4
|
+
ScaleType[ScaleType["Lydian"] = 0] = "Lydian";
|
|
5
|
+
ScaleType[ScaleType["MinorPentatonic"] = 1] = "MinorPentatonic";
|
|
6
|
+
ScaleType[ScaleType["MajorPentatonic"] = 2] = "MajorPentatonic";
|
|
7
|
+
ScaleType[ScaleType["Dorian"] = 3] = "Dorian";
|
|
8
|
+
ScaleType[ScaleType["Minor"] = 4] = "Minor";
|
|
9
|
+
ScaleType[ScaleType["Major"] = 5] = "Major";
|
|
10
|
+
ScaleType[ScaleType["Mixolydian"] = 6] = "Mixolydian";
|
|
11
|
+
ScaleType[ScaleType["Phrygian"] = 7] = "Phrygian";
|
|
12
|
+
ScaleType[ScaleType["Blues"] = 8] = "Blues";
|
|
13
|
+
ScaleType[ScaleType["Hirajoshi"] = 9] = "Hirajoshi";
|
|
14
|
+
})(ScaleType ||= {});
|
|
15
|
+
var PlayMode;
|
|
16
|
+
((PlayMode) => {
|
|
17
|
+
PlayMode[PlayMode["Melody"] = 0] = "Melody";
|
|
18
|
+
PlayMode[PlayMode["Arpeggio"] = 1] = "Arpeggio";
|
|
19
|
+
PlayMode[PlayMode["Chords"] = 2] = "Chords";
|
|
20
|
+
})(PlayMode ||= {});
|
|
21
|
+
var TimeSignature;
|
|
22
|
+
((TimeSignature) => {
|
|
23
|
+
TimeSignature[TimeSignature["FourFour"] = 0] = "FourFour";
|
|
24
|
+
TimeSignature[TimeSignature["ThreeFour"] = 1] = "ThreeFour";
|
|
25
|
+
TimeSignature[TimeSignature["SevenEight"] = 2] = "SevenEight";
|
|
26
|
+
})(TimeSignature ||= {});
|
|
27
|
+
var MIDI_FREQUENCIES = [
|
|
28
|
+
8,
|
|
29
|
+
9,
|
|
30
|
+
9,
|
|
31
|
+
10,
|
|
32
|
+
10,
|
|
33
|
+
11,
|
|
34
|
+
12,
|
|
35
|
+
12,
|
|
36
|
+
13,
|
|
37
|
+
14,
|
|
38
|
+
15,
|
|
39
|
+
15,
|
|
40
|
+
16,
|
|
41
|
+
17,
|
|
42
|
+
18,
|
|
43
|
+
19,
|
|
44
|
+
21,
|
|
45
|
+
22,
|
|
46
|
+
23,
|
|
47
|
+
24,
|
|
48
|
+
26,
|
|
49
|
+
28,
|
|
50
|
+
29,
|
|
51
|
+
31,
|
|
52
|
+
33,
|
|
53
|
+
35,
|
|
54
|
+
37,
|
|
55
|
+
39,
|
|
56
|
+
41,
|
|
57
|
+
44,
|
|
58
|
+
46,
|
|
59
|
+
49,
|
|
60
|
+
52,
|
|
61
|
+
55,
|
|
62
|
+
58,
|
|
63
|
+
62,
|
|
64
|
+
65,
|
|
65
|
+
69,
|
|
66
|
+
73,
|
|
67
|
+
78,
|
|
68
|
+
82,
|
|
69
|
+
87,
|
|
70
|
+
92,
|
|
71
|
+
98,
|
|
72
|
+
104,
|
|
73
|
+
110,
|
|
74
|
+
117,
|
|
75
|
+
123,
|
|
76
|
+
131,
|
|
77
|
+
139,
|
|
78
|
+
147,
|
|
79
|
+
156,
|
|
80
|
+
165,
|
|
81
|
+
175,
|
|
82
|
+
185,
|
|
83
|
+
196,
|
|
84
|
+
208,
|
|
85
|
+
220,
|
|
86
|
+
233,
|
|
87
|
+
247,
|
|
88
|
+
262,
|
|
89
|
+
277,
|
|
90
|
+
294,
|
|
91
|
+
311,
|
|
92
|
+
330,
|
|
93
|
+
349,
|
|
94
|
+
370,
|
|
95
|
+
392,
|
|
96
|
+
415,
|
|
97
|
+
440,
|
|
98
|
+
466,
|
|
99
|
+
494,
|
|
100
|
+
523,
|
|
101
|
+
554,
|
|
102
|
+
587,
|
|
103
|
+
622,
|
|
104
|
+
659,
|
|
105
|
+
698,
|
|
106
|
+
740,
|
|
107
|
+
784,
|
|
108
|
+
831,
|
|
109
|
+
880,
|
|
110
|
+
932,
|
|
111
|
+
988,
|
|
112
|
+
1047,
|
|
113
|
+
1109,
|
|
114
|
+
1175,
|
|
115
|
+
1245,
|
|
116
|
+
1319,
|
|
117
|
+
1397,
|
|
118
|
+
1480,
|
|
119
|
+
1568,
|
|
120
|
+
1661,
|
|
121
|
+
1760,
|
|
122
|
+
1865,
|
|
123
|
+
1976,
|
|
124
|
+
2093,
|
|
125
|
+
2217,
|
|
126
|
+
2349,
|
|
127
|
+
2489,
|
|
128
|
+
2637,
|
|
129
|
+
2794,
|
|
130
|
+
2960,
|
|
131
|
+
3136,
|
|
132
|
+
3322,
|
|
133
|
+
3520,
|
|
134
|
+
3729,
|
|
135
|
+
3951,
|
|
136
|
+
4186,
|
|
137
|
+
4435,
|
|
138
|
+
4699,
|
|
139
|
+
4978,
|
|
140
|
+
5274,
|
|
141
|
+
5588,
|
|
142
|
+
5920,
|
|
143
|
+
6272,
|
|
144
|
+
6645,
|
|
145
|
+
7040,
|
|
146
|
+
7459,
|
|
147
|
+
7902,
|
|
148
|
+
8372,
|
|
149
|
+
8870,
|
|
150
|
+
9397,
|
|
151
|
+
9956,
|
|
152
|
+
10548,
|
|
153
|
+
11175,
|
|
154
|
+
11840,
|
|
155
|
+
12544
|
|
156
|
+
];
|
|
157
|
+
var NOTE_NAMES = [
|
|
158
|
+
"C",
|
|
159
|
+
"C#",
|
|
160
|
+
"D",
|
|
161
|
+
"D#",
|
|
162
|
+
"E",
|
|
163
|
+
"F",
|
|
164
|
+
"F#",
|
|
165
|
+
"G",
|
|
166
|
+
"G#",
|
|
167
|
+
"A",
|
|
168
|
+
"A#",
|
|
169
|
+
"B"
|
|
170
|
+
];
|
|
171
|
+
var SCALE_INTERVALS = {
|
|
172
|
+
[0 /* Lydian */]: [0, 2, 4, 6, 7, 9, 11],
|
|
173
|
+
[1 /* MinorPentatonic */]: [0, 3, 5, 7, 10],
|
|
174
|
+
[2 /* MajorPentatonic */]: [0, 2, 4, 7, 9],
|
|
175
|
+
[3 /* Dorian */]: [0, 2, 3, 5, 7, 9, 10],
|
|
176
|
+
[4 /* Minor */]: [0, 2, 3, 5, 7, 8, 10],
|
|
177
|
+
[5 /* Major */]: [0, 2, 4, 5, 7, 9, 11],
|
|
178
|
+
[6 /* Mixolydian */]: [0, 2, 4, 5, 7, 9, 10],
|
|
179
|
+
[7 /* Phrygian */]: [0, 1, 3, 5, 7, 8, 10],
|
|
180
|
+
[8 /* Blues */]: [0, 3, 5, 6, 7, 10],
|
|
181
|
+
[9 /* Hirajoshi */]: [0, 2, 3, 7, 8]
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
class SomniaEngine {
|
|
185
|
+
seed;
|
|
186
|
+
state = {
|
|
187
|
+
phaseQ8: 0,
|
|
188
|
+
stepIndex: 0
|
|
189
|
+
};
|
|
190
|
+
constructor(seed = 424242) {
|
|
191
|
+
this.seed = seed;
|
|
192
|
+
}
|
|
193
|
+
nextEvent(scale = 3 /* Dorian */, rootNote = 0, bpm = 120, hasRests = true) {
|
|
194
|
+
if (bpm <= 0) {
|
|
195
|
+
throw new RangeError("BPM must be greater than 0");
|
|
196
|
+
}
|
|
197
|
+
const timeStepQ8 = 64;
|
|
198
|
+
this.state.phaseQ8 = this.state.phaseQ8 + timeStepQ8 & 2147483647;
|
|
199
|
+
this.state.stepIndex++;
|
|
200
|
+
let h = (this.seed ^ this.state.stepIndex * 1664525 + 1013904223) >>> 0;
|
|
201
|
+
h = (h ^ h >>> 16) * 73244475 >>> 0;
|
|
202
|
+
h = (h ^ h >>> 16) * 73244475 >>> 0;
|
|
203
|
+
h = (h ^ h >>> 16) >>> 0;
|
|
204
|
+
if (hasRests && h % 100 < 15) {
|
|
205
|
+
const stepDurationMs = Math.round(60000 / bpm);
|
|
206
|
+
return {
|
|
207
|
+
frequency: 0,
|
|
208
|
+
durationMs: stepDurationMs,
|
|
209
|
+
label: "REST",
|
|
210
|
+
midiNote: 0,
|
|
211
|
+
velocity: 0,
|
|
212
|
+
isRest: true
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
const intervals = SCALE_INTERVALS[scale];
|
|
216
|
+
const degree = h % intervals.length;
|
|
217
|
+
const octaveOffset = Math.floor(h / 7) % 3 - 1;
|
|
218
|
+
const baseOctave = 4 + octaveOffset;
|
|
219
|
+
const midiNote = Math.min(127, Math.max(0, 12 * baseOctave + rootNote + intervals[degree]));
|
|
220
|
+
const frequency = MIDI_FREQUENCIES[midiNote] || 440;
|
|
221
|
+
const noteName = `${NOTE_NAMES[midiNote % 12]}${Math.floor(midiNote / 12) - 1}`;
|
|
222
|
+
const durationRatio = [0.25, 0.5, 0.75, 1][(h >>> 8) % 4];
|
|
223
|
+
const durationMs = Math.round(60000 / bpm * durationRatio);
|
|
224
|
+
const velocity = 70 + (h >>> 12) % 45;
|
|
225
|
+
return {
|
|
226
|
+
frequency,
|
|
227
|
+
durationMs,
|
|
228
|
+
label: noteName,
|
|
229
|
+
midiNote,
|
|
230
|
+
velocity,
|
|
231
|
+
isRest: false
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
reset(newSeed) {
|
|
235
|
+
if (newSeed !== undefined) {
|
|
236
|
+
this.seed = newSeed;
|
|
237
|
+
}
|
|
238
|
+
this.state = {
|
|
239
|
+
phaseQ8: 0,
|
|
240
|
+
stepIndex: 0
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
getState() {
|
|
244
|
+
return { ...this.state };
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
function playNote(ctx, event, destination) {
|
|
248
|
+
if (event.isRest || event.frequency <= 0) {
|
|
249
|
+
return null;
|
|
250
|
+
}
|
|
251
|
+
const dest = destination || ctx.destination;
|
|
252
|
+
const now = ctx.currentTime;
|
|
253
|
+
const durationSec = event.durationMs / 1000;
|
|
254
|
+
const osc = ctx.createOscillator();
|
|
255
|
+
const gain = ctx.createGain();
|
|
256
|
+
osc.type = "sine";
|
|
257
|
+
osc.frequency.setValueAtTime(event.frequency, now);
|
|
258
|
+
const peakGain = event.velocity / 127 * 0.25;
|
|
259
|
+
gain.gain.setValueAtTime(0.001, now);
|
|
260
|
+
gain.gain.exponentialRampToValueAtTime(peakGain, now + 0.02);
|
|
261
|
+
gain.gain.exponentialRampToValueAtTime(0.001, now + durationSec);
|
|
262
|
+
osc.connect(gain);
|
|
263
|
+
gain.connect(dest);
|
|
264
|
+
osc.start(now);
|
|
265
|
+
osc.stop(now + durationSec + 0.05);
|
|
266
|
+
return {
|
|
267
|
+
stop: () => {
|
|
268
|
+
gain.gain.cancelScheduledValues(ctx.currentTime);
|
|
269
|
+
gain.gain.setValueAtTime(0.001, ctx.currentTime);
|
|
270
|
+
osc.stop(ctx.currentTime + 0.02);
|
|
271
|
+
}
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
export {
|
|
275
|
+
MIDI_FREQUENCIES,
|
|
276
|
+
NOTE_NAMES,
|
|
277
|
+
PlayMode,
|
|
278
|
+
SCALE_INTERVALS,
|
|
279
|
+
ScaleType,
|
|
280
|
+
SomniaEngine,
|
|
281
|
+
TimeSignature,
|
|
282
|
+
playNote
|
|
283
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* Somnia TypeScript / Web Audio API procedural generative sound engine.
|
|
4
|
+
*
|
|
5
|
+
* Provides deterministic, zero-allocation procedural melody generation,
|
|
6
|
+
* 12-TET tuning lookup tables, and real-time Web Audio API playback helpers.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Supported musical modes and scale quantizations.
|
|
10
|
+
*
|
|
11
|
+
* @public
|
|
12
|
+
*/
|
|
13
|
+
export declare enum ScaleType {
|
|
14
|
+
/** Lydian mode (Major with augmented 4th: 1, 2, 3, #4, 5, 6, 7). */
|
|
15
|
+
Lydian = 0,
|
|
16
|
+
/** Minor Pentatonic scale (1, b3, 4, 5, b7). */
|
|
17
|
+
MinorPentatonic = 1,
|
|
18
|
+
/** Major Pentatonic scale (1, 2, 3, 5, 6). */
|
|
19
|
+
MajorPentatonic = 2,
|
|
20
|
+
/** Dorian mode (Minor with major 6th: 1, 2, b3, 4, 5, 6, b7). */
|
|
21
|
+
Dorian = 3,
|
|
22
|
+
/** Natural Minor / Aeolian mode (1, 2, b3, 4, 5, b6, b7). */
|
|
23
|
+
Minor = 4,
|
|
24
|
+
/** Major / Ionian mode (1, 2, 3, 4, 5, 6, 7). */
|
|
25
|
+
Major = 5,
|
|
26
|
+
/** Mixolydian mode (Major with flat 7th: 1, 2, 3, 4, 5, 6, b7). */
|
|
27
|
+
Mixolydian = 6,
|
|
28
|
+
/** Phrygian mode (Minor with flat 2nd: 1, b2, b3, 4, 5, b6, b7). */
|
|
29
|
+
Phrygian = 7,
|
|
30
|
+
/** Hexatonic Blues scale (1, b3, 4, b5, 5, b7). */
|
|
31
|
+
Blues = 8,
|
|
32
|
+
/** Traditional Japanese Hirajoshi pentatonic scale (1, 2, b3, 5, b6). */
|
|
33
|
+
Hirajoshi = 9
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Melodic movement and voice leading modes.
|
|
37
|
+
*
|
|
38
|
+
* @public
|
|
39
|
+
*/
|
|
40
|
+
export declare enum PlayMode {
|
|
41
|
+
/** Stepwise continuous melodic traversal. */
|
|
42
|
+
Melody = 0,
|
|
43
|
+
/** Broken chord arpeggiation across octaves. */
|
|
44
|
+
Arpeggio = 1,
|
|
45
|
+
/** Harmonic cluster chords and dyads. */
|
|
46
|
+
Chords = 2
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Rhythmic metric time signatures.
|
|
50
|
+
*
|
|
51
|
+
* @public
|
|
52
|
+
*/
|
|
53
|
+
export declare enum TimeSignature {
|
|
54
|
+
/** Common time (4 beats per measure). */
|
|
55
|
+
FourFour = 0,
|
|
56
|
+
/** Triple meter (3 beats per measure). */
|
|
57
|
+
ThreeFour = 1,
|
|
58
|
+
/** Complex additive meter (7 eighth-notes per measure). */
|
|
59
|
+
SevenEight = 2
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Discrete musical event generated by the procedural engine.
|
|
63
|
+
*
|
|
64
|
+
* @public
|
|
65
|
+
*/
|
|
66
|
+
export interface NoteEvent {
|
|
67
|
+
/** Fundamental frequency in Hertz (Hz). */
|
|
68
|
+
frequency: number;
|
|
69
|
+
/** Duration of the note in milliseconds. */
|
|
70
|
+
durationMs: number;
|
|
71
|
+
/** Scientific pitch label (e.g. "C4", "A#5", "REST"). */
|
|
72
|
+
label: string;
|
|
73
|
+
/** MIDI note number (0..127). */
|
|
74
|
+
midiNote: number;
|
|
75
|
+
/** MIDI velocity dynamic level (0..127). */
|
|
76
|
+
velocity: number;
|
|
77
|
+
/** Whether this event represents a musical rest (silence). */
|
|
78
|
+
isRest: boolean;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Internal fixed-point Q8.8 state representation for stream continuity.
|
|
82
|
+
*
|
|
83
|
+
* @public
|
|
84
|
+
*/
|
|
85
|
+
export interface StreamStateQ8 {
|
|
86
|
+
/** Phase accumulator in fixed-point Q8.8 format. */
|
|
87
|
+
phaseQ8: number;
|
|
88
|
+
/** Monotonic step counter. */
|
|
89
|
+
stepIndex: number;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Standard 12-TET Equal Temperament Frequency Lookup Table (MIDI 0..127).
|
|
93
|
+
*
|
|
94
|
+
* @internal
|
|
95
|
+
*/
|
|
96
|
+
export declare const MIDI_FREQUENCIES: readonly number[];
|
|
97
|
+
/**
|
|
98
|
+
* Standard Western chromatic note pitch names.
|
|
99
|
+
*
|
|
100
|
+
* @internal
|
|
101
|
+
*/
|
|
102
|
+
export declare const NOTE_NAMES: readonly string[];
|
|
103
|
+
/**
|
|
104
|
+
* Scale intervals in semitones above the tonic.
|
|
105
|
+
*
|
|
106
|
+
* @internal
|
|
107
|
+
*/
|
|
108
|
+
export declare const SCALE_INTERVALS: Record<ScaleType, readonly number[]>;
|
|
109
|
+
/**
|
|
110
|
+
* State-preserving deterministic procedural audio generator.
|
|
111
|
+
*
|
|
112
|
+
* @public
|
|
113
|
+
* @example
|
|
114
|
+
* ```typescript
|
|
115
|
+
* import { SomniaEngine, ScaleType } from "@sxnnyside/somnia";
|
|
116
|
+
*
|
|
117
|
+
* const engine = new SomniaEngine(424242);
|
|
118
|
+
* const note = engine.nextEvent(ScaleType.Dorian, 0, 120);
|
|
119
|
+
* console.log(`Frequency: ${note.frequency} Hz, Duration: ${note.durationMs} ms`);
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
122
|
+
export declare class SomniaEngine {
|
|
123
|
+
seed: number;
|
|
124
|
+
/**
|
|
125
|
+
* Internal stream state.
|
|
126
|
+
* @internal
|
|
127
|
+
*/
|
|
128
|
+
private state;
|
|
129
|
+
/**
|
|
130
|
+
* Creates a new instance of the Somnia generator engine.
|
|
131
|
+
*
|
|
132
|
+
* @param seed - Initial PRNG integer seed.
|
|
133
|
+
*/
|
|
134
|
+
constructor(seed?: number);
|
|
135
|
+
/**
|
|
136
|
+
* Evaluates and yields the next procedural note event.
|
|
137
|
+
*
|
|
138
|
+
* @param scale - Musical mode to quantize pitches into. Defaults to Dorian.
|
|
139
|
+
* @param rootNote - Chromatic transposition in semitones (0 = C). Defaults to 0.
|
|
140
|
+
* @param bpm - Playback tempo in Beats Per Minute (must be > 0). Defaults to 120.
|
|
141
|
+
* @param hasRests - Whether to probabilistically insert musical pauses. Defaults to true.
|
|
142
|
+
* @returns Generated note event structure.
|
|
143
|
+
* @throws RangeError - If bpm is less than or equal to 0.
|
|
144
|
+
*/
|
|
145
|
+
nextEvent(scale?: ScaleType, rootNote?: number, bpm?: number, hasRests?: boolean): NoteEvent;
|
|
146
|
+
/**
|
|
147
|
+
* Resets the internal generator state, optionally applying a new seed.
|
|
148
|
+
*
|
|
149
|
+
* @param newSeed - Optional new integer seed value.
|
|
150
|
+
*/
|
|
151
|
+
reset(newSeed?: number): void;
|
|
152
|
+
/**
|
|
153
|
+
* Returns a snapshot copy of the current generator stream state.
|
|
154
|
+
*
|
|
155
|
+
* @returns Current fixed-point stream state.
|
|
156
|
+
*/
|
|
157
|
+
getState(): Readonly<StreamStateQ8>;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Controller handle returned by playNote for runtime cancellation.
|
|
161
|
+
*
|
|
162
|
+
* @public
|
|
163
|
+
*/
|
|
164
|
+
export interface NotePlaybackHandle {
|
|
165
|
+
/** Immediately silences and stops the oscillator voice. */
|
|
166
|
+
stop: () => void;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Synthesizes and plays a procedural note event using the browser Web Audio API.
|
|
170
|
+
*
|
|
171
|
+
* @param ctx - Active AudioContext instance.
|
|
172
|
+
* @param event - Procedural NoteEvent yielded by SomniaEngine.
|
|
173
|
+
* @param destination - Optional audio node destination. Defaults to ctx.destination.
|
|
174
|
+
* @returns Playback handle with a stop() method, or null if the event is a rest.
|
|
175
|
+
*
|
|
176
|
+
* @public
|
|
177
|
+
* @example
|
|
178
|
+
* ```typescript
|
|
179
|
+
* import { SomniaEngine, playNote } from "@sxnnyside/somnia";
|
|
180
|
+
*
|
|
181
|
+
* const ctx = new AudioContext();
|
|
182
|
+
* const engine = new SomniaEngine();
|
|
183
|
+
* const note = engine.nextEvent();
|
|
184
|
+
* const handle = playNote(ctx, note);
|
|
185
|
+
* // Later: handle?.stop();
|
|
186
|
+
* ```
|
|
187
|
+
*/
|
|
188
|
+
export declare function playNote(ctx: AudioContext, event: NoteEvent, destination?: AudioNode): NotePlaybackHandle | null;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
var ScaleType;
|
|
3
|
+
((ScaleType) => {
|
|
4
|
+
ScaleType[ScaleType["Lydian"] = 0] = "Lydian";
|
|
5
|
+
ScaleType[ScaleType["MinorPentatonic"] = 1] = "MinorPentatonic";
|
|
6
|
+
ScaleType[ScaleType["MajorPentatonic"] = 2] = "MajorPentatonic";
|
|
7
|
+
ScaleType[ScaleType["Dorian"] = 3] = "Dorian";
|
|
8
|
+
ScaleType[ScaleType["Minor"] = 4] = "Minor";
|
|
9
|
+
ScaleType[ScaleType["Major"] = 5] = "Major";
|
|
10
|
+
ScaleType[ScaleType["Mixolydian"] = 6] = "Mixolydian";
|
|
11
|
+
ScaleType[ScaleType["Phrygian"] = 7] = "Phrygian";
|
|
12
|
+
ScaleType[ScaleType["Blues"] = 8] = "Blues";
|
|
13
|
+
ScaleType[ScaleType["Hirajoshi"] = 9] = "Hirajoshi";
|
|
14
|
+
})(ScaleType ||= {});
|
|
15
|
+
var PlayMode;
|
|
16
|
+
((PlayMode) => {
|
|
17
|
+
PlayMode[PlayMode["Melody"] = 0] = "Melody";
|
|
18
|
+
PlayMode[PlayMode["Arpeggio"] = 1] = "Arpeggio";
|
|
19
|
+
PlayMode[PlayMode["Chords"] = 2] = "Chords";
|
|
20
|
+
})(PlayMode ||= {});
|
|
21
|
+
var TimeSignature;
|
|
22
|
+
((TimeSignature) => {
|
|
23
|
+
TimeSignature[TimeSignature["FourFour"] = 0] = "FourFour";
|
|
24
|
+
TimeSignature[TimeSignature["ThreeFour"] = 1] = "ThreeFour";
|
|
25
|
+
TimeSignature[TimeSignature["SevenEight"] = 2] = "SevenEight";
|
|
26
|
+
})(TimeSignature ||= {});
|
|
27
|
+
var MIDI_FREQUENCIES = [
|
|
28
|
+
8,
|
|
29
|
+
9,
|
|
30
|
+
9,
|
|
31
|
+
10,
|
|
32
|
+
10,
|
|
33
|
+
11,
|
|
34
|
+
12,
|
|
35
|
+
12,
|
|
36
|
+
13,
|
|
37
|
+
14,
|
|
38
|
+
15,
|
|
39
|
+
15,
|
|
40
|
+
16,
|
|
41
|
+
17,
|
|
42
|
+
18,
|
|
43
|
+
19,
|
|
44
|
+
21,
|
|
45
|
+
22,
|
|
46
|
+
23,
|
|
47
|
+
24,
|
|
48
|
+
26,
|
|
49
|
+
28,
|
|
50
|
+
29,
|
|
51
|
+
31,
|
|
52
|
+
33,
|
|
53
|
+
35,
|
|
54
|
+
37,
|
|
55
|
+
39,
|
|
56
|
+
41,
|
|
57
|
+
44,
|
|
58
|
+
46,
|
|
59
|
+
49,
|
|
60
|
+
52,
|
|
61
|
+
55,
|
|
62
|
+
58,
|
|
63
|
+
62,
|
|
64
|
+
65,
|
|
65
|
+
69,
|
|
66
|
+
73,
|
|
67
|
+
78,
|
|
68
|
+
82,
|
|
69
|
+
87,
|
|
70
|
+
92,
|
|
71
|
+
98,
|
|
72
|
+
104,
|
|
73
|
+
110,
|
|
74
|
+
117,
|
|
75
|
+
123,
|
|
76
|
+
131,
|
|
77
|
+
139,
|
|
78
|
+
147,
|
|
79
|
+
156,
|
|
80
|
+
165,
|
|
81
|
+
175,
|
|
82
|
+
185,
|
|
83
|
+
196,
|
|
84
|
+
208,
|
|
85
|
+
220,
|
|
86
|
+
233,
|
|
87
|
+
247,
|
|
88
|
+
262,
|
|
89
|
+
277,
|
|
90
|
+
294,
|
|
91
|
+
311,
|
|
92
|
+
330,
|
|
93
|
+
349,
|
|
94
|
+
370,
|
|
95
|
+
392,
|
|
96
|
+
415,
|
|
97
|
+
440,
|
|
98
|
+
466,
|
|
99
|
+
494,
|
|
100
|
+
523,
|
|
101
|
+
554,
|
|
102
|
+
587,
|
|
103
|
+
622,
|
|
104
|
+
659,
|
|
105
|
+
698,
|
|
106
|
+
740,
|
|
107
|
+
784,
|
|
108
|
+
831,
|
|
109
|
+
880,
|
|
110
|
+
932,
|
|
111
|
+
988,
|
|
112
|
+
1047,
|
|
113
|
+
1109,
|
|
114
|
+
1175,
|
|
115
|
+
1245,
|
|
116
|
+
1319,
|
|
117
|
+
1397,
|
|
118
|
+
1480,
|
|
119
|
+
1568,
|
|
120
|
+
1661,
|
|
121
|
+
1760,
|
|
122
|
+
1865,
|
|
123
|
+
1976,
|
|
124
|
+
2093,
|
|
125
|
+
2217,
|
|
126
|
+
2349,
|
|
127
|
+
2489,
|
|
128
|
+
2637,
|
|
129
|
+
2794,
|
|
130
|
+
2960,
|
|
131
|
+
3136,
|
|
132
|
+
3322,
|
|
133
|
+
3520,
|
|
134
|
+
3729,
|
|
135
|
+
3951,
|
|
136
|
+
4186,
|
|
137
|
+
4435,
|
|
138
|
+
4699,
|
|
139
|
+
4978,
|
|
140
|
+
5274,
|
|
141
|
+
5588,
|
|
142
|
+
5920,
|
|
143
|
+
6272,
|
|
144
|
+
6645,
|
|
145
|
+
7040,
|
|
146
|
+
7459,
|
|
147
|
+
7902,
|
|
148
|
+
8372,
|
|
149
|
+
8870,
|
|
150
|
+
9397,
|
|
151
|
+
9956,
|
|
152
|
+
10548,
|
|
153
|
+
11175,
|
|
154
|
+
11840,
|
|
155
|
+
12544
|
|
156
|
+
];
|
|
157
|
+
var NOTE_NAMES = [
|
|
158
|
+
"C",
|
|
159
|
+
"C#",
|
|
160
|
+
"D",
|
|
161
|
+
"D#",
|
|
162
|
+
"E",
|
|
163
|
+
"F",
|
|
164
|
+
"F#",
|
|
165
|
+
"G",
|
|
166
|
+
"G#",
|
|
167
|
+
"A",
|
|
168
|
+
"A#",
|
|
169
|
+
"B"
|
|
170
|
+
];
|
|
171
|
+
var SCALE_INTERVALS = {
|
|
172
|
+
[0 /* Lydian */]: [0, 2, 4, 6, 7, 9, 11],
|
|
173
|
+
[1 /* MinorPentatonic */]: [0, 3, 5, 7, 10],
|
|
174
|
+
[2 /* MajorPentatonic */]: [0, 2, 4, 7, 9],
|
|
175
|
+
[3 /* Dorian */]: [0, 2, 3, 5, 7, 9, 10],
|
|
176
|
+
[4 /* Minor */]: [0, 2, 3, 5, 7, 8, 10],
|
|
177
|
+
[5 /* Major */]: [0, 2, 4, 5, 7, 9, 11],
|
|
178
|
+
[6 /* Mixolydian */]: [0, 2, 4, 5, 7, 9, 10],
|
|
179
|
+
[7 /* Phrygian */]: [0, 1, 3, 5, 7, 8, 10],
|
|
180
|
+
[8 /* Blues */]: [0, 3, 5, 6, 7, 10],
|
|
181
|
+
[9 /* Hirajoshi */]: [0, 2, 3, 7, 8]
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
class SomniaEngine {
|
|
185
|
+
seed;
|
|
186
|
+
state = {
|
|
187
|
+
phaseQ8: 0,
|
|
188
|
+
stepIndex: 0
|
|
189
|
+
};
|
|
190
|
+
constructor(seed = 424242) {
|
|
191
|
+
this.seed = seed;
|
|
192
|
+
}
|
|
193
|
+
nextEvent(scale = 3 /* Dorian */, rootNote = 0, bpm = 120, hasRests = true) {
|
|
194
|
+
if (bpm <= 0) {
|
|
195
|
+
throw new RangeError("BPM must be greater than 0");
|
|
196
|
+
}
|
|
197
|
+
const timeStepQ8 = 64;
|
|
198
|
+
this.state.phaseQ8 = this.state.phaseQ8 + timeStepQ8 & 2147483647;
|
|
199
|
+
this.state.stepIndex++;
|
|
200
|
+
let h = (this.seed ^ this.state.stepIndex * 1664525 + 1013904223) >>> 0;
|
|
201
|
+
h = (h ^ h >>> 16) * 73244475 >>> 0;
|
|
202
|
+
h = (h ^ h >>> 16) * 73244475 >>> 0;
|
|
203
|
+
h = (h ^ h >>> 16) >>> 0;
|
|
204
|
+
if (hasRests && h % 100 < 15) {
|
|
205
|
+
const stepDurationMs = Math.round(60000 / bpm);
|
|
206
|
+
return {
|
|
207
|
+
frequency: 0,
|
|
208
|
+
durationMs: stepDurationMs,
|
|
209
|
+
label: "REST",
|
|
210
|
+
midiNote: 0,
|
|
211
|
+
velocity: 0,
|
|
212
|
+
isRest: true
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
const intervals = SCALE_INTERVALS[scale];
|
|
216
|
+
const degree = h % intervals.length;
|
|
217
|
+
const octaveOffset = Math.floor(h / 7) % 3 - 1;
|
|
218
|
+
const baseOctave = 4 + octaveOffset;
|
|
219
|
+
const midiNote = Math.min(127, Math.max(0, 12 * baseOctave + rootNote + intervals[degree]));
|
|
220
|
+
const frequency = MIDI_FREQUENCIES[midiNote] || 440;
|
|
221
|
+
const noteName = `${NOTE_NAMES[midiNote % 12]}${Math.floor(midiNote / 12) - 1}`;
|
|
222
|
+
const durationRatio = [0.25, 0.5, 0.75, 1][(h >>> 8) % 4];
|
|
223
|
+
const durationMs = Math.round(60000 / bpm * durationRatio);
|
|
224
|
+
const velocity = 70 + (h >>> 12) % 45;
|
|
225
|
+
return {
|
|
226
|
+
frequency,
|
|
227
|
+
durationMs,
|
|
228
|
+
label: noteName,
|
|
229
|
+
midiNote,
|
|
230
|
+
velocity,
|
|
231
|
+
isRest: false
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
reset(newSeed) {
|
|
235
|
+
if (newSeed !== undefined) {
|
|
236
|
+
this.seed = newSeed;
|
|
237
|
+
}
|
|
238
|
+
this.state = {
|
|
239
|
+
phaseQ8: 0,
|
|
240
|
+
stepIndex: 0
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
getState() {
|
|
244
|
+
return { ...this.state };
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
function playNote(ctx, event, destination) {
|
|
248
|
+
if (event.isRest || event.frequency <= 0) {
|
|
249
|
+
return null;
|
|
250
|
+
}
|
|
251
|
+
const dest = destination || ctx.destination;
|
|
252
|
+
const now = ctx.currentTime;
|
|
253
|
+
const durationSec = event.durationMs / 1000;
|
|
254
|
+
const osc = ctx.createOscillator();
|
|
255
|
+
const gain = ctx.createGain();
|
|
256
|
+
osc.type = "sine";
|
|
257
|
+
osc.frequency.setValueAtTime(event.frequency, now);
|
|
258
|
+
const peakGain = event.velocity / 127 * 0.25;
|
|
259
|
+
gain.gain.setValueAtTime(0.001, now);
|
|
260
|
+
gain.gain.exponentialRampToValueAtTime(peakGain, now + 0.02);
|
|
261
|
+
gain.gain.exponentialRampToValueAtTime(0.001, now + durationSec);
|
|
262
|
+
osc.connect(gain);
|
|
263
|
+
gain.connect(dest);
|
|
264
|
+
osc.start(now);
|
|
265
|
+
osc.stop(now + durationSec + 0.05);
|
|
266
|
+
return {
|
|
267
|
+
stop: () => {
|
|
268
|
+
gain.gain.cancelScheduledValues(ctx.currentTime);
|
|
269
|
+
gain.gain.setValueAtTime(0.001, ctx.currentTime);
|
|
270
|
+
osc.stop(ctx.currentTime + 0.02);
|
|
271
|
+
}
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
export {
|
|
275
|
+
MIDI_FREQUENCIES,
|
|
276
|
+
NOTE_NAMES,
|
|
277
|
+
PlayMode,
|
|
278
|
+
SCALE_INTERVALS,
|
|
279
|
+
ScaleType,
|
|
280
|
+
SomniaEngine,
|
|
281
|
+
TimeSignature,
|
|
282
|
+
playNote
|
|
283
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,41 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sxnnyside/somnia",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.6.0",
|
|
4
|
+
"description": "Deterministic zero-allocation procedural audio generator and ambient synthesizer engine",
|
|
5
|
+
"main": "dist/index.cjs",
|
|
6
|
+
"module": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"require": "./dist/index.cjs",
|
|
12
|
+
"types": "./dist/index.d.ts"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"README.md",
|
|
18
|
+
"LICENSE"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "bun build src/index.ts --outdir dist --target browser && bun build src/index.ts --outfile dist/index.cjs --target node && bun x tsc && rm -rf dist/src"
|
|
22
|
+
},
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "git+https://github.com/core-red-project/somnia.git"
|
|
26
|
+
},
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public"
|
|
29
|
+
},
|
|
30
|
+
"keywords": [
|
|
31
|
+
"audio",
|
|
32
|
+
"synth",
|
|
33
|
+
"procedural",
|
|
34
|
+
"music",
|
|
35
|
+
"ambient",
|
|
36
|
+
"generative",
|
|
37
|
+
"webaudio"
|
|
38
|
+
],
|
|
39
|
+
"author": "Core Red Project",
|
|
40
|
+
"license": "MPL-2.0"
|
|
41
|
+
}
|