-
Notifications
You must be signed in to change notification settings - Fork 30
Add support for the "loudness" block and "loudness greater than" trigger #137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ad9deea
Greater Than and Loudness support
apple502j 90ea2d1
Fix everything
apple502j ff72cda
Make getLoudness synchronous again
adroitwhiz b6720c2
Cache loudness to be updated once per step
adroitwhiz 6022cc5
Make LoudnessHander.loudness more readable
adroitwhiz 33c93a9
Improve loudness mic connection state management
adroitwhiz e6aba20
Make use of await in LoudnessHandler.connect
adroitwhiz 938df2b
Wait for audioContext to resume (thanks Chrome!!!)
adroitwhiz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import Sound from "./Sound.js"; | ||
|
|
||
| const IGNORABLE_ERROR = ["NotAllowedError", "NotFoundError"]; | ||
|
|
||
| // https://github.com/LLK/scratch-audio/blob/develop/src/Loudness.js | ||
| export default class LoudnessHandler { | ||
| constructor() { | ||
| // TODO: use a TypeScript enum | ||
| this.connectionState = "NOT_CONNECTED"; | ||
| } | ||
|
|
||
| get audioContext() { | ||
| return Sound.audioContext; | ||
| } | ||
|
|
||
| async connect() { | ||
| // If we're in the middle of connecting, or failed to connect, | ||
| // don't attempt to connect again | ||
| if (this.connectionState !== "NOT_CONNECTED") return; | ||
| this.connectionState = "CONNECTING"; | ||
|
|
||
| try { | ||
| const stream = await navigator.mediaDevices.getUserMedia({ audio: true }); | ||
| // Chrome blocks usage of audio until the user interacts with the page. | ||
| // By calling `resume` here, we will wait until that happens. | ||
| await Sound.audioContext.resume(); | ||
| this.hasConnected = true; | ||
| this.audioStream = stream; | ||
| const mic = this.audioContext.createMediaStreamSource(stream); | ||
| this.analyser = this.audioContext.createAnalyser(); | ||
| mic.connect(this.analyser); | ||
| this.micDataArray = new Float32Array(this.analyser.fftSize); | ||
| this.connectionState = "CONNECTED"; | ||
| } catch (e) { | ||
| this.connectionState = "ERROR"; | ||
| if (IGNORABLE_ERROR.includes(e.name)) { | ||
| console.warn("Mic is not available."); | ||
| } else { | ||
| throw e; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| get loudness() { | ||
| if (this.connectionState !== "CONNECTED" || !this.audioStream.active) { | ||
| return -1; | ||
| } | ||
|
|
||
| this.analyser.getFloatTimeDomainData(this.micDataArray); | ||
| let sum = 0; | ||
| // compute the RMS of the sound | ||
| for (let i = 0; i < this.micDataArray.length; i++) { | ||
| sum += Math.pow(this.micDataArray[i], 2); | ||
| } | ||
| let rms = Math.sqrt(sum / this.micDataArray.length); | ||
| // smoothe the value with the last one, if it is descending | ||
| if (this._lastValue) { | ||
| rms = Math.max(rms, this._lastValue * 0.6); | ||
| } | ||
| this._lastValue = rms; | ||
|
|
||
| // scale the measurement so it's more sensitive to quieter sounds | ||
| rms *= 1.63; | ||
| rms = Math.sqrt(rms); | ||
| rms = Math.round(rms * 100); | ||
| rms = Math.min(rms, 100); | ||
| return rms; | ||
| } | ||
|
|
||
| getLoudness() { | ||
| this.connect(); | ||
| return this.loudness; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.