forked from Bitcoindefi/OpenAO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsound.ts
More file actions
253 lines (211 loc) · 6.97 KB
/
Copy pathsound.ts
File metadata and controls
253 lines (211 loc) · 6.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import { Howl, Howler } from "howler";
export interface SoundPosition {
map?: number;
x: number;
y: number;
}
interface PlaySoundOptions {
soundId: number;
listener?: SoundPosition;
source?: SoundPosition;
fadeInMs?: number;
fadeOutMs?: number;
}
const SOUND_FILE_EXTENSIONS = ["ogg", "mp3", "wav"] as const;
const DEFAULT_BASE_VOLUME = 0.7;
const MAX_AUDIBLE_DISTANCE_TILES = 15;
const MIN_DISTANCE_VOLUME = 0.18;
const SOUND_POOL_SIZE = 12;
const HTML5_SOUND_POOL_SIZE = 4;
const MAX_CACHED_SOUNDS = 64;
const SOUND_EXTENSION_OVERRIDES = new Map<
number,
(typeof SOUND_FILE_EXTENSIONS)[number]
>([
[23, "mp3"],
[24, "mp3"],
]);
export class GameSoundManager {
private readonly basePath: string;
private readonly unavailableSoundIds = new Set<number>();
private readonly soundCache = new Map<number, Howl>();
private readonly preferWebAudio: boolean;
private masterVolume = 1;
private preferredExtension: (typeof SOUND_FILE_EXTENSIONS)[number] =
SOUND_FILE_EXTENSIONS[0];
constructor(basePath = "/sounds") {
this.basePath = basePath.replace(/\/$/, "");
this.preferredExtension = this.resolvePreferredExtension();
this.preferWebAudio = this.shouldUseWebAudio();
}
play({
soundId,
listener,
source,
fadeInMs = 0,
fadeOutMs = 0,
}: PlaySoundOptions): void {
if (typeof window === "undefined" || soundId <= 0) {
return;
}
if (this.unavailableSoundIds.has(soundId)) {
return;
}
const computedVolume = this.getVolume(listener, source);
if (computedVolume <= 0) {
return;
}
const sound = this.getOrCreateSound(soundId);
if (!sound) {
return;
}
const playbackId = sound.play();
if (typeof playbackId === "number") {
if (fadeInMs > 0) {
sound.volume(0, playbackId);
sound.fade(0, computedVolume, fadeInMs, playbackId);
} else {
sound.volume(computedVolume, playbackId);
}
if (fadeOutMs > 0) {
const durationMs = sound.duration() * 1000;
const fadeOutStartMs = durationMs - fadeOutMs;
if (fadeOutStartMs > 0) {
window.setTimeout(() => {
if (!sound.playing(playbackId)) {
return;
}
sound.fade(
Math.max(0, computedVolume),
0,
fadeOutMs,
playbackId,
);
}, fadeOutStartMs);
}
}
}
}
setMasterVolume(volume: number): void {
this.masterVolume = Math.min(1, Math.max(0, volume));
Howler.volume(this.masterVolume);
}
destroy(): void {
for (const sound of this.soundCache.values()) {
sound.unload();
}
this.soundCache.clear();
Howler.volume(1);
}
private getOrCreateSound(soundId: number): Howl | null {
const cachedSound = this.soundCache.get(soundId);
if (cachedSound) {
this.markSoundAsRecentlyUsed(soundId, cachedSound);
return cachedSound;
}
if (this.unavailableSoundIds.has(soundId)) {
return null;
}
if (!this.ensureSoundCacheCapacity()) {
return null;
}
const sound = new Howl({
src: this.getSoundUrls(soundId),
format: [...SOUND_FILE_EXTENSIONS],
preload: true,
html5: !this.preferWebAudio,
pool: this.preferWebAudio ? SOUND_POOL_SIZE : HTML5_SOUND_POOL_SIZE,
onloaderror: () => {
this.unavailableSoundIds.add(soundId);
this.soundCache.delete(soundId);
sound.unload();
},
onplayerror: () => {
this.unavailableSoundIds.add(soundId);
this.soundCache.delete(soundId);
sound.unload();
},
});
this.markSoundAsRecentlyUsed(soundId, sound);
return sound;
}
private getSoundUrls(soundId: number): string[] {
const preferredExtension =
SOUND_EXTENSION_OVERRIDES.get(soundId) ?? this.preferredExtension;
const extensions = [
preferredExtension,
...SOUND_FILE_EXTENSIONS.filter(
(extension) => extension !== preferredExtension,
),
];
return extensions.map((extension) => `${this.basePath}/${soundId}.${extension}`);
}
private shouldUseWebAudio(): boolean {
if (typeof window === "undefined") {
return false;
}
const webAudioContext =
window.AudioContext ??
(
window as typeof window & {
webkitAudioContext?: typeof AudioContext;
}
).webkitAudioContext;
return typeof webAudioContext === "function";
}
private markSoundAsRecentlyUsed(soundId: number, sound: Howl): void {
this.soundCache.delete(soundId);
this.soundCache.set(soundId, sound);
}
private ensureSoundCacheCapacity(): boolean {
if (this.soundCache.size < MAX_CACHED_SOUNDS) {
return true;
}
for (const [cachedSoundId, cachedSound] of this.soundCache) {
if (cachedSound.playing()) {
continue;
}
this.soundCache.delete(cachedSoundId);
cachedSound.unload();
return true;
}
return false;
}
private resolvePreferredExtension(): (typeof SOUND_FILE_EXTENSIONS)[number] {
if (typeof document === "undefined") {
return SOUND_FILE_EXTENSIONS[0];
}
const probe = document.createElement("audio");
if (probe.canPlayType("audio/ogg") !== "") {
return "ogg";
}
if (probe.canPlayType("audio/mpeg") !== "") {
return "mp3";
}
return "wav";
}
private getVolume(
listener?: SoundPosition,
source?: SoundPosition,
): number {
if (!listener || !source) {
return DEFAULT_BASE_VOLUME;
}
if (
listener.map != null &&
source.map != null &&
listener.map !== source.map
) {
return 0;
}
const deltaX = listener.x - source.x;
const deltaY = listener.y - source.y;
const distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
if (distance >= MAX_AUDIBLE_DISTANCE_TILES) {
return 0;
}
const attenuation = 1 - distance / MAX_AUDIBLE_DISTANCE_TILES;
const normalizedVolume = Math.max(MIN_DISTANCE_VOLUME, attenuation);
return Math.min(1, normalizedVolume * DEFAULT_BASE_VOLUME);
}
}