@al8b/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.
Files changed (56) hide show
  1. package/README.md +23 -0
  2. package/dist/constants.d.mts +8 -0
  3. package/dist/constants.d.ts +8 -0
  4. package/dist/constants.js +37 -0
  5. package/dist/constants.js.map +1 -0
  6. package/dist/constants.mjs +10 -0
  7. package/dist/constants.mjs.map +1 -0
  8. package/dist/core/audio-core.d.mts +98 -0
  9. package/dist/core/audio-core.d.ts +98 -0
  10. package/dist/core/audio-core.js +664 -0
  11. package/dist/core/audio-core.js.map +1 -0
  12. package/dist/core/audio-core.mjs +641 -0
  13. package/dist/core/audio-core.mjs.map +1 -0
  14. package/dist/core/audio-worklet.d.mts +3 -0
  15. package/dist/core/audio-worklet.d.ts +3 -0
  16. package/dist/core/audio-worklet.js +153 -0
  17. package/dist/core/audio-worklet.js.map +1 -0
  18. package/dist/core/audio-worklet.mjs +128 -0
  19. package/dist/core/audio-worklet.mjs.map +1 -0
  20. package/dist/core/index.d.mts +2 -0
  21. package/dist/core/index.d.ts +2 -0
  22. package/dist/core/index.js +666 -0
  23. package/dist/core/index.js.map +1 -0
  24. package/dist/core/index.mjs +641 -0
  25. package/dist/core/index.mjs.map +1 -0
  26. package/dist/devices/beeper.d.mts +21 -0
  27. package/dist/devices/beeper.d.ts +21 -0
  28. package/dist/devices/beeper.js +286 -0
  29. package/dist/devices/beeper.js.map +1 -0
  30. package/dist/devices/beeper.mjs +261 -0
  31. package/dist/devices/beeper.mjs.map +1 -0
  32. package/dist/devices/index.d.mts +3 -0
  33. package/dist/devices/index.d.ts +3 -0
  34. package/dist/devices/index.js +534 -0
  35. package/dist/devices/index.js.map +1 -0
  36. package/dist/devices/index.mjs +507 -0
  37. package/dist/devices/index.mjs.map +1 -0
  38. package/dist/devices/music.d.mts +27 -0
  39. package/dist/devices/music.d.ts +27 -0
  40. package/dist/devices/music.js +104 -0
  41. package/dist/devices/music.js.map +1 -0
  42. package/dist/devices/music.mjs +81 -0
  43. package/dist/devices/music.mjs.map +1 -0
  44. package/dist/devices/sound.d.mts +22 -0
  45. package/dist/devices/sound.d.ts +22 -0
  46. package/dist/devices/sound.js +198 -0
  47. package/dist/devices/sound.js.map +1 -0
  48. package/dist/devices/sound.mjs +175 -0
  49. package/dist/devices/sound.mjs.map +1 -0
  50. package/dist/index.d.mts +4 -0
  51. package/dist/index.d.ts +4 -0
  52. package/dist/index.js +916 -0
  53. package/dist/index.js.map +1 -0
  54. package/dist/index.mjs +888 -0
  55. package/dist/index.mjs.map +1 -0
  56. package/package.json +37 -0
@@ -0,0 +1,104 @@
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 __name = (target, value) => __defProp(target, "name", { value, configurable: true });
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+
21
+ // src/devices/music.ts
22
+ var music_exports = {};
23
+ __export(music_exports, {
24
+ Music: () => Music
25
+ });
26
+ module.exports = __toCommonJS(music_exports);
27
+ var Music = class {
28
+ static {
29
+ __name(this, "Music");
30
+ }
31
+ ready = 1;
32
+ name = "";
33
+ url;
34
+ tag;
35
+ playing = false;
36
+ audio;
37
+ constructor(audio, url) {
38
+ this.audio = audio;
39
+ this.url = url;
40
+ this.tag = new Audio(this.url);
41
+ this.ready = 1;
42
+ }
43
+ /**
44
+ * Play music with volume and loop (HTML5 Audio implementation)
45
+ */
46
+ play(volume = 1, loopit = false) {
47
+ this.playing = true;
48
+ this.tag.loop = !!loopit;
49
+ this.tag.volume = Math.max(0, Math.min(1, volume));
50
+ if (this.audio.isStarted()) {
51
+ this.tag.play();
52
+ } else {
53
+ this.audio.addToWakeUpList(this);
54
+ }
55
+ this.audio.addPlaying(this);
56
+ return {
57
+ play: /* @__PURE__ */ __name(() => {
58
+ return this.tag.play();
59
+ }, "play"),
60
+ stop: /* @__PURE__ */ __name(() => {
61
+ this.playing = false;
62
+ this.tag.pause();
63
+ this.audio.removePlaying(this);
64
+ }, "stop"),
65
+ setVolume: /* @__PURE__ */ __name((v) => {
66
+ this.tag.volume = Math.max(0, Math.min(1, v));
67
+ }, "setVolume"),
68
+ getPosition: /* @__PURE__ */ __name(() => {
69
+ return this.tag.currentTime;
70
+ }, "getPosition"),
71
+ getDuration: /* @__PURE__ */ __name(() => {
72
+ return this.tag.duration;
73
+ }, "getDuration"),
74
+ setPosition: /* @__PURE__ */ __name((pos) => {
75
+ this.tag.pause();
76
+ this.tag.currentTime = Math.max(0, Math.min(this.tag.duration, pos));
77
+ if (this.playing) {
78
+ this.tag.play();
79
+ }
80
+ }, "setPosition")
81
+ };
82
+ }
83
+ /**
84
+ * Wake up audio (for autoplay policy)
85
+ */
86
+ wakeUp() {
87
+ if (this.playing) {
88
+ this.tag.play();
89
+ }
90
+ }
91
+ /**
92
+ * Stop music
93
+ */
94
+ stop() {
95
+ this.playing = false;
96
+ this.tag.pause();
97
+ this.audio.removePlaying(this);
98
+ }
99
+ };
100
+ // Annotate the CommonJS export names for ESM import in node:
101
+ 0 && (module.exports = {
102
+ Music
103
+ });
104
+ //# sourceMappingURL=music.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/devices/music.ts"],"sourcesContent":["/**\n * Music - Music streaming and playback using HTML5 Audio\n * L8B Music class for LootiScript\n */\nexport class Music {\n\tpublic ready: number = 1;\n\tpublic name: string = \"\";\n\tpublic url: string;\n\tpublic tag: HTMLAudioElement;\n\tpublic playing: boolean = false;\n\tprivate audio: any;\n\n\tconstructor(audio: any, url: string) {\n\t\tthis.audio = audio;\n\t\tthis.url = url;\n\t\tthis.tag = new Audio(this.url);\n\t\tthis.ready = 1;\n\t}\n\n\t/**\n\t * Play music with volume and loop (HTML5 Audio implementation)\n\t */\n\tpublic play(volume: number = 1, loopit: boolean = false): any {\n\t\tthis.playing = true;\n\t\tthis.tag.loop = !!loopit;\n\t\tthis.tag.volume = Math.max(0, Math.min(1, volume));\n\n\t\tif (this.audio.isStarted()) {\n\t\t\tthis.tag.play();\n\t\t} else {\n\t\t\tthis.audio.addToWakeUpList(this);\n\t\t}\n\n\t\tthis.audio.addPlaying(this);\n\n\t\treturn {\n\t\t\tplay: () => {\n\t\t\t\treturn this.tag.play();\n\t\t\t},\n\t\t\tstop: () => {\n\t\t\t\tthis.playing = false;\n\t\t\t\tthis.tag.pause();\n\t\t\t\tthis.audio.removePlaying(this);\n\t\t\t},\n\t\t\tsetVolume: (v: number) => {\n\t\t\t\tthis.tag.volume = Math.max(0, Math.min(1, v));\n\t\t\t},\n\t\t\tgetPosition: () => {\n\t\t\t\treturn this.tag.currentTime;\n\t\t\t},\n\t\t\tgetDuration: () => {\n\t\t\t\treturn this.tag.duration;\n\t\t\t},\n\t\t\tsetPosition: (pos: number) => {\n\t\t\t\tthis.tag.pause();\n\t\t\t\tthis.tag.currentTime = Math.max(0, Math.min(this.tag.duration, pos));\n\t\t\t\tif (this.playing) {\n\t\t\t\t\tthis.tag.play();\n\t\t\t\t}\n\t\t\t},\n\t\t};\n\t}\n\n\t/**\n\t * Wake up audio (for autoplay policy)\n\t */\n\tpublic wakeUp(): void {\n\t\tif (this.playing) {\n\t\t\tthis.tag.play();\n\t\t}\n\t}\n\n\t/**\n\t * Stop music\n\t */\n\tpublic stop(): void {\n\t\tthis.playing = false;\n\t\tthis.tag.pause();\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AAA;AAC/D,YAAI,KAAKvB,SAAS;AACjB,eAAKD,IAAII,KAAI;QACd;MACD,GANa;IAOd;EACD;;;;EAKOqB,SAAe;AACrB,QAAI,KAAKxB,SAAS;AACjB,WAAKD,IAAII,KAAI;IACd;EACD;;;;EAKOU,OAAa;AACnB,SAAKb,UAAU;AACf,SAAKD,IAAIe,MAAK;AACd,SAAKb,MAAMc,cAAc,IAAI;EAC9B;AACD;","names":["Music","ready","name","url","tag","playing","audio","Audio","play","volume","loopit","loop","Math","max","min","isStarted","addToWakeUpList","addPlaying","stop","pause","removePlaying","setVolume","v","getPosition","currentTime","getDuration","duration","setPosition","pos","wakeUp"]}
@@ -0,0 +1,81 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // src/devices/music.ts
5
+ var Music = class {
6
+ static {
7
+ __name(this, "Music");
8
+ }
9
+ ready = 1;
10
+ name = "";
11
+ url;
12
+ tag;
13
+ playing = false;
14
+ audio;
15
+ constructor(audio, url) {
16
+ this.audio = audio;
17
+ this.url = url;
18
+ this.tag = new Audio(this.url);
19
+ this.ready = 1;
20
+ }
21
+ /**
22
+ * Play music with volume and loop (HTML5 Audio implementation)
23
+ */
24
+ play(volume = 1, loopit = false) {
25
+ this.playing = true;
26
+ this.tag.loop = !!loopit;
27
+ this.tag.volume = Math.max(0, Math.min(1, volume));
28
+ if (this.audio.isStarted()) {
29
+ this.tag.play();
30
+ } else {
31
+ this.audio.addToWakeUpList(this);
32
+ }
33
+ this.audio.addPlaying(this);
34
+ return {
35
+ play: /* @__PURE__ */ __name(() => {
36
+ return this.tag.play();
37
+ }, "play"),
38
+ stop: /* @__PURE__ */ __name(() => {
39
+ this.playing = false;
40
+ this.tag.pause();
41
+ this.audio.removePlaying(this);
42
+ }, "stop"),
43
+ setVolume: /* @__PURE__ */ __name((v) => {
44
+ this.tag.volume = Math.max(0, Math.min(1, v));
45
+ }, "setVolume"),
46
+ getPosition: /* @__PURE__ */ __name(() => {
47
+ return this.tag.currentTime;
48
+ }, "getPosition"),
49
+ getDuration: /* @__PURE__ */ __name(() => {
50
+ return this.tag.duration;
51
+ }, "getDuration"),
52
+ setPosition: /* @__PURE__ */ __name((pos) => {
53
+ this.tag.pause();
54
+ this.tag.currentTime = Math.max(0, Math.min(this.tag.duration, pos));
55
+ if (this.playing) {
56
+ this.tag.play();
57
+ }
58
+ }, "setPosition")
59
+ };
60
+ }
61
+ /**
62
+ * Wake up audio (for autoplay policy)
63
+ */
64
+ wakeUp() {
65
+ if (this.playing) {
66
+ this.tag.play();
67
+ }
68
+ }
69
+ /**
70
+ * Stop music
71
+ */
72
+ stop() {
73
+ this.playing = false;
74
+ this.tag.pause();
75
+ this.audio.removePlaying(this);
76
+ }
77
+ };
78
+ export {
79
+ Music
80
+ };
81
+ //# sourceMappingURL=music.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/devices/music.ts"],"sourcesContent":["/**\n * Music - Music streaming and playback using HTML5 Audio\n * L8B Music class for LootiScript\n */\nexport class Music {\n\tpublic ready: number = 1;\n\tpublic name: string = \"\";\n\tpublic url: string;\n\tpublic tag: HTMLAudioElement;\n\tpublic playing: boolean = false;\n\tprivate audio: any;\n\n\tconstructor(audio: any, url: string) {\n\t\tthis.audio = audio;\n\t\tthis.url = url;\n\t\tthis.tag = new Audio(this.url);\n\t\tthis.ready = 1;\n\t}\n\n\t/**\n\t * Play music with volume and loop (HTML5 Audio implementation)\n\t */\n\tpublic play(volume: number = 1, loopit: boolean = false): any {\n\t\tthis.playing = true;\n\t\tthis.tag.loop = !!loopit;\n\t\tthis.tag.volume = Math.max(0, Math.min(1, volume));\n\n\t\tif (this.audio.isStarted()) {\n\t\t\tthis.tag.play();\n\t\t} else {\n\t\t\tthis.audio.addToWakeUpList(this);\n\t\t}\n\n\t\tthis.audio.addPlaying(this);\n\n\t\treturn {\n\t\t\tplay: () => {\n\t\t\t\treturn this.tag.play();\n\t\t\t},\n\t\t\tstop: () => {\n\t\t\t\tthis.playing = false;\n\t\t\t\tthis.tag.pause();\n\t\t\t\tthis.audio.removePlaying(this);\n\t\t\t},\n\t\t\tsetVolume: (v: number) => {\n\t\t\t\tthis.tag.volume = Math.max(0, Math.min(1, v));\n\t\t\t},\n\t\t\tgetPosition: () => {\n\t\t\t\treturn this.tag.currentTime;\n\t\t\t},\n\t\t\tgetDuration: () => {\n\t\t\t\treturn this.tag.duration;\n\t\t\t},\n\t\t\tsetPosition: (pos: number) => {\n\t\t\t\tthis.tag.pause();\n\t\t\t\tthis.tag.currentTime = Math.max(0, Math.min(this.tag.duration, pos));\n\t\t\t\tif (this.playing) {\n\t\t\t\t\tthis.tag.play();\n\t\t\t\t}\n\t\t\t},\n\t\t};\n\t}\n\n\t/**\n\t * Wake up audio (for autoplay policy)\n\t */\n\tpublic wakeUp(): void {\n\t\tif (this.playing) {\n\t\t\tthis.tag.play();\n\t\t}\n\t}\n\n\t/**\n\t * Stop music\n\t */\n\tpublic stop(): void {\n\t\tthis.playing = false;\n\t\tthis.tag.pause();\n\t\tthis.audio.removePlaying(this);\n\t}\n}\n"],"mappings":";;;;AAIO,IAAMA,QAAN,MAAMA;EAJb,OAIaA;;;EACLC,QAAgB;EAChBC,OAAe;EACfC;EACAC;EACAC,UAAmB;EAClBC;EAER,YAAYA,OAAYH,KAAa;AACpC,SAAKG,QAAQA;AACb,SAAKH,MAAMA;AACX,SAAKC,MAAM,IAAIG,MAAM,KAAKJ,GAAG;AAC7B,SAAKF,QAAQ;EACd;;;;EAKOO,KAAKC,SAAiB,GAAGC,SAAkB,OAAY;AAC7D,SAAKL,UAAU;AACf,SAAKD,IAAIO,OAAO,CAAC,CAACD;AAClB,SAAKN,IAAIK,SAASG,KAAKC,IAAI,GAAGD,KAAKE,IAAI,GAAGL,MAAAA,CAAAA;AAE1C,QAAI,KAAKH,MAAMS,UAAS,GAAI;AAC3B,WAAKX,IAAII,KAAI;IACd,OAAO;AACN,WAAKF,MAAMU,gBAAgB,IAAI;IAChC;AAEA,SAAKV,MAAMW,WAAW,IAAI;AAE1B,WAAO;MACNT,MAAM,6BAAA;AACL,eAAO,KAAKJ,IAAII,KAAI;MACrB,GAFM;MAGNU,MAAM,6BAAA;AACL,aAAKb,UAAU;AACf,aAAKD,IAAIe,MAAK;AACd,aAAKb,MAAMc,cAAc,IAAI;MAC9B,GAJM;MAKNC,WAAW,wBAACC,MAAAA;AACX,aAAKlB,IAAIK,SAASG,KAAKC,IAAI,GAAGD,KAAKE,IAAI,GAAGQ,CAAAA,CAAAA;MAC3C,GAFW;MAGXC,aAAa,6BAAA;AACZ,eAAO,KAAKnB,IAAIoB;MACjB,GAFa;MAGbC,aAAa,6BAAA;AACZ,eAAO,KAAKrB,IAAIsB;MACjB,GAFa;MAGbC,aAAa,wBAACC,QAAAA;AACb,aAAKxB,IAAIe,MAAK;AACd,aAAKf,IAAIoB,cAAcZ,KAAKC,IAAI,GAAGD,KAAKE,IAAI,KAAKV,IAAIsB,UAAUE,GAAAA,CAAAA;AAC/D,YAAI,KAAKvB,SAAS;AACjB,eAAKD,IAAII,KAAI;QACd;MACD,GANa;IAOd;EACD;;;;EAKOqB,SAAe;AACrB,QAAI,KAAKxB,SAAS;AACjB,WAAKD,IAAII,KAAI;IACd;EACD;;;;EAKOU,OAAa;AACnB,SAAKb,UAAU;AACf,SAAKD,IAAIe,MAAK;AACd,SAAKb,MAAMc,cAAc,IAAI;EAC9B;AACD;","names":["Music","ready","name","url","tag","playing","audio","Audio","play","volume","loopit","loop","Math","max","min","isStarted","addToWakeUpList","addPlaying","stop","pause","removePlaying","setVolume","v","getPosition","currentTime","getDuration","duration","setPosition","pos","wakeUp"]}
@@ -0,0 +1,22 @@
1
+ declare class Sound {
2
+ ready: number;
3
+ buffer?: AudioBuffer;
4
+ name: string;
5
+ url: string;
6
+ private audio;
7
+ constructor(audio: any, url: string | AudioBuffer);
8
+ /**
9
+ * Load sound from URL
10
+ */
11
+ private loadSound;
12
+ /**
13
+ * Play sound with volume, pitch, pan, and loop
14
+ */
15
+ play(volume?: number, pitch?: number, pan?: number, loopit?: boolean): any;
16
+ /**
17
+ * Create MicroSound class for procedural sound generation
18
+ */
19
+ static createSoundClass(audiocore: any): any;
20
+ }
21
+
22
+ export { Sound };
@@ -0,0 +1,22 @@
1
+ declare class Sound {
2
+ ready: number;
3
+ buffer?: AudioBuffer;
4
+ name: string;
5
+ url: string;
6
+ private audio;
7
+ constructor(audio: any, url: string | AudioBuffer);
8
+ /**
9
+ * Load sound from URL
10
+ */
11
+ private loadSound;
12
+ /**
13
+ * Play sound with volume, pitch, pan, and loop
14
+ */
15
+ play(volume?: number, pitch?: number, pan?: number, loopit?: boolean): any;
16
+ /**
17
+ * Create MicroSound class for procedural sound generation
18
+ */
19
+ static createSoundClass(audiocore: any): any;
20
+ }
21
+
22
+ export { Sound };
@@ -0,0 +1,198 @@
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 __name = (target, value) => __defProp(target, "name", { value, configurable: true });
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+
21
+ // src/devices/sound.ts
22
+ var sound_exports = {};
23
+ __export(sound_exports, {
24
+ Sound: () => Sound
25
+ });
26
+ module.exports = __toCommonJS(sound_exports);
27
+ var import_diagnostics = require("@al8b/diagnostics");
28
+ var Sound = class _Sound {
29
+ static {
30
+ __name(this, "Sound");
31
+ }
32
+ ready = 0;
33
+ buffer;
34
+ name = "";
35
+ url;
36
+ audio;
37
+ constructor(audio, url) {
38
+ this.audio = audio;
39
+ if (url instanceof AudioBuffer) {
40
+ this.buffer = url;
41
+ this.url = "";
42
+ this.ready = 1;
43
+ } else {
44
+ this.url = url;
45
+ this.loadSound(url);
46
+ }
47
+ }
48
+ /**
49
+ * Load sound from URL
50
+ */
51
+ loadSound(url) {
52
+ const request = new XMLHttpRequest();
53
+ request.open("GET", url, true);
54
+ request.responseType = "arraybuffer";
55
+ request.onload = () => {
56
+ this.audio.context.decodeAudioData(request.response, (buffer) => {
57
+ this.buffer = buffer;
58
+ this.ready = 1;
59
+ }, (err) => {
60
+ (0, import_diagnostics.reportRuntimeError)(this.audio?.runtime?.listener, import_diagnostics.APIErrorCode.E7016, {
61
+ error: `Audio decoding failed: ${String(err)}`
62
+ });
63
+ });
64
+ };
65
+ request.onerror = () => {
66
+ (0, import_diagnostics.reportRuntimeError)(this.audio?.runtime?.listener, import_diagnostics.APIErrorCode.E7016, {
67
+ error: `Failed to load sound: ${url}`
68
+ });
69
+ };
70
+ request.send();
71
+ }
72
+ /**
73
+ * Play sound with volume, pitch, pan, and loop
74
+ */
75
+ play(volume = 1, pitch = 1, pan = 0, loopit = false) {
76
+ if (!this.buffer) return {
77
+ stop: /* @__PURE__ */ __name(() => {
78
+ }, "stop"),
79
+ finished: true
80
+ };
81
+ const context = this.audio.context;
82
+ const source = context.createBufferSource();
83
+ source.playbackRate.value = pitch;
84
+ source.buffer = this.buffer;
85
+ if (loopit) source.loop = true;
86
+ const gain = context.createGain();
87
+ gain.gain.value = volume;
88
+ const panner = context.createPanner();
89
+ panner.panningModel = "equalpower";
90
+ panner.setPan = (p) => {
91
+ panner.setPosition(p, 0, 1 - Math.abs(p));
92
+ };
93
+ panner.setPan(pan);
94
+ source.connect(gain);
95
+ gain.connect(panner);
96
+ panner.connect(context.destination);
97
+ source.start();
98
+ let playing = null;
99
+ if (loopit) {
100
+ playing = {
101
+ stop: /* @__PURE__ */ __name(() => {
102
+ source.stop();
103
+ }, "stop")
104
+ };
105
+ this.audio.addPlaying(playing);
106
+ }
107
+ const res = {
108
+ stop: /* @__PURE__ */ __name(() => {
109
+ source.stop();
110
+ if (playing) this.audio.removePlaying(playing);
111
+ return 1;
112
+ }, "stop"),
113
+ setVolume: /* @__PURE__ */ __name((v) => {
114
+ gain.gain.value = Math.max(0, Math.min(1, v));
115
+ }, "setVolume"),
116
+ setPitch: /* @__PURE__ */ __name((p) => {
117
+ source.playbackRate.value = Math.max(1e-3, Math.min(1e3, p));
118
+ }, "setPitch"),
119
+ setPan: /* @__PURE__ */ __name((p) => {
120
+ panner.setPan(Math.max(-1, Math.min(1, p)));
121
+ }, "setPan"),
122
+ getDuration: /* @__PURE__ */ __name(() => {
123
+ return source.buffer ? source.buffer.duration : 0;
124
+ }, "getDuration"),
125
+ finished: false
126
+ };
127
+ source.onended = () => {
128
+ res.finished = true;
129
+ };
130
+ return res;
131
+ }
132
+ /**
133
+ * Create MicroSound class for procedural sound generation
134
+ */
135
+ static createSoundClass(audiocore) {
136
+ return class MicroSound {
137
+ static {
138
+ __name(this, "MicroSound");
139
+ }
140
+ static classname = "Sound";
141
+ channels;
142
+ length;
143
+ sampleRate;
144
+ sound;
145
+ buffer;
146
+ constructor(channels, length, sampleRate = 44100) {
147
+ channels = channels === 1 ? 1 : 2;
148
+ if (!(length > 1 && length < 44100 * 1e3)) {
149
+ length = 44100;
150
+ }
151
+ if (!(sampleRate >= 8e3 && sampleRate <= 96e3)) {
152
+ sampleRate = 44100;
153
+ }
154
+ this.channels = channels;
155
+ this.length = length;
156
+ this.sampleRate = sampleRate;
157
+ this.buffer = audiocore.context.createBuffer(channels, length, sampleRate);
158
+ this.sound = new _Sound(audiocore, this.buffer);
159
+ }
160
+ /**
161
+ * Play the sound
162
+ */
163
+ play(volume, pitch, pan, loopit) {
164
+ return this.sound.play(volume, pitch, pan, loopit);
165
+ }
166
+ /**
167
+ * Write sample value to buffer
168
+ */
169
+ write(channel, position, value) {
170
+ if (channel === 0) {
171
+ const ch1 = this.buffer.getChannelData(0);
172
+ ch1[position] = value;
173
+ } else if (this.channels === 2) {
174
+ const ch2 = this.buffer.getChannelData(1);
175
+ ch2[position] = value;
176
+ }
177
+ }
178
+ /**
179
+ * Read sample value from buffer
180
+ */
181
+ read(channel, position) {
182
+ if (channel === 0) {
183
+ const ch1 = this.buffer.getChannelData(0);
184
+ return ch1[position];
185
+ } else if (this.channels === 2) {
186
+ const ch2 = this.buffer.getChannelData(1);
187
+ return ch2[position];
188
+ }
189
+ return 0;
190
+ }
191
+ };
192
+ }
193
+ };
194
+ // Annotate the CommonJS export names for ESM import in node:
195
+ 0 && (module.exports = {
196
+ Sound
197
+ });
198
+ //# sourceMappingURL=sound.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/devices/sound.ts"],"sourcesContent":["/**\n * Sound - Sound effect playback\n * Handles loading and playing audio buffers\n */\nimport { APIErrorCode, reportRuntimeError } from \"@al8b/diagnostics\";\n\nexport class Sound {\n\tpublic ready: number = 0;\n\tpublic buffer?: AudioBuffer;\n\tpublic name: string = \"\";\n\tpublic url: string;\n\tprivate audio: any;\n\n\tconstructor(audio: any, url: string | AudioBuffer) {\n\t\tthis.audio = audio;\n\n\t\tif (url instanceof AudioBuffer) {\n\t\t\tthis.buffer = url;\n\t\t\tthis.url = \"\";\n\t\t\tthis.ready = 1;\n\t\t} else {\n\t\t\tthis.url = url;\n\t\t\tthis.loadSound(url);\n\t\t}\n\t}\n\n\t/**\n\t * Load sound from URL\n\t */\n\tprivate loadSound(url: string): void {\n\t\tconst request = new XMLHttpRequest();\n\t\trequest.open(\"GET\", url, true);\n\t\trequest.responseType = \"arraybuffer\";\n\n\t\trequest.onload = () => {\n\t\t\tthis.audio.context.decodeAudioData(\n\t\t\t\trequest.response,\n\t\t\t\t(buffer: AudioBuffer) => {\n\t\t\t\t\tthis.buffer = buffer;\n\t\t\t\t\tthis.ready = 1;\n\t\t\t\t},\n\t\t\t\t(err: any) => {\n\t\t\t\t\treportRuntimeError(this.audio?.runtime?.listener, APIErrorCode.E7016, {\n\t\t\t\t\t\terror: `Audio decoding failed: ${String(err)}`,\n\t\t\t\t\t});\n\t\t\t\t},\n\t\t\t);\n\t\t};\n\n\t\trequest.onerror = () => {\n\t\t\treportRuntimeError(this.audio?.runtime?.listener, APIErrorCode.E7016, {\n\t\t\t\terror: `Failed to load sound: ${url}`,\n\t\t\t});\n\t\t};\n\n\t\trequest.send();\n\t}\n\n\t/**\n\t * Play sound with volume, pitch, pan, and loop\n\t */\n\tpublic play(volume: number = 1, pitch: number = 1, pan: number = 0, loopit: boolean = false): any {\n\t\tif (!this.buffer)\n\t\t\treturn {\n\t\t\t\tstop: () => {},\n\t\t\t\tfinished: true,\n\t\t\t};\n\n\t\tconst context = this.audio.context;\n\n\t\t// Create source\n\t\tconst source = context.createBufferSource();\n\t\tsource.playbackRate.value = pitch;\n\t\tsource.buffer = this.buffer;\n\t\tif (loopit) source.loop = true;\n\n\t\t// Create gain node\n\t\tconst gain = context.createGain();\n\t\tgain.gain.value = volume;\n\n\t\t// Create panner\n\t\tconst panner: any = context.createPanner();\n\t\tpanner.panningModel = \"equalpower\";\n\t\tpanner.setPan = (p: number) => {\n\t\t\tpanner.setPosition(p, 0, 1 - Math.abs(p));\n\t\t};\n\t\tpanner.setPan(pan);\n\n\t\t// Connect nodes\n\t\tsource.connect(gain);\n\t\tgain.connect(panner);\n\t\tpanner.connect(context.destination);\n\n\t\t// Start playback\n\t\tsource.start();\n\n\t\t// Track playing sounds for looping\n\t\tlet playing: any = null;\n\t\tif (loopit) {\n\t\t\tplaying = {\n\t\t\t\tstop: () => {\n\t\t\t\t\tsource.stop();\n\t\t\t\t},\n\t\t\t};\n\t\t\tthis.audio.addPlaying(playing);\n\t\t}\n\n\t\t// Return control interface\n\t\tconst res = {\n\t\t\tstop: () => {\n\t\t\t\tsource.stop();\n\t\t\t\tif (playing) this.audio.removePlaying(playing);\n\t\t\t\treturn 1;\n\t\t\t},\n\t\t\tsetVolume: (v: number) => {\n\t\t\t\tgain.gain.value = Math.max(0, Math.min(1, v));\n\t\t\t},\n\t\t\tsetPitch: (p: number) => {\n\t\t\t\tsource.playbackRate.value = Math.max(0.001, Math.min(1000, p));\n\t\t\t},\n\t\t\tsetPan: (p: number) => {\n\t\t\t\tpanner.setPan(Math.max(-1, Math.min(1, p)));\n\t\t\t},\n\t\t\tgetDuration: () => {\n\t\t\t\treturn source.buffer ? source.buffer.duration : 0;\n\t\t\t},\n\t\t\tfinished: false,\n\t\t};\n\n\t\tsource.onended = () => {\n\t\t\tres.finished = true;\n\t\t};\n\n\t\treturn res;\n\t}\n\n\t/**\n\t * Create MicroSound class for procedural sound generation\n\t */\n\tpublic static createSoundClass(audiocore: any): any {\n\t\treturn class MicroSound {\n\t\t\tpublic static classname = \"Sound\";\n\t\t\tpublic channels: number;\n\t\t\tpublic length: number;\n\t\t\tpublic sampleRate: number;\n\t\t\tprivate sound: Sound;\n\t\t\tprivate buffer: AudioBuffer;\n\n\t\t\tconstructor(channels: number, length: number, sampleRate: number = 44100) {\n\t\t\t\t// Validate parameters\n\t\t\t\tchannels = channels === 1 ? 1 : 2;\n\t\t\t\tif (!(length > 1 && length < 44100 * 1000)) {\n\t\t\t\t\tlength = 44100;\n\t\t\t\t}\n\t\t\t\tif (!(sampleRate >= 8000 && sampleRate <= 96000)) {\n\t\t\t\t\tsampleRate = 44100;\n\t\t\t\t}\n\n\t\t\t\tthis.channels = channels;\n\t\t\t\tthis.length = length;\n\t\t\t\tthis.sampleRate = sampleRate;\n\n\t\t\t\t// Create audio buffer\n\t\t\t\tthis.buffer = audiocore.context.createBuffer(channels, length, sampleRate);\n\t\t\t\tthis.sound = new Sound(audiocore, this.buffer);\n\t\t\t}\n\n\t\t\t/**\n\t\t\t * Play the sound\n\t\t\t */\n\t\t\tpublic play(volume?: number, pitch?: number, pan?: number, loopit?: boolean): any {\n\t\t\t\treturn this.sound.play(volume, pitch, pan, loopit);\n\t\t\t}\n\n\t\t\t/**\n\t\t\t * Write sample value to buffer\n\t\t\t */\n\t\t\tpublic write(channel: number, position: number, value: number): void {\n\t\t\t\tif (channel === 0) {\n\t\t\t\t\tconst ch1 = this.buffer.getChannelData(0);\n\t\t\t\t\tch1[position] = value;\n\t\t\t\t} else if (this.channels === 2) {\n\t\t\t\t\tconst ch2 = this.buffer.getChannelData(1);\n\t\t\t\t\tch2[position] = value;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t/**\n\t\t\t * Read sample value from buffer\n\t\t\t */\n\t\t\tpublic read(channel: number, position: number): number {\n\t\t\t\tif (channel === 0) {\n\t\t\t\t\tconst ch1 = this.buffer.getChannelData(0);\n\t\t\t\t\treturn ch1[position];\n\t\t\t\t} else if (this.channels === 2) {\n\t\t\t\t\tconst ch2 = this.buffer.getChannelData(1);\n\t\t\t\t\treturn ch2[position];\n\t\t\t\t}\n\t\t\t\treturn 0;\n\t\t\t}\n\t\t};\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;;;;;AAIA,yBAAiD;AAE1C,IAAMA,QAAN,MAAMA,OAAAA;EANb,OAMaA;;;EACLC,QAAgB;EAChBC;EACAC,OAAe;EACfC;EACCC;EAER,YAAYA,OAAYD,KAA2B;AAClD,SAAKC,QAAQA;AAEb,QAAID,eAAeE,aAAa;AAC/B,WAAKJ,SAASE;AACd,WAAKA,MAAM;AACX,WAAKH,QAAQ;IACd,OAAO;AACN,WAAKG,MAAMA;AACX,WAAKG,UAAUH,GAAAA;IAChB;EACD;;;;EAKQG,UAAUH,KAAmB;AACpC,UAAMI,UAAU,IAAIC,eAAAA;AACpBD,YAAQE,KAAK,OAAON,KAAK,IAAA;AACzBI,YAAQG,eAAe;AAEvBH,YAAQI,SAAS,MAAA;AAChB,WAAKP,MAAMQ,QAAQC,gBAClBN,QAAQO,UACR,CAACb,W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B,KAAKC,QAAiBC,OAAgBC,KAAcC,QAAuB;AACjF,eAAO,KAAKwC,MAAM5C,KAAKC,QAAQC,OAAOC,KAAKC,MAAAA;MAC5C;;;;MAKO0C,MAAMC,SAAiBC,UAAkBtC,OAAqB;AACpE,YAAIqC,YAAY,GAAG;AAClB,gBAAME,MAAM,KAAKzE,OAAO0E,eAAe,CAAA;AACvCD,cAAID,QAAAA,IAAYtC;QACjB,WAAW,KAAK+B,aAAa,GAAG;AAC/B,gBAAMU,MAAM,KAAK3E,OAAO0E,eAAe,CAAA;AACvCC,cAAIH,QAAAA,IAAYtC;QACjB;MACD;;;;MAKO0C,KAAKL,SAAiBC,UAA0B;AACtD,YAAID,YAAY,GAAG;AAClB,gBAAME,MAAM,KAAKzE,OAAO0E,eAAe,CAAA;AACvC,iBAAOD,IAAID,QAAAA;QACZ,WAAW,KAAKP,aAAa,GAAG;AAC/B,gBAAMU,MAAM,KAAK3E,OAAO0E,eAAe,CAAA;AACvC,iBAAOC,IAAIH,QAAAA;QACZ;AACA,eAAO;MACR;IACD;EACD;AACD;","names":["Sound","ready","buffer","name","url","audio","AudioBuffer","loadSound","request","XMLHttpRequest","open","responseType","onload","context","decodeAudioData","response","err","reportRuntimeError","runtime","listener","APIErrorCode","E7016","error","String","onerror","send","play","volume","pitch","pan","loopit","stop","finished","source","createBufferSource","playbackRate","value","loop","gain","createGain","panner","createPanner","panningModel","setPan","p","setPosition","Math","abs","connect","destination","start","playing","addPlaying","res","removePlaying","setVolume","v","max","min","setPitch","getDuration","duration","onended","createSoundClass","audiocore","MicroSound","classname","channels","length","sampleRate","sound","createBuffer","write","channel","position","ch1","getChannelData","ch2","read"]}
@@ -0,0 +1,175 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // src/devices/sound.ts
5
+ import { APIErrorCode, reportRuntimeError } from "@al8b/diagnostics";
6
+ var Sound = class _Sound {
7
+ static {
8
+ __name(this, "Sound");
9
+ }
10
+ ready = 0;
11
+ buffer;
12
+ name = "";
13
+ url;
14
+ audio;
15
+ constructor(audio, url) {
16
+ this.audio = audio;
17
+ if (url instanceof AudioBuffer) {
18
+ this.buffer = url;
19
+ this.url = "";
20
+ this.ready = 1;
21
+ } else {
22
+ this.url = url;
23
+ this.loadSound(url);
24
+ }
25
+ }
26
+ /**
27
+ * Load sound from URL
28
+ */
29
+ loadSound(url) {
30
+ const request = new XMLHttpRequest();
31
+ request.open("GET", url, true);
32
+ request.responseType = "arraybuffer";
33
+ request.onload = () => {
34
+ this.audio.context.decodeAudioData(request.response, (buffer) => {
35
+ this.buffer = buffer;
36
+ this.ready = 1;
37
+ }, (err) => {
38
+ reportRuntimeError(this.audio?.runtime?.listener, APIErrorCode.E7016, {
39
+ error: `Audio decoding failed: ${String(err)}`
40
+ });
41
+ });
42
+ };
43
+ request.onerror = () => {
44
+ reportRuntimeError(this.audio?.runtime?.listener, APIErrorCode.E7016, {
45
+ error: `Failed to load sound: ${url}`
46
+ });
47
+ };
48
+ request.send();
49
+ }
50
+ /**
51
+ * Play sound with volume, pitch, pan, and loop
52
+ */
53
+ play(volume = 1, pitch = 1, pan = 0, loopit = false) {
54
+ if (!this.buffer) return {
55
+ stop: /* @__PURE__ */ __name(() => {
56
+ }, "stop"),
57
+ finished: true
58
+ };
59
+ const context = this.audio.context;
60
+ const source = context.createBufferSource();
61
+ source.playbackRate.value = pitch;
62
+ source.buffer = this.buffer;
63
+ if (loopit) source.loop = true;
64
+ const gain = context.createGain();
65
+ gain.gain.value = volume;
66
+ const panner = context.createPanner();
67
+ panner.panningModel = "equalpower";
68
+ panner.setPan = (p) => {
69
+ panner.setPosition(p, 0, 1 - Math.abs(p));
70
+ };
71
+ panner.setPan(pan);
72
+ source.connect(gain);
73
+ gain.connect(panner);
74
+ panner.connect(context.destination);
75
+ source.start();
76
+ let playing = null;
77
+ if (loopit) {
78
+ playing = {
79
+ stop: /* @__PURE__ */ __name(() => {
80
+ source.stop();
81
+ }, "stop")
82
+ };
83
+ this.audio.addPlaying(playing);
84
+ }
85
+ const res = {
86
+ stop: /* @__PURE__ */ __name(() => {
87
+ source.stop();
88
+ if (playing) this.audio.removePlaying(playing);
89
+ return 1;
90
+ }, "stop"),
91
+ setVolume: /* @__PURE__ */ __name((v) => {
92
+ gain.gain.value = Math.max(0, Math.min(1, v));
93
+ }, "setVolume"),
94
+ setPitch: /* @__PURE__ */ __name((p) => {
95
+ source.playbackRate.value = Math.max(1e-3, Math.min(1e3, p));
96
+ }, "setPitch"),
97
+ setPan: /* @__PURE__ */ __name((p) => {
98
+ panner.setPan(Math.max(-1, Math.min(1, p)));
99
+ }, "setPan"),
100
+ getDuration: /* @__PURE__ */ __name(() => {
101
+ return source.buffer ? source.buffer.duration : 0;
102
+ }, "getDuration"),
103
+ finished: false
104
+ };
105
+ source.onended = () => {
106
+ res.finished = true;
107
+ };
108
+ return res;
109
+ }
110
+ /**
111
+ * Create MicroSound class for procedural sound generation
112
+ */
113
+ static createSoundClass(audiocore) {
114
+ return class MicroSound {
115
+ static {
116
+ __name(this, "MicroSound");
117
+ }
118
+ static classname = "Sound";
119
+ channels;
120
+ length;
121
+ sampleRate;
122
+ sound;
123
+ buffer;
124
+ constructor(channels, length, sampleRate = 44100) {
125
+ channels = channels === 1 ? 1 : 2;
126
+ if (!(length > 1 && length < 44100 * 1e3)) {
127
+ length = 44100;
128
+ }
129
+ if (!(sampleRate >= 8e3 && sampleRate <= 96e3)) {
130
+ sampleRate = 44100;
131
+ }
132
+ this.channels = channels;
133
+ this.length = length;
134
+ this.sampleRate = sampleRate;
135
+ this.buffer = audiocore.context.createBuffer(channels, length, sampleRate);
136
+ this.sound = new _Sound(audiocore, this.buffer);
137
+ }
138
+ /**
139
+ * Play the sound
140
+ */
141
+ play(volume, pitch, pan, loopit) {
142
+ return this.sound.play(volume, pitch, pan, loopit);
143
+ }
144
+ /**
145
+ * Write sample value to buffer
146
+ */
147
+ write(channel, position, value) {
148
+ if (channel === 0) {
149
+ const ch1 = this.buffer.getChannelData(0);
150
+ ch1[position] = value;
151
+ } else if (this.channels === 2) {
152
+ const ch2 = this.buffer.getChannelData(1);
153
+ ch2[position] = value;
154
+ }
155
+ }
156
+ /**
157
+ * Read sample value from buffer
158
+ */
159
+ read(channel, position) {
160
+ if (channel === 0) {
161
+ const ch1 = this.buffer.getChannelData(0);
162
+ return ch1[position];
163
+ } else if (this.channels === 2) {
164
+ const ch2 = this.buffer.getChannelData(1);
165
+ return ch2[position];
166
+ }
167
+ return 0;
168
+ }
169
+ };
170
+ }
171
+ };
172
+ export {
173
+ Sound
174
+ };
175
+ //# sourceMappingURL=sound.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/devices/sound.ts"],"sourcesContent":["/**\n * Sound - Sound effect playback\n * Handles loading and playing audio buffers\n */\nimport { APIErrorCode, reportRuntimeError } from \"@al8b/diagnostics\";\n\nexport class Sound {\n\tpublic ready: number = 0;\n\tpublic buffer?: AudioBuffer;\n\tpublic name: string = \"\";\n\tpublic url: string;\n\tprivate audio: any;\n\n\tconstructor(audio: any, url: string | AudioBuffer) {\n\t\tthis.audio = audio;\n\n\t\tif (url instanceof AudioBuffer) {\n\t\t\tthis.buffer = url;\n\t\t\tthis.url = \"\";\n\t\t\tthis.ready = 1;\n\t\t} else {\n\t\t\tthis.url = url;\n\t\t\tthis.loadSound(url);\n\t\t}\n\t}\n\n\t/**\n\t * Load sound from URL\n\t */\n\tprivate loadSound(url: string): void {\n\t\tconst request = new XMLHttpRequest();\n\t\trequest.open(\"GET\", url, true);\n\t\trequest.responseType = \"arraybuffer\";\n\n\t\trequest.onload = () => {\n\t\t\tthis.audio.context.decodeAudioData(\n\t\t\t\trequest.response,\n\t\t\t\t(buffer: AudioBuffer) => {\n\t\t\t\t\tthis.buffer = buffer;\n\t\t\t\t\tthis.ready = 1;\n\t\t\t\t},\n\t\t\t\t(err: any) => {\n\t\t\t\t\treportRuntimeError(this.audio?.runtime?.listener, APIErrorCode.E7016, {\n\t\t\t\t\t\terror: `Audio decoding failed: ${String(err)}`,\n\t\t\t\t\t});\n\t\t\t\t},\n\t\t\t);\n\t\t};\n\n\t\trequest.onerror = () => {\n\t\t\treportRuntimeError(this.audio?.runtime?.listener, APIErrorCode.E7016, {\n\t\t\t\terror: `Failed to load sound: ${url}`,\n\t\t\t});\n\t\t};\n\n\t\trequest.send();\n\t}\n\n\t/**\n\t * Play sound with volume, pitch, pan, and loop\n\t */\n\tpublic play(volume: number = 1, pitch: number = 1, pan: number = 0, loopit: boolean = false): any {\n\t\tif (!this.buffer)\n\t\t\treturn {\n\t\t\t\tstop: () => {},\n\t\t\t\tfinished: true,\n\t\t\t};\n\n\t\tconst context = this.audio.context;\n\n\t\t// Create source\n\t\tconst source = context.createBufferSource();\n\t\tsource.playbackRate.value = pitch;\n\t\tsource.buffer = this.buffer;\n\t\tif (loopit) source.loop = true;\n\n\t\t// Create gain node\n\t\tconst gain = context.createGain();\n\t\tgain.gain.value = volume;\n\n\t\t// Create panner\n\t\tconst panner: any = context.createPanner();\n\t\tpanner.panningModel = \"equalpower\";\n\t\tpanner.setPan = (p: number) => {\n\t\t\tpanner.setPosition(p, 0, 1 - Math.abs(p));\n\t\t};\n\t\tpanner.setPan(pan);\n\n\t\t// Connect nodes\n\t\tsource.connect(gain);\n\t\tgain.connect(panner);\n\t\tpanner.connect(context.destination);\n\n\t\t// Start playback\n\t\tsource.start();\n\n\t\t// Track playing sounds for looping\n\t\tlet playing: any = null;\n\t\tif (loopit) {\n\t\t\tplaying = {\n\t\t\t\tstop: () => {\n\t\t\t\t\tsource.stop();\n\t\t\t\t},\n\t\t\t};\n\t\t\tthis.audio.addPlaying(playing);\n\t\t}\n\n\t\t// Return control interface\n\t\tconst res = {\n\t\t\tstop: () => {\n\t\t\t\tsource.stop();\n\t\t\t\tif (playing) this.audio.removePlaying(playing);\n\t\t\t\treturn 1;\n\t\t\t},\n\t\t\tsetVolume: (v: number) => {\n\t\t\t\tgain.gain.value = Math.max(0, Math.min(1, v));\n\t\t\t},\n\t\t\tsetPitch: (p: number) => {\n\t\t\t\tsource.playbackRate.value = Math.max(0.001, Math.min(1000, p));\n\t\t\t},\n\t\t\tsetPan: (p: number) => {\n\t\t\t\tpanner.setPan(Math.max(-1, Math.min(1, p)));\n\t\t\t},\n\t\t\tgetDuration: () => {\n\t\t\t\treturn source.buffer ? source.buffer.duration : 0;\n\t\t\t},\n\t\t\tfinished: false,\n\t\t};\n\n\t\tsource.onended = () => {\n\t\t\tres.finished = true;\n\t\t};\n\n\t\treturn res;\n\t}\n\n\t/**\n\t * Create MicroSound class for procedural sound generation\n\t */\n\tpublic static createSoundClass(audiocore: any): any {\n\t\treturn class MicroSound {\n\t\t\tpublic static classname = \"Sound\";\n\t\t\tpublic channels: number;\n\t\t\tpublic length: number;\n\t\t\tpublic sampleRate: number;\n\t\t\tprivate sound: Sound;\n\t\t\tprivate buffer: AudioBuffer;\n\n\t\t\tconstructor(channels: number, length: number, sampleRate: number = 44100) {\n\t\t\t\t// Validate parameters\n\t\t\t\tchannels = channels === 1 ? 1 : 2;\n\t\t\t\tif (!(length > 1 && length < 44100 * 1000)) {\n\t\t\t\t\tlength = 44100;\n\t\t\t\t}\n\t\t\t\tif (!(sampleRate >= 8000 && sampleRate <= 96000)) {\n\t\t\t\t\tsampleRate = 44100;\n\t\t\t\t}\n\n\t\t\t\tthis.channels = channels;\n\t\t\t\tthis.length = length;\n\t\t\t\tthis.sampleRate = sampleRate;\n\n\t\t\t\t// Create audio buffer\n\t\t\t\tthis.buffer = audiocore.context.createBuffer(channels, length, sampleRate);\n\t\t\t\tthis.sound = new Sound(audiocore, this.buffer);\n\t\t\t}\n\n\t\t\t/**\n\t\t\t * Play the sound\n\t\t\t */\n\t\t\tpublic play(volume?: number, pitch?: number, pan?: number, loopit?: boolean): any {\n\t\t\t\treturn this.sound.play(volume, pitch, pan, loopit);\n\t\t\t}\n\n\t\t\t/**\n\t\t\t * Write sample value to buffer\n\t\t\t */\n\t\t\tpublic write(channel: number, position: number, value: number): void {\n\t\t\t\tif (channel === 0) {\n\t\t\t\t\tconst ch1 = this.buffer.getChannelData(0);\n\t\t\t\t\tch1[position] = value;\n\t\t\t\t} else if (this.channels === 2) {\n\t\t\t\t\tconst ch2 = this.buffer.getChannelData(1);\n\t\t\t\t\tch2[position] = value;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t/**\n\t\t\t * Read sample value from buffer\n\t\t\t */\n\t\t\tpublic read(channel: number, position: number): number {\n\t\t\t\tif (channel === 0) {\n\t\t\t\t\tconst ch1 = this.buffer.getChannelData(0);\n\t\t\t\t\treturn ch1[position];\n\t\t\t\t} else if (this.channels === 2) {\n\t\t\t\t\tconst ch2 = this.buffer.getChannelData(1);\n\t\t\t\t\treturn ch2[position];\n\t\t\t\t}\n\t\t\t\treturn 0;\n\t\t\t}\n\t\t};\n\t}\n}\n"],"mappings":";;;;AAIA,SAASA,cAAcC,0BAA0B;AAE1C,IAAMC,QAAN,MAAMA,OAAAA;EANb,OAMaA;;;EACLC,QAAgB;EAChBC;EACAC,OAAe;EACfC;EACCC;EAER,YAAYA,OAAYD,KAA2B;AAClD,SAAKC,QAAQA;AAEb,QAAID,eAAeE,aAAa;AAC/B,WAAKJ,SAASE;AACd,WAAKA,MAAM;AACX,WAAKH,QAAQ;IACd,OAAO;AACN,WAAKG,MAAMA;AACX,WAAKG,UAAUH,GAAAA;IAChB;EACD;;;;EAKQG,UAAUH,KAAmB;AACpC,UAAMI,UAAU,IAAIC,eAAAA;AACpBD,YAAQE,KAAK,OAAON,KAAK,IAAA;AACzBI,YAAQG,eAAe;AAEvBH,YAAQI,SAAS,MAAA;AAChB,WAAKP,MAAMQ,QAAQC,gBAClBN,QAAQO,UACR,CAACb,WAAAA;AACA,aAAKA,SAASA;AACd,aAAKD,QAAQ;MACd,GACA,CAACe,QAAAA;AACAC,2BAAmB,KAAKZ,OAAOa,SAASC,UAAUC,aAAaC,OAAO;UACrEC,OAAO,0BAA0BC,O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,OAAqB;AACpE,YAAIqC,YAAY,GAAG;AAClB,gBAAME,MAAM,KAAKzE,OAAO0E,eAAe,CAAA;AACvCD,cAAID,QAAAA,IAAYtC;QACjB,WAAW,KAAK+B,aAAa,GAAG;AAC/B,gBAAMU,MAAM,KAAK3E,OAAO0E,eAAe,CAAA;AACvCC,cAAIH,QAAAA,IAAYtC;QACjB;MACD;;;;MAKO0C,KAAKL,SAAiBC,UAA0B;AACtD,YAAID,YAAY,GAAG;AAClB,gBAAME,MAAM,KAAKzE,OAAO0E,eAAe,CAAA;AACvC,iBAAOD,IAAID,QAAAA;QACZ,WAAW,KAAKP,aAAa,GAAG;AAC/B,gBAAMU,MAAM,KAAK3E,OAAO0E,eAAe,CAAA;AACvC,iBAAOC,IAAIH,QAAAA;QACZ;AACA,eAAO;MACR;IACD;EACD;AACD;","names":["APIErrorCode","reportRuntimeError","Sound","ready","buffer","name","url","audio","AudioBuffer","loadSound","request","XMLHttpRequest","open","responseType","onload","context","decodeAudioData","response","err","reportRuntimeError","runtime","listener","APIErrorCode","E7016","error","String","onerror","send","play","volume","pitch","pan","loopit","stop","finished","source","createBufferSource","playbackRate","value","loop","gain","createGain","panner","createPanner","panningModel","setPan","p","setPosition","Math","abs","connect","destination","start","playing","addPlaying","res","removePlaying","setVolume","v","max","min","setPitch","getDuration","duration","onended","createSoundClass","audiocore","MicroSound","classname","channels","length","sampleRate","sound","createBuffer","write","channel","position","ch1","getChannelData","ch2","read"]}
@@ -0,0 +1,4 @@
1
+ export { AudioCore } from './core/audio-core.mjs';
2
+ export { Beeper } from './devices/beeper.mjs';
3
+ export { Music } from './devices/music.mjs';
4
+ export { Sound } from './devices/sound.mjs';