Reading properties of sound files?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 5
- Joined: Thu Dec 12, 2013 10:25 pm
- Location: Ohio, USA
Reading properties of sound files?
Is there a way in lua/love to get the properties of a sound file? (Artist, Length, Album, Artwork). The goal is to create a music player that would be able to display such information from a local file. Thanks in advance!
- zorg
- Party member
- Posts: 3465
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: Reading properties of sound files?
First, not all types of files support metadata, but many do. (mp3 has two types, wav can also have them, probably ogg as well)
Second, not all files of a type will have such data. (wav files in particular usually don't, and i know of players that botch playback if it does contain it... weirdly enough)
But, it's relatively simple to implement a small parser for them.
I'm assuming you will use love.filesystem functions because who doesn't like dragndropping files onto the app, or having every music file in your project's save folder?
You'll need to look up how these tags are stored in the files, search for any headers / magic numbers, and if they exist, read in the data, pretty it up, and that's it.
No concrete examples from me at the moment though.
Also, löve does support tons of other music files besides those 3, but they are the main "culprits".
Doing the same for other filetypes is a similar process, but of course, different. You'd have to implement it differently for all types you want to support this way. Again, not that hard of a task.
Second, not all files of a type will have such data. (wav files in particular usually don't, and i know of players that botch playback if it does contain it... weirdly enough)
But, it's relatively simple to implement a small parser for them.
I'm assuming you will use love.filesystem functions because who doesn't like dragndropping files onto the app, or having every music file in your project's save folder?
You'll need to look up how these tags are stored in the files, search for any headers / magic numbers, and if they exist, read in the data, pretty it up, and that's it.
No concrete examples from me at the moment though.
Also, löve does support tons of other music files besides those 3, but they are the main "culprits".
Doing the same for other filetypes is a similar process, but of course, different. You'd have to implement it differently for all types you want to support this way. Again, not that hard of a task.
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
-
- Prole
- Posts: 5
- Joined: Thu Dec 12, 2013 10:25 pm
- Location: Ohio, USA
Re: Reading properties of sound files?
So I used on an MP3 file and when I print it it's coming out as ID3[]. I'm assuming I must now find a way to decode the ID3 tags. Is there any documentation on how I would go about doing this?
Code: Select all
data = love.filesystem.read(file)
- zorg
- Party member
- Posts: 3465
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: Reading properties of sound files?
Wikipedia has an article about the layout of the v1 and v1 extended versions, and it does detail some of the frame names for the v2 version.
For v1 and "v1EX", i'd suggest you only read in the required amount of data, and not the whole files.
Probably similar methods work with the v2 tag as well, except that their size is not static (i.e. predetermined), and they are at the start of the file instead.
And yes, this means that all 3 type of tags may exist in an mp3 file.
wav files probably store metadata in RIFF chunks i know not much about. ogg, i have no idea, maybe this.
Unrelated, but reading tracker module information is way more tricky (since löve supports those too), but again, not impossible.
For v1 and "v1EX", i'd suggest you only read in the required amount of data, and not the whole files.
Code: Select all
local file = love.filesystem.newFile('path/to/file.mp3')
file:open('r')
file:seek(file:getSize()-128) -- may be off by 1 or 2 bytes, best to make sure with tests.
local s = file:read(3)
if s == 'TAG' then
-- read fields into table or something
end
-- Extended v1 tag is before the original type, so:
file:seek(file:getSize()-128-227) -- again, may be off by a few bytes.
local s = file:read(4)
if s == 'TAG+' then
-- read fields into table or something
end
And yes, this means that all 3 type of tags may exist in an mp3 file.
wav files probably store metadata in RIFF chunks i know not much about. ogg, i have no idea, maybe this.
Unrelated, but reading tracker module information is way more tricky (since löve supports those too), but again, not impossible.
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Who is online
Users browsing this forum: Bing [Bot] and 1 guest