Sign in to follow this  
ochi12

Playing ogg file with openal and stb_vorbis?

Recommended Posts

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!

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Edited by Twk Amz

Share this post


Link to post
Share on other sites

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.

Edited by Wyrframe

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this