View Full Version : Ogg Vorbis in C/OpenAL
Joseph Duchesne
2004.08.04, 01:23 PM
I'm trying to play Ogg Vorbis files in an xcode project using C and OpenAL. I have not found any easy way of doing this. (one way involved making a custom framework from some half baked source)
Does anyone have any hints/tricks or example files lying around?
Use AAC? Is it really worth the trouble of supporting a MAYBE better format that is less compatible?
Sohta
2004.08.04, 01:30 PM
Ryan Gordon's ( icculus.org ) implementation of OpenAL ( probably the source you are mentionning ) is actually rather fast and efficient, on top of that, it support AL_EXT_VORBIS. it works really well ( for me at least ), and is easy to implement.
Edit: Added sample code:
Once you have an OpenAL implementation with AL_EXT_VORBIS, I load the sounds using the following:
#include <sys/stat.h>
ALuint SDDatabase::LoadSoundFromFile( char* inSoundFile )
{
ALuint lResult ;
void * ovdata ;
FILE * fh ;
alGenBuffers( 1 , &lResult ) ;
fh = fopen( inSoundFile , "rb") ;
if( fh != NULL )
{
struct stat sbuf ;
if( stat( inSoundFile , &sbuf ) != -1 )
{
ovdata = malloc( sbuf.st_size ) ;
if( ovdata != NULL )
{
fread( ovdata ,
1 ,
sbuf.st_size ,
fh ) ;
alBufferData( lResult ,
AL_FORMAT_VORBIS_EXT ,
ovdata ,
sbuf.st_size ,
1 ) ;
free( ovdata ) ;
}
fclose( fh ) ;
}
}
return lResult ;
}
Joseph Duchesne
2004.08.04, 01:39 PM
I have been trying to get the source to work. How do I use it? I have no clue how to use a dylib.
Sohta
2004.08.04, 01:41 PM
1. You must include the OpenAL headers in your project.
However, the AL_EXT_VORBIS value might be undefined, so add:
#define AL_FORMAT_VORBIS_EXT 0x10003
somewhere.
2. Add the .dylib to your project as you would add a framework
3. Make sure a copy of the .dylib is in your build folder
For the release version ( unnecessary during development ):
4. IIRC, ( not sure ), you must also put the dylib somewhere in the .app package ( in the same folder as the executable I think )
Edit: added:
I would also recommend toying a bit with the makefile beforehand:
OPTIMIZE := true
USE_G4_OPCODES := false <--------- true if no G3 support
USE_G5_OPCODES := false
USE_CCACHE := false <-------- HERE!
USE_IBM_COMPILER := false
USE_VBAP := false
SUPPORT_ALC_ENUMERATION_EXT := true
SUPPORT_ALC_EXT_CAPTURE := true
SUPPORT_ALC_EXT_SPEAKER_ATTRS := false
SUPPORT_AL_EXT_VORBIS := true <--------- HERE!
SUPPORT_AL_EXT_VECTOR_UNIT := true
SUPPORT_AL_EXT_BUFFER_OFFSET := true
SUPPORT_AL_EXT_FLOAT32 := true
Joseph Duchesne
2004.08.04, 01:54 PM
Thanks, I have got it to compile before (that dumb ccache thing tripped me up for a while though). This is exactly the info I needed to save me the hours/days of fribbling it would have taken me to figure this out on my own this.
Sohta
2004.08.04, 01:58 PM
(that dumb ccache thing tripped me up for a while though)
same here... :mad:
OneSadCookie
2004.08.04, 03:19 PM
Ryan's OpenAL implementation isn't complete in any way shape or form. It only does the bare minimum needed to support UT2k4, which isn't much. Also, the Ogg/Vorbis support in it is simply through libogg/libvorbis: http://www.vorbis.com/download_unix_1.0.1.psp
Joseph Duchesne
2004.08.04, 03:45 PM
so using your function what would I call to load "../file.ogg" and then play it?
Sohta
2004.08.04, 04:05 PM
OSC:
Oh? hmmm, I wasn't aware of that. Maybe I should revise my stuff then. Thanks!
Jospeh Duchesne:
well, For now I just use it to play ambient music, so it goes as follow ( well not exactly, but it would work for you ) :
ALuint mALSource , mALBuffer ;
alGenSources( 1 , &mALSource );
alSource3f( mALSource , AL_POSITION , 0.0f , 0.0f , 0.0f );
alSource3f( mALSource , AL_VELOCITY , 0.0f , 0.0f , 0.0f );
alSource3f( mALSource , AL_DIRECTION , 0.0f , 0.0f , 0.0f );
alSourcef ( mALSource, AL_ROLLOFF_FACTOR , 0.0 );
alSourcei ( mALSource, AL_SOURCE_RELATIVE , AL_TRUE );
mALBuffer = LoadSoundFromFile( "../file.ogg" );
alSourcei(mALSource, AL_BUFFER , mALBuffer);
alSourcei(mALSource, AL_LOOPING, AL_TRUE);
alSourcePlay( mALSource ) ;
Joseph Duchesne
2004.08.04, 07:17 PM
#include <sys/stat.h>
ALuint SDDatabase::LoadSoundFromFile( char* inSoundFile )
{
ALuint lResult ;
void * ovdata ;
FILE * fh ;
alGenBuffers( 1 , &lResult ) ;
fh = fopen( inSoundFile , "rb") ;
if( fh != NULL )
{
struct stat sbuf ;
if( stat( inSoundFile , &sbuf ) != -1 )
{
ovdata = malloc( sbuf.st_size ) ;
if( ovdata != NULL )
{
fread( ovdata ,
1 ,
sbuf.st_size ,
fh ) ;
alBufferData( lResult ,
AL_FORMAT_VORBIS_EXT ,
ovdata ,
sbuf.st_size ,
1 ) ;
free( ovdata ) ;
}
fclose( fh ) ;
}
}
return lResult ;
}
Is this C++? My C compiler doesn't like it and when I change the file type to C++ it still gives errors. Is there anything I need to change in this?
Sohta
2004.08.04, 07:22 PM
My bad,
ALuint SDDatabase::LoadSoundFromFile( char* inSoundFile )
should be:
ALuint LoadSoundFromFile( char* inSoundFile )
I copy/pasted a member method of one of my classes.
Edit: you should also include "al.h"
It should be okay as C.
Joseph Duchesne
2004.08.04, 07:54 PM
OK, now I'm trying to use the Ogg and Vorbis frameworks. They worked well, and come with xcode/project builder files. All you have to remember is to include the ogg one in the vorbis project after compiling ogg in order to compile vorbis. I tried a few things in attempts to include them, but it floods me with over 100 errors every time. What are the correct includes?
vBulletin® v3.8.4, Copyright ©2000-2010, Jelsoft Enterprises Ltd.