Add 'stopAllSounds' block

This commit is contained in:
Caitlin Hennessy 2017-12-13 16:31:42 -08:00
parent 14f57f54bf
commit 2d81be3b24
5 changed files with 35 additions and 1 deletions

View File

@ -26,5 +26,6 @@
"music.setTempo|param|bpm": "The new tempo in beats per minute, eg: 120", "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": "Set the output volume of the sound synthesizer.",
"music.setVolume|param|volume": "the volume 0...256, eg: 128", "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." "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."
} }

View File

@ -31,6 +31,7 @@
"music.ringTone|block": "ring tone|at %note=device_note", "music.ringTone|block": "ring tone|at %note=device_note",
"music.setTempo|block": "set tempo to %value|(bpm)", "music.setTempo|block": "set tempo to %value|(bpm)",
"music.setVolume|block": "set volume %volume", "music.setVolume|block": "set volume %volume",
"music.stopSounds|block": "stop all sounds",
"music.tempo|block": "tempo (bpm)", "music.tempo|block": "tempo (bpm)",
"music|block": "music", "music|block": "music",
"sounds.animalsCatPurr|block": "Animals cat purr", "sounds.animalsCatPurr|block": "Animals cat purr",

View File

@ -163,6 +163,18 @@ void playTone(int frequency, int ms) {
sleep_ms(1); 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. */ /** Makes a sound bound to a buffer in WAV format. */
//% //%
Sound fromWAV(Buffer buf) { Sound fromWAV(Buffer buf) {

10
libs/music/shims.d.ts vendored
View File

@ -25,6 +25,16 @@ declare namespace music {
//% weight=76 blockGap=8 shim=music::playTone //% weight=76 blockGap=8 shim=music::playTone
function playTone(frequency: int32, ms: int32): void; 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. */ /** Makes a sound bound to a buffer in WAV format. */
//% shim=music::fromWAV //% shim=music::fromWAV
function fromWAV(buf: Buffer): Sound; function fromWAV(buf: Buffer): Sound;

View File

@ -3,11 +3,16 @@ namespace pxsim.music {
export function fromWAV(buf: RefBuffer) { export function fromWAV(buf: RefBuffer) {
return incr(buf) return incr(buf)
} }
export function stopSounds() {
SoundMethods.stop()
}
} }
namespace pxsim.SoundMethods { namespace pxsim.SoundMethods {
let numSoundsPlaying = 0; let numSoundsPlaying = 0;
const soundsLimit = 1; const soundsLimit = 1;
let audio: HTMLAudioElement;
export function buffer(buf: RefBuffer) { export function buffer(buf: RefBuffer) {
return incr(buf) return incr(buf)
@ -27,7 +32,7 @@ namespace pxsim.SoundMethods {
} }
return new Promise<void>(resolve => { return new Promise<void>(resolve => {
let url = "data:audio/wav;base64," + btoa(uint8ArrayToString(buf.data)) let url = "data:audio/wav;base64," + btoa(uint8ArrayToString(buf.data))
let audio = new Audio(url) audio = new Audio(url)
audio.onended = () => { audio.onended = () => {
resolve(); resolve();
numSoundsPlaying--; numSoundsPlaying--;
@ -36,5 +41,10 @@ namespace pxsim.SoundMethods {
audio.play() audio.play()
}) })
} }
export function stop() {
audio.pause();
}
} }