2010-03-11 186 views
1

我有一個音頻項目,我正在使用來自Un4seen的BASS。這個庫主要使用BYTES,但我有一個轉換,讓我以毫秒爲單位顯示歌曲的當前位置。將毫秒轉換爲時間碼

明知MS =樣品* 1000 /採樣率 和樣品=字節* 8 /位/通道

因此,這裏是我的主要問題,這是相當簡單的......我在我的項目中的功能轉換分鐘中的毫秒到時間碼:秒:毫秒。

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 

現在問題出現在秒計算中。任何時候MS計算結束.5秒的地方都會到達下一秒。所以1.5秒實際打印2.5秒。我知道肯定使用Int轉換導致了一輪下降,我知道我的數學是正確的,因爲我已經在計算器中檢查了100次。我無法弄清楚爲什麼這個數字是四捨五入的。有什麼建議麼?

+0

這裏有一個功能我離開了: 專用功能CheckForLeadingZero(爲ByRef的intValue作爲整數,可選BYVAL intLength作爲整數= 2)作爲字符串 昏暗strValue中作爲字符串 將strValue = CStr的(的intValue) 如果len (strValue中) Jeff 2010-03-11 12:27:49

回答

1

。在你的秒轉換邏輯的一個缺陷。例如,假設你想轉換1500毫秒。你的代碼來計算秒:

Int((lngCurrentMSTimeValue/1000) Mod 60) 

會返回2. 1500毫秒不超過兩秒!爲了計算秒,由1000執行毫秒整數除法(在「\」運算符):

(lngCurrentMSTimeValue \ 1000) Mod 60 

如預期這將返回1。以下功能是您所需要的。它甚至通過使用內置的格式化功能省去了您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 
+0

誰的woulda咚呢?那裏烏鴉的偉大反應!喜歡學習關於VB6的新東西,我不知道。非常感謝那裏的幫助。我也沒有考慮過內置的Format函數,因爲我用同名的DLL中的屬性替換了Format函數。我從那以後爲這個不好的約定找到了一個新的名字。 – Jeff 2010-03-29 20:59:34

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 

端功能

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 
} 
+0

這是不是一個好主意,發佈相同的答案是什麼是相當舊的帖子,已經有一個接受的答案。 – Kev 2011-09-18 22:57:21