mirror of
https://github.com/scratchfoundation/scratch-audio.git
synced 2024-12-22 22:12:48 -05:00
Add mic input for loudness block
This commit is contained in:
parent
4eb12ea075
commit
7d6adf5864
1 changed files with 23 additions and 0 deletions
23
src/index.js
23
src/index.js
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -143,6 +147,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 0;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Names of the audio effects.
|
||||
* @readonly
|
||||
|
|
Loading…
Reference in a new issue