mirror of
https://github.com/scratchfoundation/scratch-audio.git
synced 2025-01-10 06:41:56 -05:00
More refactoring/code-cleanup addressing PR comments.
This commit is contained in:
parent
158adb0868
commit
a3ecd2ddc2
1 changed files with 33 additions and 27 deletions
60
src/index.js
60
src/index.js
|
@ -15,6 +15,27 @@ const ADPCMSoundDecoder = require('./ADPCMSoundDecoder');
|
||||||
* that handles global functionality, and AudioPlayers, belonging to individual sprites and clones.
|
* that handles global functionality, and AudioPlayers, belonging to individual sprites and clones.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrapper to ensure that audioContext.decodeAudioData is a promise
|
||||||
|
* @param {object} audioContext The current AudioContext
|
||||||
|
* @param {ArrayBuffer} buffer Audio data buffer to decode
|
||||||
|
* @return {Promise} A promise that resolves to the decoded audio
|
||||||
|
*/
|
||||||
|
const decodeAudioData = function (audioContext, buffer) {
|
||||||
|
// Check for newer promise-based API
|
||||||
|
if (audioContext.decodeAudioData.length === 1) {
|
||||||
|
return audioContext.decodeAudioData(buffer);
|
||||||
|
}
|
||||||
|
// Fall back to callback API
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
audioContext.decodeAudioData(buffer,
|
||||||
|
decodedAudio => resolve(decodedAudio),
|
||||||
|
error => reject(error)
|
||||||
|
);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
class AudioPlayer {
|
class AudioPlayer {
|
||||||
/**
|
/**
|
||||||
* Each sprite or clone has an audio player
|
* Each sprite or clone has an audio player
|
||||||
|
@ -192,32 +213,18 @@ class AudioEngine {
|
||||||
* @returns {?Promise} - a promise which will resolve to the soundId if decoded and stored.
|
* @returns {?Promise} - a promise which will resolve to the soundId if decoded and stored.
|
||||||
*/
|
*/
|
||||||
decodeSound (sound) {
|
decodeSound (sound) {
|
||||||
const soundId = uid();
|
|
||||||
let loaderPromise = null;
|
|
||||||
|
|
||||||
// Make a copy of the buffer because decoding detaches the original buffer
|
// Make a copy of the buffer because decoding detaches the original buffer
|
||||||
const bufferCopy1 = sound.data.buffer.slice(0);
|
const bufferCopy1 = sound.data.buffer.slice(0);
|
||||||
|
|
||||||
// Attempt to decode the sound using the browser's native audio data decoder
|
const soundId = uid();
|
||||||
// Check for newer promise-based API
|
// Partially apply updateSoundBuffer function with the current
|
||||||
if (this.audioContext.decodeAudioData.length === 1) {
|
// soundId so that it's ready to be used on successfully decoded audio
|
||||||
loaderPromise = this.audioContext.decodeAudioData(bufferCopy1);
|
const addDecodedAudio = this.updateSoundBuffer.bind(this, soundId);
|
||||||
} else {
|
|
||||||
// Fall back to callback API
|
|
||||||
loaderPromise = new Promise((resolve, reject) => {
|
|
||||||
this.audioContext.decodeAudioData(bufferCopy1,
|
|
||||||
decodedAudio => resolve(decodedAudio),
|
|
||||||
error => reject(error)
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const storedContext = this;
|
// Attempt to decode the sound using the browser's native audio data decoder
|
||||||
return loaderPromise.then(
|
// If that fails, attempt to decode as ADPCM
|
||||||
decodedAudio => {
|
return decodeAudioData(this.audioContext, bufferCopy1).then(
|
||||||
storedContext.updateSoundBuffer(soundId, decodedAudio);
|
addDecodedAudio,
|
||||||
return soundId;
|
|
||||||
},
|
|
||||||
() => {
|
() => {
|
||||||
// The audio context failed to parse the sound data
|
// The audio context failed to parse the sound data
|
||||||
// we gave it, so try to decode as 'adpcm'
|
// we gave it, so try to decode as 'adpcm'
|
||||||
|
@ -227,10 +234,7 @@ class AudioEngine {
|
||||||
// Try decoding as adpcm
|
// Try decoding as adpcm
|
||||||
return (new ADPCMSoundDecoder(this.audioContext)).decode(bufferCopy2)
|
return (new ADPCMSoundDecoder(this.audioContext)).decode(bufferCopy2)
|
||||||
.then(
|
.then(
|
||||||
decodedAudio => {
|
addDecodedAudio,
|
||||||
storedContext.updateSoundBuffer(soundId, decodedAudio);
|
|
||||||
return soundId;
|
|
||||||
},
|
|
||||||
sndError => {
|
sndError => {
|
||||||
log.warn('audio data could not be decoded', sndError);
|
log.warn('audio data could not be decoded', sndError);
|
||||||
}
|
}
|
||||||
|
@ -249,12 +253,14 @@ class AudioEngine {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update the in-memory audio buffer to a new one by soundId.
|
* Add or update the in-memory audio buffer to a new one by soundId.
|
||||||
* @param {!string} soundId - the id of the sound buffer to update.
|
* @param {!string} soundId - the id of the sound buffer to update.
|
||||||
* @param {AudioBuffer} newBuffer - the new buffer to swap in.
|
* @param {AudioBuffer} newBuffer - the new buffer to swap in.
|
||||||
|
* @return {string} The uid of the sound that was updated or added
|
||||||
*/
|
*/
|
||||||
updateSoundBuffer (soundId, newBuffer) {
|
updateSoundBuffer (soundId, newBuffer) {
|
||||||
this.audioBuffers[soundId] = newBuffer;
|
this.audioBuffers[soundId] = newBuffer;
|
||||||
|
return soundId;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue