Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » AllegroMP3 == chaos in my mind.

This thread is locked; no one can reply to it. rss feed Print
 1   2 
AllegroMP3 == chaos in my mind.
Albin Engström
Member #8,110
December 2006
avatar

I have never seen something more confusing than the AllegroMP3 example file.. yes, i don't have enough knowledge but still, an example file should only include the things necessary and related to the subject, in this case to play an mp3 file.
And it should be commented.

This might sound a little lazy but.. could someone please post a piece of code of AllegroMP3 that only includes the very minimum required code to play an mp3 file?.

thanks!

Kibiz0r
Member #6,203
September 2005
avatar

It's been a long time since I used it, but I thought it was something like ALMP3_load(), ALMP3_play()... I'll go check it out, though.

Heh, okay... maybe not.

Quote:

Server not found

:-/

CGamesPlay
Member #2,559
July 2002
avatar

You have to open a file, seek to the end of it, find the offset of that position, seek to the beginning, create a buffer of the same size as the file, read the file into the buffer, than pass that to AlMP3, IIRC.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Archon
Member #4,195
January 2004
avatar

Quote:

create a buffer of the same size as the file

Does 'seeking' actually give you purely PCM data (as it has VBR and probably metadata in each frame)?

OP: Basically, MP3 and all other codecs should decode to a common format (like .wav files - which is why they're so big), but each codec does it differently.

Unless you want to stream it, you'll need to create a normal SAMPLE and then uncompress the PCM data of the MP3 file into the data attribute, as well as give it the metadata (bits[rate], stereo, freq[uency], len[gth]) so the SAMPLE knows how much data data describes. Then you should be able to just play it like you play a normal .wav file.

CGamesPlay
Member #2,559
July 2002
avatar

Quote:

Unless you want to stream it, you'll need to create a normal SAMPLE and then uncompress the PCM data of the MP3 file into the data attribute, as well as give it the metadata (bits[rate], stereo, freq[uency], len[gth]) so the SAMPLE knows how much data data describes. Then you should be able to just play it like you play a normal .wav file.

And why would anyone use AlMP3 if they had to do this? :P

Quote:

Does 'seeking' actually give you purely PCM data (as it has VBR and probably metadata in each frame)?

No, it gives you the size of the MP3 file. AlMP3 doesn't know how to read files, so you can for instance pass in an MP3 stored in a datafile.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Albin Engström
Member #8,110
December 2006
avatar

OK.. maybe all that 'crap' wasn't unnecessary.. thanks for your help!!
Unfortunately i have to spend some time with my friends watching "eurovision song contest" or whatever, then i'll be able to get down to work.

If you have a more detailed version on how to make it work then don't hesitate to post it.

I thank thee!

Archon
Member #4,195
January 2004
avatar

Quote:

And why would anyone use AlMP3 if they had to do this? :P

I don't know! I can't think straight (it was 2:10am) :P

I'm just recalling what I'm trying to do with libvorbisfile and libFLAC for OpenAL... Not so fun if I can't test it in the middle of programming it.

Kibiz0r
Member #6,203
September 2005
avatar

Wow, how is ALMP3 a billion times more complicated than I remember it?

I was able to use it while I was still working on my first game, which was pure C with ridiculously awful practices and an endearingly noob-ish lack of skill/knowledge... I got through by the skin of my teeth. I wonder how I was able to contend with ALMP3.

Thomas Fjellstrom
Member #476
June 2000
avatar

You can't stream the mp3s directly of the disk by just sending chunks of data instead of it all? Loading up a 3-5MB mp3 just to send to almp3 seems a bit silly when libmad lets you load and decode on the fly.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Albin Engström
Member #8,110
December 2006
avatar

Thomas Fjellstrom said:

You can't stream the mp3s directly of the disk by just sending chunks of data instead of it all? Loading up a 3-5MB mp3 just to send to almp3 seems a bit silly when libmad lets you load and decode on the fly.

I think you can.
ALMP3_MP3 *s; to create a non-stream type.
ALMP3_MP3STREAM *s; to create a stream type.

Anyway: i'm accepting the AllegroMP3 challenge and now i'm facing my first opponent. PACKFILES! i'm having a huge problem understanding these fellas.
First of: how do i create a packfile? is there a tool?

Thomas Fjellstrom
Member #476
June 2000
avatar

A normal allegro packfile is just a regular file that can optionally be moderately compressed. pack_fopen pack_fread pack_fclose etc. The api is very similar to the libc standard file io routines.

Now if you mean datafiles, theres dat.exe and grabber.exe

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Albin Engström
Member #8,110
December 2006
avatar

nono! not datafiles, i have those under control.. :).
so how would i go about to create a packfile with for example a mp3file?
would i create a new program and use pack routines with fstream? or something..

