From 7d6adf58645bc8a7956431dd6607e561cca67fff Mon Sep 17 00:00:00 2001 From: Eric Rosenbaum Date: Mon, 20 Mar 2017 18:49:37 -0400 Subject: [PATCH 1/3] Add mic input for loudness block --- src/index.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/index.js b/src/index.js index 047d83b..94049ae 100644 --- a/src/index.js +++ b/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 From b14852ab3c147ed4569a5037e338d78ff771ce35 Mon Sep 17 00:00:00 2001 From: Eric Rosenbaum Date: Mon, 27 Mar 2017 13:23:01 -0400 Subject: [PATCH 2/3] If mic is not enabled, return -1 To match Scratch 2.0 behavior --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 94049ae..e38d7a2 100644 --- a/src/index.js +++ b/src/index.js @@ -162,7 +162,7 @@ AudioEngine.prototype.getLoudness = function () { if (this.mic && this.mic.state == 'started') { return this.micMeter.value * 100; } else { - return 0; + return -1; } }; From d08b5d2af9c680c15d0c561280259f45712bccb8 Mon Sep 17 00:00:00 2001 From: Eric Rosenbaum Date: Tue, 4 Apr 2017 15:00:32 -0400 Subject: [PATCH 3/3] Update tone version 0.9.0 So we can use Tone.UserMedia --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ad66548..9f44834 100644 --- a/package.json +++ b/package.json @@ -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" },