From aa636aef9633e7ea8d94858e7a64f29fff14dfaf Mon Sep 17 00:00:00 2001 From: Caitlin Hennessy Date: Thu, 30 Nov 2017 15:49:50 -0800 Subject: [PATCH 1/4] Separate blocks for 'play sound' and 'play sound until done' --- libs/music/_locales/music-jsdoc-strings.json | 13 ++++--------- libs/music/_locales/music-strings.json | 15 ++------------- libs/music/melodies.ts | 1 + libs/music/sounds.ts | 15 ++++++++++++--- 4 files changed, 19 insertions(+), 25 deletions(-) create mode 100644 libs/music/melodies.ts diff --git a/libs/music/_locales/music-jsdoc-strings.json b/libs/music/_locales/music-jsdoc-strings.json index ddbf3375..3a616a81 100644 --- a/libs/music/_locales/music-jsdoc-strings.json +++ b/libs/music/_locales/music-jsdoc-strings.json @@ -11,12 +11,10 @@ "music.fromWAV": "Makes a sound bound to a buffer in WAV format.", "music.noteFrequency": "Get the frequency of a note.", "music.noteFrequency|param|name": "the note name, eg: Note.C", - "music.playSound": "Start playing a sound and don't wait for it to finish.\nNotes are expressed as a string of characters with this format: NOTE[octave][:duration]", - "music.playSoundEffect": "Plays a sound", - "music.playSoundEffect|param|sound": "the sound to play", - "music.playSoundUntilDone": "Play a sound and wait until the sound is done.\nNotes are expressed as a string of characters with this format: NOTE[octave][:duration]", - "music.playSoundUntilDone|param|sound": "the melody to play, eg: music.sounds(Sounds.PowerUp)", - "music.playSound|param|sound": "the melody to play, eg: music.sounds(Sounds.PowerUp)", + "music.playSound": "Start playing a sound and don't wait for it to finish.", + "music.playSoundUntilDone": "Plays a sound", + "music.playSoundUntilDone|param|sound": "the sound to play", + "music.playSound|param|sound": "the sound to play", "music.playTone": "Play a tone through the speaker for some amount of time.", "music.playTone|param|frequency": "pitch of the tone to play in Hertz (Hz)", "music.playTone|param|ms": "tone duration in milliseconds (ms)", @@ -28,8 +26,5 @@ "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.sounds": "Get the melody string for a built-in melody.", - "music.sounds|param|name": "the note name, eg: Note.C", - "music.stopAllSounds": "Stop all sounds from playing.", "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 96657c5d..52485175 100644 --- a/libs/music/_locales/music-strings.json +++ b/libs/music/_locales/music-strings.json @@ -20,28 +20,17 @@ "Note.GSharp|block": "G#", "SoundOutputDestination.Pin|block": "pin", "SoundOutputDestination.Speaker|block": "speaker", - "Sounds.BaDing|block": "ba ding", - "Sounds.JumpDown|block": "jump down", - "Sounds.JumpUp|block": "jump up", - "Sounds.MagicWand|block": "magic wand", - "Sounds.PowerDown|block": "power down", - "Sounds.PowerUp|block": "power up", - "Sounds.Siren|block": "siren", - "Sounds.Wawawawaa|block": "wawawawaa", "music._soundPicker|block": "%sound", "music.beat|block": "%fraction|beat", "music.changeTempoBy|block": "change tempo by %value|(bpm)", "music.noteFrequency|block": "%note", - "music.playSoundEffect|block": "play %sound", - "music.playSoundUntilDone|block": "play sound %sound=music_sounds|until done", - "music.playSound|block": "play sound %sound=music_sounds", + "music.playSoundUntilDone|block": "play sound %sound|until done", + "music.playSound|block": "play sound %sound", "music.playTone|block": "play tone|at %note=device_note|for %duration=device_beat", "music.rest|block": "rest|for %duration=device_beat", "music.ringTone|block": "ring tone|at %note=device_note", "music.setTempo|block": "set tempo to %value|(bpm)", "music.setVolume|block": "set volume %volume", - "music.sounds|block": "%name", - "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/melodies.ts b/libs/music/melodies.ts new file mode 100644 index 00000000..fc78e0c7 --- /dev/null +++ b/libs/music/melodies.ts @@ -0,0 +1 @@ +namespace music {} \ No newline at end of file diff --git a/libs/music/sounds.ts b/libs/music/sounds.ts index 845c97c8..fe5e6152 100644 --- a/libs/music/sounds.ts +++ b/libs/music/sounds.ts @@ -260,11 +260,10 @@ namespace music { * Plays a sound * @param sound the sound to play */ - //% blockId=music_play_sound_effect block="play %sound" + //% blockId=music_play_sound_until_done block="play sound %sound|until done" //% weight=98 - export function playSoundEffect(sound: Sound) { + export function playSoundUntilDone(sound: Sound) { if (!sound) return; - sound.play(); } @@ -277,4 +276,14 @@ namespace music { export function _soundPicker(sound: Sound): Sound { return sound; } + + /** + * Start playing a sound and don't wait for it to finish. + * @param sound the sound to play + */ + //% blockId=music_play_sound block="play sound %sound" + //% weight=99 + export function playSound(sound: Sound) { + control.runInBackground(() => sound.play()); + } } From 330aff8082382f55cfd2f48be346328347bb90ca Mon Sep 17 00:00:00 2001 From: Caitlin Hennessy Date: Fri, 1 Dec 2017 09:59:59 -0800 Subject: [PATCH 2/4] Restore 'play sound effect' name --- libs/music/_locales/music-jsdoc-strings.json | 8 ++++---- libs/music/_locales/music-strings.json | 4 ++-- libs/music/sounds.ts | 8 ++++---- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/libs/music/_locales/music-jsdoc-strings.json b/libs/music/_locales/music-jsdoc-strings.json index 3a616a81..2d67cdcf 100644 --- a/libs/music/_locales/music-jsdoc-strings.json +++ b/libs/music/_locales/music-jsdoc-strings.json @@ -11,10 +11,10 @@ "music.fromWAV": "Makes a sound bound to a buffer in WAV format.", "music.noteFrequency": "Get the frequency of a note.", "music.noteFrequency|param|name": "the note name, eg: Note.C", - "music.playSound": "Start playing a sound and don't wait for it to finish.", - "music.playSoundUntilDone": "Plays a sound", - "music.playSoundUntilDone|param|sound": "the sound to play", - "music.playSound|param|sound": "the sound to play", + "music.playSoundEffect": "Start playing a sound and don't wait for it to finish.", + "music.playSoundEffectUntilDone": "Plays a sound", + "music.playSoundEffectUntilDone|param|sound": "the sound to play", + "music.playSoundEffect|param|sound": "the sound to play", "music.playTone": "Play a tone through the speaker for some amount of time.", "music.playTone|param|frequency": "pitch of the tone to play in Hertz (Hz)", "music.playTone|param|ms": "tone duration in milliseconds (ms)", diff --git a/libs/music/_locales/music-strings.json b/libs/music/_locales/music-strings.json index 52485175..a74eae44 100644 --- a/libs/music/_locales/music-strings.json +++ b/libs/music/_locales/music-strings.json @@ -24,8 +24,8 @@ "music.beat|block": "%fraction|beat", "music.changeTempoBy|block": "change tempo by %value|(bpm)", "music.noteFrequency|block": "%note", - "music.playSoundUntilDone|block": "play sound %sound|until done", - "music.playSound|block": "play sound %sound", + "music.playSoundEffectUntilDone|block": "play sound effect %sound|until done", + "music.playSoundEffect|block": "play sound effect %sound", "music.playTone|block": "play tone|at %note=device_note|for %duration=device_beat", "music.rest|block": "rest|for %duration=device_beat", "music.ringTone|block": "ring tone|at %note=device_note", diff --git a/libs/music/sounds.ts b/libs/music/sounds.ts index fe5e6152..464b0e5f 100644 --- a/libs/music/sounds.ts +++ b/libs/music/sounds.ts @@ -260,9 +260,9 @@ namespace music { * Plays a sound * @param sound the sound to play */ - //% blockId=music_play_sound_until_done block="play sound %sound|until done" + //% blockId=music_play_sound_effect_until_done block="play sound effect %sound|until done" //% weight=98 - export function playSoundUntilDone(sound: Sound) { + export function playSoundEffectUntilDone(sound: Sound) { if (!sound) return; sound.play(); } @@ -281,9 +281,9 @@ namespace music { * Start playing a sound and don't wait for it to finish. * @param sound the sound to play */ - //% blockId=music_play_sound block="play sound %sound" + //% blockId=music_play_sound_effect block="play sound effect %sound" //% weight=99 - export function playSound(sound: Sound) { + export function playSoundEffect(sound: Sound) { control.runInBackground(() => sound.play()); } } From 064d6f9411ac7991a2f6e4c5dcf3bd1d2d42831c Mon Sep 17 00:00:00 2001 From: Caitlin Hennessy Date: Mon, 4 Dec 2017 12:24:41 -0800 Subject: [PATCH 3/4] Check if sound not defined --- libs/music/sounds.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/libs/music/sounds.ts b/libs/music/sounds.ts index 464b0e5f..42c9be73 100644 --- a/libs/music/sounds.ts +++ b/libs/music/sounds.ts @@ -284,6 +284,7 @@ namespace music { //% blockId=music_play_sound_effect block="play sound effect %sound" //% weight=99 export function playSoundEffect(sound: Sound) { + if (!sound) return; control.runInBackground(() => sound.play()); } } From 4e4aa266d5347f2c5d35460251a8d856f9c6061b Mon Sep 17 00:00:00 2001 From: Caitlin Hennessy Date: Thu, 7 Dec 2017 11:27:29 -0800 Subject: [PATCH 4/4] Limit sound concurrency --- libs/music/sounds.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/libs/music/sounds.ts b/libs/music/sounds.ts index 42c9be73..44c7cef0 100644 --- a/libs/music/sounds.ts +++ b/libs/music/sounds.ts @@ -256,6 +256,9 @@ namespace sounds { } namespace music { + let numSoundsPlaying = 0; + let soundsLimit = 3; + /** * Plays a sound * @param sound the sound to play @@ -284,7 +287,8 @@ namespace music { //% blockId=music_play_sound_effect block="play sound effect %sound" //% weight=99 export function playSoundEffect(sound: Sound) { - if (!sound) return; - control.runInBackground(() => sound.play()); + if (!sound || numSoundsPlaying >= soundsLimit) return; + numSoundsPlaying++; + control.runInBackground(() => {sound.play(); numSoundsPlaying--;}); } }