gah, something like this would have made my day:

packfile.write("iman.mp3");

- -.

Thomas Fjellstrom
Member #476
June 2000
avatar

Ah, the packfile stuff is very similar to "stdio".

PACKFILE *pf = pack_fopen("w", "filename");
pack_fwrite(data, sizeof_data, pf);
pack_fclose(pf);

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Albin Engström
Member #8,110
December 2006
avatar

"PACKFILE *pf = pack_fopen("w", "filename");"
This loads a packfile right?

pack_fwrite(data, sizeof_data, pf);
Is this where you write to the opened datafile?
so whats data representing? or rather how would i load this data and to use with this function? and how would i get sizeof_data?

Thomas Fjellstrom
Member #476
June 2000
avatar

Quote:

This loads a packfile right?

It just opens it, for "w"riting in this case.

Quote:

Is this where you write to the opened datafile?

Packfile, and yes.

char data[256];
PACKFILE *pf = pack_fopen("r", "filename");
pack_fread(data, 256, pf);
pack_fclose(pf);

And that will read the first 256 bytes of whatever is in "filename", and place it in data.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

CGamesPlay
Member #2,559
July 2002
avatar

Quote:

You can't stream the mp3s directly of the disk by just sending chunks of data instead of it all?

No, you can. That's why AlMP3 doesn't provide filename-based functions and lets you do the dirty work. If it did, it would cut out a lot of the features.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Albin Engström
Member #8,110
December 2006
avatar

char data[256];
PACKFILE *pf = pack_fopen("r", "filename");
pack_fread(data, 256, pf);
pack_fclose(pf);

ok, so thats how i read..

to write however:

pack_fwrite(data, sizeof_data, pf);

manual said:

