I have an audio project I’m working on using BASS from Un4seen. This library uses BYTES mainly but I have a conversion in place that let’s me show the current position of the song in Milliseconds.
Knowing that MS = Samples * 1000 / SampleRate
and that Samples = Bytes * 8 / Bits / Channels
So here’s my main issue and it’s fairly simple… I have a function in my project that converts the Milliseconds to TimeCode in Mins:Secs:Milliseconds.
Public Function ConvertMStoTimeCode(ByVal lngCurrentMSTimeValue As Long)
ConvertMStoTimeCode = CheckForLeadingZero(Fix(lngCurrentMSTimeValue / 1000 / 60)) & ":" & _
CheckForLeadingZero(Int((lngCurrentMSTimeValue / 1000) Mod 60)) & ":" & _
CheckForLeadingZero(Int((lngCurrentMSTimeValue / 10) Mod 100))
End Function
Now the issue comes within the Seconds calculation. Anytime the MS calculation is over .5 the seconds place rounds up to the next second. So 1.5 seconds actually prints as 2.5 seconds. I know for sure that using the Int conversion causes a round down and I know my math is correct as I’ve checked in a calculator 100 times. I can’t figure out why the number is rounding up. Any suggestions?
There is a flaw in your seconds conversion logic. For instance, suppose you want to convert 1500 ms. Your code to calculate seconds:
Would return 2. 1500 ms is not over two seconds! To calculate seconds, perform integer division (the “\” operator) of the milliseconds by 1000:
This returns 1 as expected. The following function is all you need. It even eliminates the need for your CheckForLeadingZero function by using the built-in Format function: