@music-ui/osc 0.0.0-stage → 0.18.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 +34 -2
- package/dist/index.cjs +496 -0
- package/dist/index.d.cts +185 -0
- package/dist/index.d.ts +185 -0
- package/dist/index.js +466 -0
- package/dist/styles/index.css +7 -0
- package/dist/styles/index.d.cts +2 -0
- package/dist/styles/index.d.ts +2 -0
- package/package.json +52 -3
package/README.md
CHANGED
|
@@ -1,3 +1,35 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Music UI - Osc
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Oscillator utilities.
|
|
4
|
+
|
|
5
|
+
## Installation and usage
|
|
6
|
+
|
|
7
|
+
[See documentation](https://moonwave99.github.io/music-ui/javascript/music-notation)
|
|
8
|
+
|
|
9
|
+
## AI Policy
|
|
10
|
+
|
|
11
|
+
This project does not accept contributions generated by [LLMs (Large Language Models)](https://en.wikipedia.org/wiki/Large_language_model), sometimes also referred to as "AI". This policy is not open to discussion, any content submitted that is clearly labelled as LLM-generated (including issues, merge requests, and merge request descriptions) will be immediately closed, and any attempt to bypass this policy will result in a ban from the project.
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## MIT License
|
|
16
|
+
|
|
17
|
+
Copyright (c) 2026 Diego Caponera
|
|
18
|
+
|
|
19
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
20
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
21
|
+
in the Software without restriction, including without limitation the rights
|
|
22
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
23
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
24
|
+
furnished to do so, subject to the following conditions:
|
|
25
|
+
|
|
26
|
+
The above copyright notice and this permission notice shall be included in all
|
|
27
|
+
copies or substantial portions of the Software.
|
|
28
|
+
|
|
29
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
30
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
31
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
32
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
33
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
34
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
35
|
+
SOFTWARE.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,496 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
DEFAULT_OPTIONS: () => DEFAULT_OPTIONS,
|
|
24
|
+
DEFAULT_OSCILLATOR_PARAMS: () => DEFAULT_OSCILLATOR_PARAMS,
|
|
25
|
+
OscPlayer: () => OscPlayer,
|
|
26
|
+
Oscillator: () => Oscillator,
|
|
27
|
+
cssClasses: () => cssClasses,
|
|
28
|
+
initOscillator: () => initOscillator,
|
|
29
|
+
initOscillatorWithOscilloscope: () => initOscillatorWithOscilloscope
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(index_exports);
|
|
32
|
+
|
|
33
|
+
// src/OscPlayer.ts
|
|
34
|
+
var import_events = require("events");
|
|
35
|
+
var DEFAULT_MAX_VOICES = 2;
|
|
36
|
+
var DEFAULT_FADE_IN_TIME = 0.05;
|
|
37
|
+
var DEFAULT_FADE_OUT_TIME = 0.05;
|
|
38
|
+
var OscPlayer = class {
|
|
39
|
+
context;
|
|
40
|
+
eventEmitter;
|
|
41
|
+
analyser;
|
|
42
|
+
data;
|
|
43
|
+
oscillators;
|
|
44
|
+
/**
|
|
45
|
+
* Creates an `OscPlayer` instance.
|
|
46
|
+
*
|
|
47
|
+
* @param __namedParameters The accepted params
|
|
48
|
+
*/
|
|
49
|
+
constructor({
|
|
50
|
+
context,
|
|
51
|
+
analyzer,
|
|
52
|
+
voices = DEFAULT_MAX_VOICES
|
|
53
|
+
}) {
|
|
54
|
+
this.context = context;
|
|
55
|
+
this.eventEmitter = new import_events.EventEmitter();
|
|
56
|
+
this.analyser = analyzer;
|
|
57
|
+
this.analyser.connect(context.destination);
|
|
58
|
+
this.data = new Uint8Array(this.analyser.frequencyBinCount);
|
|
59
|
+
this.oscillators = Array.from({ length: voices }, () => {
|
|
60
|
+
const osc = context.createOscillator();
|
|
61
|
+
const gain = context.createGain();
|
|
62
|
+
osc.connect(gain);
|
|
63
|
+
gain.connect(this.analyser);
|
|
64
|
+
return {
|
|
65
|
+
playing: false,
|
|
66
|
+
started: false,
|
|
67
|
+
osc,
|
|
68
|
+
gain,
|
|
69
|
+
timeout: -1
|
|
70
|
+
};
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Returns the analyzer data at the current time
|
|
75
|
+
* @returns the analyzer data
|
|
76
|
+
*/
|
|
77
|
+
getData() {
|
|
78
|
+
this.analyser.getByteTimeDomainData(this.data);
|
|
79
|
+
return this.data;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* @returns the current AudioContext
|
|
83
|
+
*/
|
|
84
|
+
getAudioContext() {
|
|
85
|
+
return this.context;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Listens to the passed OscPlayer event
|
|
89
|
+
* @param eventName The event name
|
|
90
|
+
* @param callback The event callback
|
|
91
|
+
* @returns The corresponding unsubscribe function
|
|
92
|
+
*/
|
|
93
|
+
on(eventName, callback) {
|
|
94
|
+
this.eventEmitter.addListener(eventName, callback);
|
|
95
|
+
return () => this.eventEmitter.removeListener(eventName, callback);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Clears all event listeners and future playback scheduled events.
|
|
99
|
+
*/
|
|
100
|
+
destroy() {
|
|
101
|
+
this.oscillators.forEach(({ osc, gain, timeout, started }) => {
|
|
102
|
+
gain.disconnect();
|
|
103
|
+
osc.disconnect();
|
|
104
|
+
if (started) {
|
|
105
|
+
osc.stop();
|
|
106
|
+
}
|
|
107
|
+
clearTimeout(timeout);
|
|
108
|
+
});
|
|
109
|
+
this.eventEmitter.removeAllListeners();
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Schedules immediate playback.
|
|
113
|
+
* @param __namedParameters The accepted params
|
|
114
|
+
* @returns The current OscPlayer instance
|
|
115
|
+
*/
|
|
116
|
+
play({
|
|
117
|
+
id,
|
|
118
|
+
frequency,
|
|
119
|
+
duration,
|
|
120
|
+
volume,
|
|
121
|
+
fadeInTime = DEFAULT_FADE_IN_TIME,
|
|
122
|
+
fadeOutTime = DEFAULT_FADE_OUT_TIME
|
|
123
|
+
}) {
|
|
124
|
+
if (this.oscillators.every(({ timeout }) => timeout > -1)) {
|
|
125
|
+
console.warn("All oscillators are already playing");
|
|
126
|
+
this.eventEmitter.emit("noAvailableVoices", { id });
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
const { currentTime } = this.context;
|
|
130
|
+
const oscillator = this.oscillators.find(({ timeout }) => timeout < 0);
|
|
131
|
+
oscillator.osc.frequency.value = frequency;
|
|
132
|
+
if (!oscillator.started) {
|
|
133
|
+
oscillator.started = true;
|
|
134
|
+
oscillator.osc.start();
|
|
135
|
+
}
|
|
136
|
+
oscillator.gain.gain.setValueAtTime(0, currentTime);
|
|
137
|
+
oscillator.gain.gain.linearRampToValueAtTime(
|
|
138
|
+
volume,
|
|
139
|
+
currentTime + fadeInTime
|
|
140
|
+
);
|
|
141
|
+
oscillator.gain.gain.setValueAtTime(
|
|
142
|
+
volume,
|
|
143
|
+
currentTime + duration - fadeOutTime
|
|
144
|
+
);
|
|
145
|
+
oscillator.gain.gain.linearRampToValueAtTime(0, currentTime + duration);
|
|
146
|
+
this.eventEmitter.emit("start", { id });
|
|
147
|
+
oscillator.timeout = setTimeout(() => {
|
|
148
|
+
oscillator.timeout = -1;
|
|
149
|
+
this.eventEmitter.emit("finished", { id });
|
|
150
|
+
if (this.oscillators.some(({ timeout }) => timeout === -1)) {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
this.eventEmitter.emit("allFinished");
|
|
154
|
+
}, duration * 1e3);
|
|
155
|
+
return this;
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
// src/oscillator.ts
|
|
160
|
+
var FREQUENCY_BOUNDS = [20, 2e4];
|
|
161
|
+
var cssClasses = {
|
|
162
|
+
oscillator: "oscillator",
|
|
163
|
+
value: "value",
|
|
164
|
+
controls: "controls"
|
|
165
|
+
};
|
|
166
|
+
var DEFAULT_OSCILLATOR_PARAMS = {
|
|
167
|
+
frequency: 440,
|
|
168
|
+
volume: 0.5,
|
|
169
|
+
duration: 1,
|
|
170
|
+
label: "",
|
|
171
|
+
playbackLabel: "Play"
|
|
172
|
+
};
|
|
173
|
+
var Oscillator = class {
|
|
174
|
+
player;
|
|
175
|
+
element;
|
|
176
|
+
id;
|
|
177
|
+
frequency = DEFAULT_OSCILLATOR_PARAMS.frequency;
|
|
178
|
+
volume;
|
|
179
|
+
duration;
|
|
180
|
+
label;
|
|
181
|
+
playbackLabel;
|
|
182
|
+
rendered;
|
|
183
|
+
playbackButton;
|
|
184
|
+
/**
|
|
185
|
+
* Creates an `Oscillator` instance.
|
|
186
|
+
*
|
|
187
|
+
* @param __namedParameters The accepted params
|
|
188
|
+
*/
|
|
189
|
+
constructor({
|
|
190
|
+
player,
|
|
191
|
+
element,
|
|
192
|
+
id,
|
|
193
|
+
frequency = DEFAULT_OSCILLATOR_PARAMS.frequency,
|
|
194
|
+
volume = DEFAULT_OSCILLATOR_PARAMS.volume,
|
|
195
|
+
duration = DEFAULT_OSCILLATOR_PARAMS.duration,
|
|
196
|
+
label = DEFAULT_OSCILLATOR_PARAMS.label,
|
|
197
|
+
playbackLabel = DEFAULT_OSCILLATOR_PARAMS.playbackLabel
|
|
198
|
+
}) {
|
|
199
|
+
if (!element) {
|
|
200
|
+
throw new Error("Element not found");
|
|
201
|
+
}
|
|
202
|
+
this.player = player;
|
|
203
|
+
this.element = element;
|
|
204
|
+
this.id = id;
|
|
205
|
+
this.setFrequency(frequency);
|
|
206
|
+
this.volume = volume;
|
|
207
|
+
this.duration = duration;
|
|
208
|
+
this.label = label;
|
|
209
|
+
this.playbackLabel = playbackLabel;
|
|
210
|
+
this.rendered = false;
|
|
211
|
+
this.playbackButton = null;
|
|
212
|
+
player.on("start", ({ id: id2 }) => {
|
|
213
|
+
if (id2 !== this.id) {
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
this.playbackButton.disabled = true;
|
|
217
|
+
});
|
|
218
|
+
player.on("noAvailableVoices", ({ id: id2 }) => {
|
|
219
|
+
if (id2 !== this.id) {
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
this.playbackButton.disabled = false;
|
|
223
|
+
});
|
|
224
|
+
player.on("finished", ({ id: id2 }) => {
|
|
225
|
+
if (id2 !== this.id) {
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
this.playbackButton.disabled = false;
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Sets the new frequency value, clamped to the audible frequency bounds.
|
|
233
|
+
* @param frequency The new frequency value
|
|
234
|
+
* @returns The current Oscillator instance
|
|
235
|
+
*/
|
|
236
|
+
setFrequency(frequency) {
|
|
237
|
+
const [lowFreq, highFreq] = FREQUENCY_BOUNDS;
|
|
238
|
+
if (frequency < lowFreq) {
|
|
239
|
+
this.frequency = lowFreq;
|
|
240
|
+
console.warn(`Oscillator frequency cannot be lower than ${lowFreq}`);
|
|
241
|
+
return this;
|
|
242
|
+
}
|
|
243
|
+
if (frequency > highFreq) {
|
|
244
|
+
this.frequency = highFreq;
|
|
245
|
+
console.warn(`Oscillator frequency cannot be higher than ${highFreq}`);
|
|
246
|
+
return this;
|
|
247
|
+
}
|
|
248
|
+
this.frequency = frequency;
|
|
249
|
+
return this;
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* @returns the oscillator frequency
|
|
253
|
+
*/
|
|
254
|
+
getFrequency() {
|
|
255
|
+
return this.frequency;
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Renders the UI inside the current element.
|
|
259
|
+
* @returns The current Oscillator instance.
|
|
260
|
+
*/
|
|
261
|
+
render() {
|
|
262
|
+
if (this.rendered) {
|
|
263
|
+
return this;
|
|
264
|
+
}
|
|
265
|
+
this.baseRender();
|
|
266
|
+
return this;
|
|
267
|
+
}
|
|
268
|
+
onPlay() {
|
|
269
|
+
this.player.play({
|
|
270
|
+
id: this.id,
|
|
271
|
+
frequency: this.frequency,
|
|
272
|
+
duration: this.duration,
|
|
273
|
+
volume: this.volume
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
baseRender() {
|
|
277
|
+
const valueElement = document.createElement("div");
|
|
278
|
+
valueElement.classList.add(cssClasses.value);
|
|
279
|
+
valueElement.textContent = this.label ? `${this.frequency} - ${this.label}` : `${this.frequency}`;
|
|
280
|
+
const controlsElement = document.createElement("div");
|
|
281
|
+
controlsElement.classList.add(cssClasses.controls);
|
|
282
|
+
this.playbackButton = document.createElement("button");
|
|
283
|
+
this.playbackButton.textContent = this.playbackLabel;
|
|
284
|
+
this.playbackButton.setAttribute(
|
|
285
|
+
"aria-label",
|
|
286
|
+
`Playback oscillator with frequency: ${this.frequency}`
|
|
287
|
+
);
|
|
288
|
+
this.playbackButton.addEventListener("click", this.onPlay.bind(this));
|
|
289
|
+
controlsElement.appendChild(this.playbackButton);
|
|
290
|
+
this.element.appendChild(valueElement);
|
|
291
|
+
this.element.appendChild(controlsElement);
|
|
292
|
+
this.rendered = true;
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
// src/init.ts
|
|
297
|
+
var import_core = require("@music-ui/core");
|
|
298
|
+
var DEFAULT_OPTIONS = {
|
|
299
|
+
selection: ".oscillator"
|
|
300
|
+
};
|
|
301
|
+
function initOscillator(params = {}) {
|
|
302
|
+
const { selection, player } = { ...DEFAULT_OPTIONS, ...params };
|
|
303
|
+
if (!(player instanceof OscPlayer)) {
|
|
304
|
+
throw new Error("You must pass an OscPlayer instance");
|
|
305
|
+
}
|
|
306
|
+
(0, import_core.ensureSelection)(selection).forEach((element, index) => {
|
|
307
|
+
new Oscillator({
|
|
308
|
+
player,
|
|
309
|
+
element,
|
|
310
|
+
id: `osc-${index + 1}`,
|
|
311
|
+
...(0, import_core.extractElementOptions)(element, DEFAULT_OSCILLATOR_PARAMS)
|
|
312
|
+
}).render();
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// src/initWithOscilloscope.ts
|
|
317
|
+
var import_core2 = require("@music-ui/core");
|
|
318
|
+
|
|
319
|
+
// src/Oscilloscope.ts
|
|
320
|
+
var DEFAULT_AXIS_COLOR = "#1C3A26";
|
|
321
|
+
var DEFAULT_AXIS_WIDTH = 4;
|
|
322
|
+
var DEFAULT_BACKGROUND_COLOR = "#07140C";
|
|
323
|
+
var DEFAULT_WAVEFORM_COLOR = "#86FCA4";
|
|
324
|
+
var DEFAULT_WAVEFORM_WIDTH = 2;
|
|
325
|
+
var Oscilloscope = class {
|
|
326
|
+
element;
|
|
327
|
+
rendered;
|
|
328
|
+
player;
|
|
329
|
+
shouldRefresh;
|
|
330
|
+
canvas;
|
|
331
|
+
resizeObserver;
|
|
332
|
+
axisColor;
|
|
333
|
+
axisStrokeWidth;
|
|
334
|
+
waveFormColor;
|
|
335
|
+
waveFormStrokeWidth;
|
|
336
|
+
backgroundColor;
|
|
337
|
+
/**
|
|
338
|
+
* Creates an `Oscilloscope` instance.
|
|
339
|
+
*
|
|
340
|
+
* @param __namedParameters The accepted params
|
|
341
|
+
*/
|
|
342
|
+
constructor({
|
|
343
|
+
element,
|
|
344
|
+
player,
|
|
345
|
+
axisColor = DEFAULT_AXIS_COLOR,
|
|
346
|
+
axisStrokeWidth = DEFAULT_AXIS_WIDTH,
|
|
347
|
+
waveFormColor = DEFAULT_WAVEFORM_COLOR,
|
|
348
|
+
waveFormStrokeWidth = DEFAULT_WAVEFORM_WIDTH,
|
|
349
|
+
backgroundColor = DEFAULT_BACKGROUND_COLOR
|
|
350
|
+
}) {
|
|
351
|
+
this.element = element;
|
|
352
|
+
this.player = player;
|
|
353
|
+
this.rendered = false;
|
|
354
|
+
this.shouldRefresh = false;
|
|
355
|
+
this.canvas = null;
|
|
356
|
+
this.resizeObserver = null;
|
|
357
|
+
this.axisColor = axisColor;
|
|
358
|
+
this.axisStrokeWidth = axisStrokeWidth;
|
|
359
|
+
this.waveFormColor = waveFormColor;
|
|
360
|
+
this.waveFormStrokeWidth = waveFormStrokeWidth;
|
|
361
|
+
this.backgroundColor = backgroundColor;
|
|
362
|
+
this.player.on("start", () => {
|
|
363
|
+
this.shouldRefresh = true;
|
|
364
|
+
this.drawWave();
|
|
365
|
+
});
|
|
366
|
+
this.player.on("allFinished", () => {
|
|
367
|
+
this.shouldRefresh = false;
|
|
368
|
+
this.render();
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Renders the oscilloscope inside the current element.
|
|
373
|
+
* @returns The current Oscilloscope instance.
|
|
374
|
+
*/
|
|
375
|
+
render() {
|
|
376
|
+
this.baseRender();
|
|
377
|
+
this.resizeCanvas();
|
|
378
|
+
this.drawBackground();
|
|
379
|
+
this.drawAxis();
|
|
380
|
+
return this;
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* Clears all event listeners
|
|
384
|
+
*/
|
|
385
|
+
destroy() {
|
|
386
|
+
this.resizeObserver?.disconnect();
|
|
387
|
+
}
|
|
388
|
+
drawBackground() {
|
|
389
|
+
const canvas = this.canvas;
|
|
390
|
+
const c = canvas.getContext("2d");
|
|
391
|
+
c.fillStyle = this.backgroundColor;
|
|
392
|
+
c.fillRect(0, 0, canvas.width, canvas.height);
|
|
393
|
+
}
|
|
394
|
+
drawAxis() {
|
|
395
|
+
const canvas = this.canvas;
|
|
396
|
+
const c = canvas.getContext("2d");
|
|
397
|
+
c.strokeStyle = this.axisColor;
|
|
398
|
+
c.lineWidth = this.axisStrokeWidth;
|
|
399
|
+
c.beginPath();
|
|
400
|
+
c.moveTo(0, canvas.height / 2);
|
|
401
|
+
c.lineTo(canvas.width, canvas.height / 2);
|
|
402
|
+
c.stroke();
|
|
403
|
+
c.closePath();
|
|
404
|
+
}
|
|
405
|
+
drawWave() {
|
|
406
|
+
const data = this.player.getData();
|
|
407
|
+
const canvas = this.canvas;
|
|
408
|
+
const c = canvas.getContext("2d");
|
|
409
|
+
this.drawBackground();
|
|
410
|
+
this.drawAxis();
|
|
411
|
+
if (!this.shouldRefresh) {
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
c.beginPath();
|
|
415
|
+
c.strokeStyle = this.waveFormColor;
|
|
416
|
+
c.lineWidth = this.waveFormStrokeWidth;
|
|
417
|
+
const segmentWidth = canvas.width / data.length;
|
|
418
|
+
const startIndex = data.findIndex(
|
|
419
|
+
(value, index) => index > 0 && data[index - 1] < 128 && value >= 128
|
|
420
|
+
);
|
|
421
|
+
for (let i = startIndex; i < data.length; i += 1) {
|
|
422
|
+
const x = (i - startIndex) * segmentWidth;
|
|
423
|
+
const v = data[i] / 128;
|
|
424
|
+
const y = v * canvas.height / 2;
|
|
425
|
+
c.lineTo(x, y);
|
|
426
|
+
}
|
|
427
|
+
c.stroke();
|
|
428
|
+
c.closePath();
|
|
429
|
+
requestAnimationFrame(this.drawWave.bind(this));
|
|
430
|
+
}
|
|
431
|
+
resizeCanvas() {
|
|
432
|
+
const { width, height } = this.element.getBoundingClientRect();
|
|
433
|
+
this.canvas.width = width;
|
|
434
|
+
this.canvas.height = height;
|
|
435
|
+
}
|
|
436
|
+
baseRender() {
|
|
437
|
+
if (this.rendered) {
|
|
438
|
+
return;
|
|
439
|
+
}
|
|
440
|
+
this.canvas = document.createElement("canvas");
|
|
441
|
+
this.element.appendChild(this.canvas);
|
|
442
|
+
this.rendered = true;
|
|
443
|
+
if (!this.resizeObserver) {
|
|
444
|
+
this.resizeObserver = new ResizeObserver(this.render.bind(this));
|
|
445
|
+
this.resizeObserver.observe(this.element);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
};
|
|
449
|
+
|
|
450
|
+
// src/initWithOscilloscope.ts
|
|
451
|
+
var cssClasses2 = {
|
|
452
|
+
wrapper: "oscillators-with-oscilloscope",
|
|
453
|
+
oscillators: "oscillators",
|
|
454
|
+
oscillator: "oscillator",
|
|
455
|
+
oscilloscope: "oscilloscope"
|
|
456
|
+
};
|
|
457
|
+
function initOscillatorWithOscilloscope(params) {
|
|
458
|
+
const { selection, player } = {
|
|
459
|
+
selection: cssClasses2.wrapper,
|
|
460
|
+
...params
|
|
461
|
+
};
|
|
462
|
+
if (!(player instanceof OscPlayer)) {
|
|
463
|
+
throw new Error("You must pass an OscPlayer instance");
|
|
464
|
+
}
|
|
465
|
+
(0, import_core2.ensureSelection)(selection).forEach((element, index) => {
|
|
466
|
+
const id = element.dataset.id || `${index + 1}`;
|
|
467
|
+
const { oscillatorsElement, oscilloscopeElement } = (0, import_core2.ensureElements)({
|
|
468
|
+
id,
|
|
469
|
+
parentElement: element,
|
|
470
|
+
elements: {
|
|
471
|
+
oscillatorsElement: `.${cssClasses2.oscillators}`,
|
|
472
|
+
oscilloscopeElement: `.${cssClasses2.oscilloscope}`
|
|
473
|
+
}
|
|
474
|
+
});
|
|
475
|
+
initOscillator({
|
|
476
|
+
selection: oscillatorsElement.querySelectorAll(
|
|
477
|
+
`.${cssClasses2.oscillator}`
|
|
478
|
+
),
|
|
479
|
+
player
|
|
480
|
+
});
|
|
481
|
+
new Oscilloscope({
|
|
482
|
+
element: oscilloscopeElement,
|
|
483
|
+
player
|
|
484
|
+
}).render();
|
|
485
|
+
});
|
|
486
|
+
}
|
|
487
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
488
|
+
0 && (module.exports = {
|
|
489
|
+
DEFAULT_OPTIONS,
|
|
490
|
+
DEFAULT_OSCILLATOR_PARAMS,
|
|
491
|
+
OscPlayer,
|
|
492
|
+
Oscillator,
|
|
493
|
+
cssClasses,
|
|
494
|
+
initOscillator,
|
|
495
|
+
initOscillatorWithOscilloscope
|
|
496
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { EventEmitter } from 'events';
|
|
2
|
+
import { ElementOrSelector } from '@music-ui/core';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The events triggered by the player.
|
|
6
|
+
*/
|
|
7
|
+
type OscPlayerEvents = "start" | "finished" | "noAvailableVoices" | "allFinished";
|
|
8
|
+
/**
|
|
9
|
+
* The osc player event listener callback.
|
|
10
|
+
* @property id The current osc id.
|
|
11
|
+
*/
|
|
12
|
+
type OscPlayerCallback = (params: {
|
|
13
|
+
id: string;
|
|
14
|
+
}) => void;
|
|
15
|
+
/**
|
|
16
|
+
* The parameters accepted by the OscPlayer constructor.
|
|
17
|
+
* @property context The AudioContext used for playback
|
|
18
|
+
* @property analyzer The AnalyserNode used by the oscilloscope
|
|
19
|
+
* @property voices The max allowed playback voices
|
|
20
|
+
*/
|
|
21
|
+
type OscPlayerConstructors = {
|
|
22
|
+
context: AudioContext;
|
|
23
|
+
analyzer: AnalyserNode;
|
|
24
|
+
voices?: number;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* The parameters accepted by the play function.
|
|
28
|
+
* @property id The oscillator unique identifier
|
|
29
|
+
* @property frequency The oscillator frequency
|
|
30
|
+
* @property duration The playback duration
|
|
31
|
+
* @property volume The playback volume
|
|
32
|
+
* @property fadeInTime The playback fade in time
|
|
33
|
+
* @property fadeOutTime The playback fade out time
|
|
34
|
+
*/
|
|
35
|
+
type OscPlayParams = {
|
|
36
|
+
id: string;
|
|
37
|
+
frequency: number;
|
|
38
|
+
duration: number;
|
|
39
|
+
volume: number;
|
|
40
|
+
fadeInTime?: number;
|
|
41
|
+
fadeOutTime?: number;
|
|
42
|
+
};
|
|
43
|
+
declare class OscPlayer {
|
|
44
|
+
private context;
|
|
45
|
+
private eventEmitter;
|
|
46
|
+
private analyser;
|
|
47
|
+
private data;
|
|
48
|
+
private oscillators;
|
|
49
|
+
/**
|
|
50
|
+
* Creates an `OscPlayer` instance.
|
|
51
|
+
*
|
|
52
|
+
* @param __namedParameters The accepted params
|
|
53
|
+
*/
|
|
54
|
+
constructor({ context, analyzer, voices, }: OscPlayerConstructors);
|
|
55
|
+
/**
|
|
56
|
+
* Returns the analyzer data at the current time
|
|
57
|
+
* @returns the analyzer data
|
|
58
|
+
*/
|
|
59
|
+
getData(): Uint8Array<ArrayBuffer>;
|
|
60
|
+
/**
|
|
61
|
+
* @returns the current AudioContext
|
|
62
|
+
*/
|
|
63
|
+
getAudioContext(): AudioContext;
|
|
64
|
+
/**
|
|
65
|
+
* Listens to the passed OscPlayer event
|
|
66
|
+
* @param eventName The event name
|
|
67
|
+
* @param callback The event callback
|
|
68
|
+
* @returns The corresponding unsubscribe function
|
|
69
|
+
*/
|
|
70
|
+
on(eventName: OscPlayerEvents, callback: OscPlayerCallback): () => EventEmitter;
|
|
71
|
+
/**
|
|
72
|
+
* Clears all event listeners and future playback scheduled events.
|
|
73
|
+
*/
|
|
74
|
+
destroy(): void;
|
|
75
|
+
/**
|
|
76
|
+
* Schedules immediate playback.
|
|
77
|
+
* @param __namedParameters The accepted params
|
|
78
|
+
* @returns The current OscPlayer instance
|
|
79
|
+
*/
|
|
80
|
+
play({ id, frequency, duration, volume, fadeInTime, fadeOutTime, }: OscPlayParams): this | undefined;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* @property {string} content The oscillator element class
|
|
85
|
+
* @property {string} value The oscillator value element class
|
|
86
|
+
* @property {string} controls The oscillator controls element class
|
|
87
|
+
*/
|
|
88
|
+
declare const cssClasses: {
|
|
89
|
+
readonly oscillator: "oscillator";
|
|
90
|
+
readonly value: "value";
|
|
91
|
+
readonly controls: "controls";
|
|
92
|
+
};
|
|
93
|
+
declare const DEFAULT_OSCILLATOR_PARAMS: {
|
|
94
|
+
readonly frequency: 440;
|
|
95
|
+
readonly volume: 0.5;
|
|
96
|
+
readonly duration: 1;
|
|
97
|
+
readonly label: "";
|
|
98
|
+
readonly playbackLabel: "Play";
|
|
99
|
+
};
|
|
100
|
+
/**
|
|
101
|
+
* The parameters accepted by the Oscillator constructor.
|
|
102
|
+
*
|
|
103
|
+
* @property player The OscPlayer instance
|
|
104
|
+
* @property element The element where the oscillator will be rendered
|
|
105
|
+
* @property id The oscillator unique identifier
|
|
106
|
+
* @property frequency The oscillator frequency
|
|
107
|
+
* @property volume The oscillator volume
|
|
108
|
+
* @property label The oscillator label
|
|
109
|
+
* @property playbackLabel The oscillator playback button label
|
|
110
|
+
*/
|
|
111
|
+
type OscillatorParams = {
|
|
112
|
+
player: OscPlayer;
|
|
113
|
+
element: HTMLElement;
|
|
114
|
+
id: string;
|
|
115
|
+
frequency?: number;
|
|
116
|
+
volume?: number;
|
|
117
|
+
duration?: number;
|
|
118
|
+
label?: string;
|
|
119
|
+
playbackLabel?: string;
|
|
120
|
+
};
|
|
121
|
+
declare class Oscillator {
|
|
122
|
+
private player;
|
|
123
|
+
private element;
|
|
124
|
+
private id;
|
|
125
|
+
private frequency;
|
|
126
|
+
private volume;
|
|
127
|
+
private duration;
|
|
128
|
+
private label;
|
|
129
|
+
private playbackLabel;
|
|
130
|
+
private rendered;
|
|
131
|
+
private playbackButton;
|
|
132
|
+
/**
|
|
133
|
+
* Creates an `Oscillator` instance.
|
|
134
|
+
*
|
|
135
|
+
* @param __namedParameters The accepted params
|
|
136
|
+
*/
|
|
137
|
+
constructor({ player, element, id, frequency, volume, duration, label, playbackLabel, }: OscillatorParams);
|
|
138
|
+
/**
|
|
139
|
+
* Sets the new frequency value, clamped to the audible frequency bounds.
|
|
140
|
+
* @param frequency The new frequency value
|
|
141
|
+
* @returns The current Oscillator instance
|
|
142
|
+
*/
|
|
143
|
+
setFrequency(frequency: number): Oscillator;
|
|
144
|
+
/**
|
|
145
|
+
* @returns the oscillator frequency
|
|
146
|
+
*/
|
|
147
|
+
getFrequency(): number;
|
|
148
|
+
/**
|
|
149
|
+
* Renders the UI inside the current element.
|
|
150
|
+
* @returns The current Oscillator instance.
|
|
151
|
+
*/
|
|
152
|
+
render(): Oscillator;
|
|
153
|
+
private onPlay;
|
|
154
|
+
private baseRender;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* The params expected by the `initOscillator` function.
|
|
159
|
+
* @property selection The elements to be initialized.
|
|
160
|
+
*/
|
|
161
|
+
type InitOscillatorParams<T extends HTMLElement> = {
|
|
162
|
+
selection: ElementOrSelector<T>;
|
|
163
|
+
player: OscPlayer;
|
|
164
|
+
};
|
|
165
|
+
declare const DEFAULT_OPTIONS: {
|
|
166
|
+
readonly selection: ".oscillator";
|
|
167
|
+
};
|
|
168
|
+
/**
|
|
169
|
+
* Initializes oscillators on the passed selection.
|
|
170
|
+
* @param params The initialization params.
|
|
171
|
+
*/
|
|
172
|
+
declare function initOscillator<T extends HTMLElement>(params?: Partial<InitOscillatorParams<T>>): void;
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* The params expected by the `initOscillatorWithOscilloscope` function.
|
|
176
|
+
* @property player An `OscPlayer` instance.
|
|
177
|
+
*/
|
|
178
|
+
type InitOscillatorWithOscilloscopeParams<T extends HTMLElement> = InitOscillatorParams<T>;
|
|
179
|
+
/**
|
|
180
|
+
* Initializes oscillators and oscilloscope on the passed selection.
|
|
181
|
+
* @param params The initialization params.
|
|
182
|
+
*/
|
|
183
|
+
declare function initOscillatorWithOscilloscope<T extends HTMLElement>(params: Partial<InitOscillatorWithOscilloscopeParams<T>>): void;
|
|
184
|
+
|
|
185
|
+
export { DEFAULT_OPTIONS, DEFAULT_OSCILLATOR_PARAMS, type InitOscillatorParams, OscPlayer, type OscPlayerCallback, type OscPlayerEvents, Oscillator, type OscillatorParams, cssClasses, initOscillator, initOscillatorWithOscilloscope };
|