@music-ui/osc 0.0.0-stage → 0.17.3

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 CHANGED
@@ -1,3 +1,35 @@
1
- # Temporary Holding Version
1
+ # Music UI - ABC
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Abc notation rendering 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,243 @@
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
+ });
30
+ module.exports = __toCommonJS(index_exports);
31
+
32
+ // src/OscPlayer.ts
33
+ var import_events = require("events");
34
+ var DEFAULT_MAX_VOICES = 2;
35
+ var OscPlayer = class {
36
+ context;
37
+ eventEmitter;
38
+ oscillators;
39
+ constructor({ context, voices = DEFAULT_MAX_VOICES }) {
40
+ this.context = context;
41
+ this.eventEmitter = new import_events.EventEmitter();
42
+ this.oscillators = Array.from({ length: voices }, () => {
43
+ const osc = context.createOscillator();
44
+ const gain = context.createGain();
45
+ osc.connect(gain);
46
+ gain.connect(context.destination);
47
+ return {
48
+ playing: false,
49
+ started: false,
50
+ osc,
51
+ gain,
52
+ timeout: -1
53
+ };
54
+ });
55
+ }
56
+ on(eventName, callback) {
57
+ this.eventEmitter.addListener(eventName, callback);
58
+ return () => this.eventEmitter.removeListener(eventName, callback);
59
+ }
60
+ play({ id, frequency, duration, volume }) {
61
+ if (this.oscillators.every(({ timeout }) => timeout > -1)) {
62
+ console.warn("All oscillators are already playing");
63
+ this.eventEmitter.emit("noAvailableVoices", { id });
64
+ return;
65
+ }
66
+ const { currentTime } = this.context;
67
+ const oscillator = this.oscillators.find(({ timeout }) => timeout < 0);
68
+ oscillator.osc.frequency.value = frequency;
69
+ if (!oscillator.started) {
70
+ oscillator.started = true;
71
+ oscillator.osc.start();
72
+ }
73
+ oscillator.gain.gain.setValueAtTime(volume, currentTime);
74
+ oscillator.gain.gain.setValueAtTime(0, currentTime + duration);
75
+ this.eventEmitter.emit("start", { id });
76
+ oscillator.timeout = setTimeout(() => {
77
+ oscillator.timeout = -1;
78
+ this.eventEmitter.emit("finished", { id });
79
+ }, duration * 1e3);
80
+ }
81
+ destroy() {
82
+ this.oscillators.forEach(({ osc, gain, timeout }) => {
83
+ gain.disconnect();
84
+ osc.disconnect();
85
+ osc.stop();
86
+ clearTimeout(timeout);
87
+ });
88
+ this.eventEmitter.removeAllListeners();
89
+ }
90
+ };
91
+
92
+ // src/oscillator.ts
93
+ var FREQUENCY_BOUNDS = [20, 2e4];
94
+ var cssClasses = {
95
+ oscillator: "oscillator",
96
+ value: "value",
97
+ controls: "controls"
98
+ };
99
+ var DEFAULT_OSCILLATOR_PARAMS = {
100
+ frequency: 440,
101
+ volume: -6,
102
+ duration: 1,
103
+ label: "",
104
+ playbackLabel: "Play"
105
+ };
106
+ var Oscillator = class {
107
+ player;
108
+ element;
109
+ id;
110
+ frequency = DEFAULT_OSCILLATOR_PARAMS.frequency;
111
+ volume;
112
+ duration;
113
+ label;
114
+ playbackLabel;
115
+ rendered;
116
+ playbackButton;
117
+ constructor({
118
+ player,
119
+ element,
120
+ id,
121
+ frequency = DEFAULT_OSCILLATOR_PARAMS.frequency,
122
+ volume = DEFAULT_OSCILLATOR_PARAMS.volume,
123
+ duration = DEFAULT_OSCILLATOR_PARAMS.duration,
124
+ label = DEFAULT_OSCILLATOR_PARAMS.label,
125
+ playbackLabel = DEFAULT_OSCILLATOR_PARAMS.playbackLabel
126
+ }) {
127
+ if (!element) {
128
+ throw new Error("Element not found");
129
+ }
130
+ this.player = player;
131
+ this.element = element;
132
+ this.id = id;
133
+ this.setFrequency(frequency);
134
+ this.volume = volume;
135
+ this.duration = duration;
136
+ this.label = label;
137
+ this.playbackLabel = playbackLabel;
138
+ this.rendered = false;
139
+ this.playbackButton = null;
140
+ player.on("start", ({ id: id2 }) => {
141
+ if (id2 !== this.id) {
142
+ return;
143
+ }
144
+ this.playbackButton.disabled = true;
145
+ });
146
+ player.on("noAvailableVoices", ({ id: id2 }) => {
147
+ if (id2 !== this.id) {
148
+ return;
149
+ }
150
+ this.playbackButton.disabled = false;
151
+ });
152
+ player.on("finished", ({ id: id2 }) => {
153
+ if (id2 !== this.id) {
154
+ return;
155
+ }
156
+ this.playbackButton.disabled = false;
157
+ });
158
+ }
159
+ setFrequency(frequency) {
160
+ const [lowFreq, highFreq] = FREQUENCY_BOUNDS;
161
+ if (frequency < lowFreq) {
162
+ this.frequency = lowFreq;
163
+ console.warn(`Oscillator frequency cannot be lower than ${lowFreq}`);
164
+ return this;
165
+ }
166
+ if (frequency > highFreq) {
167
+ this.frequency = highFreq;
168
+ console.warn(`Oscillator frequency cannot be higher than ${highFreq}`);
169
+ return this;
170
+ }
171
+ this.frequency = frequency;
172
+ return this;
173
+ }
174
+ getFrequency() {
175
+ return this.frequency;
176
+ }
177
+ /**
178
+ * Renders the UI inside the current element.
179
+ * @returns The current Oscillator instance.
180
+ */
181
+ render() {
182
+ if (this.rendered) {
183
+ return this;
184
+ }
185
+ this.baseRender();
186
+ return this;
187
+ }
188
+ onPlay() {
189
+ this.player.play({
190
+ id: this.id,
191
+ frequency: this.frequency,
192
+ duration: this.duration,
193
+ volume: this.volume
194
+ });
195
+ }
196
+ baseRender() {
197
+ const valueElement = document.createElement("div");
198
+ valueElement.classList.add(cssClasses.value);
199
+ valueElement.textContent = this.label ? `${this.frequency} - ${this.label}` : `${this.frequency}`;
200
+ const controlsElement = document.createElement("div");
201
+ controlsElement.classList.add(cssClasses.controls);
202
+ this.playbackButton = document.createElement("button");
203
+ this.playbackButton.textContent = this.playbackLabel;
204
+ this.playbackButton.setAttribute(
205
+ "aria-label",
206
+ `Playback oscillator with frequency: ${this.frequency}`
207
+ );
208
+ this.playbackButton.addEventListener("click", this.onPlay.bind(this));
209
+ controlsElement.appendChild(this.playbackButton);
210
+ this.element.appendChild(valueElement);
211
+ this.element.appendChild(controlsElement);
212
+ this.rendered = true;
213
+ }
214
+ };
215
+
216
+ // src/init.ts
217
+ var import_core = require("@music-ui/core");
218
+ var DEFAULT_OPTIONS = {
219
+ selection: "[data-oscillator]"
220
+ };
221
+ function initOscillator(params = {}) {
222
+ const { selection, player } = { ...DEFAULT_OPTIONS, ...params };
223
+ if (!(player instanceof OscPlayer)) {
224
+ throw new Error("You must pass an OscPlayer instance");
225
+ }
226
+ (0, import_core.ensureSelection)(selection).forEach((element, index) => {
227
+ new Oscillator({
228
+ player,
229
+ element,
230
+ id: `osc-${index + 1}`,
231
+ ...(0, import_core.extractElementOptions)(element, DEFAULT_OSCILLATOR_PARAMS)
232
+ }).render();
233
+ });
234
+ }
235
+ // Annotate the CommonJS export names for ESM import in node:
236
+ 0 && (module.exports = {
237
+ DEFAULT_OPTIONS,
238
+ DEFAULT_OSCILLATOR_PARAMS,
239
+ OscPlayer,
240
+ Oscillator,
241
+ cssClasses,
242
+ initOscillator
243
+ });
@@ -0,0 +1,102 @@
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";
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
+ type OscPlayerConstructors = {
16
+ context: AudioContext;
17
+ voices?: number;
18
+ };
19
+ type OscPlayParams = {
20
+ id: string;
21
+ frequency: number;
22
+ duration: number;
23
+ volume: number;
24
+ };
25
+ declare class OscPlayer {
26
+ private context;
27
+ private eventEmitter;
28
+ private oscillators;
29
+ constructor({ context, voices }: OscPlayerConstructors);
30
+ on(eventName: OscPlayerEvents, callback: OscPlayerCallback): () => EventEmitter;
31
+ play({ id, frequency, duration, volume }: OscPlayParams): void;
32
+ destroy(): void;
33
+ }
34
+
35
+ /**
36
+ * @property {string} content The oscillator element class
37
+ * @property {string} value The oscillator value element class
38
+ * @property {string} controls The oscillator controls element class
39
+ */
40
+ declare const cssClasses: {
41
+ readonly oscillator: "oscillator";
42
+ readonly value: "value";
43
+ readonly controls: "controls";
44
+ };
45
+ declare const DEFAULT_OSCILLATOR_PARAMS: {
46
+ readonly frequency: 440;
47
+ readonly volume: -6;
48
+ readonly duration: 1;
49
+ readonly label: "";
50
+ readonly playbackLabel: "Play";
51
+ };
52
+ type OscillatorParams = {
53
+ player: OscPlayer;
54
+ element: HTMLElement;
55
+ id: string;
56
+ frequency?: number;
57
+ volume?: number;
58
+ duration?: number;
59
+ label?: string;
60
+ playbackLabel?: string;
61
+ };
62
+ declare class Oscillator {
63
+ private player;
64
+ private element;
65
+ private id;
66
+ private frequency;
67
+ private volume;
68
+ private duration;
69
+ private label;
70
+ private playbackLabel;
71
+ private rendered;
72
+ private playbackButton;
73
+ constructor({ player, element, id, frequency, volume, duration, label, playbackLabel, }: OscillatorParams);
74
+ setFrequency(frequency: number): Oscillator;
75
+ getFrequency(): number;
76
+ /**
77
+ * Renders the UI inside the current element.
78
+ * @returns The current Oscillator instance.
79
+ */
80
+ render(): Oscillator;
81
+ private onPlay;
82
+ private baseRender;
83
+ }
84
+
85
+ /**
86
+ * The params expected by the `initOscillator` function.
87
+ * @property selection The elements to be initialized.
88
+ */
89
+ type InitOscillatorParams<T extends HTMLElement> = {
90
+ selection: ElementOrSelector<T>;
91
+ player: OscPlayer;
92
+ };
93
+ declare const DEFAULT_OPTIONS: {
94
+ readonly selection: "[data-oscillator]";
95
+ };
96
+ /**
97
+ * Initializes oscillators on the passed selection.
98
+ * @param params The initialization params.
99
+ */
100
+ declare function initOscillator<T extends HTMLElement>(params?: Partial<InitOscillatorParams<T>>): void;
101
+
102
+ export { DEFAULT_OPTIONS, DEFAULT_OSCILLATOR_PARAMS, type InitOscillatorParams, OscPlayer, type OscPlayerCallback, type OscPlayerEvents, Oscillator, type OscillatorParams, cssClasses, initOscillator };
@@ -0,0 +1,102 @@
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";
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
+ type OscPlayerConstructors = {
16
+ context: AudioContext;
17
+ voices?: number;
18
+ };
19
+ type OscPlayParams = {
20
+ id: string;
21
+ frequency: number;
22
+ duration: number;
23
+ volume: number;
24
+ };
25
+ declare class OscPlayer {
26
+ private context;
27
+ private eventEmitter;
28
+ private oscillators;
29
+ constructor({ context, voices }: OscPlayerConstructors);
30
+ on(eventName: OscPlayerEvents, callback: OscPlayerCallback): () => EventEmitter;
31
+ play({ id, frequency, duration, volume }: OscPlayParams): void;
32
+ destroy(): void;
33
+ }
34
+
35
+ /**
36
+ * @property {string} content The oscillator element class
37
+ * @property {string} value The oscillator value element class
38
+ * @property {string} controls The oscillator controls element class
39
+ */
40
+ declare const cssClasses: {
41
+ readonly oscillator: "oscillator";
42
+ readonly value: "value";
43
+ readonly controls: "controls";
44
+ };
45
+ declare const DEFAULT_OSCILLATOR_PARAMS: {
46
+ readonly frequency: 440;
47
+ readonly volume: -6;
48
+ readonly duration: 1;
49
+ readonly label: "";
50
+ readonly playbackLabel: "Play";
51
+ };
52
+ type OscillatorParams = {
53
+ player: OscPlayer;
54
+ element: HTMLElement;
55
+ id: string;
56
+ frequency?: number;
57
+ volume?: number;
58
+ duration?: number;
59
+ label?: string;
60
+ playbackLabel?: string;
61
+ };
62
+ declare class Oscillator {
63
+ private player;
64
+ private element;
65
+ private id;
66
+ private frequency;
67
+ private volume;
68
+ private duration;
69
+ private label;
70
+ private playbackLabel;
71
+ private rendered;
72
+ private playbackButton;
73
+ constructor({ player, element, id, frequency, volume, duration, label, playbackLabel, }: OscillatorParams);
74
+ setFrequency(frequency: number): Oscillator;
75
+ getFrequency(): number;
76
+ /**
77
+ * Renders the UI inside the current element.
78
+ * @returns The current Oscillator instance.
79
+ */
80
+ render(): Oscillator;
81
+ private onPlay;
82
+ private baseRender;
83
+ }
84
+
85
+ /**
86
+ * The params expected by the `initOscillator` function.
87
+ * @property selection The elements to be initialized.
88
+ */
89
+ type InitOscillatorParams<T extends HTMLElement> = {
90
+ selection: ElementOrSelector<T>;
91
+ player: OscPlayer;
92
+ };
93
+ declare const DEFAULT_OPTIONS: {
94
+ readonly selection: "[data-oscillator]";
95
+ };
96
+ /**
97
+ * Initializes oscillators on the passed selection.
98
+ * @param params The initialization params.
99
+ */
100
+ declare function initOscillator<T extends HTMLElement>(params?: Partial<InitOscillatorParams<T>>): void;
101
+
102
+ export { DEFAULT_OPTIONS, DEFAULT_OSCILLATOR_PARAMS, type InitOscillatorParams, OscPlayer, type OscPlayerCallback, type OscPlayerEvents, Oscillator, type OscillatorParams, cssClasses, initOscillator };
package/dist/index.js ADDED
@@ -0,0 +1,214 @@
1
+ // src/OscPlayer.ts
2
+ import { EventEmitter } from "events";
3
+ var DEFAULT_MAX_VOICES = 2;
4
+ var OscPlayer = class {
5
+ context;
6
+ eventEmitter;
7
+ oscillators;
8
+ constructor({ context, voices = DEFAULT_MAX_VOICES }) {
9
+ this.context = context;
10
+ this.eventEmitter = new EventEmitter();
11
+ this.oscillators = Array.from({ length: voices }, () => {
12
+ const osc = context.createOscillator();
13
+ const gain = context.createGain();
14
+ osc.connect(gain);
15
+ gain.connect(context.destination);
16
+ return {
17
+ playing: false,
18
+ started: false,
19
+ osc,
20
+ gain,
21
+ timeout: -1
22
+ };
23
+ });
24
+ }
25
+ on(eventName, callback) {
26
+ this.eventEmitter.addListener(eventName, callback);
27
+ return () => this.eventEmitter.removeListener(eventName, callback);
28
+ }
29
+ play({ id, frequency, duration, volume }) {
30
+ if (this.oscillators.every(({ timeout }) => timeout > -1)) {
31
+ console.warn("All oscillators are already playing");
32
+ this.eventEmitter.emit("noAvailableVoices", { id });
33
+ return;
34
+ }
35
+ const { currentTime } = this.context;
36
+ const oscillator = this.oscillators.find(({ timeout }) => timeout < 0);
37
+ oscillator.osc.frequency.value = frequency;
38
+ if (!oscillator.started) {
39
+ oscillator.started = true;
40
+ oscillator.osc.start();
41
+ }
42
+ oscillator.gain.gain.setValueAtTime(volume, currentTime);
43
+ oscillator.gain.gain.setValueAtTime(0, currentTime + duration);
44
+ this.eventEmitter.emit("start", { id });
45
+ oscillator.timeout = setTimeout(() => {
46
+ oscillator.timeout = -1;
47
+ this.eventEmitter.emit("finished", { id });
48
+ }, duration * 1e3);
49
+ }
50
+ destroy() {
51
+ this.oscillators.forEach(({ osc, gain, timeout }) => {
52
+ gain.disconnect();
53
+ osc.disconnect();
54
+ osc.stop();
55
+ clearTimeout(timeout);
56
+ });
57
+ this.eventEmitter.removeAllListeners();
58
+ }
59
+ };
60
+
61
+ // src/oscillator.ts
62
+ var FREQUENCY_BOUNDS = [20, 2e4];
63
+ var cssClasses = {
64
+ oscillator: "oscillator",
65
+ value: "value",
66
+ controls: "controls"
67
+ };
68
+ var DEFAULT_OSCILLATOR_PARAMS = {
69
+ frequency: 440,
70
+ volume: -6,
71
+ duration: 1,
72
+ label: "",
73
+ playbackLabel: "Play"
74
+ };
75
+ var Oscillator = class {
76
+ player;
77
+ element;
78
+ id;
79
+ frequency = DEFAULT_OSCILLATOR_PARAMS.frequency;
80
+ volume;
81
+ duration;
82
+ label;
83
+ playbackLabel;
84
+ rendered;
85
+ playbackButton;
86
+ constructor({
87
+ player,
88
+ element,
89
+ id,
90
+ frequency = DEFAULT_OSCILLATOR_PARAMS.frequency,
91
+ volume = DEFAULT_OSCILLATOR_PARAMS.volume,
92
+ duration = DEFAULT_OSCILLATOR_PARAMS.duration,
93
+ label = DEFAULT_OSCILLATOR_PARAMS.label,
94
+ playbackLabel = DEFAULT_OSCILLATOR_PARAMS.playbackLabel
95
+ }) {
96
+ if (!element) {
97
+ throw new Error("Element not found");
98
+ }
99
+ this.player = player;
100
+ this.element = element;
101
+ this.id = id;
102
+ this.setFrequency(frequency);
103
+ this.volume = volume;
104
+ this.duration = duration;
105
+ this.label = label;
106
+ this.playbackLabel = playbackLabel;
107
+ this.rendered = false;
108
+ this.playbackButton = null;
109
+ player.on("start", ({ id: id2 }) => {
110
+ if (id2 !== this.id) {
111
+ return;
112
+ }
113
+ this.playbackButton.disabled = true;
114
+ });
115
+ player.on("noAvailableVoices", ({ id: id2 }) => {
116
+ if (id2 !== this.id) {
117
+ return;
118
+ }
119
+ this.playbackButton.disabled = false;
120
+ });
121
+ player.on("finished", ({ id: id2 }) => {
122
+ if (id2 !== this.id) {
123
+ return;
124
+ }
125
+ this.playbackButton.disabled = false;
126
+ });
127
+ }
128
+ setFrequency(frequency) {
129
+ const [lowFreq, highFreq] = FREQUENCY_BOUNDS;
130
+ if (frequency < lowFreq) {
131
+ this.frequency = lowFreq;
132
+ console.warn(`Oscillator frequency cannot be lower than ${lowFreq}`);
133
+ return this;
134
+ }
135
+ if (frequency > highFreq) {
136
+ this.frequency = highFreq;
137
+ console.warn(`Oscillator frequency cannot be higher than ${highFreq}`);
138
+ return this;
139
+ }
140
+ this.frequency = frequency;
141
+ return this;
142
+ }
143
+ getFrequency() {
144
+ return this.frequency;
145
+ }
146
+ /**
147
+ * Renders the UI inside the current element.
148
+ * @returns The current Oscillator instance.
149
+ */
150
+ render() {
151
+ if (this.rendered) {
152
+ return this;
153
+ }
154
+ this.baseRender();
155
+ return this;
156
+ }
157
+ onPlay() {
158
+ this.player.play({
159
+ id: this.id,
160
+ frequency: this.frequency,
161
+ duration: this.duration,
162
+ volume: this.volume
163
+ });
164
+ }
165
+ baseRender() {
166
+ const valueElement = document.createElement("div");
167
+ valueElement.classList.add(cssClasses.value);
168
+ valueElement.textContent = this.label ? `${this.frequency} - ${this.label}` : `${this.frequency}`;
169
+ const controlsElement = document.createElement("div");
170
+ controlsElement.classList.add(cssClasses.controls);
171
+ this.playbackButton = document.createElement("button");
172
+ this.playbackButton.textContent = this.playbackLabel;
173
+ this.playbackButton.setAttribute(
174
+ "aria-label",
175
+ `Playback oscillator with frequency: ${this.frequency}`
176
+ );
177
+ this.playbackButton.addEventListener("click", this.onPlay.bind(this));
178
+ controlsElement.appendChild(this.playbackButton);
179
+ this.element.appendChild(valueElement);
180
+ this.element.appendChild(controlsElement);
181
+ this.rendered = true;
182
+ }
183
+ };
184
+
185
+ // src/init.ts
186
+ import {
187
+ ensureSelection,
188
+ extractElementOptions
189
+ } from "@music-ui/core";
190
+ var DEFAULT_OPTIONS = {
191
+ selection: "[data-oscillator]"
192
+ };
193
+ function initOscillator(params = {}) {
194
+ const { selection, player } = { ...DEFAULT_OPTIONS, ...params };
195
+ if (!(player instanceof OscPlayer)) {
196
+ throw new Error("You must pass an OscPlayer instance");
197
+ }
198
+ ensureSelection(selection).forEach((element, index) => {
199
+ new Oscillator({
200
+ player,
201
+ element,
202
+ id: `osc-${index + 1}`,
203
+ ...extractElementOptions(element, DEFAULT_OSCILLATOR_PARAMS)
204
+ }).render();
205
+ });
206
+ }
207
+ export {
208
+ DEFAULT_OPTIONS,
209
+ DEFAULT_OSCILLATOR_PARAMS,
210
+ OscPlayer,
211
+ Oscillator,
212
+ cssClasses,
213
+ initOscillator
214
+ };
@@ -0,0 +1,7 @@
1
+ /* src/styles/index.css */
2
+ .oscillator {
3
+ display: flex;
4
+ flex-direction: column;
5
+ align-items: center;
6
+ gap: 0.5rem;
7
+ }
@@ -0,0 +1,2 @@
1
+
2
+ export { }
@@ -0,0 +1,2 @@
1
+
2
+ export { }
package/package.json CHANGED
@@ -1,6 +1,54 @@
1
1
  {
2
2
  "name": "@music-ui/osc",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "description": "Oscillator utilities",
4
+ "homepage": "https://github.com/moonwave99/music-ui",
5
+ "keywords": [
6
+ "music",
7
+ "oscillator"
8
+ ],
9
+ "version": "0.17.3",
10
+ "type": "module",
11
+ "main": "./dist/index.cjs",
12
+ "module": "./dist/index.js",
13
+ "types": "./dist/index.d.ts",
14
+ "exports": {
15
+ ".": {
16
+ "types": {
17
+ "import": "./dist/index.d.ts",
18
+ "require": "./dist/index.d.cts"
19
+ },
20
+ "import": "./dist/index.js",
21
+ "require": "./dist/index.cjs"
22
+ },
23
+ "./styles.css": "./dist/styles/index.css"
24
+ },
25
+ "files": [
26
+ "dist",
27
+ "README.md"
28
+ ],
29
+ "devDependencies": {
30
+ "@testing-library/dom": "^10.4.1",
31
+ "@testing-library/user-event": "^14.6.1",
32
+ "@types/events": "^3.0.3",
33
+ "@vitest/coverage-istanbul": "4.1.10",
34
+ "eslint": "^9.39.1",
35
+ "jsdom": "^30.0.1",
36
+ "tsup": "^8.5.1",
37
+ "typescript": "5.9.2",
38
+ "vitest": "^4.1.10",
39
+ "@music-ui/typescript-config": "0.0.0",
40
+ "@music-ui/eslint-config": "0.0.0"
41
+ },
42
+ "dependencies": {
43
+ "events": "^3.3.0",
44
+ "@music-ui/core": "0.17.1"
45
+ },
46
+ "scripts": {
47
+ "test": "vitest run",
48
+ "test:watch": "vitest --watch",
49
+ "lint": "eslint . --max-warnings 0",
50
+ "dev": "tsup src/index.ts src/styles/index.css --format cjs,esm --dts --watch",
51
+ "build": "tsup src/index.ts src/styles/index.css --format cjs,esm --dts",
52
+ "check-types": "tsc --noEmit"
53
+ }
6
54
  }