scratch-audio
Advanced tools
Comparing version
{ | ||
"name": "scratch-audio", | ||
"version": "0.1.0-prerelease.1500992119", | ||
"version": "0.1.0-prerelease.1500997011", | ||
"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 @@ }); |
@@ -5,3 +5,2 @@ /** | ||
* 0 centers it, 100 puts it on the right. | ||
* Clamped -100 to 100 | ||
*/ | ||
@@ -15,4 +14,15 @@ class PanEffect { | ||
this.audioContext = audioContext; | ||
this.panner = this.audioContext.createStereoPanner(); | ||
this.value = 0; | ||
this.input = this.audioContext.createGain(); | ||
this.leftGain = this.audioContext.createGain(); | ||
this.rightGain = this.audioContext.createGain(); | ||
this.channelMerger = this.audioContext.createChannelMerger(2); | ||
this.input.connect(this.leftGain); | ||
this.input.connect(this.rightGain); | ||
this.leftGain.connect(this.channelMerger, 0, 0); | ||
this.rightGain.connect(this.channelMerger, 0, 1); | ||
this.set(this.value); | ||
} | ||
@@ -25,27 +35,19 @@ | ||
set (val) { | ||
this.value = this.clamp(val, -100, 100); | ||
this.panner.pan.value = this.value / 100; | ||
} | ||
this.value = val; | ||
connect (node) { | ||
this.panner.connect(node); | ||
} | ||
// Map the scratch effect value (-100 to 100) to (0 to 1) | ||
const p = (val + 100) / 200; | ||
/** | ||
* Change the effect value | ||
* @param {number} val - the value to change the effect by | ||
*/ | ||
changeBy (val) { | ||
this.set(this.value + val); | ||
// Use trig functions for equal-loudness panning | ||
// See e.g. https://docs.cycling74.com/max7/tutorials/13_panningchapter01 | ||
this.leftGain.gain.value = Math.cos(p * Math.PI / 2); | ||
this.rightGain.gain.value = Math.sin(p * Math.PI / 2); | ||
} | ||
/** | ||
* Clamp the input to a range | ||
* @param {number} input - the input to clamp | ||
* @param {number} min - the min value to clamp to | ||
* @param {number} max - the max value to clamp to | ||
* @return {number} the clamped value | ||
*/ | ||
clamp (input, min, max) { | ||
return Math.min(Math.max(input, min), max); | ||
* Connnect this effect's output to another audio node | ||
* @param {AudioNode} node - the node to connect to | ||
*/ | ||
connect (node) { | ||
this.channelMerger.connect(node); | ||
} | ||
@@ -52,0 +54,0 @@ } |
@@ -36,3 +36,3 @@ const log = require('./log'); | ||
this.effectsNode = this.audioEngine.audioContext.createGain(); | ||
this.effectsNode.connect(this.panEffect.panner); | ||
this.effectsNode.connect(this.panEffect.input); | ||
this.panEffect.connect(this.audioEngine.input); | ||
@@ -208,3 +208,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; | ||
@@ -211,0 +222,0 @@ case 'adpcm': |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
602121
0.77%3893
1.17%