Reading an ID3 tag from an MP3 file is quite simple in PowerBuild, due to the nature of the tag. It is a very structured format, always 128 bytes long at the end of the file. Here is a simple break-down of the tagging:
Item = Length - Byte(s) ------------------------------------ "TAG" = 3 characters = 1-3 Song Title = 30 characters = 4-33 Artist = 30 characters = 34-63 Album = 30 characters = 64-93 Year = 4 characters = 94-97 *Comment = 28 characters = 98-125 *Track = 2 characters = 126-127 Genre = 1 character = 128
* -- The difference between an ID3v1 and ID3v1.1 tag is in the comment/track fields. A v1 will have a 30-character comment, and no track, whereas a v1.1 will have a 28-character field and a 2-character track allocation.
To conquer the simple tag, I simply made a custom user object with the following instance variables:
PUBLIC PRIVATEWRITE Boolean HasTag PUBLIC String Title, Artist, Album, Genre, Year, Comment, Track
And then the lone function for now:
/*USER-OBJECT FUNCTION: LoadFile( as_path ) */ /*as_path = the full path to the selected mp3 file */ /*returns a boolean stating whether there is a tag */ /*written by Lee Rudisill 06/05/08 */ String ls_tag Long ll_file Blob b_mid, b_tag
//open the file (encodingANSI! by default) ll_file = FileOpen( as_path, StreamMode!, Read!, LockReadWrite! ) //set the file pointer to the 128th byte from the end FileSeek( ll_file, -128, FromEnd! ) //read the last 128 bytes of the file FileReadEx( ll_file, b_tag ) //close the file FileClose( ll_file )
//get the first three characters ls_tag = Upper( String( BlobMid( b_tag, 1, 3 ), EncodingANSI! ) )
//ensure the first three bytes state "TAG" IF ls_tag = 'TAG' THEN HasTag = TRUE //title b_mid = BlobMid( b_tag, 4, 30 ) Title = String( b_mid, EncodingANSI! ) //artist b_mid = BlobMid( b_tag, 34, 30 ) Artist = String( b_mid, EncodingANSI! ) //album b_mid = BlobMid( b_tag, 64, 30 ) Album = String( b_mid, EncodingANSI! ) //year b_mid = BlobMid( b_tag, 94, 4 ) Year = String( b_mid, EncodingANSI! ) //comment b_mid = BlobMid( b_tag, 98, 28 ) Comment = String( b_mid, EncodingANSI! ) //track b_mid = BlobMid( b_tag, 126, 2 ) Track = String( b_mid, EncodingANSI! ) //genre b_mid = BlobMid( b_tag, 128, 1 ) Genre = String( b_mid, EncodingANSI! ) ELSE //no tag HasTag = FALSE END IF
//MessageBox( '', 'Title: -' + Title + '-~rArtist: -' + Artist + '-~rAlbum: -' + Album + '-~rYear: -' + Year + '-~rComment: -' + Comment + '-~rTrack: -' + Track + '-' )
RETURN HasTag
Once the user-object is created elsewhere in script, you can simply call uo_my_mp3.LoadFile( ls_some_file ), and you will have all available tagged information available in dot-notation.