qti-clockwork-audio 0.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/LICENSE ADDED
@@ -0,0 +1,173 @@
1
+ Clockwork Engine
2
+ Copyright (C) 2026 Quiet Terminal Interactive LTD
3
+
4
+ This project is licensed under the GNU Lesser General Public License v3.0.
5
+ See the license text below.
6
+
7
+ -----------------------------------------------------------------------
8
+
9
+ GNU LESSER GENERAL PUBLIC LICENSE
10
+ Version 3, 29 June 2007
11
+
12
+ Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
13
+ Everyone is permitted to copy and distribute verbatim copies
14
+ of this license document, but changing it is not allowed.
15
+
16
+
17
+ This version of the GNU Lesser General Public License incorporates
18
+ the terms and conditions of version 3 of the GNU General Public
19
+ License, supplemented by the additional permissions listed below.
20
+
21
+ 0. Additional Definitions.
22
+
23
+ As used herein, "this License" refers to version 3 of the GNU Lesser
24
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
25
+ General Public License.
26
+
27
+ "The Library" refers to a covered work governed by this License,
28
+ other than an Application or a Combined Work as defined below.
29
+
30
+ An "Application" is any work that makes use of an interface provided
31
+ by the Library, but which is not otherwise based on the Library.
32
+ Defining a subclass of a class defined by the Library is deemed a mode
33
+ of using an interface provided by the Library.
34
+
35
+ A "Combined Work" is a work produced by combining or linking an
36
+ Application with the Library. The particular version of the Library
37
+ with which the Combined Work was made is also called the "Linked
38
+ Version".
39
+
40
+ The "Minimal Corresponding Source" for a Combined Work means the
41
+ Corresponding Source for the Combined Work, excluding any source code
42
+ for portions of the Combined Work that, considered in isolation, are
43
+ based on the Application, and not on the Linked Version.
44
+
45
+ The "Corresponding Application Code" for a Combined Work means the
46
+ object code and/or source code for the Application, including any data
47
+ and utility programs needed for reproducing the Combined Work from the
48
+ Application, but excluding the System Libraries of the Combined Work.
49
+
50
+ 1. Exception to Section 3 of the GNU GPL.
51
+
52
+ You may convey a covered work under sections 3 and 4 of this License
53
+ without being bound by section 3 of the GNU GPL.
54
+
55
+ 2. Conveying Modified Versions.
56
+
57
+ If you modify a copy of the Library, and, in your modifications, a
58
+ facility refers to a function or data to be supplied by an Application
59
+ that uses the facility (other than as an argument passed when the
60
+ facility is invoked), then you may convey a copy of the modified
61
+ version:
62
+
63
+ a) under this License, provided that you make a good faith effort to
64
+ ensure that, in the event an Application does not supply the
65
+ function or data, the facility still operates, and performs
66
+ whatever part of its purpose remains meaningful, or
67
+
68
+ b) under the GNU GPL, with none of the additional permissions of
69
+ this License applicable to that copy.
70
+
71
+ 3. Object Code Incorporating Material from Library Header Files.
72
+
73
+ The object code form of an Application may incorporate material from
74
+ a header file that is part of the Library. You may convey such object
75
+ code under terms of your choice, provided that, if the incorporated
76
+ material is not limited to numerical parameters, data structure
77
+ layouts and accessors, or small macros, inline functions and templates
78
+ (ten or fewer lines in length), you do both of the following:
79
+
80
+ a) Give prominent notice with each copy of the object code that the
81
+ Library is used in it and that the Library and its use are
82
+ covered by this License.
83
+
84
+ b) Accompany the object code with a copy of the GNU GPL and this license
85
+ document.
86
+
87
+ 4. Combined Works.
88
+
89
+ You may convey a Combined Work under terms of your choice that,
90
+ taken together, effectively do not restrict modification of the
91
+ portions of the Library contained in the Combined Work and reverse
92
+ engineering for debugging such modifications, if you also do each of
93
+ the following:
94
+
95
+ a) Give prominent notice with each copy of the Combined Work that
96
+ the Library is used in it and that the Library and its use are
97
+ covered by this License.
98
+
99
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
100
+ document.
101
+
102
+ c) For a Combined Work that displays copyright notices during
103
+ execution, include the copyright notice for the Library among
104
+ these notices, as well as a reference directing the user to the
105
+ copies of the GNU GPL and this license document.
106
+
107
+ d) Do one of the following:
108
+
109
+ 0) Convey the Minimal Corresponding Source under the terms of this
110
+ License, and the Corresponding Application Code in a form
111
+ suitable for, and under terms that permit, the user to
112
+ recombine or relink the Application with a modified version of
113
+ the Linked Version to produce a modified Combined Work, in the
114
+ manner specified by section 6 of the GNU GPL for conveying
115
+ Corresponding Source.
116
+
117
+ 1) Use a suitable shared library mechanism for linking with the
118
+ Library. A suitable mechanism is one that (a) uses at run time
119
+ a copy of the Library already present on the user's computer
120
+ system, and (b) will operate properly with a modified version
121
+ of the Library that is interface-compatible with the Linked
122
+ Version.
123
+
124
+ e) Provide Installation Information, but only if you would otherwise
125
+ be required to provide such information under section 6 of the
126
+ GNU GPL, and only to the extent that such information is
127
+ necessary to install and execute a modified version of the
128
+ Combined Work produced by recombining or relinking the
129
+ Application with a modified version of the Linked Version. (If
130
+ you use option 4d0, the Installation Information must accompany
131
+ the Minimal Corresponding Source and Corresponding Application
132
+ Code. If you use option 4d1, you must provide the Installation
133
+ Information in the manner specified by section 6 of the GNU GPL
134
+ for conveying Corresponding Source.)
135
+
136
+ 5. Combined Libraries.
137
+
138
+ You may place library facilities that are a work based on the
139
+ Library side by side in a single library together with other library
140
+ facilities that are not Applications and are not covered by this
141
+ License, and convey such a combined library under terms of your
142
+ choice, if you do both of the following:
143
+
144
+ a) Accompany the combined library with a copy of the same work based
145
+ on the Library, uncombined with any other library facilities,
146
+ conveyed under the terms of this License.
147
+
148
+ b) Give prominent notice with the combined library that part of it
149
+ is a work based on the Library, and explaining where to find the
150
+ accompanying uncombined form of the same work.
151
+
152
+ 6. Revised Versions of the GNU Lesser General Public License.
153
+
154
+ The Free Software Foundation may publish revised and/or new versions
155
+ of the GNU Lesser General Public License from time to time. Such new
156
+ versions will be similar in spirit to the present version, but may
157
+ differ in detail to address new problems or concerns.
158
+
159
+ Each version is given a distinguishing version number. If the
160
+ Library as you received it specifies that a certain numbered version
161
+ of the GNU Lesser General Public License "or any later version"
162
+ applies to it, you have the option of following the terms and
163
+ conditions either of that published version or of any later version
164
+ published by the Free Software Foundation. If the Library as you
165
+ received it does not specify a version number of the GNU Lesser
166
+ General Public License, you may choose any version of the GNU Lesser
167
+ General Public License ever published by the Free Software Foundation.
168
+
169
+ If the Library as you received it specifies that a proxy can decide
170
+ whether future versions of the GNU Lesser General Public License shall
171
+ apply, that proxy's public statement of acceptance of any version is
172
+ permanent authorization for you to choose that version for the
173
+ Library.
package/dist/index.cjs ADDED
@@ -0,0 +1,239 @@
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
+ AudioBus: () => AudioBus,
24
+ AudioClip: () => AudioClip,
25
+ AudioEngine: () => AudioEngine,
26
+ AudioInstance: () => AudioInstance,
27
+ HeadlessAudioContext: () => HeadlessAudioContext,
28
+ packageId: () => packageId
29
+ });
30
+ module.exports = __toCommonJS(index_exports);
31
+ var packageId = "qti-clockwork-audio";
32
+ var AudioClip = class {
33
+ constructor(buffer) {
34
+ this.buffer = buffer;
35
+ }
36
+ get duration() {
37
+ return this.buffer.duration;
38
+ }
39
+ };
40
+ var AudioBus = class {
41
+ constructor(name, gain) {
42
+ this.name = name;
43
+ this.gain = gain;
44
+ }
45
+ volume = 1;
46
+ muted = false;
47
+ setVolume(volume) {
48
+ this.volume = Math.max(0, volume);
49
+ this.refreshGain();
50
+ }
51
+ setMuted(muted) {
52
+ this.muted = muted;
53
+ this.refreshGain();
54
+ }
55
+ refreshGain() {
56
+ this.gain.gain.value = this.muted ? 0 : this.volume;
57
+ }
58
+ };
59
+ var AudioInstance = class {
60
+ constructor(id, clip, bus, source, gain) {
61
+ this.id = id;
62
+ this.clip = clip;
63
+ this.bus = bus;
64
+ this.source = source;
65
+ this.gain = gain;
66
+ }
67
+ stopped = false;
68
+ stop() {
69
+ if (this.stopped) {
70
+ return;
71
+ }
72
+ this.stopped = true;
73
+ try {
74
+ this.source.stop();
75
+ } catch (error) {
76
+ console.warn("AudioInstance stop() ignored backend error", {
77
+ error: error instanceof Error ? error.message : String(error),
78
+ id: this.id
79
+ });
80
+ }
81
+ this.source.disconnect();
82
+ this.gain.disconnect();
83
+ }
84
+ };
85
+ var AudioEngine = class {
86
+ constructor(context) {
87
+ this.context = context;
88
+ this.master = this.createBus("master");
89
+ this.createBus("music");
90
+ this.createBus("sfx");
91
+ this.createBus("ui");
92
+ }
93
+ buses = /* @__PURE__ */ new Map();
94
+ master;
95
+ active = /* @__PURE__ */ new Map();
96
+ nextId = 1;
97
+ disposed = false;
98
+ async decodeClip(data) {
99
+ this.assertNotDisposed();
100
+ const buffer = await this.context.decodeAudioData(data);
101
+ return new AudioClip(buffer);
102
+ }
103
+ play(clip, bus = "master", options) {
104
+ this.assertNotDisposed();
105
+ if (!Number.isFinite(clip.duration) || clip.duration < 0) {
106
+ throw new Error("Audio clip has invalid duration");
107
+ }
108
+ const volume = options?.volume ?? 1;
109
+ if (!Number.isFinite(volume) || volume < 0) {
110
+ throw new Error('Audio option "volume" must be a finite number >= 0');
111
+ }
112
+ const pitch = options?.pitch ?? 1;
113
+ if (!Number.isFinite(pitch) || pitch <= 0) {
114
+ throw new Error('Audio option "pitch" must be a finite number > 0');
115
+ }
116
+ const targetBus = this.getOrCreateBus(bus);
117
+ const source = this.context.createBufferSource();
118
+ const gain = this.context.createGain();
119
+ source.buffer = clip.buffer;
120
+ source.loop = options?.loop ?? false;
121
+ source.playbackRate.value = pitch;
122
+ gain.gain.value = volume;
123
+ source.connect(gain);
124
+ gain.connect(targetBus.gain);
125
+ const instance = new AudioInstance(
126
+ this.nextId++,
127
+ clip,
128
+ targetBus,
129
+ source,
130
+ gain
131
+ );
132
+ this.active.set(instance.id, instance);
133
+ source.onended = () => {
134
+ this.active.delete(instance.id);
135
+ source.disconnect();
136
+ gain.disconnect();
137
+ };
138
+ source.start(this.context.currentTime);
139
+ return instance;
140
+ }
141
+ stop(instance) {
142
+ instance.stop();
143
+ this.active.delete(instance.id);
144
+ }
145
+ stopAll() {
146
+ for (const instance of this.active.values()) {
147
+ instance.stop();
148
+ }
149
+ this.active.clear();
150
+ }
151
+ setBusVolume(bus, volume) {
152
+ this.assertNotDisposed();
153
+ this.getOrCreateBus(bus).setVolume(volume);
154
+ }
155
+ setBusMuted(bus, muted) {
156
+ this.assertNotDisposed();
157
+ this.getOrCreateBus(bus).setMuted(muted);
158
+ }
159
+ setMasterVolume(volume) {
160
+ this.assertNotDisposed();
161
+ this.master.setVolume(volume);
162
+ }
163
+ getActiveInstances() {
164
+ return [...this.active.values()];
165
+ }
166
+ dispose() {
167
+ if (this.disposed) {
168
+ return;
169
+ }
170
+ this.stopAll();
171
+ for (const bus of this.buses.values()) {
172
+ bus.gain.disconnect();
173
+ }
174
+ this.disposed = true;
175
+ }
176
+ createBus(name) {
177
+ const node = this.context.createGain();
178
+ if (name !== "master") {
179
+ node.connect(this.master.gain);
180
+ }
181
+ const bus = new AudioBus(name, node);
182
+ this.buses.set(name, bus);
183
+ return bus;
184
+ }
185
+ getOrCreateBus(name) {
186
+ const existing = this.buses.get(name);
187
+ if (existing) {
188
+ return existing;
189
+ }
190
+ return this.createBus(name);
191
+ }
192
+ assertNotDisposed() {
193
+ if (this.disposed) {
194
+ throw new Error("AudioEngine has been disposed");
195
+ }
196
+ }
197
+ };
198
+ var HeadlessAudioContext = class {
199
+ currentTime = 0;
200
+ createGain() {
201
+ return {
202
+ gain: { value: 1 },
203
+ connect() {
204
+ },
205
+ disconnect() {
206
+ }
207
+ };
208
+ }
209
+ createBufferSource() {
210
+ return {
211
+ buffer: null,
212
+ loop: false,
213
+ playbackRate: { value: 1 },
214
+ connect() {
215
+ },
216
+ start: () => {
217
+ this.currentTime += 0.016;
218
+ },
219
+ stop() {
220
+ },
221
+ disconnect() {
222
+ },
223
+ onended: null
224
+ };
225
+ }
226
+ async decodeAudioData(data) {
227
+ const bytes = data.byteLength;
228
+ return { duration: bytes / 48e3 };
229
+ }
230
+ };
231
+ // Annotate the CommonJS export names for ESM import in node:
232
+ 0 && (module.exports = {
233
+ AudioBus,
234
+ AudioClip,
235
+ AudioEngine,
236
+ AudioInstance,
237
+ HeadlessAudioContext,
238
+ packageId
239
+ });
@@ -0,0 +1,100 @@
1
+ declare const packageId = "qti-clockwork-audio";
2
+ interface AudioBufferLike {
3
+ duration: number;
4
+ }
5
+ interface AudioContextLike {
6
+ readonly currentTime: number;
7
+ createGain(): GainNodeLike;
8
+ createBufferSource(): BufferSourceNodeLike;
9
+ decodeAudioData(data: ArrayBuffer): Promise<AudioBufferLike>;
10
+ }
11
+ interface GainNodeLike {
12
+ gain: {
13
+ value: number;
14
+ };
15
+ connect(target: unknown): void;
16
+ disconnect(): void;
17
+ }
18
+ interface BufferSourceNodeLike {
19
+ buffer: AudioBufferLike | null;
20
+ loop: boolean;
21
+ playbackRate: {
22
+ value: number;
23
+ };
24
+ connect(target: unknown): void;
25
+ start(when?: number): void;
26
+ stop(when?: number): void;
27
+ disconnect(): void;
28
+ onended: (() => void) | null;
29
+ }
30
+ interface AudioOptions {
31
+ volume?: number;
32
+ pitch?: number;
33
+ loop?: boolean;
34
+ }
35
+ /** Decoded audio buffer wrapper exposing duration metadata. */
36
+ declare class AudioClip {
37
+ readonly buffer: AudioBufferLike;
38
+ constructor(buffer: AudioBufferLike);
39
+ get duration(): number;
40
+ }
41
+ /** Named audio routing channel with independent volume and mute controls. */
42
+ declare class AudioBus {
43
+ readonly name: string;
44
+ readonly gain: GainNodeLike;
45
+ volume: number;
46
+ muted: boolean;
47
+ constructor(name: string, gain: GainNodeLike);
48
+ setVolume(volume: number): void;
49
+ setMuted(muted: boolean): void;
50
+ private refreshGain;
51
+ }
52
+ declare class AudioInstance {
53
+ readonly id: number;
54
+ readonly clip: AudioClip;
55
+ readonly bus: AudioBus;
56
+ readonly source: BufferSourceNodeLike;
57
+ readonly gain: GainNodeLike;
58
+ private stopped;
59
+ constructor(id: number, clip: AudioClip, bus: AudioBus, source: BufferSourceNodeLike, gain: GainNodeLike);
60
+ stop(): void;
61
+ }
62
+ interface AudioSource {
63
+ clip: AudioClip;
64
+ volume: number;
65
+ pitch: number;
66
+ loop: boolean;
67
+ playing: boolean;
68
+ bus: string;
69
+ instance?: AudioInstance;
70
+ }
71
+ /** WebAudio abstraction managing playback, bus routing, and lifecycle. */
72
+ declare class AudioEngine {
73
+ readonly context: AudioContextLike;
74
+ readonly buses: Map<string, AudioBus>;
75
+ private readonly master;
76
+ private readonly active;
77
+ private nextId;
78
+ private disposed;
79
+ constructor(context: AudioContextLike);
80
+ decodeClip(data: ArrayBuffer): Promise<AudioClip>;
81
+ play(clip: AudioClip, bus?: string, options?: AudioOptions): AudioInstance;
82
+ stop(instance: AudioInstance): void;
83
+ stopAll(): void;
84
+ setBusVolume(bus: string, volume: number): void;
85
+ setBusMuted(bus: string, muted: boolean): void;
86
+ setMasterVolume(volume: number): void;
87
+ getActiveInstances(): readonly AudioInstance[];
88
+ dispose(): void;
89
+ private createBus;
90
+ private getOrCreateBus;
91
+ private assertNotDisposed;
92
+ }
93
+ declare class HeadlessAudioContext implements AudioContextLike {
94
+ currentTime: number;
95
+ createGain(): GainNodeLike;
96
+ createBufferSource(): BufferSourceNodeLike;
97
+ decodeAudioData(data: ArrayBuffer): Promise<AudioBufferLike>;
98
+ }
99
+
100
+ export { type AudioBufferLike, AudioBus, AudioClip, type AudioContextLike, AudioEngine, AudioInstance, type AudioOptions, type AudioSource, type BufferSourceNodeLike, type GainNodeLike, HeadlessAudioContext, packageId };
@@ -0,0 +1,100 @@
1
+ declare const packageId = "qti-clockwork-audio";
2
+ interface AudioBufferLike {
3
+ duration: number;
4
+ }
5
+ interface AudioContextLike {
6
+ readonly currentTime: number;
7
+ createGain(): GainNodeLike;
8
+ createBufferSource(): BufferSourceNodeLike;
9
+ decodeAudioData(data: ArrayBuffer): Promise<AudioBufferLike>;
10
+ }
11
+ interface GainNodeLike {
12
+ gain: {
13
+ value: number;
14
+ };
15
+ connect(target: unknown): void;
16
+ disconnect(): void;
17
+ }
18
+ interface BufferSourceNodeLike {
19
+ buffer: AudioBufferLike | null;
20
+ loop: boolean;
21
+ playbackRate: {
22
+ value: number;
23
+ };
24
+ connect(target: unknown): void;
25
+ start(when?: number): void;
26
+ stop(when?: number): void;
27
+ disconnect(): void;
28
+ onended: (() => void) | null;
29
+ }
30
+ interface AudioOptions {
31
+ volume?: number;
32
+ pitch?: number;
33
+ loop?: boolean;
34
+ }
35
+ /** Decoded audio buffer wrapper exposing duration metadata. */
36
+ declare class AudioClip {
37
+ readonly buffer: AudioBufferLike;
38
+ constructor(buffer: AudioBufferLike);
39
+ get duration(): number;
40
+ }
41
+ /** Named audio routing channel with independent volume and mute controls. */
42
+ declare class AudioBus {
43
+ readonly name: string;
44
+ readonly gain: GainNodeLike;
45
+ volume: number;
46
+ muted: boolean;
47
+ constructor(name: string, gain: GainNodeLike);
48
+ setVolume(volume: number): void;
49
+ setMuted(muted: boolean): void;
50
+ private refreshGain;
51
+ }
52
+ declare class AudioInstance {
53
+ readonly id: number;
54
+ readonly clip: AudioClip;
55
+ readonly bus: AudioBus;
56
+ readonly source: BufferSourceNodeLike;
57
+ readonly gain: GainNodeLike;
58
+ private stopped;
59
+ constructor(id: number, clip: AudioClip, bus: AudioBus, source: BufferSourceNodeLike, gain: GainNodeLike);
60
+ stop(): void;
61
+ }
62
+ interface AudioSource {
63
+ clip: AudioClip;
64
+ volume: number;
65
+ pitch: number;
66
+ loop: boolean;
67
+ playing: boolean;
68
+ bus: string;
69
+ instance?: AudioInstance;
70
+ }
71
+ /** WebAudio abstraction managing playback, bus routing, and lifecycle. */
72
+ declare class AudioEngine {
73
+ readonly context: AudioContextLike;
74
+ readonly buses: Map<string, AudioBus>;
75
+ private readonly master;
76
+ private readonly active;
77
+ private nextId;
78
+ private disposed;
79
+ constructor(context: AudioContextLike);
80
+ decodeClip(data: ArrayBuffer): Promise<AudioClip>;
81
+ play(clip: AudioClip, bus?: string, options?: AudioOptions): AudioInstance;
82
+ stop(instance: AudioInstance): void;
83
+ stopAll(): void;
84
+ setBusVolume(bus: string, volume: number): void;
85
+ setBusMuted(bus: string, muted: boolean): void;
86
+ setMasterVolume(volume: number): void;
87
+ getActiveInstances(): readonly AudioInstance[];
88
+ dispose(): void;
89
+ private createBus;
90
+ private getOrCreateBus;
91
+ private assertNotDisposed;
92
+ }
93
+ declare class HeadlessAudioContext implements AudioContextLike {
94
+ currentTime: number;
95
+ createGain(): GainNodeLike;
96
+ createBufferSource(): BufferSourceNodeLike;
97
+ decodeAudioData(data: ArrayBuffer): Promise<AudioBufferLike>;
98
+ }
99
+
100
+ export { type AudioBufferLike, AudioBus, AudioClip, type AudioContextLike, AudioEngine, AudioInstance, type AudioOptions, type AudioSource, type BufferSourceNodeLike, type GainNodeLike, HeadlessAudioContext, packageId };
package/dist/index.js ADDED
@@ -0,0 +1,209 @@
1
+ // src/index.ts
2
+ var packageId = "qti-clockwork-audio";
3
+ var AudioClip = class {
4
+ constructor(buffer) {
5
+ this.buffer = buffer;
6
+ }
7
+ get duration() {
8
+ return this.buffer.duration;
9
+ }
10
+ };
11
+ var AudioBus = class {
12
+ constructor(name, gain) {
13
+ this.name = name;
14
+ this.gain = gain;
15
+ }
16
+ volume = 1;
17
+ muted = false;
18
+ setVolume(volume) {
19
+ this.volume = Math.max(0, volume);
20
+ this.refreshGain();
21
+ }
22
+ setMuted(muted) {
23
+ this.muted = muted;
24
+ this.refreshGain();
25
+ }
26
+ refreshGain() {
27
+ this.gain.gain.value = this.muted ? 0 : this.volume;
28
+ }
29
+ };
30
+ var AudioInstance = class {
31
+ constructor(id, clip, bus, source, gain) {
32
+ this.id = id;
33
+ this.clip = clip;
34
+ this.bus = bus;
35
+ this.source = source;
36
+ this.gain = gain;
37
+ }
38
+ stopped = false;
39
+ stop() {
40
+ if (this.stopped) {
41
+ return;
42
+ }
43
+ this.stopped = true;
44
+ try {
45
+ this.source.stop();
46
+ } catch (error) {
47
+ console.warn("AudioInstance stop() ignored backend error", {
48
+ error: error instanceof Error ? error.message : String(error),
49
+ id: this.id
50
+ });
51
+ }
52
+ this.source.disconnect();
53
+ this.gain.disconnect();
54
+ }
55
+ };
56
+ var AudioEngine = class {
57
+ constructor(context) {
58
+ this.context = context;
59
+ this.master = this.createBus("master");
60
+ this.createBus("music");
61
+ this.createBus("sfx");
62
+ this.createBus("ui");
63
+ }
64
+ buses = /* @__PURE__ */ new Map();
65
+ master;
66
+ active = /* @__PURE__ */ new Map();
67
+ nextId = 1;
68
+ disposed = false;
69
+ async decodeClip(data) {
70
+ this.assertNotDisposed();
71
+ const buffer = await this.context.decodeAudioData(data);
72
+ return new AudioClip(buffer);
73
+ }
74
+ play(clip, bus = "master", options) {
75
+ this.assertNotDisposed();
76
+ if (!Number.isFinite(clip.duration) || clip.duration < 0) {
77
+ throw new Error("Audio clip has invalid duration");
78
+ }
79
+ const volume = options?.volume ?? 1;
80
+ if (!Number.isFinite(volume) || volume < 0) {
81
+ throw new Error('Audio option "volume" must be a finite number >= 0');
82
+ }
83
+ const pitch = options?.pitch ?? 1;
84
+ if (!Number.isFinite(pitch) || pitch <= 0) {
85
+ throw new Error('Audio option "pitch" must be a finite number > 0');
86
+ }
87
+ const targetBus = this.getOrCreateBus(bus);
88
+ const source = this.context.createBufferSource();
89
+ const gain = this.context.createGain();
90
+ source.buffer = clip.buffer;
91
+ source.loop = options?.loop ?? false;
92
+ source.playbackRate.value = pitch;
93
+ gain.gain.value = volume;
94
+ source.connect(gain);
95
+ gain.connect(targetBus.gain);
96
+ const instance = new AudioInstance(
97
+ this.nextId++,
98
+ clip,
99
+ targetBus,
100
+ source,
101
+ gain
102
+ );
103
+ this.active.set(instance.id, instance);
104
+ source.onended = () => {
105
+ this.active.delete(instance.id);
106
+ source.disconnect();
107
+ gain.disconnect();
108
+ };
109
+ source.start(this.context.currentTime);
110
+ return instance;
111
+ }
112
+ stop(instance) {
113
+ instance.stop();
114
+ this.active.delete(instance.id);
115
+ }
116
+ stopAll() {
117
+ for (const instance of this.active.values()) {
118
+ instance.stop();
119
+ }
120
+ this.active.clear();
121
+ }
122
+ setBusVolume(bus, volume) {
123
+ this.assertNotDisposed();
124
+ this.getOrCreateBus(bus).setVolume(volume);
125
+ }
126
+ setBusMuted(bus, muted) {
127
+ this.assertNotDisposed();
128
+ this.getOrCreateBus(bus).setMuted(muted);
129
+ }
130
+ setMasterVolume(volume) {
131
+ this.assertNotDisposed();
132
+ this.master.setVolume(volume);
133
+ }
134
+ getActiveInstances() {
135
+ return [...this.active.values()];
136
+ }
137
+ dispose() {
138
+ if (this.disposed) {
139
+ return;
140
+ }
141
+ this.stopAll();
142
+ for (const bus of this.buses.values()) {
143
+ bus.gain.disconnect();
144
+ }
145
+ this.disposed = true;
146
+ }
147
+ createBus(name) {
148
+ const node = this.context.createGain();
149
+ if (name !== "master") {
150
+ node.connect(this.master.gain);
151
+ }
152
+ const bus = new AudioBus(name, node);
153
+ this.buses.set(name, bus);
154
+ return bus;
155
+ }
156
+ getOrCreateBus(name) {
157
+ const existing = this.buses.get(name);
158
+ if (existing) {
159
+ return existing;
160
+ }
161
+ return this.createBus(name);
162
+ }
163
+ assertNotDisposed() {
164
+ if (this.disposed) {
165
+ throw new Error("AudioEngine has been disposed");
166
+ }
167
+ }
168
+ };
169
+ var HeadlessAudioContext = class {
170
+ currentTime = 0;
171
+ createGain() {
172
+ return {
173
+ gain: { value: 1 },
174
+ connect() {
175
+ },
176
+ disconnect() {
177
+ }
178
+ };
179
+ }
180
+ createBufferSource() {
181
+ return {
182
+ buffer: null,
183
+ loop: false,
184
+ playbackRate: { value: 1 },
185
+ connect() {
186
+ },
187
+ start: () => {
188
+ this.currentTime += 0.016;
189
+ },
190
+ stop() {
191
+ },
192
+ disconnect() {
193
+ },
194
+ onended: null
195
+ };
196
+ }
197
+ async decodeAudioData(data) {
198
+ const bytes = data.byteLength;
199
+ return { duration: bytes / 48e3 };
200
+ }
201
+ };
202
+ export {
203
+ AudioBus,
204
+ AudioClip,
205
+ AudioEngine,
206
+ AudioInstance,
207
+ HeadlessAudioContext,
208
+ packageId
209
+ };
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "qti-clockwork-audio",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "main": "dist/index.cjs",
6
+ "module": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js",
12
+ "require": "./dist/index.cjs"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "scripts": {
19
+ "build": "tsup src/index.ts --format esm,cjs --dts --clean",
20
+ "test": "vitest run",
21
+ "typecheck": "tsc -p tsconfig.json --noEmit"
22
+ }
23
+ }