将毫秒转换为时间码

时间:2010-03-11 12:25:49

标签: vb6 time

我有一个音频项目,我正在使用Un4seen的BASS。这个库主要使用BYTES,但我有一个转换位置,让我以毫秒为单位显示歌曲的当前位置。

知道MS = Samples * 1000 / SampleRate 并且Samples = Bytes * 8 / Bits / Channels

所以这是我的主要问题,而且相当简单......我的项目中有一个函数,可以在毫秒内将毫秒转换为TimeCode:秒:毫秒。

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

现在问题出现在Seconds计算中。无论何时MS计算结束.5秒的位置都会向上舍入到下一秒。所以1.5秒实际打印为2.5秒。我确信使用 Int 转换导致向下舍入,我知道我的数学是正确的,因为我已经在计算器中检查了100次。我无法弄清楚为什么数字会四舍五入。有什么建议吗?

3 个答案:

答案 0 :(得分:1)

你的秒转换逻辑有一个缺陷。例如,假设您要转换1500毫秒。你计算秒数的代码:

Int((lngCurrentMSTimeValue / 1000) Mod 60)

将返回2. 1500毫秒不超过两秒!要计算秒数,请执行毫秒的整数除法(“\”运算符)1000:

(lngCurrentMSTimeValue \ 1000) Mod 60

按预期返回1。您只需要以下功能。它甚至通过使用内置的Format函数消除了对CheckForLeadingZero函数的需求:

Public Function ConvertMStoTimeCode(ByVal lngCurrentMSTimeValue As Long)

  Dim minutes As Long
  Dim seconds As Long
  Dim milliseconds As Long

  minutes = (lngCurrentMSTimeValue / 1000) \ 60
  seconds = (lngCurrentMSTimeValue \ 1000) Mod 60
  milliseconds = lngCurrentMSTimeValue Mod 1000

  ConvertMStoTimeCode = Format(minutes, "00") & ":" & Format(seconds, "00") & _
    ":" & Format(milliseconds, "000")

End Function

答案 1 :(得分:0)

现在这可行:

Public Function ConvertMStoTimeCode(ByVal lngCurrentMSTimeValue As Long)
Dim strMinute As String
Dim strSecond As String
Dim strFrames As String

strMinute = CheckForLeadingZero(Fix(lngCurrentMSTimeValue / 1000 / 60))
strSecond = CheckForLeadingZero(Int((lngCurrentMSTimeValue / 1000) Mod 60))
strFrames = CheckForLeadingZero(Int((lngCurrentMSTimeValue / 10) Mod 100))

If (strFrames > 49) Then
    strSecond = CheckForLeadingZero(Int((lngCurrentMSTimeValue / 1000) Mod 60) - 1)
End If

ConvertMStoTimeCode = strMinute & ":" & strSecond & ":" & strFrames

结束功能

答案 2 :(得分:0)

这是一个处理/ Java等价物,可以很容易地重新调整用途。

String timecodeString(int fps) {
  float ms = millis();
  return String.format("%02d:%02d:%02d+%02d", floor(ms/1000/60/60),    // H
                                              floor(ms/1000/60),       // M
                                              floor(ms/1000%60),       // S
                                              floor(ms/1000*fps%fps)); // F
}
相关问题