@combos-fun/plugin-sound 0.0.1

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 ADDED
@@ -0,0 +1,3 @@
1
+ # @combos-fun/plugin-sound
2
+
3
+ Internal workspace package (Combos Fun monorepo).
@@ -0,0 +1,396 @@
1
+ 'use strict';
2
+
3
+ var tslib = require('tslib');
4
+ var engine = require('@combos-fun/engine');
5
+
6
+ let SoundSystem = class SoundSystem extends engine.System {
7
+ static { this.systemName = 'SoundSystem'; }
8
+ get muted() {
9
+ return this.gainNode ? this.gainNode.gain.value === 0 : false;
10
+ }
11
+ set muted(v) {
12
+ if (!this.gainNode) {
13
+ return;
14
+ }
15
+ this.gainNode.gain.setValueAtTime(v ? 0 : 1, 0);
16
+ }
17
+ get volume() {
18
+ return this.gainNode ? this.gainNode.gain.value : 1;
19
+ }
20
+ set volume(v) {
21
+ if (!this.gainNode || typeof v !== 'number' || v < 0 || v > 1) {
22
+ return;
23
+ }
24
+ this.gainNode.gain.setValueAtTime(v, 0);
25
+ }
26
+ get audioLocked() {
27
+ if (!this.ctx) {
28
+ return true;
29
+ }
30
+ return this.ctx.state !== 'running';
31
+ }
32
+ constructor(obj) {
33
+ super();
34
+ /** 是否和游戏同步暂停和启动 */
35
+ this.autoPauseAndStart = true;
36
+ this.components = [];
37
+ this.pausedComponents = [];
38
+ this.audioBufferCache = {};
39
+ this.decodeAudioPromiseMap = {};
40
+ Object.assign(this, obj);
41
+ }
42
+ /**
43
+ * 恢复播放所有被暂停的音频
44
+ */
45
+ resumeAll() {
46
+ const handleResume = () => {
47
+ this.pausedComponents.forEach(component => {
48
+ component.play();
49
+ });
50
+ // 清理之前缓存的暂停列表
51
+ this.pausedComponents = [];
52
+ };
53
+ this.ctx.resume().then(handleResume, handleResume);
54
+ }
55
+ /**
56
+ * 暂停所有正在播放的音频
57
+ */
58
+ pauseAll() {
59
+ this.components.forEach(component => {
60
+ if (component.playing) {
61
+ this.pausedComponents.push(component);
62
+ component.pause();
63
+ }
64
+ });
65
+ this.ctx.suspend().then();
66
+ }
67
+ /**
68
+ * 停止所有正在播放的音频
69
+ */
70
+ stopAll() {
71
+ this.components.forEach(component => {
72
+ if (component.playing) {
73
+ component.stop();
74
+ }
75
+ });
76
+ // 清理之前缓存的暂停列表
77
+ this.pausedComponents = [];
78
+ this.ctx.suspend().then();
79
+ }
80
+ /**
81
+ * System 初始化用,可以配置参数,游戏未开始
82
+ *
83
+ * System init, set params, game is not begain
84
+ */
85
+ init() {
86
+ this.setupAudioContext();
87
+ }
88
+ update() {
89
+ const changes = this.componentObserver.clear();
90
+ for (const changed of changes) {
91
+ this.componentChanged(changed);
92
+ }
93
+ }
94
+ /**
95
+ * 游戏开始和游戏暂停后开始播放的时候调用。
96
+ *
97
+ * Called while the game to play when game pause.
98
+ */
99
+ onResume() {
100
+ if (!this.autoPauseAndStart) {
101
+ return;
102
+ }
103
+ this.resumeAll();
104
+ }
105
+ /**
106
+ * 游戏暂停的时候调用。
107
+ *
108
+ * Called while the game paused.
109
+ */
110
+ onPause() {
111
+ if (!this.autoPauseAndStart) {
112
+ return;
113
+ }
114
+ this.pauseAll();
115
+ }
116
+ /**
117
+ * System 被销毁的时候调用。
118
+ * Called while the system be destroyed.
119
+ */
120
+ onDestroy() {
121
+ this.components.forEach(component => {
122
+ component.onDestroy();
123
+ });
124
+ this.components = [];
125
+ if (this.ctx) {
126
+ this.gainNode.disconnect();
127
+ this.gainNode = null;
128
+ this.ctx.close();
129
+ this.ctx = null;
130
+ }
131
+ }
132
+ async componentChanged(changed) {
133
+ if (changed.componentName !== 'Sound')
134
+ return;
135
+ if (changed.type === engine.OBSERVER_TYPE.ADD) {
136
+ this.add(changed);
137
+ }
138
+ }
139
+ setupAudioContext() {
140
+ try {
141
+ const AudioContext = window.AudioContext || window.webkitAudioContext;
142
+ this.ctx = new AudioContext();
143
+ }
144
+ catch (error) {
145
+ console.error(error);
146
+ if (this.onError) {
147
+ this.onError(error);
148
+ }
149
+ }
150
+ if (!this.ctx) {
151
+ return;
152
+ }
153
+ this.gainNode =
154
+ typeof this.ctx.createGain === 'undefined' ? this.ctx.createGainNode() : this.ctx.createGain();
155
+ this.gainNode.gain.setValueAtTime(this.muted ? 0 : this.volume, this.ctx.currentTime);
156
+ this.gainNode.connect(this.ctx.destination);
157
+ this.unlockAudio();
158
+ }
159
+ unlockAudio() {
160
+ if (!this.ctx || !this.audioLocked) {
161
+ return;
162
+ }
163
+ const unlock = () => {
164
+ if (this.ctx) {
165
+ const removeListenerFn = () => {
166
+ document.body.removeEventListener('touchstart', unlock);
167
+ document.body.removeEventListener('touchend', unlock);
168
+ document.body.removeEventListener('click', unlock);
169
+ };
170
+ this.ctx.resume().then(removeListenerFn, removeListenerFn);
171
+ }
172
+ };
173
+ document.body.addEventListener('touchstart', unlock);
174
+ document.body.addEventListener('touchend', unlock);
175
+ document.body.addEventListener('click', unlock);
176
+ }
177
+ async add(changed) {
178
+ const component = changed.component;
179
+ this.components.push(component);
180
+ try {
181
+ const { config } = component;
182
+ component.state = 'loading';
183
+ const audio = await engine.resource.getResource(config.resource);
184
+ if (!this.audioBufferCache[audio.name] && audio?.data?.audio) {
185
+ this.audioBufferCache[audio.name] = await this.decodeAudioData(audio.data.audio, audio.name);
186
+ }
187
+ if (this.audioBufferCache[audio.name]) {
188
+ component.systemContext = this.ctx;
189
+ component.systemDestination = this.gainNode;
190
+ component.onload(this.audioBufferCache[audio.name]);
191
+ }
192
+ }
193
+ catch (error) {
194
+ if (this.onError) {
195
+ this.onError(error);
196
+ }
197
+ }
198
+ }
199
+ decodeAudioData(arraybuffer, name) {
200
+ if (this.decodeAudioPromiseMap[name]) {
201
+ return this.decodeAudioPromiseMap[name];
202
+ }
203
+ const promise = new Promise((resolve, reject) => {
204
+ if (!this.ctx) {
205
+ reject(new Error('No audio support'));
206
+ }
207
+ const success = (decodedData) => {
208
+ if (this.decodeAudioPromiseMap[name]) {
209
+ delete this.decodeAudioPromiseMap[name];
210
+ }
211
+ if (decodedData) {
212
+ resolve(decodedData);
213
+ }
214
+ else {
215
+ reject(new Error(`Error decoding audio ${name}`));
216
+ }
217
+ };
218
+ const error = (err) => {
219
+ if (this.decodeAudioPromiseMap[name]) {
220
+ delete this.decodeAudioPromiseMap[name];
221
+ }
222
+ reject(new Error(`${err}. arrayBuffer byteLength: ${arraybuffer ? arraybuffer.byteLength : 0}`));
223
+ };
224
+ const promise = this.ctx.decodeAudioData(arraybuffer, success, error);
225
+ if (promise instanceof Promise) {
226
+ promise.catch((err) => {
227
+ reject(new Error(`catch ${err}, arrayBuffer byteLength: ${arraybuffer ? arraybuffer.byteLength : 0}`));
228
+ });
229
+ }
230
+ });
231
+ this.decodeAudioPromiseMap[name] = promise;
232
+ return promise;
233
+ }
234
+ };
235
+ SoundSystem = tslib.__decorate([
236
+ engine.decorators.componentObserver({
237
+ Sound: [],
238
+ })
239
+ ], SoundSystem);
240
+ var SoundSystem_default = SoundSystem;
241
+
242
+ class Sound extends engine.Component {
243
+ constructor() {
244
+ super(...arguments);
245
+ this.state = 'unloaded';
246
+ this.config = {
247
+ resource: '',
248
+ autoplay: false,
249
+ muted: false,
250
+ volume: 1,
251
+ loop: false,
252
+ seek: 0,
253
+ };
254
+ this.playTime = 0;
255
+ this.startTime = 0;
256
+ this.duration = 0;
257
+ this.actionQueue = [];
258
+ }
259
+ static { this.componentName = 'Sound'; }
260
+ get muted() {
261
+ return this.gainNode ? this.gainNode.gain.value === 0 : false;
262
+ }
263
+ set muted(v) {
264
+ if (!this.gainNode) {
265
+ return;
266
+ }
267
+ this.gainNode.gain.setValueAtTime(v ? 0 : this.config.volume, 0);
268
+ }
269
+ get volume() {
270
+ return this.gainNode ? this.gainNode.gain.value : 1;
271
+ }
272
+ set volume(v) {
273
+ if (typeof v !== 'number' || v < 0 || v > 1) {
274
+ return;
275
+ }
276
+ this.config.volume = v;
277
+ if (!this.gainNode) {
278
+ return;
279
+ }
280
+ this.gainNode.gain.setValueAtTime(v, 0);
281
+ }
282
+ init(obj) {
283
+ if (!obj) {
284
+ return;
285
+ }
286
+ Object.assign(this.config, obj);
287
+ if (this.config.autoplay) {
288
+ this.actionQueue.push(this.play.bind(this));
289
+ }
290
+ }
291
+ play() {
292
+ if (this.state !== 'loaded') {
293
+ this.actionQueue.push(this.play.bind(this));
294
+ }
295
+ this.destroySource();
296
+ this.createSource();
297
+ if (!this.sourceNode) {
298
+ return;
299
+ }
300
+ const when = this.systemContext.currentTime;
301
+ const offset = this.config.seek;
302
+ const duration = this.config.duration;
303
+ this.sourceNode.start(0, offset, duration);
304
+ this.startTime = when;
305
+ this.playTime = when - offset;
306
+ this.paused = false;
307
+ this.playing = true;
308
+ this.resetConfig();
309
+ this.endedListener = () => {
310
+ if (!this.sourceNode) {
311
+ return;
312
+ }
313
+ if (this.config.onEnd) {
314
+ this.config.onEnd();
315
+ }
316
+ // 非交互事件播放完成需要销毁资源
317
+ if (this.playing) {
318
+ this.destroySource();
319
+ }
320
+ };
321
+ this.sourceNode.addEventListener('ended', this.endedListener);
322
+ }
323
+ pause() {
324
+ if (this.state !== 'loaded') {
325
+ this.actionQueue.push(this.pause.bind(this));
326
+ }
327
+ if (this.paused || !this.playing) {
328
+ return;
329
+ }
330
+ this.paused = true;
331
+ this.playing = false;
332
+ this.config.seek = this.getCurrentTime();
333
+ this.destroySource();
334
+ }
335
+ stop() {
336
+ if (this.state !== 'loaded') {
337
+ this.actionQueue.push(this.stop.bind(this));
338
+ }
339
+ if (!this.paused && !this.playing) {
340
+ return;
341
+ }
342
+ this.playing = false;
343
+ this.paused = false;
344
+ this.destroySource();
345
+ this.resetConfig();
346
+ }
347
+ onload(buffer) {
348
+ this.state = 'loaded';
349
+ this.buffer = buffer;
350
+ this.duration = this.buffer.duration;
351
+ this.actionQueue.forEach(action => action());
352
+ this.actionQueue.length = 0;
353
+ }
354
+ onDestroy() {
355
+ this.actionQueue.length = 0;
356
+ this.destroySource();
357
+ }
358
+ resetConfig() {
359
+ this.config.seek = 0;
360
+ }
361
+ getCurrentTime() {
362
+ if (this.config.loop && this.duration > 0) {
363
+ return (this.systemContext.currentTime - this.playTime) % this.duration;
364
+ }
365
+ return this.systemContext.currentTime - this.playTime;
366
+ }
367
+ createSource() {
368
+ if (!this.systemContext || this.state !== 'loaded') {
369
+ return;
370
+ }
371
+ this.sourceNode = this.systemContext.createBufferSource();
372
+ this.sourceNode.buffer = this.buffer;
373
+ this.sourceNode.loop = this.config.loop;
374
+ if (!this.gainNode) {
375
+ this.gainNode = this.systemContext.createGain();
376
+ this.gainNode.connect(this.systemDestination);
377
+ Object.assign(this, this.config);
378
+ }
379
+ this.sourceNode.connect(this.gainNode);
380
+ }
381
+ destroySource() {
382
+ if (!this.sourceNode)
383
+ return;
384
+ this.sourceNode.removeEventListener('ended', this.endedListener);
385
+ this.sourceNode.stop();
386
+ this.sourceNode.disconnect();
387
+ this.sourceNode = null;
388
+ this.startTime = 0;
389
+ this.playTime = 0;
390
+ this.playing = false;
391
+ }
392
+ }
393
+
394
+ exports.Sound = Sound;
395
+ exports.SoundSystem = SoundSystem_default;
396
+ //# sourceMappingURL=plugin-sound.cjs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin-sound.cjs.js","sources":["../lib/SoundSystem.ts","../lib/Sound.ts"],"sourcesContent":["import { System, decorators, ComponentChanged, OBSERVER_TYPE, resource } from '@combos-fun/engine';\nimport SoundComponent from './Sound';\n\ninterface SoundSystemParams {\n autoPauseAndStart?: boolean;\n onError: (error: any) => void;\n}\n\n@decorators.componentObserver({\n Sound: [],\n})\nclass SoundSystem extends System {\n static systemName = 'SoundSystem';\n\n private ctx: AudioContext;\n\n private gainNode: GainNode;\n\n /** 是否和游戏同步暂停和启动 */\n private autoPauseAndStart = true;\n\n private onError: (error: any) => void;\n\n private components: SoundComponent[] = [];\n\n private pausedComponents: SoundComponent[] = [];\n\n private audioBufferCache = {};\n\n private decodeAudioPromiseMap = {};\n\n get muted(): boolean {\n return this.gainNode ? this.gainNode.gain.value === 0 : false;\n }\n\n set muted(v: boolean) {\n if (!this.gainNode) {\n return;\n }\n this.gainNode.gain.setValueAtTime(v ? 0 : 1, 0);\n }\n\n get volume(): number {\n return this.gainNode ? this.gainNode.gain.value : 1;\n }\n\n set volume(v: number) {\n if (!this.gainNode || typeof v !== 'number' || v < 0 || v > 1) {\n return;\n }\n this.gainNode.gain.setValueAtTime(v, 0);\n }\n\n get audioLocked(): boolean {\n if (!this.ctx) {\n return true;\n }\n return this.ctx.state !== 'running';\n }\n\n constructor(obj?: SoundSystemParams) {\n super();\n Object.assign(this, obj);\n }\n\n /**\n * 恢复播放所有被暂停的音频\n */\n resumeAll() {\n const handleResume = () => {\n this.pausedComponents.forEach(component => {\n component.play();\n });\n // 清理之前缓存的暂停列表\n this.pausedComponents = [];\n };\n this.ctx.resume().then(handleResume, handleResume);\n }\n\n /**\n * 暂停所有正在播放的音频\n */\n pauseAll() {\n this.components.forEach(component => {\n if (component.playing) {\n this.pausedComponents.push(component);\n component.pause();\n }\n });\n this.ctx.suspend().then();\n }\n\n /**\n * 停止所有正在播放的音频\n */\n stopAll() {\n this.components.forEach(component => {\n if (component.playing) {\n component.stop();\n }\n });\n // 清理之前缓存的暂停列表\n this.pausedComponents = [];\n this.ctx.suspend().then();\n }\n\n /**\n * System 初始化用,可以配置参数,游戏未开始\n *\n * System init, set params, game is not begain\n */\n init() {\n this.setupAudioContext();\n }\n\n update() {\n const changes = this.componentObserver.clear();\n for (const changed of changes) {\n this.componentChanged(changed);\n }\n }\n\n /**\n * 游戏开始和游戏暂停后开始播放的时候调用。\n *\n * Called while the game to play when game pause.\n */\n onResume() {\n if (!this.autoPauseAndStart) {\n return;\n }\n this.resumeAll();\n }\n\n /**\n * 游戏暂停的时候调用。\n *\n * Called while the game paused.\n */\n onPause() {\n if (!this.autoPauseAndStart) {\n return;\n }\n this.pauseAll();\n }\n\n /**\n * System 被销毁的时候调用。\n * Called while the system be destroyed.\n */\n onDestroy() {\n this.components.forEach(component => {\n component.onDestroy();\n });\n this.components = [];\n if (this.ctx) {\n this.gainNode.disconnect();\n this.gainNode = null;\n this.ctx.close();\n this.ctx = null;\n }\n }\n\n async componentChanged(changed: ComponentChanged) {\n if (changed.componentName !== 'Sound') return;\n\n if (changed.type === OBSERVER_TYPE.ADD) {\n this.add(changed);\n }\n }\n\n private setupAudioContext() {\n try {\n const AudioContext = window.AudioContext || (window as any).webkitAudioContext;\n this.ctx = new AudioContext();\n } catch (error) {\n console.error(error);\n if (this.onError) {\n this.onError(error);\n }\n }\n\n if (!this.ctx) {\n return;\n }\n this.gainNode =\n typeof this.ctx.createGain === 'undefined' ? (this.ctx as any).createGainNode() : this.ctx.createGain();\n this.gainNode.gain.setValueAtTime(this.muted ? 0 : this.volume, this.ctx.currentTime);\n this.gainNode.connect(this.ctx.destination);\n this.unlockAudio();\n }\n\n private unlockAudio() {\n if (!this.ctx || !this.audioLocked) {\n return;\n }\n\n const unlock = () => {\n if (this.ctx) {\n const removeListenerFn = () => {\n document.body.removeEventListener('touchstart', unlock);\n document.body.removeEventListener('touchend', unlock);\n document.body.removeEventListener('click', unlock);\n };\n this.ctx.resume().then(removeListenerFn, removeListenerFn);\n }\n };\n document.body.addEventListener('touchstart', unlock);\n document.body.addEventListener('touchend', unlock);\n document.body.addEventListener('click', unlock);\n }\n\n private async add(changed: ComponentChanged) {\n const component = changed.component as SoundComponent;\n this.components.push(component);\n try {\n const { config } = component;\n component.state = 'loading';\n\n const audio = await resource.getResource(config.resource);\n if (!this.audioBufferCache[audio.name] && audio?.data?.audio) {\n this.audioBufferCache[audio.name] = await this.decodeAudioData(audio.data.audio, audio.name);\n }\n if (this.audioBufferCache[audio.name]) {\n component.systemContext = this.ctx;\n component.systemDestination = this.gainNode;\n component.onload(this.audioBufferCache[audio.name]);\n }\n } catch (error) {\n if (this.onError) {\n this.onError(error);\n }\n }\n }\n\n private decodeAudioData(arraybuffer: ArrayBuffer, name: string) {\n if (this.decodeAudioPromiseMap[name]) {\n return this.decodeAudioPromiseMap[name];\n }\n\n const promise = new Promise<AudioBuffer>((resolve, reject) => {\n if (!this.ctx) {\n reject(new Error('No audio support'));\n }\n\n const success = (decodedData: AudioBuffer) => {\n if (this.decodeAudioPromiseMap[name]) {\n delete this.decodeAudioPromiseMap[name];\n }\n if (decodedData) {\n resolve(decodedData);\n } else {\n reject(new Error(`Error decoding audio ${name}`));\n }\n };\n\n const error = (err: DOMException) => {\n if (this.decodeAudioPromiseMap[name]) {\n delete this.decodeAudioPromiseMap[name];\n }\n reject(new Error(`${err}. arrayBuffer byteLength: ${arraybuffer ? arraybuffer.byteLength : 0}`));\n };\n\n const promise = this.ctx.decodeAudioData(arraybuffer, success, error)\n if (promise instanceof Promise) {\n promise.catch((err) => {\n reject(new Error(`catch ${err}, arrayBuffer byteLength: ${arraybuffer ? arraybuffer.byteLength : 0}`));\n });\n }\n });\n\n this.decodeAudioPromiseMap[name] = promise;\n return promise;\n }\n}\n\nexport default SoundSystem;\n","import { Component } from '@combos-fun/engine';\n\nexport interface SoundParams {\n resource: string;\n autoplay?: boolean;\n muted?: boolean;\n volume?: number;\n loop?: boolean;\n seek?: number;\n duration?: number;\n onEnd?: () => void;\n}\n\nclass Sound extends Component<SoundParams> {\n static componentName = 'Sound';\n\n systemContext: AudioContext;\n\n systemDestination: GainNode;\n\n playing: boolean;\n\n state: 'unloaded' | 'loading' | 'loaded' = 'unloaded';\n\n config: SoundParams = {\n resource: '',\n autoplay: false,\n muted: false,\n volume: 1,\n loop: false,\n seek: 0,\n };\n\n private buffer: AudioBuffer;\n\n private sourceNode: AudioBufferSourceNode;\n\n private gainNode: GainNode;\n\n private paused: boolean;\n\n private playTime: number = 0;\n\n private startTime: number = 0;\n\n private duration: number = 0;\n\n private actionQueue: (() => void)[] = [];\n\n private endedListener: () => void;\n\n get muted(): boolean {\n return this.gainNode ? this.gainNode.gain.value === 0 : false;\n }\n\n set muted(v: boolean) {\n if (!this.gainNode) {\n return;\n }\n this.gainNode.gain.setValueAtTime(v ? 0 : this.config.volume, 0);\n }\n\n get volume(): number {\n return this.gainNode ? this.gainNode.gain.value : 1;\n }\n\n set volume(v: number) {\n if (typeof v !== 'number' || v < 0 || v > 1) {\n return;\n }\n this.config.volume = v;\n if (!this.gainNode) {\n return;\n }\n this.gainNode.gain.setValueAtTime(v, 0);\n }\n\n init(obj?: SoundParams) {\n if (!obj) {\n return;\n }\n\n Object.assign(this.config, obj);\n if (this.config.autoplay) {\n this.actionQueue.push(this.play.bind(this));\n }\n }\n\n play() {\n if (this.state !== 'loaded') {\n this.actionQueue.push(this.play.bind(this));\n }\n this.destroySource();\n this.createSource();\n\n if (!this.sourceNode) {\n return;\n }\n const when = this.systemContext.currentTime;\n const offset = this.config.seek;\n const duration = this.config.duration;\n\n this.sourceNode.start(0, offset, duration);\n\n this.startTime = when;\n this.playTime = when - offset;\n this.paused = false;\n this.playing = true;\n this.resetConfig();\n this.endedListener = () => {\n if (!this.sourceNode) {\n return;\n }\n if (this.config.onEnd) {\n this.config.onEnd();\n }\n // 非交互事件播放完成需要销毁资源\n if (this.playing) {\n this.destroySource();\n }\n };\n this.sourceNode.addEventListener('ended', this.endedListener);\n }\n\n pause() {\n if (this.state !== 'loaded') {\n this.actionQueue.push(this.pause.bind(this));\n }\n if (this.paused || !this.playing) {\n return;\n }\n this.paused = true;\n this.playing = false;\n this.config.seek = this.getCurrentTime();\n this.destroySource();\n }\n\n stop() {\n if (this.state !== 'loaded') {\n this.actionQueue.push(this.stop.bind(this));\n }\n if (!this.paused && !this.playing) {\n return;\n }\n this.playing = false;\n this.paused = false;\n this.destroySource();\n this.resetConfig();\n }\n\n onload(buffer: AudioBuffer) {\n this.state = 'loaded';\n this.buffer = buffer;\n this.duration = this.buffer.duration;\n this.actionQueue.forEach(action => action());\n this.actionQueue.length = 0;\n }\n\n onDestroy() {\n this.actionQueue.length = 0;\n this.destroySource();\n }\n\n private resetConfig() {\n this.config.seek = 0;\n }\n\n private getCurrentTime() {\n if (this.config.loop && this.duration > 0) {\n return (this.systemContext.currentTime - this.playTime) % this.duration;\n }\n\n return this.systemContext.currentTime - this.playTime;\n }\n\n private createSource() {\n if (!this.systemContext || this.state !== 'loaded') {\n return;\n }\n this.sourceNode = this.systemContext.createBufferSource();\n this.sourceNode.buffer = this.buffer;\n this.sourceNode.loop = this.config.loop;\n\n if (!this.gainNode) {\n this.gainNode = this.systemContext.createGain();\n this.gainNode.connect(this.systemDestination);\n Object.assign(this, this.config);\n }\n this.sourceNode.connect(this.gainNode);\n }\n\n private destroySource() {\n if (!this.sourceNode) return;\n this.sourceNode.removeEventListener('ended', this.endedListener);\n this.sourceNode.stop();\n this.sourceNode.disconnect();\n this.sourceNode = null;\n\n this.startTime = 0;\n this.playTime = 0;\n this.playing = false;\n }\n}\n\nexport default Sound;\n"],"names":["System","OBSERVER_TYPE","resource","__decorate","decorators","Component"],"mappings":";;;;;AAWA,IAAM,WAAW,GAAjB,MAAM,WAAY,SAAQA,aAAM,CAAA;aACvB,IAAA,CAAA,UAAU,GAAG,aAAH,CAAiB;AAmBlC,IAAA,IAAI,KAAK,GAAA;AACP,QAAA,OAAO,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,KAAK,CAAC,GAAG,KAAK;IAC/D;IAEA,IAAI,KAAK,CAAC,CAAU,EAAA;AAClB,QAAA,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE;YAClB;QACF;AACA,QAAA,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IACjD;AAEA,IAAA,IAAI,MAAM,GAAA;AACR,QAAA,OAAO,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,GAAG,CAAC;IACrD;IAEA,IAAI,MAAM,CAAC,CAAS,EAAA;AAClB,QAAA,IAAI,CAAC,IAAI,CAAC,QAAQ,IAAI,OAAO,C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A;QACnB,IAAI,CAAC,IAAI,CAAC,UAAU;YAAE;QACtB,IAAI,CAAC,UAAU,CAAC,mBAAmB,CAAC,OAAO,EAAE,IAAI,CAAC,aAAa,CAAC;AAChE,QAAA,IAAI,CAAC,UAAU,CAAC,IAAI,EAAE;AACtB,QAAA,IAAI,CAAC,UAAU,CAAC,UAAU,EAAE;AAC5B,QAAA,IAAI,CAAC,UAAU,GAAG,IAAI;AAEtB,QAAA,IAAI,CAAC,SAAS,GAAG,CAAC;AAClB,QAAA,IAAI,CAAC,QAAQ,GAAG,CAAC;AACjB,QAAA,IAAI,CAAC,OAAO,GAAG,KAAK;IACtB;;;;;;"}
@@ -0,0 +1 @@
1
+ "use strict";var t=require("tslib"),e=require("@combos-fun/engine");let s=class extends e.System{static{this.systemName="SoundSystem"}get muted(){return!!this.gainNode&&0===this.gainNode.gain.value}set muted(t){this.gainNode&&this.gainNode.gain.setValueAtTime(t?0:1,0)}get volume(){return this.gainNode?this.gainNode.gain.value:1}set volume(t){!this.gainNode||"number"!=typeof t||t<0||t>1||this.gainNode.gain.setValueAtTime(t,0)}get audioLocked(){return!this.ctx||"running"!==this.ctx.state}constructor(t){super(),this.autoPauseAndStart=!0,this.components=[],this.pausedComponents=[],this.audioBufferCache={},this.decodeAudioPromiseMap={},Object.assign(this,t)}resumeAll(){const t=()=>{this.pausedComponents.forEach(t=>{t.play()}),this.pausedComponents=[]};this.ctx.resume().then(t,t)}pauseAll(){this.components.forEach(t=>{t.playing&&(this.pausedComponents.push(t),t.pause())}),this.ctx.suspend().then()}stopAll(){this.components.forEach(t=>{t.playing&&t.stop()}),this.pausedComponents=[],this.ctx.suspend().then()}init(){this.setupAudioContext()}update(){const t=this.componentObserver.clear();for(const e of t)this.componentChanged(e)}onResume(){this.autoPauseAndStart&&this.resumeAll()}onPause(){this.autoPauseAndStart&&this.pauseAll()}onDestroy(){this.components.forEach(t=>{t.onDestroy()}),this.components=[],this.ctx&&(this.gainNode.disconnect(),this.gainNode=null,this.ctx.close(),this.ctx=null)}async componentChanged(t){"Sound"===t.componentName&&t.type===e.OBSERVER_TYPE.ADD&&this.add(t)}setupAudioContext(){try{const t=window.AudioContext||window.webkitAudioContext;this.ctx=new t}catch(t){console.error(t),this.onError&&this.onError(t)}this.ctx&&(this.gainNode=void 0===this.ctx.createGain?this.ctx.createGainNode():this.ctx.createGain(),this.gainNode.gain.setValueAtTime(this.muted?0:this.volume,this.ctx.currentTime),this.gainNode.connect(this.ctx.destination),this.unlockAudio())}unlockAudio(){if(!this.ctx||!this.audioLocked)return;const t=()=>{if(this.ctx){const e=()=>{document.body.removeEventListener("touchstart",t),document.body.removeEventListener("touchend",t),document.body.removeEventListener("click",t)};this.ctx.resume().then(e,e)}};document.body.addEventListener("touchstart",t),document.body.addEventListener("touchend",t),document.body.addEventListener("click",t)}async add(t){const s=t.component;this.components.push(s);try{const{config:t}=s;s.state="loading";const i=await e.resource.getResource(t.resource);!this.audioBufferCache[i.name]&&i?.data?.audio&&(this.audioBufferCache[i.name]=await this.decodeAudioData(i.data.audio,i.name)),this.audioBufferCache[i.name]&&(s.systemContext=this.ctx,s.systemDestination=this.gainNode,s.onload(this.audioBufferCache[i.name]))}catch(t){this.onError&&this.onError(t)}}decodeAudioData(t,e){if(this.decodeAudioPromiseMap[e])return this.decodeAudioPromiseMap[e];const s=new Promise((s,i)=>{this.ctx||i(new Error("No audio support"));const o=this.ctx.decodeAudioData(t,t=>{this.decodeAudioPromiseMap[e]&&delete this.decodeAudioPromiseMap[e],t?s(t):i(new Error(`Error decoding audio ${e}`))},s=>{this.decodeAudioPromiseMap[e]&&delete this.decodeAudioPromiseMap[e],i(new Error(`${s}. arrayBuffer byteLength: ${t?t.byteLength:0}`))});o instanceof Promise&&o.catch(e=>{i(new Error(`catch ${e}, arrayBuffer byteLength: ${t?t.byteLength:0}`))})});return this.decodeAudioPromiseMap[e]=s,s}};s=t.__decorate([e.decorators.componentObserver({Sound:[]})],s);var i=s;class o extends e.Component{constructor(){super(...arguments),this.state="unloaded",this.config={resource:"",autoplay:!1,muted:!1,volume:1,loop:!1,seek:0},this.playTime=0,this.startTime=0,this.duration=0,this.actionQueue=[]}static{this.componentName="Sound"}get muted(){return!!this.gainNode&&0===this.gainNode.gain.value}set muted(t){this.gainNode&&this.gainNode.gain.setValueAtTime(t?0:this.config.volume,0)}get volume(){return this.gainNode?this.gainNode.gain.value:1}set volume(t){"number"!=typeof t||t<0||t>1||(this.config.volume=t,this.gainNode&&this.gainNode.gain.setValueAtTime(t,0))}init(t){t&&(Object.assign(this.config,t),this.config.autoplay&&this.actionQueue.push(this.play.bind(this)))}play(){if("loaded"!==this.state&&this.actionQueue.push(this.play.bind(this)),this.destroySource(),this.createSource(),!this.sourceNode)return;const t=this.systemContext.currentTime,e=this.config.seek,s=this.config.duration;this.sourceNode.start(0,e,s),this.startTime=t,this.playTime=t-e,this.paused=!1,this.playing=!0,this.resetConfig(),this.endedListener=()=>{this.sourceNode&&(this.config.onEnd&&this.config.onEnd(),this.playing&&this.destroySource())},this.sourceNode.addEventListener("ended",this.endedListener)}pause(){"loaded"!==this.state&&this.actionQueue.push(this.pause.bind(this)),!this.paused&&this.playing&&(this.paused=!0,this.playing=!1,this.config.seek=this.getCurrentTime(),this.destroySource())}stop(){"loaded"!==this.state&&this.actionQueue.push(this.stop.bind(this)),(this.paused||this.playing)&&(this.playing=!1,this.paused=!1,this.destroySource(),this.resetConfig())}onload(t){this.state="loaded",this.buffer=t,this.duration=this.buffer.duration,this.actionQueue.forEach(t=>t()),this.actionQueue.length=0}onDestroy(){this.actionQueue.length=0,this.destroySource()}resetConfig(){this.config.seek=0}getCurrentTime(){return this.config.loop&&this.duration>0?(this.systemContext.currentTime-this.playTime)%this.duration:this.systemContext.currentTime-this.playTime}createSource(){this.systemContext&&"loaded"===this.state&&(this.sourceNode=this.systemContext.createBufferSource(),this.sourceNode.buffer=this.buffer,this.sourceNode.loop=this.config.loop,this.gainNode||(this.gainNode=this.systemContext.createGain(),this.gainNode.connect(this.systemDestination),Object.assign(this,this.config)),this.sourceNode.connect(this.gainNode))}destroySource(){this.sourceNode&&(this.sourceNode.removeEventListener("ended",this.endedListener),this.sourceNode.stop(),this.sourceNode.disconnect(),this.sourceNode=null,this.startTime=0,this.playTime=0,this.playing=!1)}}exports.Sound=o,exports.SoundSystem=i;
@@ -0,0 +1,109 @@
1
+ import { System, ComponentChanged, Component } from '@combos-fun/engine';
2
+
3
+ interface SoundSystemParams {
4
+ autoPauseAndStart?: boolean;
5
+ onError: (error: any) => void;
6
+ }
7
+ declare class SoundSystem extends System {
8
+ static systemName: string;
9
+ private ctx;
10
+ private gainNode;
11
+ /** 是否和游戏同步暂停和启动 */
12
+ private autoPauseAndStart;
13
+ private onError;
14
+ private components;
15
+ private pausedComponents;
16
+ private audioBufferCache;
17
+ private decodeAudioPromiseMap;
18
+ get muted(): boolean;
19
+ set muted(v: boolean);
20
+ get volume(): number;
21
+ set volume(v: number);
22
+ get audioLocked(): boolean;
23
+ constructor(obj?: SoundSystemParams);
24
+ /**
25
+ * 恢复播放所有被暂停的音频
26
+ */
27
+ resumeAll(): void;
28
+ /**
29
+ * 暂停所有正在播放的音频
30
+ */
31
+ pauseAll(): void;
32
+ /**
33
+ * 停止所有正在播放的音频
34
+ */
35
+ stopAll(): void;
36
+ /**
37
+ * System 初始化用,可以配置参数,游戏未开始
38
+ *
39
+ * System init, set params, game is not begain
40
+ */
41
+ init(): void;
42
+ update(): void;
43
+ /**
44
+ * 游戏开始和游戏暂停后开始播放的时候调用。
45
+ *
46
+ * Called while the game to play when game pause.
47
+ */
48
+ onResume(): void;
49
+ /**
50
+ * 游戏暂停的时候调用。
51
+ *
52
+ * Called while the game paused.
53
+ */
54
+ onPause(): void;
55
+ /**
56
+ * System 被销毁的时候调用。
57
+ * Called while the system be destroyed.
58
+ */
59
+ onDestroy(): void;
60
+ componentChanged(changed: ComponentChanged): Promise<void>;
61
+ private setupAudioContext;
62
+ private unlockAudio;
63
+ private add;
64
+ private decodeAudioData;
65
+ }
66
+
67
+ interface SoundParams {
68
+ resource: string;
69
+ autoplay?: boolean;
70
+ muted?: boolean;
71
+ volume?: number;
72
+ loop?: boolean;
73
+ seek?: number;
74
+ duration?: number;
75
+ onEnd?: () => void;
76
+ }
77
+ declare class Sound extends Component<SoundParams> {
78
+ static componentName: string;
79
+ systemContext: AudioContext;
80
+ systemDestination: GainNode;
81
+ playing: boolean;
82
+ state: 'unloaded' | 'loading' | 'loaded';
83
+ config: SoundParams;
84
+ private buffer;
85
+ private sourceNode;
86
+ private gainNode;
87
+ private paused;
88
+ private playTime;
89
+ private startTime;
90
+ private duration;
91
+ private actionQueue;
92
+ private endedListener;
93
+ get muted(): boolean;
94
+ set muted(v: boolean);
95
+ get volume(): number;
96
+ set volume(v: number);
97
+ init(obj?: SoundParams): void;
98
+ play(): void;
99
+ pause(): void;
100
+ stop(): void;
101
+ onload(buffer: AudioBuffer): void;
102
+ onDestroy(): void;
103
+ private resetConfig;
104
+ private getCurrentTime;
105
+ private createSource;
106
+ private destroySource;
107
+ }
108
+
109
+ export { Sound, SoundSystem };
@@ -0,0 +1,393 @@
1
+ import { __decorate } from 'tslib';
2
+ import { System, OBSERVER_TYPE, resource, decorators, Component } from '@combos-fun/engine';
3
+
4
+ let SoundSystem = class SoundSystem extends System {
5
+ static { this.systemName = 'SoundSystem'; }
6
+ get muted() {
7
+ return this.gainNode ? this.gainNode.gain.value === 0 : false;
8
+ }
9
+ set muted(v) {
10
+ if (!this.gainNode) {
11
+ return;
12
+ }
13
+ this.gainNode.gain.setValueAtTime(v ? 0 : 1, 0);
14
+ }
15
+ get volume() {
16
+ return this.gainNode ? this.gainNode.gain.value : 1;
17
+ }
18
+ set volume(v) {
19
+ if (!this.gainNode || typeof v !== 'number' || v < 0 || v > 1) {
20
+ return;
21
+ }
22
+ this.gainNode.gain.setValueAtTime(v, 0);
23
+ }
24
+ get audioLocked() {
25
+ if (!this.ctx) {
26
+ return true;
27
+ }
28
+ return this.ctx.state !== 'running';
29
+ }
30
+ constructor(obj) {
31
+ super();
32
+ /** 是否和游戏同步暂停和启动 */
33
+ this.autoPauseAndStart = true;
34
+ this.components = [];
35
+ this.pausedComponents = [];
36
+ this.audioBufferCache = {};
37
+ this.decodeAudioPromiseMap = {};
38
+ Object.assign(this, obj);
39
+ }
40
+ /**
41
+ * 恢复播放所有被暂停的音频
42
+ */
43
+ resumeAll() {
44
+ const handleResume = () => {
45
+ this.pausedComponents.forEach(component => {
46
+ component.play();
47
+ });
48
+ // 清理之前缓存的暂停列表
49
+ this.pausedComponents = [];
50
+ };
51
+ this.ctx.resume().then(handleResume, handleResume);
52
+ }
53
+ /**
54
+ * 暂停所有正在播放的音频
55
+ */
56
+ pauseAll() {
57
+ this.components.forEach(component => {
58
+ if (component.playing) {
59
+ this.pausedComponents.push(component);
60
+ component.pause();
61
+ }
62
+ });
63
+ this.ctx.suspend().then();
64
+ }
65
+ /**
66
+ * 停止所有正在播放的音频
67
+ */
68
+ stopAll() {
69
+ this.components.forEach(component => {
70
+ if (component.playing) {
71
+ component.stop();
72
+ }
73
+ });
74
+ // 清理之前缓存的暂停列表
75
+ this.pausedComponents = [];
76
+ this.ctx.suspend().then();
77
+ }
78
+ /**
79
+ * System 初始化用,可以配置参数,游戏未开始
80
+ *
81
+ * System init, set params, game is not begain
82
+ */
83
+ init() {
84
+ this.setupAudioContext();
85
+ }
86
+ update() {
87
+ const changes = this.componentObserver.clear();
88
+ for (const changed of changes) {
89
+ this.componentChanged(changed);
90
+ }
91
+ }
92
+ /**
93
+ * 游戏开始和游戏暂停后开始播放的时候调用。
94
+ *
95
+ * Called while the game to play when game pause.
96
+ */
97
+ onResume() {
98
+ if (!this.autoPauseAndStart) {
99
+ return;
100
+ }
101
+ this.resumeAll();
102
+ }
103
+ /**
104
+ * 游戏暂停的时候调用。
105
+ *
106
+ * Called while the game paused.
107
+ */
108
+ onPause() {
109
+ if (!this.autoPauseAndStart) {
110
+ return;
111
+ }
112
+ this.pauseAll();
113
+ }
114
+ /**
115
+ * System 被销毁的时候调用。
116
+ * Called while the system be destroyed.
117
+ */
118
+ onDestroy() {
119
+ this.components.forEach(component => {
120
+ component.onDestroy();
121
+ });
122
+ this.components = [];
123
+ if (this.ctx) {
124
+ this.gainNode.disconnect();
125
+ this.gainNode = null;
126
+ this.ctx.close();
127
+ this.ctx = null;
128
+ }
129
+ }
130
+ async componentChanged(changed) {
131
+ if (changed.componentName !== 'Sound')
132
+ return;
133
+ if (changed.type === OBSERVER_TYPE.ADD) {
134
+ this.add(changed);
135
+ }
136
+ }
137
+ setupAudioContext() {
138
+ try {
139
+ const AudioContext = window.AudioContext || window.webkitAudioContext;
140
+ this.ctx = new AudioContext();
141
+ }
142
+ catch (error) {
143
+ console.error(error);
144
+ if (this.onError) {
145
+ this.onError(error);
146
+ }
147
+ }
148
+ if (!this.ctx) {
149
+ return;
150
+ }
151
+ this.gainNode =
152
+ typeof this.ctx.createGain === 'undefined' ? this.ctx.createGainNode() : this.ctx.createGain();
153
+ this.gainNode.gain.setValueAtTime(this.muted ? 0 : this.volume, this.ctx.currentTime);
154
+ this.gainNode.connect(this.ctx.destination);
155
+ this.unlockAudio();
156
+ }
157
+ unlockAudio() {
158
+ if (!this.ctx || !this.audioLocked) {
159
+ return;
160
+ }
161
+ const unlock = () => {
162
+ if (this.ctx) {
163
+ const removeListenerFn = () => {
164
+ document.body.removeEventListener('touchstart', unlock);
165
+ document.body.removeEventListener('touchend', unlock);
166
+ document.body.removeEventListener('click', unlock);
167
+ };
168
+ this.ctx.resume().then(removeListenerFn, removeListenerFn);
169
+ }
170
+ };
171
+ document.body.addEventListener('touchstart', unlock);
172
+ document.body.addEventListener('touchend', unlock);
173
+ document.body.addEventListener('click', unlock);
174
+ }
175
+ async add(changed) {
176
+ const component = changed.component;
177
+ this.components.push(component);
178
+ try {
179
+ const { config } = component;
180
+ component.state = 'loading';
181
+ const audio = await resource.getResource(config.resource);
182
+ if (!this.audioBufferCache[audio.name] && audio?.data?.audio) {
183
+ this.audioBufferCache[audio.name] = await this.decodeAudioData(audio.data.audio, audio.name);
184
+ }
185
+ if (this.audioBufferCache[audio.name]) {
186
+ component.systemContext = this.ctx;
187
+ component.systemDestination = this.gainNode;
188
+ component.onload(this.audioBufferCache[audio.name]);
189
+ }
190
+ }
191
+ catch (error) {
192
+ if (this.onError) {
193
+ this.onError(error);
194
+ }
195
+ }
196
+ }
197
+ decodeAudioData(arraybuffer, name) {
198
+ if (this.decodeAudioPromiseMap[name]) {
199
+ return this.decodeAudioPromiseMap[name];
200
+ }
201
+ const promise = new Promise((resolve, reject) => {
202
+ if (!this.ctx) {
203
+ reject(new Error('No audio support'));
204
+ }
205
+ const success = (decodedData) => {
206
+ if (this.decodeAudioPromiseMap[name]) {
207
+ delete this.decodeAudioPromiseMap[name];
208
+ }
209
+ if (decodedData) {
210
+ resolve(decodedData);
211
+ }
212
+ else {
213
+ reject(new Error(`Error decoding audio ${name}`));
214
+ }
215
+ };
216
+ const error = (err) => {
217
+ if (this.decodeAudioPromiseMap[name]) {
218
+ delete this.decodeAudioPromiseMap[name];
219
+ }
220
+ reject(new Error(`${err}. arrayBuffer byteLength: ${arraybuffer ? arraybuffer.byteLength : 0}`));
221
+ };
222
+ const promise = this.ctx.decodeAudioData(arraybuffer, success, error);
223
+ if (promise instanceof Promise) {
224
+ promise.catch((err) => {
225
+ reject(new Error(`catch ${err}, arrayBuffer byteLength: ${arraybuffer ? arraybuffer.byteLength : 0}`));
226
+ });
227
+ }
228
+ });
229
+ this.decodeAudioPromiseMap[name] = promise;
230
+ return promise;
231
+ }
232
+ };
233
+ SoundSystem = __decorate([
234
+ decorators.componentObserver({
235
+ Sound: [],
236
+ })
237
+ ], SoundSystem);
238
+ var SoundSystem_default = SoundSystem;
239
+
240
+ class Sound extends Component {
241
+ constructor() {
242
+ super(...arguments);
243
+ this.state = 'unloaded';
244
+ this.config = {
245
+ resource: '',
246
+ autoplay: false,
247
+ muted: false,
248
+ volume: 1,
249
+ loop: false,
250
+ seek: 0,
251
+ };
252
+ this.playTime = 0;
253
+ this.startTime = 0;
254
+ this.duration = 0;
255
+ this.actionQueue = [];
256
+ }
257
+ static { this.componentName = 'Sound'; }
258
+ get muted() {
259
+ return this.gainNode ? this.gainNode.gain.value === 0 : false;
260
+ }
261
+ set muted(v) {
262
+ if (!this.gainNode) {
263
+ return;
264
+ }
265
+ this.gainNode.gain.setValueAtTime(v ? 0 : this.config.volume, 0);
266
+ }
267
+ get volume() {
268
+ return this.gainNode ? this.gainNode.gain.value : 1;
269
+ }
270
+ set volume(v) {
271
+ if (typeof v !== 'number' || v < 0 || v > 1) {
272
+ return;
273
+ }
274
+ this.config.volume = v;
275
+ if (!this.gainNode) {
276
+ return;
277
+ }
278
+ this.gainNode.gain.setValueAtTime(v, 0);
279
+ }
280
+ init(obj) {
281
+ if (!obj) {
282
+ return;
283
+ }
284
+ Object.assign(this.config, obj);
285
+ if (this.config.autoplay) {
286
+ this.actionQueue.push(this.play.bind(this));
287
+ }
288
+ }
289
+ play() {
290
+ if (this.state !== 'loaded') {
291
+ this.actionQueue.push(this.play.bind(this));
292
+ }
293
+ this.destroySource();
294
+ this.createSource();
295
+ if (!this.sourceNode) {
296
+ return;
297
+ }
298
+ const when = this.systemContext.currentTime;
299
+ const offset = this.config.seek;
300
+ const duration = this.config.duration;
301
+ this.sourceNode.start(0, offset, duration);
302
+ this.startTime = when;
303
+ this.playTime = when - offset;
304
+ this.paused = false;
305
+ this.playing = true;
306
+ this.resetConfig();
307
+ this.endedListener = () => {
308
+ if (!this.sourceNode) {
309
+ return;
310
+ }
311
+ if (this.config.onEnd) {
312
+ this.config.onEnd();
313
+ }
314
+ // 非交互事件播放完成需要销毁资源
315
+ if (this.playing) {
316
+ this.destroySource();
317
+ }
318
+ };
319
+ this.sourceNode.addEventListener('ended', this.endedListener);
320
+ }
321
+ pause() {
322
+ if (this.state !== 'loaded') {
323
+ this.actionQueue.push(this.pause.bind(this));
324
+ }
325
+ if (this.paused || !this.playing) {
326
+ return;
327
+ }
328
+ this.paused = true;
329
+ this.playing = false;
330
+ this.config.seek = this.getCurrentTime();
331
+ this.destroySource();
332
+ }
333
+ stop() {
334
+ if (this.state !== 'loaded') {
335
+ this.actionQueue.push(this.stop.bind(this));
336
+ }
337
+ if (!this.paused && !this.playing) {
338
+ return;
339
+ }
340
+ this.playing = false;
341
+ this.paused = false;
342
+ this.destroySource();
343
+ this.resetConfig();
344
+ }
345
+ onload(buffer) {
346
+ this.state = 'loaded';
347
+ this.buffer = buffer;
348
+ this.duration = this.buffer.duration;
349
+ this.actionQueue.forEach(action => action());
350
+ this.actionQueue.length = 0;
351
+ }
352
+ onDestroy() {
353
+ this.actionQueue.length = 0;
354
+ this.destroySource();
355
+ }
356
+ resetConfig() {
357
+ this.config.seek = 0;
358
+ }
359
+ getCurrentTime() {
360
+ if (this.config.loop && this.duration > 0) {
361
+ return (this.systemContext.currentTime - this.playTime) % this.duration;
362
+ }
363
+ return this.systemContext.currentTime - this.playTime;
364
+ }
365
+ createSource() {
366
+ if (!this.systemContext || this.state !== 'loaded') {
367
+ return;
368
+ }
369
+ this.sourceNode = this.systemContext.createBufferSource();
370
+ this.sourceNode.buffer = this.buffer;
371
+ this.sourceNode.loop = this.config.loop;
372
+ if (!this.gainNode) {
373
+ this.gainNode = this.systemContext.createGain();
374
+ this.gainNode.connect(this.systemDestination);
375
+ Object.assign(this, this.config);
376
+ }
377
+ this.sourceNode.connect(this.gainNode);
378
+ }
379
+ destroySource() {
380
+ if (!this.sourceNode)
381
+ return;
382
+ this.sourceNode.removeEventListener('ended', this.endedListener);
383
+ this.sourceNode.stop();
384
+ this.sourceNode.disconnect();
385
+ this.sourceNode = null;
386
+ this.startTime = 0;
387
+ this.playTime = 0;
388
+ this.playing = false;
389
+ }
390
+ }
391
+
392
+ export { Sound, SoundSystem_default as SoundSystem };
393
+ //# sourceMappingURL=plugin-sound.esm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin-sound.esm.js","sources":["../lib/SoundSystem.ts","../lib/Sound.ts"],"sourcesContent":["import { System, decorators, ComponentChanged, OBSERVER_TYPE, resource } from '@combos-fun/engine';\nimport SoundComponent from './Sound';\n\ninterface SoundSystemParams {\n autoPauseAndStart?: boolean;\n onError: (error: any) => void;\n}\n\n@decorators.componentObserver({\n Sound: [],\n})\nclass SoundSystem extends System {\n static systemName = 'SoundSystem';\n\n private ctx: AudioContext;\n\n private gainNode: GainNode;\n\n /** 是否和游戏同步暂停和启动 */\n private autoPauseAndStart = true;\n\n private onError: (error: any) => void;\n\n private components: SoundComponent[] = [];\n\n private pausedComponents: SoundComponent[] = [];\n\n private audioBufferCache = {};\n\n private decodeAudioPromiseMap = {};\n\n get muted(): boolean {\n return this.gainNode ? this.gainNode.gain.value === 0 : false;\n }\n\n set muted(v: boolean) {\n if (!this.gainNode) {\n return;\n }\n this.gainNode.gain.setValueAtTime(v ? 0 : 1, 0);\n }\n\n get volume(): number {\n return this.gainNode ? this.gainNode.gain.value : 1;\n }\n\n set volume(v: number) {\n if (!this.gainNode || typeof v !== 'number' || v < 0 || v > 1) {\n return;\n }\n this.gainNode.gain.setValueAtTime(v, 0);\n }\n\n get audioLocked(): boolean {\n if (!this.ctx) {\n return true;\n }\n return this.ctx.state !== 'running';\n }\n\n constructor(obj?: SoundSystemParams) {\n super();\n Object.assign(this, obj);\n }\n\n /**\n * 恢复播放所有被暂停的音频\n */\n resumeAll() {\n const handleResume = () => {\n this.pausedComponents.forEach(component => {\n component.play();\n });\n // 清理之前缓存的暂停列表\n this.pausedComponents = [];\n };\n this.ctx.resume().then(handleResume, handleResume);\n }\n\n /**\n * 暂停所有正在播放的音频\n */\n pauseAll() {\n this.components.forEach(component => {\n if (component.playing) {\n this.pausedComponents.push(component);\n component.pause();\n }\n });\n this.ctx.suspend().then();\n }\n\n /**\n * 停止所有正在播放的音频\n */\n stopAll() {\n this.components.forEach(component => {\n if (component.playing) {\n component.stop();\n }\n });\n // 清理之前缓存的暂停列表\n this.pausedComponents = [];\n this.ctx.suspend().then();\n }\n\n /**\n * System 初始化用,可以配置参数,游戏未开始\n *\n * System init, set params, game is not begain\n */\n init() {\n this.setupAudioContext();\n }\n\n update() {\n const changes = this.componentObserver.clear();\n for (const changed of changes) {\n this.componentChanged(changed);\n }\n }\n\n /**\n * 游戏开始和游戏暂停后开始播放的时候调用。\n *\n * Called while the game to play when game pause.\n */\n onResume() {\n if (!this.autoPauseAndStart) {\n return;\n }\n this.resumeAll();\n }\n\n /**\n * 游戏暂停的时候调用。\n *\n * Called while the game paused.\n */\n onPause() {\n if (!this.autoPauseAndStart) {\n return;\n }\n this.pauseAll();\n }\n\n /**\n * System 被销毁的时候调用。\n * Called while the system be destroyed.\n */\n onDestroy() {\n this.components.forEach(component => {\n component.onDestroy();\n });\n this.components = [];\n if (this.ctx) {\n this.gainNode.disconnect();\n this.gainNode = null;\n this.ctx.close();\n this.ctx = null;\n }\n }\n\n async componentChanged(changed: ComponentChanged) {\n if (changed.componentName !== 'Sound') return;\n\n if (changed.type === OBSERVER_TYPE.ADD) {\n this.add(changed);\n }\n }\n\n private setupAudioContext() {\n try {\n const AudioContext = window.AudioContext || (window as any).webkitAudioContext;\n this.ctx = new AudioContext();\n } catch (error) {\n console.error(error);\n if (this.onError) {\n this.onError(error);\n }\n }\n\n if (!this.ctx) {\n return;\n }\n this.gainNode =\n typeof this.ctx.createGain === 'undefined' ? (this.ctx as any).createGainNode() : this.ctx.createGain();\n this.gainNode.gain.setValueAtTime(this.muted ? 0 : this.volume, this.ctx.currentTime);\n this.gainNode.connect(this.ctx.destination);\n this.unlockAudio();\n }\n\n private unlockAudio() {\n if (!this.ctx || !this.audioLocked) {\n return;\n }\n\n const unlock = () => {\n if (this.ctx) {\n const removeListenerFn = () => {\n document.body.removeEventListener('touchstart', unlock);\n document.body.removeEventListener('touchend', unlock);\n document.body.removeEventListener('click', unlock);\n };\n this.ctx.resume().then(removeListenerFn, removeListenerFn);\n }\n };\n document.body.addEventListener('touchstart', unlock);\n document.body.addEventListener('touchend', unlock);\n document.body.addEventListener('click', unlock);\n }\n\n private async add(changed: ComponentChanged) {\n const component = changed.component as SoundComponent;\n this.components.push(component);\n try {\n const { config } = component;\n component.state = 'loading';\n\n const audio = await resource.getResource(config.resource);\n if (!this.audioBufferCache[audio.name] && audio?.data?.audio) {\n this.audioBufferCache[audio.name] = await this.decodeAudioData(audio.data.audio, audio.name);\n }\n if (this.audioBufferCache[audio.name]) {\n component.systemContext = this.ctx;\n component.systemDestination = this.gainNode;\n component.onload(this.audioBufferCache[audio.name]);\n }\n } catch (error) {\n if (this.onError) {\n this.onError(error);\n }\n }\n }\n\n private decodeAudioData(arraybuffer: ArrayBuffer, name: string) {\n if (this.decodeAudioPromiseMap[name]) {\n return this.decodeAudioPromiseMap[name];\n }\n\n const promise = new Promise<AudioBuffer>((resolve, reject) => {\n if (!this.ctx) {\n reject(new Error('No audio support'));\n }\n\n const success = (decodedData: AudioBuffer) => {\n if (this.decodeAudioPromiseMap[name]) {\n delete this.decodeAudioPromiseMap[name];\n }\n if (decodedData) {\n resolve(decodedData);\n } else {\n reject(new Error(`Error decoding audio ${name}`));\n }\n };\n\n const error = (err: DOMException) => {\n if (this.decodeAudioPromiseMap[name]) {\n delete this.decodeAudioPromiseMap[name];\n }\n reject(new Error(`${err}. arrayBuffer byteLength: ${arraybuffer ? arraybuffer.byteLength : 0}`));\n };\n\n const promise = this.ctx.decodeAudioData(arraybuffer, success, error)\n if (promise instanceof Promise) {\n promise.catch((err) => {\n reject(new Error(`catch ${err}, arrayBuffer byteLength: ${arraybuffer ? arraybuffer.byteLength : 0}`));\n });\n }\n });\n\n this.decodeAudioPromiseMap[name] = promise;\n return promise;\n }\n}\n\nexport default SoundSystem;\n","import { Component } from '@combos-fun/engine';\n\nexport interface SoundParams {\n resource: string;\n autoplay?: boolean;\n muted?: boolean;\n volume?: number;\n loop?: boolean;\n seek?: number;\n duration?: number;\n onEnd?: () => void;\n}\n\nclass Sound extends Component<SoundParams> {\n static componentName = 'Sound';\n\n systemContext: AudioContext;\n\n systemDestination: GainNode;\n\n playing: boolean;\n\n state: 'unloaded' | 'loading' | 'loaded' = 'unloaded';\n\n config: SoundParams = {\n resource: '',\n autoplay: false,\n muted: false,\n volume: 1,\n loop: false,\n seek: 0,\n };\n\n private buffer: AudioBuffer;\n\n private sourceNode: AudioBufferSourceNode;\n\n private gainNode: GainNode;\n\n private paused: boolean;\n\n private playTime: number = 0;\n\n private startTime: number = 0;\n\n private duration: number = 0;\n\n private actionQueue: (() => void)[] = [];\n\n private endedListener: () => void;\n\n get muted(): boolean {\n return this.gainNode ? this.gainNode.gain.value === 0 : false;\n }\n\n set muted(v: boolean) {\n if (!this.gainNode) {\n return;\n }\n this.gainNode.gain.setValueAtTime(v ? 0 : this.config.volume, 0);\n }\n\n get volume(): number {\n return this.gainNode ? this.gainNode.gain.value : 1;\n }\n\n set volume(v: number) {\n if (typeof v !== 'number' || v < 0 || v > 1) {\n return;\n }\n this.config.volume = v;\n if (!this.gainNode) {\n return;\n }\n this.gainNode.gain.setValueAtTime(v, 0);\n }\n\n init(obj?: SoundParams) {\n if (!obj) {\n return;\n }\n\n Object.assign(this.config, obj);\n if (this.config.autoplay) {\n this.actionQueue.push(this.play.bind(this));\n }\n }\n\n play() {\n if (this.state !== 'loaded') {\n this.actionQueue.push(this.play.bind(this));\n }\n this.destroySource();\n this.createSource();\n\n if (!this.sourceNode) {\n return;\n }\n const when = this.systemContext.currentTime;\n const offset = this.config.seek;\n const duration = this.config.duration;\n\n this.sourceNode.start(0, offset, duration);\n\n this.startTime = when;\n this.playTime = when - offset;\n this.paused = false;\n this.playing = true;\n this.resetConfig();\n this.endedListener = () => {\n if (!this.sourceNode) {\n return;\n }\n if (this.config.onEnd) {\n this.config.onEnd();\n }\n // 非交互事件播放完成需要销毁资源\n if (this.playing) {\n this.destroySource();\n }\n };\n this.sourceNode.addEventListener('ended', this.endedListener);\n }\n\n pause() {\n if (this.state !== 'loaded') {\n this.actionQueue.push(this.pause.bind(this));\n }\n if (this.paused || !this.playing) {\n return;\n }\n this.paused = true;\n this.playing = false;\n this.config.seek = this.getCurrentTime();\n this.destroySource();\n }\n\n stop() {\n if (this.state !== 'loaded') {\n this.actionQueue.push(this.stop.bind(this));\n }\n if (!this.paused && !this.playing) {\n return;\n }\n this.playing = false;\n this.paused = false;\n this.destroySource();\n this.resetConfig();\n }\n\n onload(buffer: AudioBuffer) {\n this.state = 'loaded';\n this.buffer = buffer;\n this.duration = this.buffer.duration;\n this.actionQueue.forEach(action => action());\n this.actionQueue.length = 0;\n }\n\n onDestroy() {\n this.actionQueue.length = 0;\n this.destroySource();\n }\n\n private resetConfig() {\n this.config.seek = 0;\n }\n\n private getCurrentTime() {\n if (this.config.loop && this.duration > 0) {\n return (this.systemContext.currentTime - this.playTime) % this.duration;\n }\n\n return this.systemContext.currentTime - this.playTime;\n }\n\n private createSource() {\n if (!this.systemContext || this.state !== 'loaded') {\n return;\n }\n this.sourceNode = this.systemContext.createBufferSource();\n this.sourceNode.buffer = this.buffer;\n this.sourceNode.loop = this.config.loop;\n\n if (!this.gainNode) {\n this.gainNode = this.systemContext.createGain();\n this.gainNode.connect(this.systemDestination);\n Object.assign(this, this.config);\n }\n this.sourceNode.connect(this.gainNode);\n }\n\n private destroySource() {\n if (!this.sourceNode) return;\n this.sourceNode.removeEventListener('ended', this.endedListener);\n this.sourceNode.stop();\n this.sourceNode.disconnect();\n this.sourceNode = null;\n\n this.startTime = 0;\n this.playTime = 0;\n this.playing = false;\n }\n}\n\nexport default Sound;\n"],"names":[],"mappings":";;;AAWA,IAAM,WAAW,GAAjB,MAAM,WAAY,SAAQ,MAAM,CAAA;aACvB,IAAA,CAAA,UAAU,GAAG,aAAH,CAAiB;AAmBlC,IAAA,IAAI,KAAK,GAAA;AACP,QAAA,OAAO,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,KAAK,CAAC,GAAG,KAAK;IAC/D;IAEA,IAAI,KAAK,CAAC,CAAU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AAC,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI;AAEvC,QAAA,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE;YAClB,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,aAAa,CAAC,UAAU,EAAE;YAC/C,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,iBAAiB,CAAC;YAC7C,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,MAAM,CAAC;QAClC;QACA,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC;IACxC;IAEQ,aAAa,GAAA;QACnB,IAAI,CAAC,IAAI,CAAC,UAAU;YAAE;QACtB,IAAI,CAAC,UAAU,CAAC,mBAAmB,CAAC,OAAO,EAAE,IAAI,CAAC,aAAa,CAAC;AAChE,QAAA,IAAI,CAAC,UAAU,CAAC,IAAI,EAAE;AACtB,QAAA,IAAI,CAAC,UAAU,CAAC,UAAU,EAAE;AAC5B,QAAA,IAAI,CAAC,UAAU,GAAG,IAAI;AAEtB,QAAA,IAAI,CAAC,SAAS,GAAG,CAAC;AAClB,QAAA,IAAI,CAAC,QAAQ,GAAG,CAAC;AACjB,QAAA,IAAI,CAAC,OAAO,GAAG,KAAK;IACtB;;;;;"}
package/index.js ADDED
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ if (process.env.NODE_ENV === 'production') {
4
+ module.exports = require('./dist/plugin-sound.cjs.prod.js');
5
+ } else {
6
+ module.exports = require('./dist/plugin-sound.cjs.js');
7
+ }
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "@combos-fun/plugin-sound",
3
+ "version": "0.0.1",
4
+ "description": "@combos-fun/plugin-sound",
5
+ "main": "index.js",
6
+ "module": "dist/plugin-sound.esm.js",
7
+ "bundle": "CombosFun.plugin.sound",
8
+ "unpkg": "dist/CombosFun.plugin.sound.min.js",
9
+ "files": [
10
+ "index.js",
11
+ "dist"
12
+ ],
13
+ "types": "dist/plugin-sound.d.ts",
14
+ "keywords": [
15
+ "combos-fun",
16
+ "game"
17
+ ],
18
+ "author": "sun668 <q947692259@gmail.com>",
19
+ "dependencies": {
20
+ "eventemitter3": "^5.0.4",
21
+ "@combos-fun/engine": "0.0.1"
22
+ },
23
+ "scripts": {
24
+ "build": "node ../../scripts/build-package.mjs"
25
+ }
26
+ }