goldfire / howler.js

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

Duration calculation in sprites are wrong (?) #640

Closed Theodeus closed 7 years ago

Theodeus commented 7 years ago
 // Determine how long to play for and where to start playing.
  var seek = sound._seek > 0 ? sound._seek : self._sprite[sprite][0] / 1000;
  var duration = ((self._sprite[sprite][0] + self._sprite[sprite][1]) / 1000) - seek;

This doesn't look right, or perhaps I'm not getting how the sprite definition works. :) So I have this:

sound = new Howl({
        src: ['assets/audio/effects/sprite.ogg', 'assets/audio/effects/sprite.mp3'],
        sprite: {
           ...
            cloud_out: [4132, 6130],
            loading: [6131, 8630],
            jingle: [8631, 10110],
           ...
        }
    });

... and in this case howler thinks the loading part of the sprite is 8.629999999999999 seconds long. I've just assumed I'm giving the start and end time of the sprite in milliseconds. If I'm correct in that assumption, shouldn't the calculation just be

 var duration = ((self._sprite[sprite][1] - self._sprite[sprite][0]) / 1000);

I'd be happy to submit a PR if I'm right. I'd also be happy to be corrected if I'm wrong. :)

Theodeus commented 7 years ago

Oh, I'm at line 628 in howler.core.js.

goldfire commented 7 years ago

The sprite definition is [start, duration], not [start, end]. If you look at https://howlerjs.com/#sprite you'll see that sprites are indeed working correctly.

Theodeus commented 7 years ago

Ah, gotcha! I thought I'd read the entire documentation, but what do you know. :)

The fact that the numbers in the quickstart were sequential totally threw me off! Thanks for the quick reply!

goldfire commented 7 years ago

Yeah that is a good point, I should probably change that as I can see how that is confusing.

Theodeus commented 7 years ago

By the way, why not just do

var duration = self._sprite[sprite][1] / 1000;
goldfire commented 7 years ago

The seek position isn't necessarily the starting position of the sprite. That would cause the duration to be incorrect in the event that you started to play, paused and then resumed.

Theodeus commented 7 years ago

Aah. Gotcha!