bbc / peaks.js

JavaScript UI component for interacting with audio waveforms
https://waveform.prototyping.bbc.co.uk
GNU Lesser General Public License v3.0
3.2k stars 279 forks source link

Issue with current time. #530

Closed vivekd95 closed 3 months ago

vivekd95 commented 3 months ago

Hi @chrisn

When working with zoomview, a new issue is found (checked in the latest release). The current time is getting rounded off (to the ceiling value) when the value after 3 decimals starts with 9 like 0.346999. This value will get rounded off to 0.347 when we use player.getCurrentTime() while on zoomview it will show the value before rounding off. Please take a look at the screenshots I've attached below.

Screenshot of Chrome's console Screenshot from 2024-06-13 12-36-38

Screenshot of the zoomview Screenshot from 2024-06-13 12-36-16

chrisn commented 3 months ago

The code intentionally floors (i.e., rounds towards zero), so in your example 0.346999 will display as 0.346.

chrisn commented 3 months ago

What you're seeing is the effect of different floating point rounding implementations, when converting a floating point number to a string. Peaks.js does it one way, the browser console does it differently. The underlying value is still the same, though, and there's no loss of precision.

vivekd95 commented 3 months ago

So, the value that I am getting should be the same? If yes, I tried several more things and discovered these screenshot values.

Screenshot of Chrome's console image_2024_06_14T06_10_54_601Z

Screenshot of zoomview image_2024_06_14T06_10_26_398Z

Screenshot of the event triggered image_2024_06_14T06_09_55_652Z

So, is the console showing 2 at the end from the browser's side? The main issue that I am facing is getting the difference of 0.001 when I create in my example.

chrisn commented 3 months ago

So, the value that I am getting should be the same?

No, I wouldn't expect the values to be the same, because the Peaks.js library uses a different algorithm to convert the number to a string than the browser console.

The formatPlayheadTime option allows you to format the value differently, if you want to.

chrisn commented 3 months ago

I'll close this, but please re-open if you think more discussion or changes are needed.