Playing ogg file with openal and stb_vorbis?

Started by
5 comments, last by jfirjebshw 8 years, 11 months ago

Hi, I'm getting into sound programming with openal and I'm having trouble loading ogg files with openal. I'm trying to use stb_voribs to avoid any unnecessary dependencies. The point is, I am able to load sound files and play them with openal, but when I try to load a file with stb_vorbis, it gets really complicated. I am not sure about what I should be doing to load the sound, as the only documentation is the source code(which I find complicated) I don't know what to do. I tried using


stb_vorbis_decode_filename("file.ogg" ,&channels, &rate, &output)

but it takes too much time to load and there's no way of knowing the size of output in bytes.

I'm just asking if anybody has any experience with this library, how should I be doing it. Thanks in advance!

Advertisement

I like this one-file method, so I checked the file, the function seem to return the length used:


5277. data = (short *) malloc(total * sizeof(*data)); // here's the array created, 'total' is total len
5301. return data_len; // num of 'short's used in array

I would be curious too if this .ogg player works, I used the libs some year ago, they were all right.

I like this one-file method, so I checked the file, the function seem to return the length used:


5277. data = (short *) malloc(total * sizeof(*data)); // here's the array created, 'total' is total len
5301. return data_len; // num of 'short's used in array

I would be curious too if this .ogg player works, I used the libs some year ago, they were all right.

yeah, but it gives me the length used in samples, not bytes. stb_vorbis seems to be useful, but I can't get it to work with openal, mainly because I can't understand the source code pretty well and there's no documentation about it tongue.png .

and yes, it works. But the stb_vorbis_decode takes like a few seconds to load, I am sure I'm using it the wrong way, because it's supposed to be fast

Try using stb_vorbis_open_filename() instead; that returns an stb_vorbis*, which you can pass to stb_vorbis_get_info() to get an stb_vorbis_info struct; that will tell you the number of channels and the sample rate, which you then multiply together with stb_vorbis_stream_length_in_samples() to get the total file-in-memory size you will have when you've read the entire file, if you want to have the entire file in memory at once.

Then, you can pull the audio data progressively into memory as you need it, using the stb_vorbis_get_frame[...] or stb_vorbis_get_samples[...] families of functions.

Edit: just looked at the stb_vorbis_decode_filename function; it returns the number of samples decoded, and you told it the number of channels and the sample rate. Multiply those three together, and you get the total number of shorts in the buffer; multiply by sizeof(short), and voilá; size of that buffer in bytes.

RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.

Try using stb_vorbis_open_filename() instead; that returns an stb_vorbis*, which you can pass to stb_vorbis_get_info() to get an stb_vorbis_info struct; that will tell you the number of channels and the sample rate, which you then multiply together with stb_vorbis_stream_length_in_samples() to get the total file-in-memory size you will have when you've read the entire file, if you want to have the entire file in memory at once.

Then, you can pull the audio data progressively into memory as you need it, using the stb_vorbis_get_frame[...] or stb_vorbis_get_samples[...] families of functions.

Edit: just looked at the stb_vorbis_decode_filename function; it returns the number of samples decoded, and you told it the number of channels and the sample rate. Multiply those three together, and you get the total number of shorts in the buffer; multiply by sizeof(short), and voilá; size of that buffer in bytes.

thanks man! that's very informative, I was having a hard time trying to get how to use it. Thanks a lot!

I created a 13 part video tutorial series that you may be interested which I posted on my website here:

It steps you through the process of loading ogg and wav files for 2d and 3d audio playback. I also show how to setup regular sound playback and streaming audio playback.

Well, just in case somebody is having the same issue, I found a nice example of stb vorbis decoding here:https://gist.github.com/Oddity007/965399. Hpe it helps!

This topic is closed to new replies.

Advertisement