Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MP3 info with VB?

Status
Not open for further replies.

fenris

Programmer
May 20, 1999
824
CA
Does anyone know how to get information like bitrate, sampling frequency and track length from an mp3 using VB?<br>
<br>
fenris
 
Private Sub Form_Load()<br>
Dim fNum As Integer<br>
Dim sTagIdent As String * 3<br>
Dim sTitle As String * 30<br>
Dim sArtist As String * 30<br>
Dim sAlbum As String * 30<br>
Dim sYear As String * 4<br>
Dim sComment As String * 30<br>
fNum = FreeFile<br>
<br>
Open &quot;c:\MediaFiles\Elvis.mp3&quot; For Binary As fNum<br>
<br>
Seek #fNum, LOF(fNum) - 127<br>
Get #fNum, , sTagIdent<br>
If sTagIdent = &quot;TAG&quot; Then<br>
Get #fNum, , sTitle<br>
Get #fNum, , sArtist<br>
Get #fNum, , sAlbum<br>
Get #fNum, , sYear<br>
Get #fNum, , sComment<br>
End If<br>
Close #fNum<br>
MsgBox sTitle & &quot;,&quot; & sArtist & &quot;,&quot; & sAlbum & &quot;,&quot; & sYear & &quot;,&quot; & sComment<br>
<br>
End Sub<br>
<br>
<p>Eric De Decker<br><a href=mailto:vbg.be@vbgroup.nl>vbg.be@vbgroup.nl</a><br><a href= Basic Center</a><br>
 
