From 2d81be3b2460b0d40147ed14148c5f8602ebd335 Mon Sep 17 00:00:00 2001 From: Caitlin Hennessy Date: Wed, 13 Dec 2017 16:31:42 -0800 Subject: [PATCH 1/5] Add 'stopAllSounds' block --- libs/music/_locales/music-jsdoc-strings.json | 1 + libs/music/_locales/music-strings.json | 1 + libs/music/music.cpp | 12 ++++++++++++ libs/music/shims.d.ts | 10 ++++++++++ sim/state/sounds.ts | 12 +++++++++++- 5 files changed, 35 insertions(+), 1 deletion(-) diff --git a/libs/music/_locales/music-jsdoc-strings.json b/libs/music/_locales/music-jsdoc-strings.json index 2d67cdcf..d06a5b53 100644 --- a/libs/music/_locales/music-jsdoc-strings.json +++ b/libs/music/_locales/music-jsdoc-strings.json @@ -26,5 +26,6 @@ "music.setTempo|param|bpm": "The new tempo in beats per minute, eg: 120", "music.setVolume": "Set the output volume of the sound synthesizer.", "music.setVolume|param|volume": "the volume 0...256, eg: 128", + "music.stopSounds": "Play a tone through the speaker for some amount of time.", "music.tempo": "Return the tempo in beats per minute (bpm).\nTempo is the speed (bpm = beats per minute) at which notes play. The larger the tempo value, the faster the notes will play." } \ No newline at end of file diff --git a/libs/music/_locales/music-strings.json b/libs/music/_locales/music-strings.json index a74eae44..0e0cfc2f 100644 --- a/libs/music/_locales/music-strings.json +++ b/libs/music/_locales/music-strings.json @@ -31,6 +31,7 @@ "music.ringTone|block": "ring tone|at %note=device_note", "music.setTempo|block": "set tempo to %value|(bpm)", "music.setVolume|block": "set volume %volume", + "music.stopSounds|block": "stop all sounds", "music.tempo|block": "tempo (bpm)", "music|block": "music", "sounds.animalsCatPurr|block": "Animals cat purr", diff --git a/libs/music/music.cpp b/libs/music/music.cpp index 5912ae5c..d0409613 100644 --- a/libs/music/music.cpp +++ b/libs/music/music.cpp @@ -163,6 +163,18 @@ void playTone(int frequency, int ms) { sleep_ms(1); } +/** +* Play a tone through the speaker for some amount of time. +*/ +//% help=music/stop-sound +//% blockId=music_stop_sounds block="stop all sounds" +//% parts="headphone" async +//% blockNamespace=music +//% weight=76 blockGap=8 +void stopSounds() { + _stopSound(); +} + /** Makes a sound bound to a buffer in WAV format. */ //% Sound fromWAV(Buffer buf) { diff --git a/libs/music/shims.d.ts b/libs/music/shims.d.ts index cb3e2a62..e704615d 100644 --- a/libs/music/shims.d.ts +++ b/libs/music/shims.d.ts @@ -25,6 +25,16 @@ declare namespace music { //% weight=76 blockGap=8 shim=music::playTone function playTone(frequency: int32, ms: int32): void; + /** + * Play a tone through the speaker for some amount of time. + */ + //% help=music/stop-sound + //% blockId=music_stop_sounds block="stop all sounds" + //% parts="headphone" async + //% blockNamespace=music + //% weight=76 blockGap=8 shim=music::stopSounds + function stopSounds(): void; + /** Makes a sound bound to a buffer in WAV format. */ //% shim=music::fromWAV function fromWAV(buf: Buffer): Sound; diff --git a/sim/state/sounds.ts b/sim/state/sounds.ts index a43aacde..e75d32e4 100644 --- a/sim/state/sounds.ts +++ b/sim/state/sounds.ts @@ -3,11 +3,16 @@ namespace pxsim.music { export function fromWAV(buf: RefBuffer) { return incr(buf) } + + export function stopSounds() { + SoundMethods.stop() + } } namespace pxsim.SoundMethods { let numSoundsPlaying = 0; const soundsLimit = 1; + let audio: HTMLAudioElement; export function buffer(buf: RefBuffer) { return incr(buf) @@ -27,7 +32,7 @@ namespace pxsim.SoundMethods { } return new Promise(resolve => { let url = "data:audio/wav;base64," + btoa(uint8ArrayToString(buf.data)) - let audio = new Audio(url) + audio = new Audio(url) audio.onended = () => { resolve(); numSoundsPlaying--; @@ -36,5 +41,10 @@ namespace pxsim.SoundMethods { audio.play() }) } + + export function stop() { + audio.pause(); + } + } From cb648019bb1a651ec280325afa20cbff75149237 Mon Sep 17 00:00:00 2001 From: Caitlin Hennessy Date: Thu, 14 Dec 2017 09:17:47 -0800 Subject: [PATCH 2/5] Progress --- libs/music/music.cpp | 5 ++++- libs/music/shims.d.ts | 2 +- sim/state/sounds.ts | 4 +++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/libs/music/music.cpp b/libs/music/music.cpp index d0409613..7d3aec9e 100644 --- a/libs/music/music.cpp +++ b/libs/music/music.cpp @@ -168,10 +168,13 @@ void playTone(int frequency, int ms) { */ //% help=music/stop-sound //% blockId=music_stop_sounds block="stop all sounds" -//% parts="headphone" async +//% parts="headphone" //% blockNamespace=music //% weight=76 blockGap=8 void stopSounds() { + if (currentSample) { + samplePtr = currentSample->length; + } _stopSound(); } diff --git a/libs/music/shims.d.ts b/libs/music/shims.d.ts index e704615d..4778d66b 100644 --- a/libs/music/shims.d.ts +++ b/libs/music/shims.d.ts @@ -30,7 +30,7 @@ declare namespace music { */ //% help=music/stop-sound //% blockId=music_stop_sounds block="stop all sounds" - //% parts="headphone" async + //% parts="headphone" //% blockNamespace=music //% weight=76 blockGap=8 shim=music::stopSounds function stopSounds(): void; diff --git a/sim/state/sounds.ts b/sim/state/sounds.ts index e75d32e4..1747dee0 100644 --- a/sim/state/sounds.ts +++ b/sim/state/sounds.ts @@ -43,7 +43,9 @@ namespace pxsim.SoundMethods { } export function stop() { - audio.pause(); + if (audio) { + audio.pause(); + } } } From d436bd1227cc1fbb45d354df73cc94342aba6fec Mon Sep 17 00:00:00 2001 From: Caitlin Hennessy Date: Thu, 14 Dec 2017 09:41:01 -0800 Subject: [PATCH 3/5] Fix simulator stop() method --- sim/state/sounds.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/sim/state/sounds.ts b/sim/state/sounds.ts index 1747dee0..ae84fd85 100644 --- a/sim/state/sounds.ts +++ b/sim/state/sounds.ts @@ -43,9 +43,12 @@ namespace pxsim.SoundMethods { } export function stop() { - if (audio) { - audio.pause(); - } + return new Promise(resolve => { + if (audio) { + audio.pause(); + numSoundsPlaying--; + } + }) } } From a02f364a4cae8070901f0fe7fd4384380d8a022a Mon Sep 17 00:00:00 2001 From: Caitlin Hennessy Date: Fri, 15 Dec 2017 10:42:44 -0800 Subject: [PATCH 4/5] Update function names for consistency --- libs/music/music.cpp | 6 +++--- sim/state/sounds.ts | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/libs/music/music.cpp b/libs/music/music.cpp index 7d3aec9e..e6b999ba 100644 --- a/libs/music/music.cpp +++ b/libs/music/music.cpp @@ -166,12 +166,12 @@ void playTone(int frequency, int ms) { /** * Play a tone through the speaker for some amount of time. */ -//% help=music/stop-sound -//% blockId=music_stop_sounds block="stop all sounds" +//% help=music/stop-all-sounds +//% blockId=music_stop_all_sounds block="stop all sounds" //% parts="headphone" //% blockNamespace=music //% weight=76 blockGap=8 -void stopSounds() { +void stopAllSounds() { if (currentSample) { samplePtr = currentSample->length; } diff --git a/sim/state/sounds.ts b/sim/state/sounds.ts index ae84fd85..fd7771ed 100644 --- a/sim/state/sounds.ts +++ b/sim/state/sounds.ts @@ -4,7 +4,7 @@ namespace pxsim.music { return incr(buf) } - export function stopSounds() { + export function stopAllSounds() { SoundMethods.stop() } } From bfd34cedd61fa44cbbee19597ef88f097164e8d7 Mon Sep 17 00:00:00 2001 From: System Administrator Date: Fri, 15 Dec 2017 11:04:16 -0800 Subject: [PATCH 5/5] Update built files --- libs/music/_locales/music-jsdoc-strings.json | 2 +- libs/music/_locales/music-strings.json | 2 +- libs/music/shims.d.ts | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/libs/music/_locales/music-jsdoc-strings.json b/libs/music/_locales/music-jsdoc-strings.json index d06a5b53..e73f87ba 100644 --- a/libs/music/_locales/music-jsdoc-strings.json +++ b/libs/music/_locales/music-jsdoc-strings.json @@ -26,6 +26,6 @@ "music.setTempo|param|bpm": "The new tempo in beats per minute, eg: 120", "music.setVolume": "Set the output volume of the sound synthesizer.", "music.setVolume|param|volume": "the volume 0...256, eg: 128", - "music.stopSounds": "Play a tone through the speaker for some amount of time.", + "music.stopAllSounds": "Play a tone through the speaker for some amount of time.", "music.tempo": "Return the tempo in beats per minute (bpm).\nTempo is the speed (bpm = beats per minute) at which notes play. The larger the tempo value, the faster the notes will play." } \ No newline at end of file diff --git a/libs/music/_locales/music-strings.json b/libs/music/_locales/music-strings.json index 0e0cfc2f..7cef7803 100644 --- a/libs/music/_locales/music-strings.json +++ b/libs/music/_locales/music-strings.json @@ -31,7 +31,7 @@ "music.ringTone|block": "ring tone|at %note=device_note", "music.setTempo|block": "set tempo to %value|(bpm)", "music.setVolume|block": "set volume %volume", - "music.stopSounds|block": "stop all sounds", + "music.stopAllSounds|block": "stop all sounds", "music.tempo|block": "tempo (bpm)", "music|block": "music", "sounds.animalsCatPurr|block": "Animals cat purr", diff --git a/libs/music/shims.d.ts b/libs/music/shims.d.ts index 4778d66b..61f381a4 100644 --- a/libs/music/shims.d.ts +++ b/libs/music/shims.d.ts @@ -28,12 +28,12 @@ declare namespace music { /** * Play a tone through the speaker for some amount of time. */ - //% help=music/stop-sound - //% blockId=music_stop_sounds block="stop all sounds" + //% help=music/stop-all-sounds + //% blockId=music_stop_all_sounds block="stop all sounds" //% parts="headphone" //% blockNamespace=music - //% weight=76 blockGap=8 shim=music::stopSounds - function stopSounds(): void; + //% weight=76 blockGap=8 shim=music::stopAllSounds + function stopAllSounds(): void; /** Makes a sound bound to a buffer in WAV format. */ //% shim=music::fromWAV