Merge pull request #88 from Microsoft/even_more_sounds
Add 'stopAllSounds' block
This commit is contained in:
commit
f7dd14ff7b
@ -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.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."
|
||||
}
|
@ -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.stopAllSounds|block": "stop all sounds",
|
||||
"music.tempo|block": "tempo (bpm)",
|
||||
"music|block": "music",
|
||||
"sounds.animalsCatPurr|block": "Animals cat purr",
|
||||
|
@ -163,6 +163,21 @@ void playTone(int frequency, int ms) {
|
||||
sleep_ms(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Play a tone through the speaker for some amount of time.
|
||||
*/
|
||||
//% help=music/stop-all-sounds
|
||||
//% blockId=music_stop_all_sounds block="stop all sounds"
|
||||
//% parts="headphone"
|
||||
//% blockNamespace=music
|
||||
//% weight=76 blockGap=8
|
||||
void stopAllSounds() {
|
||||
if (currentSample) {
|
||||
samplePtr = currentSample->length;
|
||||
}
|
||||
_stopSound();
|
||||
}
|
||||
|
||||
/** Makes a sound bound to a buffer in WAV format. */
|
||||
//%
|
||||
Sound fromWAV(Buffer buf) {
|
||||
|
10
libs/music/shims.d.ts
vendored
10
libs/music/shims.d.ts
vendored
@ -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-all-sounds
|
||||
//% blockId=music_stop_all_sounds block="stop all sounds"
|
||||
//% parts="headphone"
|
||||
//% blockNamespace=music
|
||||
//% weight=76 blockGap=8 shim=music::stopAllSounds
|
||||
function stopAllSounds(): void;
|
||||
|
||||
/** Makes a sound bound to a buffer in WAV format. */
|
||||
//% shim=music::fromWAV
|
||||
function fromWAV(buf: Buffer): Sound;
|
||||
|
@ -3,11 +3,16 @@ namespace pxsim.music {
|
||||
export function fromWAV(buf: RefBuffer) {
|
||||
return incr(buf)
|
||||
}
|
||||
|
||||
export function stopAllSounds() {
|
||||
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<void>(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,15 @@ namespace pxsim.SoundMethods {
|
||||
audio.play()
|
||||
})
|
||||
}
|
||||
|
||||
export function stop() {
|
||||
return new Promise<void>(resolve => {
|
||||
if (audio) {
|
||||
audio.pause();
|
||||
numSoundsPlaying--;
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user