Old 26th February 2002, 20:34   #1
deday
Junior Member
 
Join Date: Dec 2001
Posts: 8
****Read Mp3 Header****

This works but the tracklength is a few second different to
the Winamp tracklength par example:
with this tag i get 245 seconds and Winamp have 259 seconds. Mostly Winamp have more second. HAve anyone an idea what wrong is or a code that recieves the same tracklength like in Winamp????????????

Private Type MP3Information
ID As String 'ID
Layer As String 'Layer
Bitrate As Long 'Bitrate
SampleRate As Long 'Hz
Frames As Long 'Frame-Anzahl
Duration As Long 'Länge (in Sekunden)
End Type

Private Function GetMP3Information(ByVal MP3File As String) As MP3Information
Dim dIN As String
Dim bitrate_lookup(7, 15) As Integer
Dim bitrate_data As String
Dim actual_bitrate As Long
Dim X As Integer, Y As Integer
Dim mp3_id As Double, mp3_layer As Double, mp3_prot As Double
Dim mp3_bitrate As Double, mp3_freq As Double, mp3_pad As Double
Dim framesize As Double, total_frames As Double, track_length As Double

'Bitraten-Informationen
bitrate_data = bitrate_data & "032,032,032,032,008,008,"
bitrate_data = bitrate_data & "064,048,040,048,016,016,"
bitrate_data = bitrate_data & "096,056,048,056,024,024,"
bitrate_data = bitrate_data & "128,064,056,064,032,032,"
bitrate_data = bitrate_data & "160,080,064,080,040,040,"
bitrate_data = bitrate_data & "192,096,080,096,048,048,"
bitrate_data = bitrate_data & "224,112,096,112,056,056,"
bitrate_data = bitrate_data & "256,128,112,128,064,064,"
bitrate_data = bitrate_data & "288,160,128,144,080,080,"
bitrate_data = bitrate_data & "320,192,160,160,096,096,"
bitrate_data = bitrate_data & "352,224,192,176,112,112,"
bitrate_data = bitrate_data & "384,256,224,192,128,128,"
bitrate_data = bitrate_data & "416,320,256,224,144,144,"
bitrate_data = bitrate_data & "448,384,320,256,160,160,"

For Y = 1 To 14
For X = 7 To 5 Step -1
bitrate_lookup(X, Y) = Left(bitrate_data, 3)
bitrate_data = Right(bitrate_data, Len(bitrate_data) - 4)
Next
For X = 3 To 1 Step -1
bitrate_lookup(X, Y) = Left(bitrate_data, 3)
bitrate_data = Right(bitrate_data, Len(bitrate_data) - 4)
Next
Next

Open MP3File For Binary As #1
'4k der Datei einlesen, um einen Frameheader zu finden
dIN = Input(4096, #1)
filesize = LOF(1) 'Wird benötigt um die Tracklänge zu berechnen
Close #1

'Frame-Header beginnt mit 12 Bitsätzen
Do Until i = 4095
i = i + 1
d1 = Asc(Mid(dIN, i, 1))
d2 = Asc(Mid(dIN, i + 1, 1))

If d1 = &HFF And (d2 And &HF0) = &HF0 Then
'20 Header-Bits einlesen
temp_string = Mid(dIN, i + 1, 3)
mp3bits_string = ShiftBits(Mid(dIN, i + 1, 3))
Exit Do
End If

dSHIFT = ShiftBits(Mid(dIN, i, 3))
dd1 = Asc(Left(dSHIFT, 1))
dd2 = Asc(Right(dSHIFT, 1))

If dd1 = &HFF And (dd2 And &HF0) = &HF0 Then
'20 Header-Bits einlesen
mp3bits_string = Mid(dIN, i + 2, 3)
Exit Do
End If
Loop

'Die ersten 20 Bits sind Header-Infos für diesen Frame
'Das 1. Bit ist die ID; 0 = MPG-2, 1 = MPG-1
mp3_id = (&H80 And Asc(Left(mp3bits_string, 1))) / 128
'Die nächsten 2 Bits sind Layer
mp3_layer = (&H60 And Asc(Left(mp3bits_string, 1))) / 32
'Das nächste Bit ist Schutz
mp3_prot = &H10 And Asc(Left(mp3bits_string, 1))
'Die nächsten 4 Bits beschreiben die Bitrate
mp3_bitrate = &HF And Asc(Left(mp3bits_string, 1))
'Die nächsten 2 Bits legen die Frequenz fest
mp3_freq = &HC0 And Asc(Mid(mp3bits_string, 2, 1))
'Das nächste Bit ist Padding
mp3_pad = (&H20 And Asc(Mid(mp3bits_string, 2, 1))) / 2
actual_bitrate = 1000 * CLng((bitrate_lookup((mp3_id * 4) Or mp3_layer, mp3_bitrate)))

If mp3_id = 0 Then
GetMP3Information.ID = "MPEG-2"
Else
GetMP3Information.ID = "MPEG-1"
End If

Select Case mp3_layer
Case 1
GetMP3Information.Layer = "Layer III"
Case 2
GetMP3Information.Layer = "Layer II"
Case 3
GetMP3Information.Layer = "Layer I"
End Select
GetMP3Information.Bitrate = actual_bitrate

Select Case (mp3_id * 4) Or mp3_freq
Case 0
sample_rate = 22050
Case 1
sample_rate = 24000
Case 2
sample_rate = 16000
Case 4
sample_rate = 44100
Case 5
sample_rate = 48000
Case 6
sample_rate = 32000
End Select
GetMP3Information.SampleRate = sample_rate

'Trackdauer berechnen
framesize = ((144 * actual_bitrate) / sample_rate) + mp3_pad
total_frames = filesize / framesize
track_length = total_frames / 38.5 '38.5 Frames pro Sekunde

GetMP3Information.Frames = Int(total_frames)
GetMP3Information.Duration = Int(track_length)
End Function

Private Function ShiftBits(dIN As String) As String
Dim sd1 As Integer, sd2 As Integer, sd3 As Integer
Dim do1 As Integer, do2 As Integer

sd1 = Asc(Left(dIN, 1))
sd2 = Asc(Mid(dIN, 2, 1))
sd3 = Asc(Right(dIN, 1))

do1 = ((sd1 And &HF) * 16) Or ((sd2 And &HF0) / 16)
do2 = ((sd2 And &HF) * 16) Or ((sd3 And &HF0) / 16)

ShiftBits = Chr(do1) + Chr(do2)
End Function
Thanks
deday is offline   Reply With Quote
Reply
Go Back   Winamp & Shoutcast Forums > Developer Center > Winamp Development

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump