# Playing ogg file with openal and stb_vorbis?

This topic is 1238 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 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 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 .

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 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 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 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 on other sites
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!

1. 1
2. 2
3. 3
Rutin
19
4. 4
5. 5

• 10
• 21
• 9
• 31
• 16
• ### Forum Statistics

• Total Topics
632618
• Total Posts
3007464
• ### Who's Online (See full list)

There are no registered users currently online

×