1
我想將音頻文件規格化爲最大值。我的音頻規範化碼是否正確?
爲了做到這一點,我用這個代碼:
Dim iHighestDataValue As Integer
Dim iLowestDataValue As Integer
Dim i&
For i = 1 To dc.wavedatasize/2
Get #iFile, , DataValue(i) ' reads data value into array
If DataValue(i) > iHighestDataValue Then
iHighestDataValue = DataValue(i)
End If
If DataValue(i) < iLowestDataValue Then
iLowestDataValue = DataValue(i)
End If
Next
Dim sngVolumeLevel As Single
sngVolumeLevel = ((iHighestDataValue + (iLowestDataValue * -1))/2)/32767
它工作得很好,幾乎所有的文件,但對於一個文件,最後一行將失敗。
iHighestDataValue是13445,iLowestDataValue是-21940。
我想最後一行有些問題。 這只是VB6發生的一個錯誤,因爲它不能處理這個(正確的)計算,還是我引入了任何錯誤?
當我把最後一行到單獨的計算,它工作正常:
Dim sngHighest As Single
If iHighestDataValue > 32767 Then
sngHighest = 32767
Else
sngHighest = iHighestDataValue
End If
Dim sngLowest As Single
If iLowestDataValue < -32767 Then
sngLowest = -32767
Else
sngLowest = iLowestDataValue
End If
sngLowest = sngLowest * -1
Dim sngVolumeLevel As Single
sngVolumeLevel = (sngHighest + sngLowest)/2
sngVolumeLevel = (sngVolumeLevel/32767)
謝謝!
PS: 下面是函數的最後,歸一化部:
Dim tem As Long
For i = 1 To dc.wavedatasize/2
tem = CLng(DataValue(i) * 1/sngVolumeLevel)
If tem > 32767 Then
tem = 32767 ' prevents integer overflow error
End If
If tem < -32767 Then
tem = -32767 ' prevents integer overflow error
End If
DataValue(i) = CInt(tem) ' changes data value
Next i
wh.riffdatasize = dc.wavedatasize + Len(wh) + Len(wf) 'Riff chunk size may be different
' beacause some input wave files may contain information chunk which is not written in output file
iFile = FreeFile
Kill uPath 'Delete the original/source file. We will overwrite it with the normalized version of this file
Open uPath For Binary As #iFile
Put #iFile, , wh ' writes the wave header
Put #iFile, , wf ' writes wave format
Put #iFile, , dc ' writes the 8 byte string "data" and wave dataa size
For i = 1 To dc.wavedatasize/2
Put #iFile, , DataValue(i) ' writes data value in ouput file
Next i
Close #iFile
最好的,謝謝! – tmighty