@next2d/media 3.11.1 → 3.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +91 -0
- package/package.json +5 -5
- package/src/Sound/StreamSound.d.ts +33 -0
- package/src/Sound/StreamSound.js +317 -0
- package/src/Sound/StreamSoundRegistry.d.ts +5 -0
- package/src/Sound/StreamSoundRegistry.js +34 -0
- package/src/Sound.d.ts +7 -1
- package/src/Sound.js +49 -1
- package/src/SoundMixer/service/SoundMixerStopAllService.js +2 -0
- package/src/SoundMixer/service/SoundMixerUpdateVolumeService.js +2 -0
- package/src/index.d.ts +1 -0
- package/src/interface/ISoundOptions.d.ts +4 -0
- package/src/interface/ISoundOptions.js +1 -0
package/README.md
CHANGED
|
@@ -353,3 +353,94 @@ This package requires:
|
|
|
353
353
|
## License
|
|
354
354
|
|
|
355
355
|
This project is licensed under the [MIT License](https://opensource.org/licenses/MIT) - see the [LICENSE](LICENSE) file for details.
|
|
356
|
+
|
|
357
|
+
## Opt-in streaming BGM / ストリーミングBGM
|
|
358
|
+
|
|
359
|
+
`new Sound()` and `new Sound({ mode: "buffer" })` keep the existing AudioBuffer
|
|
360
|
+
backend, including its events, `play(start_time)`, `audioBuffer`, volume rules,
|
|
361
|
+
MovieClip integration and clone behavior. Only an explicit `mode: "stream"`
|
|
362
|
+
selects the new backend; file duration and loop count never switch it automatically.
|
|
363
|
+
|
|
364
|
+
`new Sound()` は従来の動作を維持します。長いBGMだけ、生成時に `mode: "stream"`
|
|
365
|
+
を指定してください。modeは生成後に変更できません。
|
|
366
|
+
|
|
367
|
+
```ts
|
|
368
|
+
import { Sound, SoundMixer } from "@next2d/media";
|
|
369
|
+
import { URLRequest } from "@next2d/net";
|
|
370
|
+
|
|
371
|
+
const bgm = new Sound({ mode: "stream" });
|
|
372
|
+
await bgm.load(new URLRequest("/sounds/bgm.mp3"));
|
|
373
|
+
bgm.loopCount = Infinity;
|
|
374
|
+
bgm.volume = 0.5;
|
|
375
|
+
bgm.play();
|
|
376
|
+
|
|
377
|
+
bgm.volume = 0.2; // Live GainNode adjustment / 再生中も即時反映
|
|
378
|
+
bgm.stop(); // Releases media data; play() can restart the same sound
|
|
379
|
+
bgm.play();
|
|
380
|
+
SoundMixer.stopAll(); // Includes streams as well as existing sounds/videos
|
|
381
|
+
bgm.dispose(); // Releases owned resources; load() again before reuse
|
|
382
|
+
|
|
383
|
+
const se = new Sound(); // Existing SE API, unchanged
|
|
384
|
+
await se.load(new URLRequest("/sounds/button.mp3"));
|
|
385
|
+
se.play();
|
|
386
|
+
```
|
|
387
|
+
|
|
388
|
+
### Stream-specific contract / stream専用の仕様
|
|
389
|
+
|
|
390
|
+
- BGM plays through `HTMLAudioElement → MediaElementAudioSourceNode → GainNode`.
|
|
391
|
+
The AudioContext is shared with existing sounds. Element volume stays at 1.
|
|
392
|
+
`Sound.volume` and `SoundMixer.volume` retain the existing min/overwrite rules;
|
|
393
|
+
they are not multiplied. Inactive streams receive the current mixer limit on play.
|
|
394
|
+
- The implementation does not call `decodeAudioData()` or fetch the entire BGM
|
|
395
|
+
into an ArrayBuffer/Blob. Browser buffering still consumes memory. Serve long
|
|
396
|
+
tracks as separate audio URLs rather than embedding them in JavaScript/data URLs.
|
|
397
|
+
- `load()` resolves and dispatches `Event.COMPLETE` when metadata is available,
|
|
398
|
+
**not** when the complete file has downloaded. It dispatches `Event.OPEN` at
|
|
399
|
+
load start. Byte-based `ProgressEvent.PROGRESS` is unavailable in stream mode.
|
|
400
|
+
`preload="metadata"` is a browser hint, not a strict download limit.
|
|
401
|
+
- Only GET URLs without `request.data` or custom `request.requestHeaders` are
|
|
402
|
+
supported. Unsupported requests reject with `TypeError`. `withCredentials`
|
|
403
|
+
selects `crossOrigin="use-credentials"`; otherwise `"anonymous"` is used.
|
|
404
|
+
Cross-origin servers must allow the relevant CORS request. There is no automatic
|
|
405
|
+
fallback to full-file decoding. Load errors reject and emit `IOErrorEvent.IO_ERROR`.
|
|
406
|
+
- `play()` starts at the beginning. Nonzero `play(start_time)` throws `RangeError`
|
|
407
|
+
in stream mode; scheduled starts remain a buffer-only feature. Load before play.
|
|
408
|
+
- Set `loopCount` before play: 0 plays once, N adds N repetitions, and `Infinity`
|
|
409
|
+
uses native media looping. Finite playback emits one `Event.COMPLETE` at its
|
|
410
|
+
final end. Stop/dispose and intermediate loops do not emit completion.
|
|
411
|
+
Sample-accurate gapless looping is not guaranteed by HTML media playback.
|
|
412
|
+
- Autoplay-blocked streams retry on pointer/touch/keyboard gestures. The shared
|
|
413
|
+
context can also resume after an interruption. Stopped/disposed sounds never
|
|
414
|
+
retry. Non-autoplay playback failures stop the stream and emit `IO_ERROR`.
|
|
415
|
+
- `stop()` cancels an outstanding metadata load with `AbortError`, disconnects the
|
|
416
|
+
graph and clears the media source to release inactive data. A successfully
|
|
417
|
+
loaded stream retains its URL and reuses its element/source node on replay.
|
|
418
|
+
After cancelling an unfinished load, call `load()` again before playing.
|
|
419
|
+
- `clone()` copies a loaded stream's URL, credential mode, volume and loop count.
|
|
420
|
+
The clone owns a separate media element/graph, allocated on play. No global
|
|
421
|
+
unbounded URL cache is used. Reuse BGM instances or dispose them when unused.
|
|
422
|
+
- `audioBuffer` and character `$build()` are buffer-only inputs; stream never uses
|
|
423
|
+
`audioBuffer`, and `$build()` rejects in stream mode. Embedded MovieClip sounds
|
|
424
|
+
continue to use the default buffer backend.
|
|
425
|
+
- `dispose()` disconnects/releases owned resources and clears `audioBuffer`.
|
|
426
|
+
Shared AudioBuffers belonging to other clones are unaffected. The Sound can be
|
|
427
|
+
explicitly initialized again with `load()`; its mode remains unchanged.
|
|
428
|
+
|
|
429
|
+
streamでは、`load()`の完了はメタデータ取得完了を表します。全曲のデコードを避け、
|
|
430
|
+
停止時にメディアの読み込みを解除します。既存bufferのループ処理・イベント・
|
|
431
|
+
SoundMixerの音量計算や停止処理の仕様変更は、この機能追加には含めていません。
|
|
432
|
+
音量の永続化やBGM/SEの設定値の管理は、アプリケーション側で行ってください。
|
|
433
|
+
|
|
434
|
+
### Validation / 検証
|
|
435
|
+
|
|
436
|
+
```bash
|
|
437
|
+
npx vitest run packages/media
|
|
438
|
+
npx playwright install chromium webkit
|
|
439
|
+
npx playwright test --config=e2e/media.config.ts
|
|
440
|
+
```
|
|
441
|
+
|
|
442
|
+
The dedicated browser tests use generated WAV fixtures to verify real output
|
|
443
|
+
attenuation, loops, replay/release, clone/mixer behavior and unchanged buffer
|
|
444
|
+
playback in Chromium/WebKit. For shipping iOS apps, additionally measure the
|
|
445
|
+
WebContent process during long-track playback and repeated scene transitions
|
|
446
|
+
on the target devices (JavaScript heap size alone omits native audio buffers).
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@next2d/media",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.12.0",
|
|
4
4
|
"description": "Next2D Media Package",
|
|
5
5
|
"author": "Toshiyuki Ienaga<ienaga@next2d.app> (https://github.com/ienaga/)",
|
|
6
6
|
"license": "MIT",
|
|
@@ -24,9 +24,9 @@
|
|
|
24
24
|
"url": "git+https://github.com/Next2D/Player.git"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@next2d/events": "3.
|
|
28
|
-
"@next2d/net": "3.
|
|
29
|
-
"@next2d/display": "3.
|
|
30
|
-
"@next2d/geom": "3.
|
|
27
|
+
"@next2d/events": "3.12.0",
|
|
28
|
+
"@next2d/net": "3.12.0",
|
|
29
|
+
"@next2d/display": "3.12.0",
|
|
30
|
+
"@next2d/geom": "3.12.0"
|
|
31
31
|
}
|
|
32
32
|
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { Sound } from "../Sound";
|
|
2
|
+
import type { URLRequest } from "@next2d/net";
|
|
3
|
+
/** Internal, opt-in backend. Never fetches/decodeAudioData()s a complete BGM into JS memory. */
|
|
4
|
+
export declare class StreamSound {
|
|
5
|
+
readonly sound: Sound;
|
|
6
|
+
private element;
|
|
7
|
+
private source;
|
|
8
|
+
private gain;
|
|
9
|
+
private url;
|
|
10
|
+
private credentials;
|
|
11
|
+
private ready;
|
|
12
|
+
private active;
|
|
13
|
+
private count;
|
|
14
|
+
private generation;
|
|
15
|
+
private playPending;
|
|
16
|
+
private cancelLoad;
|
|
17
|
+
private removePlaybackEvents;
|
|
18
|
+
constructor(sound: Sound);
|
|
19
|
+
get canLoop(): boolean;
|
|
20
|
+
setVolume(volume: number): void;
|
|
21
|
+
private getElement;
|
|
22
|
+
private restoreSource;
|
|
23
|
+
private emitError;
|
|
24
|
+
/** Resolves at metadata readiness, not full download. Byte progress is unavailable. */
|
|
25
|
+
load(request: URLRequest): Promise<void>;
|
|
26
|
+
play(start_time: number): void;
|
|
27
|
+
/** Both resume() and play() must be invoked synchronously inside a user gesture. */
|
|
28
|
+
retry(): void;
|
|
29
|
+
stop(): void;
|
|
30
|
+
cloneTo(target: StreamSound): void;
|
|
31
|
+
/** Release references as well as media data. load() can initialize this instance again. */
|
|
32
|
+
dispose(): void;
|
|
33
|
+
}
|
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
import { Event, IOErrorEvent } from "@next2d/events";
|
|
2
|
+
import { $getAudioContext, $getVolume } from "../MediaUtil";
|
|
3
|
+
import { register, unregister } from "./StreamSoundRegistry";
|
|
4
|
+
/** Internal, opt-in backend. Never fetches/decodeAudioData()s a complete BGM into JS memory. */
|
|
5
|
+
export class StreamSound {
|
|
6
|
+
constructor(sound) {
|
|
7
|
+
Object.defineProperty(this, "sound", {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
configurable: true,
|
|
10
|
+
writable: true,
|
|
11
|
+
value: sound
|
|
12
|
+
});
|
|
13
|
+
Object.defineProperty(this, "element", {
|
|
14
|
+
enumerable: true,
|
|
15
|
+
configurable: true,
|
|
16
|
+
writable: true,
|
|
17
|
+
value: null
|
|
18
|
+
});
|
|
19
|
+
Object.defineProperty(this, "source", {
|
|
20
|
+
enumerable: true,
|
|
21
|
+
configurable: true,
|
|
22
|
+
writable: true,
|
|
23
|
+
value: null
|
|
24
|
+
});
|
|
25
|
+
Object.defineProperty(this, "gain", {
|
|
26
|
+
enumerable: true,
|
|
27
|
+
configurable: true,
|
|
28
|
+
writable: true,
|
|
29
|
+
value: null
|
|
30
|
+
});
|
|
31
|
+
Object.defineProperty(this, "url", {
|
|
32
|
+
enumerable: true,
|
|
33
|
+
configurable: true,
|
|
34
|
+
writable: true,
|
|
35
|
+
value: null
|
|
36
|
+
});
|
|
37
|
+
Object.defineProperty(this, "credentials", {
|
|
38
|
+
enumerable: true,
|
|
39
|
+
configurable: true,
|
|
40
|
+
writable: true,
|
|
41
|
+
value: false
|
|
42
|
+
});
|
|
43
|
+
Object.defineProperty(this, "ready", {
|
|
44
|
+
enumerable: true,
|
|
45
|
+
configurable: true,
|
|
46
|
+
writable: true,
|
|
47
|
+
value: false
|
|
48
|
+
});
|
|
49
|
+
Object.defineProperty(this, "active", {
|
|
50
|
+
enumerable: true,
|
|
51
|
+
configurable: true,
|
|
52
|
+
writable: true,
|
|
53
|
+
value: false
|
|
54
|
+
});
|
|
55
|
+
Object.defineProperty(this, "count", {
|
|
56
|
+
enumerable: true,
|
|
57
|
+
configurable: true,
|
|
58
|
+
writable: true,
|
|
59
|
+
value: 0
|
|
60
|
+
});
|
|
61
|
+
Object.defineProperty(this, "generation", {
|
|
62
|
+
enumerable: true,
|
|
63
|
+
configurable: true,
|
|
64
|
+
writable: true,
|
|
65
|
+
value: 0
|
|
66
|
+
});
|
|
67
|
+
Object.defineProperty(this, "playPending", {
|
|
68
|
+
enumerable: true,
|
|
69
|
+
configurable: true,
|
|
70
|
+
writable: true,
|
|
71
|
+
value: false
|
|
72
|
+
});
|
|
73
|
+
Object.defineProperty(this, "cancelLoad", {
|
|
74
|
+
enumerable: true,
|
|
75
|
+
configurable: true,
|
|
76
|
+
writable: true,
|
|
77
|
+
value: null
|
|
78
|
+
});
|
|
79
|
+
Object.defineProperty(this, "removePlaybackEvents", {
|
|
80
|
+
enumerable: true,
|
|
81
|
+
configurable: true,
|
|
82
|
+
writable: true,
|
|
83
|
+
value: null
|
|
84
|
+
});
|
|
85
|
+
// Browser resources are allocated only by load()/play().
|
|
86
|
+
}
|
|
87
|
+
get canLoop() {
|
|
88
|
+
return this.active && this.sound.loopCount >= this.count;
|
|
89
|
+
}
|
|
90
|
+
setVolume(volume) {
|
|
91
|
+
if (this.gain) {
|
|
92
|
+
this.gain.gain.value = volume;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
getElement() {
|
|
96
|
+
if (!this.element) {
|
|
97
|
+
this.element = new Audio();
|
|
98
|
+
this.element.preload = "none";
|
|
99
|
+
this.element.volume = 1;
|
|
100
|
+
}
|
|
101
|
+
return this.element;
|
|
102
|
+
}
|
|
103
|
+
restoreSource() {
|
|
104
|
+
const element = this.getElement();
|
|
105
|
+
if (!element.getAttribute("src") && this.url) {
|
|
106
|
+
// Must be assigned before src, including when cloning/replaying a stopped stream.
|
|
107
|
+
element.crossOrigin = this.credentials ? "use-credentials" : "anonymous";
|
|
108
|
+
element.src = this.url;
|
|
109
|
+
}
|
|
110
|
+
return element;
|
|
111
|
+
}
|
|
112
|
+
emitError(error) {
|
|
113
|
+
if (this.sound.willTrigger(IOErrorEvent.IO_ERROR)) {
|
|
114
|
+
this.sound.dispatchEvent(new IOErrorEvent(IOErrorEvent.IO_ERROR, false, error instanceof Error ? error.message : String(error)));
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
/** Resolves at metadata readiness, not full download. Byte progress is unavailable. */
|
|
118
|
+
async load(request) {
|
|
119
|
+
// Do not silently replace a streaming request with a full-file fetch/blob fallback.
|
|
120
|
+
if (request.method.toUpperCase() !== "GET" || request.requestHeaders.length || request.data !== null) {
|
|
121
|
+
throw new TypeError("Stream Sound requires a GET URL without requestHeaders or data.");
|
|
122
|
+
}
|
|
123
|
+
if (!request.url) {
|
|
124
|
+
throw new TypeError("Stream Sound requires a non-empty URL.");
|
|
125
|
+
}
|
|
126
|
+
this.stop();
|
|
127
|
+
this.ready = false;
|
|
128
|
+
this.url = request.url;
|
|
129
|
+
this.credentials = request.withCredentials;
|
|
130
|
+
const element = this.getElement();
|
|
131
|
+
const generation = this.generation;
|
|
132
|
+
await new Promise((resolve, reject) => {
|
|
133
|
+
const cleanup = () => {
|
|
134
|
+
element.removeEventListener("loadstart", opened);
|
|
135
|
+
element.removeEventListener("loadedmetadata", loaded);
|
|
136
|
+
element.removeEventListener("error", failed);
|
|
137
|
+
element.removeEventListener("abort", aborted);
|
|
138
|
+
this.cancelLoad = null;
|
|
139
|
+
};
|
|
140
|
+
const opened = () => {
|
|
141
|
+
if (generation === this.generation && this.sound.willTrigger(Event.OPEN)) {
|
|
142
|
+
this.sound.dispatchEvent(new Event(Event.OPEN));
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
const loaded = () => {
|
|
146
|
+
if (generation !== this.generation) {
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
cleanup();
|
|
150
|
+
this.ready = true;
|
|
151
|
+
resolve();
|
|
152
|
+
if (this.sound.willTrigger(Event.COMPLETE)) {
|
|
153
|
+
this.sound.dispatchEvent(new Event(Event.COMPLETE));
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
const failed = () => {
|
|
157
|
+
if (generation !== this.generation) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
const error = new Error(element.error?.message || "Stream Sound could not load metadata.");
|
|
161
|
+
cleanup();
|
|
162
|
+
this.stop();
|
|
163
|
+
reject(error);
|
|
164
|
+
this.emitError(error);
|
|
165
|
+
};
|
|
166
|
+
const aborted = () => {
|
|
167
|
+
if (generation !== this.generation) {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
this.stop();
|
|
171
|
+
};
|
|
172
|
+
this.cancelLoad = () => {
|
|
173
|
+
cleanup();
|
|
174
|
+
reject(new DOMException("Stream Sound loading was cancelled.", "AbortError"));
|
|
175
|
+
};
|
|
176
|
+
element.addEventListener("loadstart", opened);
|
|
177
|
+
element.addEventListener("loadedmetadata", loaded);
|
|
178
|
+
element.addEventListener("error", failed);
|
|
179
|
+
element.addEventListener("abort", aborted);
|
|
180
|
+
try {
|
|
181
|
+
element.preload = "metadata";
|
|
182
|
+
this.restoreSource();
|
|
183
|
+
element.load();
|
|
184
|
+
}
|
|
185
|
+
catch (error) {
|
|
186
|
+
cleanup();
|
|
187
|
+
this.stop();
|
|
188
|
+
reject(error);
|
|
189
|
+
this.emitError(error);
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
play(start_time) {
|
|
194
|
+
if (start_time !== 0) {
|
|
195
|
+
throw new RangeError("Stream Sound supports play() only; scheduled start_time requires buffer mode.");
|
|
196
|
+
}
|
|
197
|
+
if (this.active || !this.ready || !this.url) {
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
const element = this.restoreSource();
|
|
201
|
+
const context = $getAudioContext();
|
|
202
|
+
// One source per element, retained across stop/replay and URL changes.
|
|
203
|
+
if (!this.source) {
|
|
204
|
+
this.source = context.createMediaElementSource(element);
|
|
205
|
+
}
|
|
206
|
+
if (!this.gain) {
|
|
207
|
+
this.gain = context.createGain();
|
|
208
|
+
}
|
|
209
|
+
this.gain.gain.value = Math.min($getVolume(), this.sound.volume);
|
|
210
|
+
this.source.connect(this.gain);
|
|
211
|
+
this.gain.connect(context.destination);
|
|
212
|
+
element.currentTime = 0;
|
|
213
|
+
element.loop = this.sound.loopCount === Infinity;
|
|
214
|
+
this.active = true;
|
|
215
|
+
this.count = 1;
|
|
216
|
+
const generation = ++this.generation;
|
|
217
|
+
const ended = () => {
|
|
218
|
+
if (generation !== this.generation || !this.active || !element.ended) {
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
if (this.canLoop) {
|
|
222
|
+
this.count++;
|
|
223
|
+
element.currentTime = 0;
|
|
224
|
+
element.loop = this.sound.loopCount === Infinity;
|
|
225
|
+
this.retry();
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
this.stop();
|
|
229
|
+
if (this.sound.willTrigger(Event.COMPLETE)) {
|
|
230
|
+
this.sound.dispatchEvent(new Event(Event.COMPLETE));
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
const failed = () => {
|
|
234
|
+
if (generation !== this.generation || !this.active) {
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
const error = new Error(element.error?.message || "Stream Sound playback failed.");
|
|
238
|
+
this.stop();
|
|
239
|
+
this.emitError(error);
|
|
240
|
+
};
|
|
241
|
+
element.addEventListener("ended", ended);
|
|
242
|
+
element.addEventListener("error", failed);
|
|
243
|
+
this.removePlaybackEvents = () => {
|
|
244
|
+
element.removeEventListener("ended", ended);
|
|
245
|
+
element.removeEventListener("error", failed);
|
|
246
|
+
};
|
|
247
|
+
register(this);
|
|
248
|
+
this.retry();
|
|
249
|
+
}
|
|
250
|
+
/** Both resume() and play() must be invoked synchronously inside a user gesture. */
|
|
251
|
+
retry() {
|
|
252
|
+
if (!this.active || !this.element) {
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
const context = $getAudioContext();
|
|
256
|
+
if (context.state !== "running") {
|
|
257
|
+
void context.resume().catch(() => { });
|
|
258
|
+
}
|
|
259
|
+
if (!this.element.paused || this.playPending) {
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
const generation = this.generation;
|
|
263
|
+
this.playPending = true;
|
|
264
|
+
void this.element.play().then(() => {
|
|
265
|
+
if (generation === this.generation) {
|
|
266
|
+
this.playPending = false;
|
|
267
|
+
}
|
|
268
|
+
}, (error) => {
|
|
269
|
+
if (generation !== this.generation) {
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
this.playPending = false;
|
|
273
|
+
// DOMException is not an Error subclass in every browser/realm.
|
|
274
|
+
const name = error && typeof error === "object" && "name" in error ? error.name : "";
|
|
275
|
+
if (name !== "NotAllowedError" && name !== "AbortError") {
|
|
276
|
+
this.stop();
|
|
277
|
+
this.emitError(error);
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
stop() {
|
|
282
|
+
++this.generation;
|
|
283
|
+
this.active = false;
|
|
284
|
+
this.count = 0;
|
|
285
|
+
this.playPending = false;
|
|
286
|
+
unregister(this);
|
|
287
|
+
this.cancelLoad?.();
|
|
288
|
+
this.removePlaybackEvents?.();
|
|
289
|
+
this.removePlaybackEvents = null;
|
|
290
|
+
if (this.gain) {
|
|
291
|
+
this.gain.gain.value = 0;
|
|
292
|
+
this.gain.disconnect();
|
|
293
|
+
}
|
|
294
|
+
this.source?.disconnect();
|
|
295
|
+
if (this.element) {
|
|
296
|
+
this.element.pause();
|
|
297
|
+
if (this.element.hasAttribute("src")) {
|
|
298
|
+
this.element.removeAttribute("src");
|
|
299
|
+
this.element.load();
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
cloneTo(target) {
|
|
304
|
+
target.url = this.url;
|
|
305
|
+
target.credentials = this.credentials;
|
|
306
|
+
target.ready = this.ready;
|
|
307
|
+
}
|
|
308
|
+
/** Release references as well as media data. load() can initialize this instance again. */
|
|
309
|
+
dispose() {
|
|
310
|
+
this.stop();
|
|
311
|
+
this.ready = false;
|
|
312
|
+
this.url = null;
|
|
313
|
+
this.element = null;
|
|
314
|
+
this.source = null;
|
|
315
|
+
this.gain = null;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { StreamSound } from "./StreamSound";
|
|
2
|
+
export declare const register: (stream: StreamSound) => void;
|
|
3
|
+
export declare const unregister: (stream: StreamSound) => void;
|
|
4
|
+
export declare const updateVolume: (volume: number) => void;
|
|
5
|
+
export declare const stopAll: () => void;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// Keep streams separate: the legacy playing-sound array and its iteration semantics stay unchanged.
|
|
2
|
+
const streams = new Set();
|
|
3
|
+
const gestures = ["pointerdown", "touchend", "keydown"];
|
|
4
|
+
const retry = () => {
|
|
5
|
+
for (const stream of streams) {
|
|
6
|
+
stream.retry();
|
|
7
|
+
}
|
|
8
|
+
};
|
|
9
|
+
export const register = (stream) => {
|
|
10
|
+
if (!streams.size) {
|
|
11
|
+
for (const gesture of gestures) {
|
|
12
|
+
window.addEventListener(gesture, retry);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
streams.add(stream);
|
|
16
|
+
};
|
|
17
|
+
export const unregister = (stream) => {
|
|
18
|
+
streams.delete(stream);
|
|
19
|
+
if (!streams.size) {
|
|
20
|
+
for (const gesture of gestures) {
|
|
21
|
+
window.removeEventListener(gesture, retry);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
export const updateVolume = (volume) => {
|
|
26
|
+
for (const stream of streams) {
|
|
27
|
+
stream.sound.volume = volume;
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
export const stopAll = () => {
|
|
31
|
+
for (const stream of Array.from(streams)) {
|
|
32
|
+
stream.stop();
|
|
33
|
+
}
|
|
34
|
+
};
|
package/src/Sound.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ISoundCharacter } from "./interface/ISoundCharacter";
|
|
2
|
+
import type { ISoundOptions } from "./interface/ISoundOptions";
|
|
2
3
|
import type { URLRequest } from "@next2d/net";
|
|
3
4
|
import { EventDispatcher } from "@next2d/events";
|
|
4
5
|
/**
|
|
@@ -13,6 +14,7 @@ import { EventDispatcher } from "@next2d/events";
|
|
|
13
14
|
* @extends EventDispatcher
|
|
14
15
|
*/
|
|
15
16
|
export declare class Sound extends EventDispatcher {
|
|
17
|
+
private readonly _$stream;
|
|
16
18
|
/**
|
|
17
19
|
* @type {AudioBufferSourceNode}
|
|
18
20
|
* @default null
|
|
@@ -65,7 +67,9 @@ export declare class Sound extends EventDispatcher {
|
|
|
65
67
|
* @constructor
|
|
66
68
|
* @public
|
|
67
69
|
*/
|
|
68
|
-
constructor();
|
|
70
|
+
constructor(options?: ISoundOptions);
|
|
71
|
+
/** The backend is selected at construction; existing callers default to buffer. */
|
|
72
|
+
get mode(): "buffer" | "stream";
|
|
69
73
|
/**
|
|
70
74
|
* @description ボリュームです。範囲は 0(無音)~ 1(フルボリューム)です。
|
|
71
75
|
* The volume, ranging from 0 (silent) to 1 (full volume).
|
|
@@ -123,6 +127,8 @@ export declare class Sound extends EventDispatcher {
|
|
|
123
127
|
* @public
|
|
124
128
|
*/
|
|
125
129
|
stop(): void;
|
|
130
|
+
/** Release held resources. Call load() again before reusing this Sound. */
|
|
131
|
+
dispose(): void;
|
|
126
132
|
/**
|
|
127
133
|
* @description Character DataからSoundを作成
|
|
128
134
|
* Create Sound from Character Data
|
package/src/Sound.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { SoundMixer } from "./SoundMixer";
|
|
2
|
+
import { StreamSound } from "./Sound/StreamSound";
|
|
2
3
|
import { execute as soundEndedEventService } from "./Sound/service/SoundEndedEventService";
|
|
3
4
|
import { execute as soundLoadUseCase } from "./Sound/usecase/SoundLoadUseCase";
|
|
4
5
|
import { execute as soundBuildFromCharacterUseCase } from "./Sound/usecase/SoundBuildFromCharacterUseCase";
|
|
@@ -20,8 +21,14 @@ export class Sound extends EventDispatcher {
|
|
|
20
21
|
* @constructor
|
|
21
22
|
* @public
|
|
22
23
|
*/
|
|
23
|
-
constructor() {
|
|
24
|
+
constructor(options = {}) {
|
|
24
25
|
super();
|
|
26
|
+
Object.defineProperty(this, "_$stream", {
|
|
27
|
+
enumerable: true,
|
|
28
|
+
configurable: true,
|
|
29
|
+
writable: true,
|
|
30
|
+
value: void 0
|
|
31
|
+
});
|
|
25
32
|
/**
|
|
26
33
|
* @type {AudioBufferSourceNode}
|
|
27
34
|
* @default null
|
|
@@ -105,6 +112,7 @@ export class Sound extends EventDispatcher {
|
|
|
105
112
|
writable: true,
|
|
106
113
|
value: void 0
|
|
107
114
|
});
|
|
115
|
+
this._$stream = options.mode === "stream" ? new StreamSound(this) : null;
|
|
108
116
|
this.loopCount = 0;
|
|
109
117
|
this.audioBuffer = null;
|
|
110
118
|
// private
|
|
@@ -114,6 +122,10 @@ export class Sound extends EventDispatcher {
|
|
|
114
122
|
this._$source = null;
|
|
115
123
|
this._$gainNode = null;
|
|
116
124
|
}
|
|
125
|
+
/** The backend is selected at construction; existing callers default to buffer. */
|
|
126
|
+
get mode() {
|
|
127
|
+
return this._$stream ? "stream" : "buffer";
|
|
128
|
+
}
|
|
117
129
|
/**
|
|
118
130
|
* @description ボリュームです。範囲は 0(無音)~ 1(フルボリューム)です。
|
|
119
131
|
* The volume, ranging from 0 (silent) to 1 (full volume).
|
|
@@ -130,6 +142,7 @@ export class Sound extends EventDispatcher {
|
|
|
130
142
|
if (this._$gainNode) {
|
|
131
143
|
this._$gainNode.gain.value = this._$volume;
|
|
132
144
|
}
|
|
145
|
+
this._$stream?.setVolume(this._$volume);
|
|
133
146
|
}
|
|
134
147
|
/**
|
|
135
148
|
* @description サウンドがループするかどうかを示します。
|
|
@@ -140,6 +153,9 @@ export class Sound extends EventDispatcher {
|
|
|
140
153
|
* @public
|
|
141
154
|
*/
|
|
142
155
|
get canLoop() {
|
|
156
|
+
if (this._$stream) {
|
|
157
|
+
return this._$stream.canLoop;
|
|
158
|
+
}
|
|
143
159
|
return !this._$stopFlag && this.loopCount >= this._$currentCount;
|
|
144
160
|
}
|
|
145
161
|
/**
|
|
@@ -151,6 +167,13 @@ export class Sound extends EventDispatcher {
|
|
|
151
167
|
* @public
|
|
152
168
|
*/
|
|
153
169
|
clone() {
|
|
170
|
+
if (this._$stream) {
|
|
171
|
+
const sound = new Sound({ "mode": "stream" });
|
|
172
|
+
sound.volume = this._$volume;
|
|
173
|
+
sound.loopCount = this.loopCount;
|
|
174
|
+
this._$stream.cloneTo(sound._$stream);
|
|
175
|
+
return sound;
|
|
176
|
+
}
|
|
154
177
|
const sound = new Sound();
|
|
155
178
|
sound.volume = this._$volume;
|
|
156
179
|
sound.loopCount = this.loopCount;
|
|
@@ -167,6 +190,10 @@ export class Sound extends EventDispatcher {
|
|
|
167
190
|
* @public
|
|
168
191
|
*/
|
|
169
192
|
async load(request) {
|
|
193
|
+
if (this._$stream) {
|
|
194
|
+
await this._$stream.load(request);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
170
197
|
await soundLoadUseCase(this, request);
|
|
171
198
|
}
|
|
172
199
|
/**
|
|
@@ -179,6 +206,10 @@ export class Sound extends EventDispatcher {
|
|
|
179
206
|
* @public
|
|
180
207
|
*/
|
|
181
208
|
play(start_time = 0) {
|
|
209
|
+
if (this._$stream) {
|
|
210
|
+
this._$stream.play(start_time);
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
182
213
|
// 再生中なら終了
|
|
183
214
|
if (!this._$stopFlag) {
|
|
184
215
|
return;
|
|
@@ -212,6 +243,10 @@ export class Sound extends EventDispatcher {
|
|
|
212
243
|
* @public
|
|
213
244
|
*/
|
|
214
245
|
stop() {
|
|
246
|
+
if (this._$stream) {
|
|
247
|
+
this._$stream.stop();
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
215
250
|
if (this._$stopFlag) {
|
|
216
251
|
return;
|
|
217
252
|
}
|
|
@@ -232,6 +267,16 @@ export class Sound extends EventDispatcher {
|
|
|
232
267
|
playingSounds.splice(index, 1);
|
|
233
268
|
}
|
|
234
269
|
}
|
|
270
|
+
/** Release held resources. Call load() again before reusing this Sound. */
|
|
271
|
+
dispose() {
|
|
272
|
+
if (this._$stream) {
|
|
273
|
+
this._$stream.dispose();
|
|
274
|
+
}
|
|
275
|
+
else {
|
|
276
|
+
this.stop();
|
|
277
|
+
}
|
|
278
|
+
this.audioBuffer = null;
|
|
279
|
+
}
|
|
235
280
|
/**
|
|
236
281
|
* @description Character DataからSoundを作成
|
|
237
282
|
* Create Sound from Character Data
|
|
@@ -242,6 +287,9 @@ export class Sound extends EventDispatcher {
|
|
|
242
287
|
* @protected
|
|
243
288
|
*/
|
|
244
289
|
async $build(character) {
|
|
290
|
+
if (this._$stream) {
|
|
291
|
+
throw new TypeError("Character audio data requires buffer mode.");
|
|
292
|
+
}
|
|
245
293
|
await soundBuildFromCharacterUseCase(this, character);
|
|
246
294
|
}
|
|
247
295
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { stopAll } from "../../Sound/StreamSoundRegistry";
|
|
1
2
|
import { $getPlayingSounds, $getPlayingVideos } from "../../MediaUtil";
|
|
2
3
|
/**
|
|
3
4
|
* @description 再生中のサウンドとビデオの全ての再生を停止
|
|
@@ -29,4 +30,5 @@ export const execute = () => {
|
|
|
29
30
|
// reset
|
|
30
31
|
playingSounds.length = 0;
|
|
31
32
|
playingVideos.length = 0;
|
|
33
|
+
stopAll();
|
|
32
34
|
};
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { updateVolume } from "../../Sound/StreamSoundRegistry";
|
|
1
2
|
import { $setVolume, $getPlayingSounds, $getPlayingVideos } from "../../MediaUtil";
|
|
2
3
|
/**
|
|
3
4
|
* @description 再生中のサウンドとビデオの全ての音量を更新
|
|
@@ -26,4 +27,5 @@ export const execute = (volume) => {
|
|
|
26
27
|
}
|
|
27
28
|
video.volume = volume;
|
|
28
29
|
}
|
|
30
|
+
updateVolume(volume);
|
|
29
31
|
};
|
package/src/index.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|