scratch-audio
Advanced tools
Comparing version 0.1.0-prerelease.1500997049 to 0.1.0-prerelease.1501074095
{ | ||
"name": "scratch-audio", | ||
"version": "0.1.0-prerelease.1500997049", | ||
"version": "0.1.0-prerelease.1501074095", | ||
"description": "audio engine for scratch 3.0", | ||
@@ -5,0 +5,0 @@ "main": "dist.js", |
@@ -47,3 +47,16 @@ const SoundPlayer = require('./SoundPlayer'); | ||
const audioData = request.response; | ||
this.audioContext.decodeAudioData(audioData).then(buffer => { | ||
// Check for newer promise-based API | ||
let loaderPromise; | ||
if (this.audioContext.decodeAudioData.length === 1) { | ||
loaderPromise = this.audioContext.decodeAudioData(audioData); | ||
} else { | ||
// Fall back to callback API | ||
loaderPromise = new Promise((resolve, reject) => { | ||
this.audioContext.decodeAudioData(audioData, | ||
decodedAudio => resolve(decodedAudio), | ||
error => reject(error) | ||
); | ||
}); | ||
} | ||
loaderPromise.then(buffer => { | ||
this.drumSounds[i].setBuffer(buffer); | ||
@@ -50,0 +63,0 @@ }); |
@@ -0,5 +1,6 @@ | ||
const AudioContext = require('audio-context'); | ||
const log = require('./log'); | ||
const uid = require('./uid'); | ||
const AudioContext = require('audio-context'); | ||
const PitchEffect = require('./effects/PitchEffect'); | ||
@@ -42,3 +43,3 @@ const PanEffect = require('./effects/PanEffect'); | ||
// sound players that are currently playing, indexed by the sound's md5 | ||
// sound players that are currently playing, indexed by the sound's soundId | ||
this.activeSoundPlayers = {}; | ||
@@ -49,8 +50,8 @@ } | ||
* Play a sound | ||
* @param {string} md5 - the md5 id of a sound file | ||
* @param {string} soundId - the soundId id of a sound file | ||
* @return {Promise} a Promise that resolves when the sound finishes playing | ||
*/ | ||
playSound (md5) { | ||
playSound (soundId) { | ||
// if this sound is not in the audio engine, return | ||
if (!this.audioEngine.audioBuffers[md5]) { | ||
if (!this.audioEngine.audioBuffers[soundId]) { | ||
return; | ||
@@ -60,4 +61,4 @@ } | ||
// if this sprite or clone is already playing this sound, stop it first | ||
if (this.activeSoundPlayers[md5]) { | ||
this.activeSoundPlayers[md5].stop(); | ||
if (this.activeSoundPlayers[soundId]) { | ||
this.activeSoundPlayers[soundId].stop(); | ||
} | ||
@@ -67,3 +68,3 @@ | ||
const player = new SoundPlayer(this.audioEngine.audioContext); | ||
player.setBuffer(this.audioEngine.audioBuffers[md5]); | ||
player.setBuffer(this.audioEngine.audioBuffers[soundId]); | ||
player.connect(this.effectsNode); | ||
@@ -74,3 +75,3 @@ this.pitchEffect.updatePlayer(player); | ||
// add it to the list of active sound players | ||
this.activeSoundPlayers[md5] = player; | ||
this.activeSoundPlayers[soundId] = player; | ||
@@ -106,4 +107,4 @@ // remove sounds that are not playing from the active sound players array | ||
// stop all active sound players | ||
for (const md5 in this.activeSoundPlayers) { | ||
this.activeSoundPlayers[md5].stop(); | ||
for (const soundId in this.activeSoundPlayers) { | ||
this.activeSoundPlayers[soundId].stop(); | ||
} | ||
@@ -177,3 +178,3 @@ | ||
// a map of md5s to audio buffers, holding sounds for all sprites | ||
// a map of soundIds to audio buffers, holding sounds for all sprites | ||
this.audioBuffers = {}; | ||
@@ -198,11 +199,10 @@ | ||
* Decode a sound, decompressing it into audio samples. | ||
* Store a reference to it the sound in the audioBuffers dictionary, indexed by md5 | ||
* Store a reference to it the sound in the audioBuffers dictionary, indexed by soundId | ||
* @param {object} sound - an object containing audio data and metadata for a sound | ||
* @property {Buffer} data - sound data loaded from scratch-storage. | ||
* @property {string} format - format type, either empty or adpcm. | ||
* @property {string} md5 - the MD5 and extension of the sound. | ||
* @returns {?Promise} - a promise which will resolve after the audio buffer is stored, or null on error. | ||
* @returns {?Promise} - a promise which will resolve to the soundId if decoded and stored. | ||
*/ | ||
decodeSound (sound) { | ||
const soundId = uid(); | ||
let loaderPromise = null; | ||
@@ -215,3 +215,14 @@ | ||
case '': | ||
loaderPromise = this.audioContext.decodeAudioData(bufferCopy); | ||
// Check for newer promise-based API | ||
if (this.audioContext.decodeAudioData.length === 1) { | ||
loaderPromise = this.audioContext.decodeAudioData(bufferCopy); | ||
} else { | ||
// Fall back to callback API | ||
loaderPromise = new Promise((resolve, reject) => { | ||
this.audioContext.decodeAudioData(bufferCopy, | ||
decodedAudio => resolve(decodedAudio), | ||
error => reject(error) | ||
); | ||
}); | ||
} | ||
break; | ||
@@ -228,3 +239,4 @@ case 'adpcm': | ||
decodedAudio => { | ||
storedContext.audioBuffers[sound.md5] = decodedAudio; | ||
storedContext.audioBuffers[soundId] = decodedAudio; | ||
return soundId; | ||
}, | ||
@@ -238,2 +250,20 @@ error => { | ||
/** | ||
* Retrieve the audio buffer as held in memory for a given sound id. | ||
* @param {!string} soundId - the id of the sound buffer to get | ||
* @return {AudioBuffer} the buffer corresponding to the given sound id. | ||
*/ | ||
getSoundBuffer (soundId) { | ||
return this.audioBuffers[soundId]; | ||
} | ||
/** | ||
* Update the in-memory audio buffer to a new one by soundId. | ||
* @param {!string} soundId - the id of the sound buffer to update. | ||
* @param {AudioBuffer} newBuffer - the new buffer to swap in. | ||
*/ | ||
updateSoundBuffer (soundId, newBuffer) { | ||
this.audioBuffers[soundId] = newBuffer; | ||
} | ||
/** | ||
* An older version of the AudioEngine had this function to load all sounds | ||
@@ -240,0 +270,0 @@ * This is a stub to provide a warning when it is called |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
607120
49
3988