Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/Syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ Note that when Snap! encodes a function to LISP syntax it automatically recogniz
| | go back _ layers | **back** | | ` (back 1) ` |
| **sound** | play sound _ | **play** | | ` (play nil) ` |
| | play sound _ until done | **playAll** | | ` (playAll nil) ` |
| | stop sound _ | **stopSound** | | ` (stopSound nil) ` |
| | stop all sounds | **stopSounds** | | ` (stopSounds) ` |
| | play sound _ at _ Hz | **playAt** | | ` (playAt nil 44100) ` |
| | _ of sound _ | **sound** | *reporter* | ` (sound [duration] nil) ` |
Expand Down
14 changes: 14 additions & 0 deletions src/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,13 @@ SpriteMorph.prototype.primitiveBlocks = function () {
(prim t doPlaySoundAtRate target rate)
(play (newSound (sound [samples] (get target)) (get rate))))`
},
doStopSound: {
type: 'command',
category: 'sound',
spec: 'stop sound %snd',
defaults: [''],
code: 'stopSound'
},
doStopAllSounds: {
type: 'command',
category: 'sound',
Expand Down Expand Up @@ -3658,6 +3665,7 @@ SpriteMorph.prototype.blockTemplates = function (

blocks.push(block('playSound'));
blocks.push(block('doPlaySoundUntilDone'));
blocks.push(block('doStopSound'));
blocks.push(block('doStopAllSounds'));
blocks.push('-');
blocks.push(block('doPlaySoundAtRate'));
Expand Down Expand Up @@ -10908,6 +10916,7 @@ StageMorph.prototype.blockTemplates = function (

blocks.push(block('playSound'));
blocks.push(block('doPlaySoundUntilDone'));
blocks.push(block('doStopSound'));
blocks.push(block('doStopAllSounds'));
blocks.push('-');
blocks.push(block('doPlaySoundAtRate'));
Expand Down Expand Up @@ -11896,6 +11905,11 @@ StageMorph.prototype.addSound
StageMorph.prototype.doPlaySound
= SpriteMorph.prototype.doPlaySound;

StageMorph.prototype.stopSound = function (src) {
this.activeSounds.forEach(audio => {if (src === audio.src) audio.pause()});
this.activeSounds = this.activeSounds.filter(audio => audio.src === src);
};

StageMorph.prototype.stopAllActiveSounds = function () {
this.activeSounds.forEach(audio => audio.pause());
this.activeSounds = [];
Expand Down
11 changes: 11 additions & 0 deletions src/threads.js
Original file line number Diff line number Diff line change
Expand Up @@ -4021,6 +4021,17 @@ Process.prototype.doPlaySoundUntilDone = function (name) {
this.pushContext();
};

Process.prototype.doStopSound = function (name) {
var stage = this.homeContext.receiver.parentThatIsA(StageMorph);
var rcvr = this.blockReceiver();
rcvr.shadowAttribute('sounds');

if (stage && rcvr.sounds.contents.some(sound => sound.name === name)) {
stage.stopSound(rcvr.sounds.contents.find(
sound => sound.name === name).audio.src);
}
};

Process.prototype.doStopAllSounds = function () {
var stage = this.homeContext.receiver.parentThatIsA(StageMorph);
if (stage) {
Expand Down