Nils Haeck Guest
|
Posted: Tue Aug 12, 2003 7:47 pm Post subject: Re: How can I read the MP3 tags |
|
|
Hi there, here's a code snippet from an ID3 tag reader I wrote as part of my
file viewer.
//
// TID3Decode
//
type
TId3Tag = packed record
Tag: array[0..2] of char;
Title,
Artist,
Album: array[0..29] of char;
Year: array[0..3] of char;
Comment: array[0..29] of char;
GenreId: byte;
end;
const
Mp3Genre: array[0..79] of string =
('Blues', 'Classic Rock', 'Country', 'Dance', 'Disco',
'Funk', 'Grunge', 'Hip-Hop', 'Jazz', 'Metal', 'New Age',
'Oldies', 'Other', 'Pop', 'R&B', 'Rap', 'Reggae', 'Rock',
'Techno', 'Industrial', 'Alternative', 'Ska', 'Death Metal' ,
'Pranks', 'Soundtrack', 'Euro-Techno', 'Ambient', 'Trip-Hop',
'Vocal', 'Jazz+Funk', 'Fusion', 'Trance', 'Classical',
'Instrumental', 'Acid', 'House', 'Game', 'Sound Clip',
'Gospel', 'Noise', 'AlternRock', 'Bass', 'Soul', 'Punk',
'Space', 'Meditative', 'Instrumental Pop', 'Instrumental Rock',
'Ethnic', 'Gothic', 'Darkwave', 'Techno-Industrial', 'Electronic',
'Pop-Folk', 'Eurodance', 'Dream', 'Southern Rock', 'Comedy',
'Cult', 'Gangsta', 'Top 40', 'Christian Rap', 'Pop/Funk',
'Jungle', 'Native American', 'Cabaret', 'New Wave', 'Psychadelic',
'Rave', 'Showtunes', 'Trailer', 'Lo-Fi', 'Tribal', 'Acid Punk',
'Acid Jazz', 'Polka', 'Retro', 'Musical', 'Rock & Roll',
'Hard Rock');
class function TID3Decode.IsValidSection(Stream: TStream): boolean;
var
MP3: word;
Tag: array[0..2] of char;
begin
Result := False;
Stream.Seek(0, soFromBeginning);
if Stream.Size >= 2 then begin
Stream.Read(MP3, 2);
MP3 := swap(MP3);
// Check the framesync (11 bits set)
if (MP3 and $FFE0) = $FFE0 then begin
// Check TAG
Stream.Seek(-128, soFromEnd);
Stream.Read(Tag, 3);
if Tag = 'TAG' then
Result := True;
end;
end;
end;
// Read the MP3 Id3 tag, add a XML tag with <ID3> if present
procedure TID3Decode.TagToXML(Stream: TStream; XML: TXMLObject);
var
Id3Tag: TId3Tag;
Genre: string;
begin
Stream.Seek(-128, soFromEnd);
Stream.Read(Id3Tag, SizeOf(Id3tag));
if (Id3Tag.Tag = 'TAG') and assigned(XML) then with ID3Tag do begin
XML.ChildAdd('Title', trim(Title));
XML.ChildAdd('Artist', trim(Artist));
XML.ChildAdd('Album', trim(Album));
XML.ChildAdd('Year', trim(Year));
XML.ChildAdd('Comment', trim(Comment));
Genre := 'Unknown';
if GenreId <= 79 then Genre := Mp3Genre[GenreID];
XML.ChildAdd('Genre', Genre);
end;
end;
Kind regards,
Nils Haeck
www.simdesign.nl
"dpap"
| Quote: | Hi,
I want to read the tags of a MP3 file (Title, artist bame etc) but I don't
know the file structure.
Can somebody help me ?
thanks in advance
|
|
|