Calculate playback position progress/percentage correctly

Started by Miran,

Miran

I'm calculating playback progress with code:
auto bytePos = BASS_ChannelGetPosition(streamInternal, BASS_POS_BYTE);
if (bytePos == -1) bytePos = 0; // error or not available yet
auto pos = BASS_ChannelBytes2Seconds(streamInternal, bytePos);

auto byteTotal = BASS_ChannelGetLength(streamInternal, BASS_POS_BYTE);
auto total = BASS_ChannelBytes2Seconds(streamInternal, byteTotal);

double peercent = 100.0 * pos / total;

Issue is, it reports values like 99.91 after playback finishes and BASS_ChannelIsActive returns BASS_ACTIVE_STOPPED.
It happens for files longer than few seconds and when BASS_ChannelSetPosition was used to start playback somewhere in the middle.
Previously I was performing bytePos / byteTotal with same results.

Miran

Adding BASS_STREAM_PRESCAN flag when calling BASS_StreamCreateFile solved the problem, even for +150 MB files.

Ian @ un4seen

That sounds like you probably have a VBR MP3 file without a VBR header? The default length estimate can be quite inaccurate in that case, but the BASS_STREAM_PRESCAN flag enables pre-scanning of the file to get the correct length (and seek points).

If you play/decode a file without the BASS_STREAM_PRESCAN flag then the length will be updated at the end, except if you seeked because the seek will have been inaccurate (which means the final position is also inaccurate). That's why you had the problem when using BASS_ChannelSetPosition.