Reading metadata (id3 tag) of *.mp3 files

Discussions about the Liberty BASIC language, with particular reference to LB Booster
flotul
Posts: 14
Joined: Fri Apr 06, 2018 7:06 am

Reading metadata (id3 tag) of *.mp3 files

Post by flotul »

Hi there,

I'm trying to extract some metadata from *.mp3 files such as BMP and Genre information.

I have made a very short test song (attachment) where the Genre is "Pop" and the BPM is "92".

Looking at this audio file with an hex editor, here is what I get:
Image


So, the apparently header for Genre is "TCON" and the one for BMP is "TBPM".

Code: Select all

OPEN DefaultDir$ + "\TestSong.mp3" FOR binary AS #TITLE

FOR i = 0 TO 100
    PRINT INPUT$(#TITLE, 1);
NEXT i
WAIT

This code will give different results when run in LB or LBB.

Here in LB:
Image


Here in LBB:
Image


I'm always using LBB but I get more usable results with LB because LBB won't find the Genre or "TCON" header.

Is there a better way to extract this type of data with LBB please?
Attachments
TestSong.zip
(62.66 KiB) Downloaded 34 times
guest
Site Admin
Posts: 203
Joined: Tue Apr 03, 2018 1:34 pm

Re: Reading metadata (id3 tag) of *.mp3 files

Post by guest »

flotul wrote: Wed Feb 12, 2025 6:06 pm This code will give different results when run in LB or LBB.
You can see from your hex dump that the text you are reading from the MP3 file is encoded in Unicode (UTF-16) format. Indeed there is an explicit UTF-16 BOM (Byte Order Mark) FF FE immediately preceding the text.

If you make allowance in your code for it being UTF-16, you should find that LB and LBB behave the same way (LBB has built-in support for UTF-8 but not for UTF-16; LB4 supports neither).
Is there a better way to extract this type of data with LBB please?
There's nothing wrong with the method you have used. Your mistake is that you expected the text to be in ASCII/ANSI format but it isn't. I haven't looked at the MP3 specification but it may be that other text encodings are supported, in which case your program would need to be able to adapt to those too.

How much effort you put into decoding UTF-16 will depend on whether your program needs to support accented characters, foreign alphabets (e.g. Cyrillic, Greek), right-to-left printing languages (e.g. Hebrew) and/or complex scripts (e.g. Arabic). Unicode text handling is a very complicated subject!

One approach you could consider is using the Windows WideCharToMultiByte API function to convert the UTF-16 text to UTF-8, and then use LBB's built-in support for UTF-8 to print it out.
flotul
Posts: 14
Joined: Fri Apr 06, 2018 7:06 am

Re: Reading metadata (id3 tag) of *.mp3 files

Post by flotul »

Thanks a lot, who ever you are 👍

I didn't expect to face that kind of difficulty which is far over my basic knowledge.

Maybe I have to make another approach of what I'm aiming to do: a listview of mp3 file, three columns: filename, genre and bpm and sort them.

I'll have a look at the API but my inexperience there will probably keep me away from that solution.

Anyway, thanks a lot again 😉
guest
Site Admin
Posts: 203
Joined: Tue Apr 03, 2018 1:34 pm

Re: Reading metadata (id3 tag) of *.mp3 files

Post by guest »

flotul wrote: Thu Feb 13, 2025 8:40 am I'll have a look at the API but my inexperience there will probably keep me away from that solution.
Fair enough. The API is the best approach if you want to retain accents and foreign-language characters, and you should have no trouble finding existing Liberty BASIC code to call it that you can copy. But if you don't, just do a crude UTF-16 to UTF-8 (or UTF-16 to ASCII) conversion yourself in BASIC code.

I don't know what part of the world you are from, but dealing with international character sets is commonplace in most regions - but sadly not in the USA where it can be something of a culture shock. :D