goldfire / howler.js

Javascript audio library for the modern web.
https://howlerjs.com
MIT License
23.85k stars 2.23k forks source link

What determines whether a sound is played "in parallell" with itself or not? #326

Closed objarni closed 9 years ago

objarni commented 9 years ago

What determines whether a sound is played "in parallell" with itself or not? I cannot find it in the documentation on howlerjs.com.

I've asked the same question in the website comments, but get no answer. I'd like to help out documenting this better as I find howler.js to be the most promising JS audio lib I've found so far ...

Reproduce on howlerjs.com:

  1. On first example, press "Play" several times. Notice the music is re-started every time.
  2. On last example, press "Play winner" 3 time quickly. Notice the sound is played in parallell, not re-started.
objarni commented 9 years ago

This could abstractly be viewed as categorising a sound as either a "sample" (think game effect sound), or "music" (think audio tag / media player).

objarni commented 9 years ago

Bump.

sergebat commented 9 years ago

It actually looks pretty simple.

This is the code for the music buttons:

sound2.stop().play();

And this is the code for sound fx button:

sound3.play('blast');

I'd agree it is bit confusing when source code on the screen is not 1-to-1 match with actual JS. :)

objarni commented 9 years ago

Thank you but I can only see one api play call?

sergebat commented 9 years ago

Below is slightly shortened complete javascript from the http://goldfirestudios.com/blog/104/howler.js-Modern-Web-Audio-Javascript-Library:

$(function(){
    hljs.initHighlightingOnLoad();

    $('#ex1-play').on('click', function(){
        sound1.stop().play();
    });
    $('#ex1-pause').on('click', function(){
        sound1.pause();
    });
    $('#ex1-stop').on('click', function(){
        sound1.stop();
    });
    $('#ex1-loop').on('click', function(){
        sound1.loop(true);
    });

    $('#ex3-play1').on('click', function(){
        sound3.play('blast');
    });
    $('#ex3-play2').on('click', function(){
        sound3.play('laser');
    });
    $('#ex3-play3').on('click', function(){
        sound3.play('winner');
    });
});

var sound1 = new Howl({
  urls: ['/proj/howlerjs/sound.ogg', '/proj/howlerjs/sound.mp3']
});
var sound3 = new Howl({
  urls: ['/proj/howlerjs/sounds.ogg', '/proj/howlerjs/sounds.mp3'],
  sprite: {
    blast: [0, 2000],
    laser: [3000, 700],
    winner: [5000, 9000]
  }
});
objarni commented 9 years ago

So the API is that, if you use a named fx, api play(id), it is played asynchronosly to any previous instance of the sound, and if you use the play(), no id specified, it discontinues previous playback...?

It could be a lot more explicit in the documentation.

goldfire commented 9 years ago

Take a look at the 2.0 branch, this concept should be cleared up much better (though any suggestions for improvements to the new docs are welcome).