# Have trouble stopping the sound

**URL:** <https://forum.babylonjs.com/t/have-trouble-stopping-the-sound/10040>\
**Category:** Questions\
**Created:** [April 16, 2020, 3:33am UTC](https://forum.babylonjs.com/t/have-trouble-stopping-the-sound/10040 "2020-04-16T03:33:04Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![FBa](https://avatars.discourse-cdn.com/v4/letter/f/f4b2a3/32.png) [@FBa](https://forum.babylonjs.com/u/FBa)\
**Post date:** [April 16, 2020, 3:33am UTC](https://forum.babylonjs.com/t/have-trouble-stopping-the-sound/10040/1 "2020-04-16T03:33:04Z")

</div>

I have an odd situation that I can’t seem to figure out what’s happening.  
I call for a sound effect to be played at 00:30 later. It’s something like this:

a.play(30, x, x);

But because of certain changes, I would like to stop that before it has started at 00:20… so I call

a.stop(); //instead of using dispose because I might need to use this sound again

But the sound still plays…and I couldn’t stop it from playing.

---

<div class="post-metadata">

**Author:** ![withADoveInOneHand](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/withadoveinonehand/32/1243_2.png) [@withADoveInOneHand](https://forum.babylonjs.com/u/withADoveInOneHand)\
**Post date:** [April 16, 2020, 5:15am UTC](https://forum.babylonjs.com/t/have-trouble-stopping-the-sound/10040/2 "2020-04-16T05:15:54Z")

</div>

Maybe try play(0) before stop(0)?

Maybe ISOUNDOPTIONS needs a “queued” t-minus property?

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [April 16, 2020, 1:10pm UTC](https://forum.babylonjs.com/t/have-trouble-stopping-the-sound/10040/3 "2020-04-16T13:10:19Z")

</div>

In your case the easiest might be to do the timing manually:

```auto
var timeout = setTimeout(() => {
  a.play();
}, 30000);

...

// Stop it
clearTimeout(timeout);
a.stop();

```

---

<div class="post-metadata">

**Author:** ![FBa](https://avatars.discourse-cdn.com/v4/letter/f/f4b2a3/32.png) [@FBa](https://forum.babylonjs.com/u/FBa)\
**Post date:** [April 16, 2020, 1:27pm UTC](https://forum.babylonjs.com/t/have-trouble-stopping-the-sound/10040/4 "2020-04-16T13:27:50Z")

</div>

Unfortunately that doesn’t work in my case. Here is the issue, it’s an automated piano-playing system. So it’s calling all the notes at once based on the database (with onsets and pitches) I have. The methods above still can’t kill the sound.

for (var i = 0; i \< pitches.length; i++) {  
btff[thenote].setVolume(1, 0.01);  
btff[thenote].play(onsets[i] / 1000 \* usertempo, 0, lengths[i] / 1000 \* usertempo);}

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [April 16, 2020, 2:10pm UTC](https://forum.babylonjs.com/t/have-trouble-stopping-the-sound/10040/5 "2020-04-16T14:10:29Z")

</div>

I do not understand, cause in this case it would have not even played, could you create a repro in the playground ???

Only one sound is enough.

---

<div class="post-metadata">

**Author:** ![FBa](https://avatars.discourse-cdn.com/v4/letter/f/f4b2a3/32.png) [@FBa](https://forum.babylonjs.com/u/FBa)\
**Post date:** [April 16, 2020, 2:39pm UTC](https://forum.babylonjs.com/t/have-trouble-stopping-the-sound/10040/6 "2020-04-16T14:39:38Z")

</div>

for (var i = 0; i \< pitches.length; i++) {  
musics[thenote].setVolume(1, 0.01);  
musics[thenote].play(onsets[i] / 1000 \* usertempo, 0, lengths[i] / 1000 \* usertempo);  
}

var StopMusic = function stopmus() {  
for (var i = 0; i \< 88; i++)  
musics[i].stop();  
}

---

<div class="post-metadata">

**Author:** ![JCPalmer](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jcpalmer/32/42_2.png) [@JCPalmer](https://forum.babylonjs.com/u/JCPalmer)\
**Post date:** [April 16, 2020, 3:30pm UTC](https://forum.babylonjs.com/t/have-trouble-stopping-the-sound/10040/7 "2020-04-16T15:30:13Z")

</div>

Think you would better to not use `BABYLON.Sound`, but WebAudio directly. Once something is scheduled on the audioContext, there is no way to stop it. It can be delayed using `context.suspend()`, but will resume later.

You can actually just `context.close()`, & then create a new context. Not sure if you can do that to the `BABYLON.Sound` context cleanly.

---

<div class="post-metadata">

**Author:** ![JCPalmer](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jcpalmer/32/42_2.png) [@JCPalmer](https://forum.babylonjs.com/u/JCPalmer)\
**Post date:** [April 16, 2020, 3:53pm UTC](https://forum.babylonjs.com/t/have-trouble-stopping-the-sound/10040/8 "2020-04-16T15:53:15Z")

</div>

Edit: try `sound.dispose()`.

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [April 16, 2020, 3:56pm UTC](https://forum.babylonjs.com/t/have-trouble-stopping-the-sound/10040/9 "2020-04-16T15:56:29Z")

</div>

This should work:

```auto
for (var i = 0; i < pitches.length; i++) { 
  musics[thenote].setVolume(1, 0.01);
  musics[thenote].timeout = setTimeout(() => {
    musics[thenote].play(0, 0, lengths[i] / 1000 * usertempo);
  }, onsets[i] / 1000 * usertempo);
}

var StopMusic = function stopmus() {
for (var i = 0; i < 88; i++)
  clearTimeout(musics[i].timeout);
  musics[i].stop();
}

```

---

<div class="post-metadata">

**Author:** ![FBa](https://avatars.discourse-cdn.com/v4/letter/f/f4b2a3/32.png) [@FBa](https://forum.babylonjs.com/u/FBa)\
**Post date:** [April 16, 2020, 4:24pm UTC](https://forum.babylonjs.com/t/have-trouble-stopping-the-sound/10040/10 "2020-04-16T16:24:40Z")

</div>

I see.

When I use the code below, I get only one pitch all the way without the other 87 pitches…LOL

Also, the stop function still doesn’t’ work

for (var i = 0; i \< pitches.length; i++) {  
thenote = c88(pitches[i].replace(“t”, “”));  
musics[thenote].timeout = setTimeout(() =\> {  
musics[thenote].setVolume(1, 0.01);  
musics[thenote].play(0, 0, lengths[i] / 1000 \* usertempo);  
}, onsets[i] \* usertempo);  
}

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [April 16, 2020, 4:30pm UTC](https://forum.babylonjs.com/t/have-trouble-stopping-the-sound/10040/11 "2020-04-16T16:30:33Z")

</div>

I guess musics[thenote].setVolume(1, 0.01); should be before the setTimeout 🙂 but could you share a playground ??? it would be way easier ?

---

<div class="post-metadata">

**Author:** ![FBa](https://avatars.discourse-cdn.com/v4/letter/f/f4b2a3/32.png) [@FBa](https://forum.babylonjs.com/u/FBa)\
**Post date:** [April 16, 2020, 4:38pm UTC](https://forum.babylonjs.com/t/have-trouble-stopping-the-sound/10040/12 "2020-04-16T16:38:31Z")

</div>

I tried that as well. Didn’t work. Sorry I’m not able to share. But here is the working product (without the stop function): [www.findbach.com/beta4](http://www.findbach.com/beta4) and [www.findbach.com/beta5](http://www.findbach.com/beta5)

---

<div class="post-metadata">

**Author:** ![JCPalmer](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jcpalmer/32/42_2.png) [@JCPalmer](https://forum.babylonjs.com/u/JCPalmer)\
**Post date:** [April 16, 2020, 5:27pm UTC](https://forum.babylonjs.com/t/have-trouble-stopping-the-sound/10040/13 "2020-04-16T17:27:22Z")

</div>

Gotta say BABYLON.Sound is geared to playing sound files, not making music with many, many short duration buffers. I see you are an actual musician. If you are ok with using audio that you did not originate, [WebAudioFonts](https://github.com/surikov/webaudiofont) is a library which handles what you wish to do in a more direct way.

I am doing not doing 88 keys, just 72, but here is a test file I made about 6 weeks ago. [https://youtu.be/O-pK260uKxw](https://youtu.be/O-pK260uKxw)

---

<div class="post-metadata">

**Author:** ![FBa](https://avatars.discourse-cdn.com/v4/letter/f/f4b2a3/32.png) [@FBa](https://forum.babylonjs.com/u/FBa)\
**Post date:** [April 16, 2020, 5:48pm UTC](https://forum.babylonjs.com/t/have-trouble-stopping-the-sound/10040/14 "2020-04-16T17:48:00Z")

</div>

Very cool. I’ll check this out! So it’s possible to combine my 88 keys on BabylonJS with sounds from WebAudioFonts, right?

---

<div class="post-metadata">

**Author:** ![JCPalmer](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jcpalmer/32/42_2.png) [@JCPalmer](https://forum.babylonjs.com/u/JCPalmer)\
**Post date:** [April 16, 2020, 6:10pm UTC](https://forum.babylonjs.com/t/have-trouble-stopping-the-sound/10040/15 "2020-04-16T18:10:34Z")

</div>

No, there are “fonts” of the various instruments, hundreds, which are in this repo,[https://github.com/surikov/webaudiofontdata](https://github.com/surikov/webaudiofontdata).

That repo is implemented as a web site. You can check out each instrument, from here [https://surikov.github.io/webaudiofontdata/sound/](https://surikov.github.io/webaudiofontdata/sound/)

Once you have an instrument you want (that’s why I asked if you were ok with not originating the audio), view the corresponding code from the repo to get you started.

he also has his own threeJS based collaboration tool, [https://github.com/surikov/riffshare](https://github.com/surikov/riffshare)

Here is an [example](https://surikov.github.io/riffshare/parse.html?riff=78-30030400-a686a234-0f1109080f13080d0f10-010102010301040105010601074140104110421043104410451046104710a010a111a211a311a411a511a611a711e740-023020440023020b40025020440040021040045020440063020a40065020a400800409400850409400c30204400c50204400e0040b400e00404400e5040440123020440123020b40125020440140021040145020440163020a40165020a401800409401850409401c30204401c50204401e0040b401e00404401e5040440223020440223020b40225020440240021040245020440263020a40265020a402800409402850409402c30204402c50204402e0040e402e00407402e5040740323020440325020440340020d40340020640345020640363020440365020440380020d403800206403850206403a3020e403a30207403a50207403c30206403c3020d403c50206403e00404403e0040b403e5040440) from Metallica.

You can of course run your code with BabylonJS too.

---

<div class="post-metadata">

**Author:** ![FBa](https://avatars.discourse-cdn.com/v4/letter/f/f4b2a3/32.png) [@FBa](https://forum.babylonjs.com/u/FBa)\
**Post date:** [April 16, 2020, 6:25pm UTC](https://forum.babylonjs.com/t/have-trouble-stopping-the-sound/10040/16 "2020-04-16T18:25:19Z")

</div>

Right now I’m working on a hybrid. Why does it keep cycling on one pitch? Am I missing something in my JS? Timing seems right.

var AudioContextFunc = window.AudioContext || window.webkitAudioContext;  
var audioContext = new AudioContextFunc();  
var player = new WebAudioFontPlayer();  
player.loader.decodeAfterLoading(audioContext, ‘\_tone\_0001\_GeneralUserGS\_sf2\_file’);  
for (var i = 0; i \< pitches.length; i++) {  
thenote = c88(pitches[i].replace(“t”, “”));  
setTimeout(() =\> {  
player.queueWaveTable(audioContext, audioContext.destination, \_tone\_0001\_GeneralUserGS\_sf2\_file, 0, thenote + 21, lengths[i] / 1000 \* usertempo, 0.5);  
}, onsets[i] \* usertempo);  
}

---

<div class="post-metadata">

**Author:** ![FBa](https://avatars.discourse-cdn.com/v4/letter/f/f4b2a3/32.png) [@FBa](https://forum.babylonjs.com/u/FBa)\
**Post date:** [April 16, 2020, 6:37pm UTC](https://forum.babylonjs.com/t/have-trouble-stopping-the-sound/10040/17 "2020-04-16T18:37:42Z")

</div>

I think thenote inside the timeout function is executed not like thenote outside the timeout function… Is there a way to pass thenote inside the settimeout function?

when I tried console.log(thenote) inside and outside the settimeout function…they are different. one updates, the other got stuck

---

<div class="post-metadata">

**Author:** ![FBa](https://avatars.discourse-cdn.com/v4/letter/f/f4b2a3/32.png) [@FBa](https://forum.babylonjs.com/u/FBa)\
**Post date:** [April 16, 2020, 6:39pm UTC](https://forum.babylonjs.com/t/have-trouble-stopping-the-sound/10040/18 "2020-04-16T18:39:08Z")

</div>

I think thenote inside the timeout function is executed not like thenote outside the timeout function… Is there a way to pass thenote inside the settimeout function?

when I tried console.log(thenote) inside and outside the settimeout function…they are different. one updates, the other got stuck

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [April 16, 2020, 6:39pm UTC](https://forum.babylonjs.com/t/have-trouble-stopping-the-sound/10040/19 "2020-04-16T18:39:31Z")

</div>

ohhhhhh my bad it is due to the capture in the loooooop …

you can simply do ☹

(let i = 0; i \< pitches.length; i++) {  
thenote = c88(pitches[i].replace(“t”, “”));  
setTimeout(() =\> {  
player.queueWaveTable(audioContext, audioContext.destination, \_tone\_0001\_GeneralUserGS\_sf2\_file, 0, thenote + 21, lengths[i] / 1000 \* usertempo, 0.5);  
}, onsets[i] \* usertempo);  
}

basically replace the var with a let…

---

<div class="post-metadata">

**Author:** ![FBa](https://avatars.discourse-cdn.com/v4/letter/f/f4b2a3/32.png) [@FBa](https://forum.babylonjs.com/u/FBa)\
**Post date:** [April 16, 2020, 6:40pm UTC](https://forum.babylonjs.com/t/have-trouble-stopping-the-sound/10040/20 "2020-04-16T18:40:16Z")

</div>

sorry I don’t see the let…

[Next page](https://forum.babylonjs.com/t/have-trouble-stopping-the-sound/10040.md?page=2)