from memory location pointed to by `p'.

memory means? can i replace data with the path to the mp3? (hopes for an easy solution)..

why: char data[256]??
why does it read the first 256bytes? can't i have more than one information in one packfile? can i nest packfiles?

Thomas Fjellstrom
Member #476
June 2000
avatar

Quote:

memory means?

Any memory address. like a pointer to something. Generally an array of some kind is used (since using it without the []s it acts like a pointer, and thus works in functions like these).
Everything in a running program is stored in memory. variables, functions, etc. You can also get a "pointer" to them as well, and use that pointer in functions that take one.

Quote:

can i replace data with the path to the mp3? (hopes for an easy solution)..

If you want to read the mp3, you just place the filename in the "filename" bit in pack_fopen, and then use pack_fread to read it in.

Quote:

why: char data[256]??

Because. No reason, just some random number I picked.

Quote:

why does it read the first 256bytes?

Because a file "read" position starts at 0 by default, and I put "256" in the size bit in pack_fread, its also the size of the array (you never want to read more data than can fit in your memory variable).

Quote:

can't i have more than one information in one packfile?

IIRC its limited to about 2-4GB

Quote:

can i nest packfiles?

Yes. Though thats a little more complex than just normal packfiles.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Albin Engström
Member #8,110
December 2006
avatar

Quote:

Though thats a little more complex than just normal packfiles.

Aka nothing i should bother with..

Well, below is my pathetic attempt to make a simple function to load and play mp3 files.. this didn't work, it doesn't crash either, which should have been preferred..

1#define DATASZ (1<<15)
2#define BUFSZ (1<<16)
3 
4void *open_mp3_file(char *filename)
5{
6 using namespace sfx::music;
7
8 PACKFILE *mp3file = NULL;
9 ALMP3_MP3STREAM *mp3 = NULL;
10 char data[DATASZ];
11 int len;
12
13 mp3file = pack_fopen(filename, F_READ);
14 len = pack_fread(data, DATASZ, mp3file);
15
16 if (len < DATASZ) {
17 mp3 = almp3_create_mp3stream(data, len, TRUE);
18 }
19 else
20 {
21 mp3 = almp3_create_mp3stream(data, DATASZ, FALSE);
22 }
23
24 almp3_play_mp3stream(mp3, DATASZ, 255, 128);
25
26}

:-/

Thomas Fjellstrom
Member #476
June 2000
avatar

DATAZ is only 256kb in size, with a 128kb/s mp3, thats about 2 seconds of mp3 data.

Also, I think the MP3STREAM/mp3stream stuff is for streaming off the disk, so you read X bytes every so often and send into the stream, where as it looks like you want to just load it all and send, in which case, you need to dynamically allocate "data" with "malloc" or "new" the size of the file, and then send it using the non stream functions.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

CGamesPlay
Member #2,559
July 2002
avatar

Quote:

DATAZ is only 256kb in size, with a 128kb/s mp3, thats about 2 seconds of mp3 data.

No, it's 32 KB.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Thomas Fjellstrom
Member #476
June 2000
avatar

Yeah, somehow I got the math wrong, its 1024kb, which is 8 seconds of playtime with a 128kb/s mp3.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

CGamesPlay
Member #2,559
July 2002
avatar

Quote:

#define DATASZ (1<<15)

It's 32,768 bytes long. 32 kilobytes.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Thomas Fjellstrom
Member #476
June 2000
avatar

OOps, I'm really fuzzy this morning. I was right to begin with. I'm talking BITS here not bytes. To match with the way mp3s are labeled (in kbits/s).

32KB * 8bits is a whole 256kbits.

Do you see now what I'm talking about?

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Albin Engström
Member #8,110
December 2006
avatar

I think my muscles was just replaced by lead.. ???.. sight..
I feel like i'm in a deadlock, to learn this, i need to learn that, but too learn that, i need to learn this. Anyway, just working my ass of usually shows results so here's some more questions(my definition of working my ass of 8-)):

Ok, the goal is to load and play a mp3 non-streamed..

trying to get a grip on >what i have to do to get it to work< is important.
see comments for comments:

1//the ultimate lazy-man project function:
2void whatever()
3{
4 PACKFILE *mp3file = NULL;// is a pack file really requiered and what is a
5 //packfile??
6
7 ALMP3_MP3 *mp3 = NULL;//how is this data type built up? and what is the idea of
8 //having this thing?
9
10 char data[x];//ok? what the hell is this?
11 //why is the "data" stored as an char array?
12
13 int len;//what is this?...
14
15 len = pack_fread(data, x, mp3file);//see above..
16 
17 mp3file = pack_fopen(filename, F_READ);//this is obviously something packfile
18 //related.. what should filename be? the path in char data type? ex: "muu.mp3".??
19
20 //following(copying the example) this section makes less sense after
21 //pathetically converting it for non streamed type.
22 if (len < x) //ok..
23 {
24 mp3 = almp3_create_mp3(data, len);
25 }
26 else
27 {
28 mp3 = almp3_create_mp3(data, x);
29 }
30 //ok.. so use the lowest value when creating the mp3.. why??
31 //is this because of the streaming thingie?
32
33 almp3_play_mp3stream(mp3, x, 255, 128);//trying to play it..
34 //mp3 = the mp3.. x = size of what? how do i allcolate the space required?
35 // 255 volume or pitch of something i don't need to focus on right now.
36
37}

It's not easy being a newbie, :).
Thanks a lot for eventual answers! really! :P

 1   2 


Go to: