-
Notifications
You must be signed in to change notification settings - Fork 68
/
SoundPlayer.tsx
222 lines (208 loc) · 6.4 KB
/
SoundPlayer.tsx
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
import React from "react";
import uuid from "uuid";
const sounds: {[id: string]: Sound} = {};
let audioContext: AudioContext | undefined = undefined;
declare global {
interface Window {
webkitAudioContext: AudioContext;
}
}
const AudioContext = window.AudioContext || window.webkitAudioContext;
if (AudioContext) audioContext = new AudioContext();
let resumed = false;
document.body.onmousemove = () => {
if (!resumed) {
audioContext?.resume();
resumed = true;
}
};
function copyToChannel(
destination: AudioBuffer,
source: Float32Array,
channelNumber: number,
) {
try {
const nowBuffering = destination.getChannelData(channelNumber);
for (let i = 0; i < source.length; i++) {
nowBuffering[i] = source[i];
}
} catch (error) {
console.error(error);
}
return destination;
}
function downMixBuffer(buffer: AudioBuffer, channel: number[] | number[][]) {
if (!audioContext) return buffer;
let buff: AudioBuffer;
if (buffer.numberOfChannels === 1) {
buff = audioContext.createBuffer(
audioContext.destination.channelCount,
buffer.duration * audioContext.sampleRate,
audioContext.sampleRate,
);
for (let c = 0; c < channel.length; c++) {
const channelObj = channel[c];
if (Array.isArray(channelObj)) {
//If there is an array within the channel array, then it is
//assumed that the values of the array correspond to LR channels
buff = copyToChannel(buff, buffer.getChannelData(0), channelObj[0]);
buff = copyToChannel(buff, buffer.getChannelData(0), channelObj[1]);
} else {
buff = copyToChannel(buff, buffer.getChannelData(0), channelObj);
}
}
} else {
//Do some downmixing to stereo
buff = audioContext.createBuffer(
audioContext.destination.channelCount,
buffer.duration * audioContext.sampleRate,
audioContext.sampleRate,
);
for (let c = 0; c < channel.length; c++) {
const channelObj = channel[c];
if (Array.isArray(channelObj)) {
//If there is an array within the channel array, then it is
//assumed that the values of the array correspond to LR channels
buff = copyToChannel(buff, buffer.getChannelData(0), channelObj[0]);
buff = copyToChannel(buff, buffer.getChannelData(1), channelObj[1]);
} else {
//Combine the two buffer channels into one.
buff = copyToChannel(buff, buffer.getChannelData(0), channelObj);
}
}
}
return buff;
}
interface Sound {
asset?: string;
url?: string;
id?: string;
muted?: boolean;
volume?: number;
paused?: boolean;
looping?: boolean;
playbackRate?: number;
ambiance?: boolean;
channel?: number | number[];
source?: AudioBufferSourceNode;
gain?: GainNode;
preserveChannels?: boolean;
onFinishedPlaying?: () => void;
}
export function playSound(opts: Sound) {
if (!opts.asset && !opts.url) return;
if (opts.id) {
removeSound(opts.id, true);
}
opts.id = opts.id || uuid.v4();
const volume = opts.muted ? 0 : opts.volume || 1;
const playbackRate = opts.paused ? 0 : opts.playbackRate || 1;
const channel = opts.channel
? Array.isArray(opts.channel)
? opts.channel
: [opts.channel]
: [0, 1];
const asset = opts.url || `/assets${opts.asset}`;
if (!asset || asset === "/assetsundefined") return;
try {
fetch(asset)
.then(res => {
if (!res.ok) return;
return res.arrayBuffer();
})
.then(arrayBuffer => {
if (!audioContext) return;
if (!arrayBuffer) return;
audioContext.destination.channelCount =
audioContext.destination.maxChannelCount;
// Connect the sound source to the volume control.
// Create a buffer from the response ArrayBuffer.
audioContext.decodeAudioData(
arrayBuffer,
buffer => {
if (!audioContext) return;
const sound = {...opts} || {};
//Create a new buffer and set it to the specified channel.
sound.source = audioContext.createBufferSource();
sound.preserveChannels ? (sound.source.buffer = buffer) : (sound.source.buffer = downMixBuffer(buffer, channel))
sound.source.loop = opts.looping || false;
sound.source.playbackRate.setValueAtTime(playbackRate, 0);
sound.gain = audioContext.createGain();
// Use an x * x curve, since linear isn't super great with volume.
sound.gain.gain.setValueAtTime(volume * volume, 0);
sound.source.connect(sound.gain);
sound.source.onended = () => {
opts.id && removeSound(opts?.id);
opts.onFinishedPlaying && opts.onFinishedPlaying();
};
sound.gain.connect(audioContext.destination);
sound.source.start();
sounds[opts.id || ""] = sound;
},
function onFailure() {
// console.error(new Error("Decoding the audio buffer failed"));
},
);
})
.catch(err => {
console.error("There was an error");
});
} catch (err) {
console.error("There was an error");
}
}
function removeSound(id: string, force?: boolean, ambiance?: boolean) {
const sound = sounds[id];
if (sound?.source) {
if (sound.ambiance && !ambiance) return;
if (!sound.looping || force) {
sound.source.stop();
delete sounds[id];
} else {
// sound.looping = false;
sound.source.loop = false;
sound.source.onended = () => removeSound(id, true);
}
}
}
function removeAllSounds(ambiance?: boolean) {
Object.keys(sounds).forEach(key => {
removeSound(key, true, ambiance);
});
}
function stopLooping(ambiance?: boolean) {
Object.keys(sounds).forEach(id => {
const sound = sounds[id];
if (sound?.source) {
if (sound.ambiance && !ambiance) return;
// sound.looping = false;
sound.source.loop = false;
sound.source.onended = () => removeSound(id, true);
}
});
}
export const useSounds = () => {
return React.useMemo(
() => ({
playSound,
removeSound,
removeAllSounds,
stopLooping,
sounds,
}),
[],
);
};
const withSound = (Comp: React.ElementType) => {
return (props: unknown) => (
<Comp
{...props}
playSound={playSound}
removeSound={removeSound}
removeAllSounds={removeAllSounds}
stopLooping={stopLooping}
sounds={sounds}
/>
);
};
export default withSound;