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(); + } + }