Thanks for the code Eric, but that looks like just mp3 tag info. I was looking for the track length, for example xxx.mp3 plays for 3:45, and the bitrate which is generally 128k and the frequency which is 44khz. Similar to what winamp shows. I believe that the track length is somehow based on the bitrate. but I don't know how to get the bit rate.<br>
fenris<br>
<br>

 
Type MP3Info<br>
Bitrate As Integer<br>
Frequency As Long<br>
Mode As String<br>
Emphasis As String<br>
'ModeExtension As String<br>
MpegVersion As Integer<br>
MpegLayer As Integer<br>
Padding As String<br>
CRC As String<br>
Duration As Long<br>
CopyRight As String<br>
Original As String<br>
PrivateBit As String<br>
HasTag As Boolean<br>
Tag As String<br>
Songname As String<br>
Artist As String<br>
Album As String<br>
Year As String<br>
Comment As String<br>
Genre As Integer<br>
Track As String<br>
VBR As Boolean<br>
Frames As Integer<br>
End Type<br>
Public GetMP3Info As MP3Info<br>
<br>
Public Function BinaryHeader(filename As String, ReadTag As Boolean, ReadHeader As Boolean) As String<br>
Dim ByteArray(4) As Byte<br>
Dim XingH As String * 4<br>
FIO% = FreeFile<br>
Open filename For Binary Access Read As FIO%<br>
n& = LOF(FIO%): If n& &lt; 256 Then Close FIO%: Return 'ny<br>
If ReadHeader = False Then GoTo 5: 'if we only want to read the IDtag goto 5<br>
Dim x As Byte<br>
'''''start check startposition for header''''''''''''<br>
'''''if start position &lt;&gt;1 then id3v2 tag exists'''''<br>
For i = 1 To 5000 'check up to 5000 bytes for the header<br>
Get #FIO%, i, x<br>
If x = 255 Then 'header always start with 255 followed by 250 or 251<br>
Get #FIO%, i + 1, x<br>
If x &gt; 249 And x &lt; 252 Then<br>
Headstart = i 'set header start position<br>
Exit For<br>
End If<br>
End If<br>
Next i<br>
'''end check start position for header'''''''''''''<br>
<br>
''start check for XingHeader'''<br>
Get #1, Headstart + 36, XingH<br>
If XingH = &quot;Xing&quot; Then<br>
GetMP3Info.VBR = True<br>
For Z = 1 To 4 '<br>
Get #1, Headstart + 43 + Z, ByteArray(Z) 'get framelength to array<br>
Next Z<br>
Frames = BinToDec(ByteToBit(ByteArray)) 'calculate # of frames<br>
GetMP3Info.Frames = Frames 'set frames<br>
Else: GetMP3Info.VBR = False<br>
End If<br>
'''end check for XingHeader<br>
<br>
'''start extract the first 4 bytes (32 bits) to an array<br>
For Z = 1 To 4 '<br>
Get #1, Headstart + Z - 1, ByteArray(Z)<br>
Next Z<br>
'''stop extract the first 4 bytes (32 bits) to an array<br>
5:<br>
If ReadTag = False Then GoTo 10 'if we dont want to read the tag goto 10<br>
''''start id3 tag''''''''''''''''''''''''''''''''''''''''''''''''<br>
Dim Inbuf As String * 256<br>
Get #FIO%, (n& - 255), Inbuf: Close FIO% 'ny<br>
p = InStr(1, Inbuf, &quot;tag&quot;, 1) 'ny<br>
If p = 0 Then<br>
With GetMP3Info<br>
.HasTag = False<br>
.Songname = &quot;&quot;<br>
.Artist = &quot;&quot;<br>
.Album = &quot;&quot;<br>
.Year = &quot;&quot;<br>
.Comment = &quot;&quot;<br>
.Track = &quot;&quot;<br>
.Genre = 255<br>
End With<br>
Else<br>
With GetMP3Info<br>
.HasTag = True<br>
.Songname = RTrim(Mid$(Inbuf, p + 3, 30))<br>
.Artist = RTrim(Mid$(Inbuf, p + 33, 30))<br>
.Album = RTrim(Mid$(Inbuf, p + 63, 30))<br>
.Year = RTrim(Mid$(Inbuf, p + 93, 4))<br>
.Comment = RTrim(Mid$(Inbuf, p + 97, 29))<br>
.Track = RTrim(Mid$(Inbuf, p + 126, 1))<br>
.Genre = Asc(RTrim(Mid$(Inbuf, p + 127, 1)))<br>
End With<br>
End If<br>
''''stop id3 tag''''''''''''''''''''''''''''''<br>
10:<br>
Close FIO%<br>
BinaryHeader = ByteToBit(ByteArray)<br>
End Function<br>
<br>
''this function converts Binary string to decimal integer<br>
Public Function BinToDec(BinValue As String) As Long<br>
BinToDec = 0<br>
For i = 1 To Len(BinValue)<br>
If Mid(BinValue, i, 1) = 1 Then<br>
BinToDec = BinToDec + 2 ^ (Len(BinValue) - i)<br>
End If<br>
Next i<br>
End Function<br>
<br>
Public Function ByteToBit(ByteArray) As String<br>
'convert 4*1 byte array to 4*8 bits'''''<br>
ByteToBit = &quot;&quot;<br>
For Z = 1 To 4<br>
For i = 7 To 0 Step -1<br>
If Int(ByteArray(Z) / (2 ^ i)) = 1 Then<br>
ByteToBit = ByteToBit & &quot;1&quot;<br>
ByteArray(Z) = ByteArray(Z) - (2 ^ i)<br>
Else<br>
If ByteToBit &lt;&gt; &quot;&quot; Then<br>
ByteToBit = ByteToBit & &quot;0&quot;<br>
End If<br>
End If<br>
Next<br>
Next Z<br>
End Function<br>
<br>
Public Function GenreText(Index As Integer) As String<br>
Matrix = Array(&quot;Blues&quot;, &quot;Classic Rock&quot;, &quot;Country&quot;, &quot;Dance&quot;, &quot;Disco&quot;, &quot;Funk&quot;, &quot;Grunge&quot;, _<br>
&quot;Hip -Hop&quot;, &quot;Jazz&quot;, &quot;Metal&quot;, &quot;New Age&quot;, &quot;Oldies&quot;, &quot;Other&quot;, &quot;Pop&quot;, &quot;R&b&quot;, &quot;Rap&quot;, &quot;Reggae&quot;, _<br>
&quot;Rock&quot;, &quot;Techno&quot;, &quot;Industrial&quot;, &quot;Alternative&quot;, &quot;Ska&quot;, &quot;Death Metal&quot;, &quot;Pranks&quot;, _<br>
&quot;Soundtrack&quot;, &quot;Euro -Techno&quot;, &quot;Ambient&quot;, &quot;Trip -Hop&quot;, &quot;Vocal&quot;, &quot;Jazz Funk&quot;, &quot;Fusion&quot;, _<br>
&quot;Trance&quot;, &quot;Classical&quot;, &quot;Instrumental&quot;, &quot;Acid&quot;, &quot;House&quot;, &quot;Game&quot;, &quot;Sound Clip&quot;, &quot;Gospel&quot;, _<br>
&quot;Noise&quot;, &quot;AlternRock&quot;, &quot;Bass&quot;, &quot;Soul&quot;, &quot;Punk&quot;, &quot;Space&quot;, &quot;Meditative&quot;, &quot;Instrumental Pop&quot;, _<br>
&quot;Instrumental Rock&quot;, &quot;Ethnic&quot;, &quot;Gothic&quot;, &quot;Darkwave&quot;, &quot;Techno -Industrial&quot;, &quot;Electronic&quot;, _<br>
&quot;Pop -Folk&quot;, &quot;Eurodance&quot;, &quot;Dream&quot;, &quot;Southern Rock&quot;, &quot;Comedy&quot;, &quot;Cult&quot;, &quot;Gangsta&quot;, &quot;Top 40&quot;, _<br>
&quot;Christian Rap&quot;, &quot;Pop/Funk&quot;, &quot;Jungle&quot;, &quot;Native American&quot;, &quot;Cabaret&quot;, &quot;New Wave&quot;, _<br>
&quot;Psychadelic&quot;, &quot;Rave&quot;, &quot;Showtunes&quot;, &quot;Trailer&quot;, &quot;Lo -Fi&quot;, &quot;Tribal&quot;, &quot;Acid Punk&quot;, &quot;Acid Jazz&quot;, _<br>
&quot;Polka&quot;, &quot;Retro&quot;, &quot;Musical&quot;, &quot;Rock & Roll&quot;, &quot;Hard Rock&quot;, &quot;Folk&quot;, &quot;Folk/Rock&quot;, &quot;National Folk&quot;, _<br>
&quot;Swing&quot;, &quot;Bebob&quot;, &quot;Latin&quot;, &quot;Revival&quot;, &quot;Celtic&quot;, &quot;Bluegrass&quot;, &quot;Avantgarde&quot;, &quot;Gothic Rock&quot;, _<br>
&quot;Progressive Rock&quot;, &quot;Psychedelic Rock&quot;, &quot;Symphonic Rock&quot;, &quot;Slow Rock&quot;, &quot;Big Band&quot;, &quot;Chorus&quot;, &quot;Easy Listening&quot;, _<br>
&quot;Acoustic&quot;, &quot;Humour&quot;, &quot;Speech&quot;, &quot;Chanson&quot;, &quot;Opera&quot;, &quot;Chamber Music&quot;, &quot;Sonata&quot;, &quot;Symphony&quot;, &quot;Booty Bass&quot;, _<br>
&quot;Primus&quot;, &quot;Porn Groove&quot;, &quot;Satire&quot;, &quot;Slow Jam&quot;, &quot;Club&quot;, &quot;Tango&quot;, &quot;Samba&quot;, &quot;Folklore&quot;, &quot;Ballad&quot;, &quot;Power Ballad&quot;, _<br>
&quot;Rhythmic Soul&quot;, &quot;Freestyle&quot;, &quot;Duet&quot;, &quot;Punk Rock&quot;, &quot;Drum Solo&quot;, &quot;A Cappella&quot;, _<br>
&quot;Euro - House&quot;, &quot;Dance Hall&quot;, &quot;Goa&quot;, &quot;Drum & Bass&quot;, &quot;Club - House&quot;, &quot;Hardcore&quot;, &quot;Terror&quot;, &quot;Indie&quot;, &quot;BritPop&quot;, _<br>
&quot;Negerpunk&quot;, &quot;Polsk Punk&quot;, &quot;Beat&quot;, &quot;Christian Gangsta Rap&quot;, &quot;Heavy Metal&quot;, &quot;Black Metal&quot;, &quot;Crossover&quot;, _<br>
&quot;Contemporary Christian&quot;, &quot;Christian Rock&quot;, &quot;Merengue&quot;, &quot;Salsa&quot;, &quot;Thrash Metal&quot;, &quot;Anime&quot;, &quot;JPop&quot;, &quot;Synthpop&quot;)<br>
GenreText = Matrix(Index)<br>
End Function<br>
<br>
Public Function ReadMP3(filename As String, ReadTag As Boolean, ReadHeader As Boolean) As MP3Info<br>
bin = BinaryHeader(filename, ReadTag, ReadHeader) 'extract all 32 bits<br>
<br>
If ReadHeader = False Then Exit Function<br>
Version = Array(25, 0, 2, 1) 'Mpegversion table<br>
MpegVersion = Version(BinToDec(Mid(bin, 12, 2))) 'get mpegversion from table<br>
layer = Array(0, 3, 2, 1) 'layer table<br>
MpegLayer = layer(BinToDec(Mid(bin, 14, 2))) 'get layer from table<br>
SMode = Array(&quot;stereo&quot;, &quot;joint stereo&quot;, &quot;dual channel&quot;, &quot;single channel&quot;) 'mode table<br>
Mode = SMode(BinToDec(Mid(bin, 25, 2))) 'get mode from table<br>
Emph = Array(&quot;no&quot;, &quot;50/15&quot;, &quot;reserved&quot;, &quot;CCITT J 17&quot;) 'empasis table<br>
Emphasis = Emph(BinToDec(Mid(bin, 31, 2))) 'get empasis from table<br>
Select Case MpegVersion 'look for version to create right table<br>
Case 1 'for version 1<br>
Freq = Array(44100, 48000, 32000)<br>
Case 2 Or 25 'for version 2 or 2.5<br>
Freq = Array(22050, 24000, 16000)<br>
Case Else<br>
Frequency = 0<br>
Exit Function<br>
End Select<br>
Frequency = Freq(BinToDec(Mid(bin, 21, 2))) 'look for frequency in table<br>
If GetMP3Info.VBR = True Then 'check if variable bitrate<br>
Temp = Array(, 12, 144, 144) 'define to calculate correct bitrate<br>
Bitrate = (FileLen(filename) * Frequency) / (Int(GetMP3Info.Frames)) / 1000 / Temp(MpegLayer)<br>
Else 'if not variable bitrate<br>
<br>
Dim LayerVersion As String<br>
LayerVersion = MpegVersion & MpegLayer 'combine version and layer to string<br>
Select Case Val(LayerVersion) 'look for the right bitrate table<br>
Case 11 'Version 1, Layer 1<br>
Brate = Array(0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448)<br>
Case 12 'V1 L1<br>
Brate = Array(0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384)<br>
Case 13 'V1 L3<br>
Brate = Array(0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320)<br>
Case 21 Or 251 'V2 L1 and 'V2.5 L1<br>
Brate = Array(0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256)<br>
Case 22 Or 252 Or 23 Or 253 ''V2 L2 and 'V2.5 L2 etc...<br>
Brate = Array(0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160)<br>
Case Else 'if variable bitrate<br>
Bitrate = 1 'e.g. for Variable bitrate<br>
Exit Function<br>
End Select<br>
Bitrate = Brate(BinToDec(Mid(bin, 17, 4)))<br>
End If<br>
<br>
<br>
NoYes = Array(&quot;no&quot;, &quot;yes&quot;)<br>
Original = NoYes(Mid(bin, 30, 1)) 'Set original bit<br>
CopyRight = NoYes(Mid(bin, 29, 1)) 'Set copyright bit<br>
Padding = NoYes(Mid(bin, 23, 1)) 'get padding bit<br>
PrivateBit = NoYes(Mid(bin, 24, 1))<br>
YesNo = Array(&quot;yes&quot;, &quot;no&quot;) 'CRC table<br>
CRC = YesNo(Mid(bin, 16, 1)) 'Get CRC<br>
ms = (FileLen(filename) * 8) / Bitrate 'calculate duration<br>
Duration = Int(ms / 1000)<br>
With GetMP3Info 'set values<br>
.Bitrate = Bitrate '<br>
.CRC = CRC<br>
.Duration = Duration<br>
.Emphasis = Emphasis<br>
.Frequency = Frequency<br>
.Mode = Mode<br>
.MpegLayer = MpegLayer<br>
.MpegVersion = MpegVersion<br>
.Padding = Padding<br>
.Original = Original<br>
.CopyRight = CopyRight<br>
.PrivateBit = PrivateBit<br>
End With<br>
End Function<br>
<br>
Public Function WriteTag(filename As String, Songname As String, _<br>
Artist As String, Album As String, Year As String, Comment As String, Genre As Integer) As Long<br>
Tag = &quot;TAG&quot;<br>
Dim sn As String * 30<br>
Dim com As String * 30<br>
Dim art As String * 30<br>
Dim alb As String * 30<br>
Dim yr As String * 4<br>
Dim gr As String * 1<br>
sn = Songname<br>
com = Comment<br>
art = Artist<br>
alb = Album<br>
yr = Year<br>
gr = Chr(Genre)<br>
Open filename For Binary Access Write As #1<br>
Seek #1, FileLen(filename) - 127<br>
Put #1, , Tag<br>
Put #1, , sn<br>
Put #1, , art<br>
Put #1, , alb<br>
Put #1, , yr<br>
Put #1, , com<br>
Put #1, , gr<br>
Close #1<br>
<br>
End Function<br>
<br>
Private Sub Command1_Click()<br>
Dim filename As String<br>
filename = &quot;c:\2.mp3&quot;<br>
Call WriteTag(filename, &quot;Loooove&quot;, &quot;Namnnnnn&quot;, &quot;Alb1&quot;, &quot;1985&quot;, &quot;Braa&quot;, 54)<br>
End Sub<br>
<br>
Succes <p>Eric De Decker<br><a href=mailto:vbg.be@vbgroup.nl>vbg.be@vbgroup.nl</a><br><a href= Basic Center</a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top