-
Notifications
You must be signed in to change notification settings - Fork 0
/
sound.js
184 lines (166 loc) · 5 KB
/
sound.js
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
(Scratch => {
'use strict';
const audioEngine = Scratch.vm.runtime.audioEngine;
const fetchAsArrayBufferWithTimeout = (url) => new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
let timeout = setTimeout(() => {
xhr.abort();
throw new Error('Timed out');
}, 5000);
xhr.onload = () => {
clearTimeout(timeout);
if (xhr.status === 200) {
resolve(xhr.response);
} else {
reject(new Error(`HTTP error ${xhr.status} while fetching ${url}`));
}
};
xhr.onerror = () => {
clearTimeout(timeout);
reject(new Error(`Failed to request ${url}`));
};
xhr.responseType = 'arraybuffer';
xhr.open('GET', url);
xhr.send();
});
/**
* @type {Map<string, {sound: AudioEngine.SoundPlayer | null, error: unknown}>}
*/
const soundPlayerCache = new Map();
/**
* @param {string} url
* @returns {Promise<AudioEngine.SoundPlayer>}
*/
const decodeSoundPlayer = async (url) => {
const cached = soundPlayerCache.get(url);
if (cached) {
if (cached.sound) {
return cached.sound;
}
throw cached.error;
}
try {
const arrayBuffer = await fetchAsArrayBufferWithTimeout(url);
const soundPlayer = await audioEngine.decodeSoundPlayer({
data: {
buffer: arrayBuffer
}
});
soundPlayerCache.set(url, {
sound: soundPlayer,
error: null
});
return soundPlayer;
} catch (e) {
soundPlayerCache.set(url, {
sound: null,
error: e
});
throw e;
}
};
/**
* @param {string} url
* @param {VM.Target} target
* @returns {Promise<boolean>} true if the sound could be played, false if the sound could not be decoded
*/
const playWithAudioEngine = async (url, target) => {
const soundBank = target.sprite.soundBank;
/** @type {AudioEngine.SoundPlayer} */
let soundPlayer;
try {
const originalSoundPlayer = await decodeSoundPlayer(url);
soundPlayer = originalSoundPlayer.take();
} catch (e) {
console.warn('Could not fetch audio; falling back to primitive approach', e);
return false;
}
soundBank.addSoundPlayer(soundPlayer);
await soundBank.playSound(target, soundPlayer.id);
delete soundBank.soundPlayers[soundPlayer.id];
soundBank.playerTargets.delete(soundPlayer.id);
soundBank.soundEffects.delete(soundPlayer.id);
return true;
};
/**
* @param {string} url
* @param {VM.Target} target
* @returns {Promise<void>}
*/
const playWithAudioElement = (url, target) => new Promise((resolve, reject) => {
// Unfortunately, we can't play all sounds with the audio engine.
// For these sounds, fall back to a primitive <audio>-based solution that will work for all
// sounds, even those without CORS.
const mediaElement = new Audio(url);
// Make a minimal effort to simulate Scratch's sound effects.
// We can get pretty close for volumes <100%.
// playbackRate does not have enough range for simulating pitch.
// There is no way for us to pan left or right.
mediaElement.volume = target.volume / 100;
mediaElement.onended = () => {
resolve();
};
mediaElement.play()
.then(() => {
// Wait for onended
})
.catch((err) => {
reject(err);
});
});
/**
* @param {string} url
* @param {VM.Target} target
* @returns {Promise<void>}
*/
const playSound = async (url, target) => {
try {
const success = await playWithAudioEngine(url, target);
if (!success) {
return await playWithAudioElement(url, target);
}
} catch (e) {
console.warn(`All attempts to play ${url} failed`, e);
}
};
class Sound {
getInfo() {
return {
// 'sound' would conflict with normal Scratch
id: 'notSound',
name: 'Sound',
blocks: [
{
opcode: 'play',
blockType: Scratch.BlockType.COMMAND,
text: 'start sound from url: [path]',
arguments: {
path: {
type: Scratch.ArgumentType.STRING,
defaultValue: 'https://extensions.turbowarp.org/meow.mp3'
}
}
},
{
opcode: 'playUntilDone',
blockType: Scratch.BlockType.COMMAND,
text: 'play sound from url: [path] until done',
arguments: {
path: {
type: Scratch.ArgumentType.STRING,
defaultValue: 'https://extensions.turbowarp.org/meow.mp3'
}
}
}
]
};
}
play({ path }, util) {
playSound(path, util.target);
}
playUntilDone({ path }, util) {
return playSound(path, util.target);
}
}
Scratch.extensions.register(new Sound());
})(Scratch);