Merge pull request #33 from LLK/feature/loudness

Add mic input for loudness block
This commit is contained in:
Eric Rosenbaum 2017-04-07 14:02:22 -04:00 committed by GitHub
commit c354189385
2 changed files with 24 additions and 1 deletions

View file

@ -29,7 +29,7 @@
"json": "9.0.4",
"minilog": "^3.0.1",
"soundfont-player": "0.10.5",
"tone": "0.8.0",
"tone": "0.9.0",
"travis-after-all": "1.4.4",
"webpack": "1.13.2"
},

View file

@ -53,6 +53,10 @@ function AudioEngine () {
// a map of md5s to audio buffers, holding sounds for all sprites
this.audioBuffers = {};
// microphone, for measuring loudness, with a level meter analyzer
this.mic = null;
this.micMeter = null;
}
/**
@ -152,6 +156,25 @@ AudioEngine.prototype.changeTempo = function (value) {
this.setTempo(this.currentTempo + value);
};
/**
* Get the current loudness of sound received by the microphone.
* Sound is measured in RMS and smoothed.
* @return {number} loudness scaled 0 to 100
*/
AudioEngine.prototype.getLoudness = function () {
if (!this.mic) {
this.mic = new Tone.UserMedia();
this.micMeter = new Tone.Meter('level', 0.5);
this.mic.open();
this.mic.connect(this.micMeter);
}
if (this.mic && this.mic.state == 'started') {
return this.micMeter.value * 100;
} else {
return -1;
}
};
/**
* Names of the audio effects.
* @readonly