scratch-audio
Advanced tools
Comparing version 0.1.0-prerelease.1483715110 to 0.1.0-prerelease.1484064494
{ | ||
"name": "scratch-audio", | ||
"version": "0.1.0-prerelease.1483715110", | ||
"version": "0.1.0-prerelease.1484064494", | ||
"description": "audio engine for scratch 3.0", | ||
@@ -5,0 +5,0 @@ "main": "dist.js", |
@@ -6,2 +6,3 @@ var log = require('./log'); | ||
var PanEffect = require('./effects/PanEffect'); | ||
var RoboticEffect = require('./effects/RoboticEffect'); | ||
@@ -13,6 +14,8 @@ var FuzzEffect = require('./effects/FuzzEffect'); | ||
var SoundPlayer = require('./SoundPlayer'); | ||
var Soundfont = require('soundfont-player'); | ||
var ADPCMSoundLoader = require('./ADPCMSoundLoader'); | ||
var InstrumentPlayer = require('./InstrumentPlayer'); | ||
function AudioEngine () { | ||
// create the global audio effects | ||
this.roboticEffect = new RoboticEffect(); | ||
@@ -23,2 +26,3 @@ this.fuzzEffect = new FuzzEffect(); | ||
// chain the global effects to the output | ||
this.input = new Tone.Gain(); | ||
@@ -30,5 +34,10 @@ this.input.chain ( | ||
// alternate version without effects: | ||
// this.input = new Tone.Gain(); | ||
// this.input.connect(Tone.Master); | ||
// global tempo in bpm (beats per minute) | ||
this.currentTempo = 60; | ||
this.instrumentPlayer = new InstrumentPlayer(this.input); | ||
} | ||
@@ -48,5 +57,5 @@ | ||
// the effects are chained to an effects node for this player, then to the master output | ||
// so audio is sent from each player or instrument, through the effects in order, then out | ||
// note that the pitch effect works differently - it sets the playback rate for each player | ||
// the effects are chained to an effects node for this player, then to the main audio engine | ||
// audio is sent from each soundplayer, through the effects in order, then to the global effects | ||
// note that the pitch effect works differently - it sets the playback rate for each soundplayer | ||
this.effectsNode = new Tone.Gain(); | ||
@@ -58,21 +67,7 @@ this.effectsNode.chain(this.panEffect, this.audioEngine.input); | ||
this.effectNames = ['PITCH', 'PAN', 'ECHO', 'REVERB', 'FUZZ', 'WOBBLE', 'ROBOTIC']; | ||
this.effectNames = ['PITCH', 'PAN', 'ECHO', 'REVERB', 'FUZZ', 'ROBOT']; | ||
// soundfont instrument setup | ||
this.currentVolume = 100; | ||
// instrument names used by Musyng Kite soundfont, in order to | ||
// match scratch instruments | ||
this.instrumentNames = ['acoustic_grand_piano', 'electric_piano_1', | ||
'drawbar_organ', 'acoustic_guitar_nylon', 'electric_guitar_clean', | ||
'acoustic_bass', 'pizzicato_strings', 'cello', 'trombone', 'clarinet', | ||
'tenor_sax', 'flute', 'pan_flute', 'bassoon', 'choir_aahs', 'vibraphone', | ||
'music_box', 'steel_drums', 'marimba', 'lead_1_square', 'fx_4_atmosphere']; | ||
this.instrumentNum; | ||
this.setInstrument(1); | ||
// tempo in bpm (beats per minute) | ||
// default is 60bpm | ||
this.currentTempo = 60; | ||
this.currentInstrument = 0; | ||
} | ||
@@ -117,2 +112,4 @@ | ||
AudioPlayer.prototype.playSound = function (index) { | ||
if (!this.soundPlayers[index]) return; | ||
this.soundPlayers[index].start(); | ||
@@ -127,16 +124,25 @@ | ||
AudioPlayer.prototype.playNoteForBeats = function (note, beats) { | ||
this.instrument.play( | ||
note, Tone.context.currentTime, {duration : Number(beats)} | ||
); | ||
var sec = this.beatsToSec(beats); | ||
this.audioEngine.instrumentPlayer.playNoteForSecWithInst(note, sec, this.currentInstrument); | ||
return this.waitForBeats(beats); | ||
}; | ||
AudioPlayer.prototype._midiToFreq = function (midiNote) { | ||
var freq = this.tone.intervalToFrequencyRatio(midiNote - 60) * 261.63; // 60 is C4 | ||
return freq; | ||
AudioPlayer.prototype.playDrumForBeats = function (beats) { | ||
// this.drumSamplers[drumNum].triggerAttack(); | ||
return this.waitForBeats(beats); | ||
}; | ||
AudioPlayer.prototype.playDrumForBeats = function () { | ||
// this.drumSamplers[drumNum].triggerAttack(); | ||
AudioPlayer.prototype.waitForBeats = function (beats) { | ||
var storedContext = this; | ||
return new Promise(function (resolve) { | ||
setTimeout(function () { | ||
resolve(); | ||
}, storedContext.beatsToSec(beats) * 1000); | ||
}); | ||
}; | ||
AudioPlayer.prototype.beatsToSec = function (beats) { | ||
return (60 / this.audioEngine.currentTempo) * beats; | ||
}; | ||
AudioPlayer.prototype.stopAllSounds = function () { | ||
@@ -154,5 +160,5 @@ // stop drum notes | ||
// stop soundfont notes | ||
if (this.instrument) { | ||
this.instrument.stop(); | ||
} | ||
// if (this.instrument) { | ||
// this.instrument.stop(); | ||
// } | ||
}; | ||
@@ -177,3 +183,3 @@ | ||
break; | ||
case 'ROBOTIC' : | ||
case 'ROBOT' : | ||
this.audioEngine.roboticEffect.set(value); | ||
@@ -201,3 +207,3 @@ break; | ||
break; | ||
case 'ROBOTIC' : | ||
case 'ROBOT' : | ||
this.audioEngine.roboticEffect.changeBy(value); | ||
@@ -221,22 +227,13 @@ break; | ||
AudioPlayer.prototype.setInstrument = function (instrumentNum) { | ||
this.instrumentNum = instrumentNum - 1; | ||
return Soundfont.instrument(Tone.context, this.instrumentNames[this.instrumentNum]).then( | ||
function (inst) { | ||
this.instrument = inst; | ||
this.instrument.connect(this.effectsNode); | ||
}.bind(this) | ||
); | ||
this.currentInstrument = instrumentNum; | ||
return this.audioEngine.instrumentPlayer.loadInstrument(this.currentInstrument); | ||
}; | ||
AudioPlayer.prototype.setVolume = function (value) { | ||
var vol = this._clamp(value, 0, 100); | ||
vol /= 100; | ||
this.effectsNode.gain.value = vol; | ||
this.currentVolume = this._clamp(value, 0, 100); | ||
this.effectsNode.gain.value = this.currentVolume / 100; | ||
}; | ||
AudioPlayer.prototype.changeVolume = function (value) { | ||
value /= 100; | ||
var newVol = this.effectsNode.gain.value + value; | ||
this.effectsNode.gain.value = this._clamp(newVol, 0, 1); | ||
this.setVolume(this.currentVolume + value); | ||
}; | ||
@@ -246,8 +243,8 @@ | ||
var newTempo = this._clamp(value, 10, 1000); | ||
this.currentTempo = newTempo; | ||
this.audioEngine.currentTempo = newTempo; | ||
}; | ||
AudioPlayer.prototype.changeTempo = function (value) { | ||
var newTempo = this._clamp(this.currentTempo + value, 10, 1000); | ||
this.currentTempo = newTempo; | ||
var newTempo = this._clamp(this.audioEngine.currentTempo + value, 10, 1000); | ||
this.audioEngine.currentTempo = newTempo; | ||
}; | ||
@@ -254,0 +251,0 @@ |
Sorry, the diff of this file is too big to display
1179070
49
24885