OpenAL
Ok, I need some help with OpenAL. All I am trying to do is get a very simple
OpenAL program working, all it does is load one WAV file and plays it.
Every thing is fine except when I run the program it isn’t able to create
a source, it sends the AL_INVALID_OPERATION error. There is probably a very simple answer for all this but I just started OpenAL yesterday.
One more thing, can OpenAL only play WAV files?
Thanks,
Lord Vader
OpenAL program working, all it does is load one WAV file and plays it.
Every thing is fine except when I run the program it isn’t able to create
a source, it sends the AL_INVALID_OPERATION error. There is probably a very simple answer for all this but I just started OpenAL yesterday.
One more thing, can OpenAL only play WAV files?
Thanks,
Lord Vader
no, it can also play .ogg files (I believe). do you have any code?
It's not magic, it's Ruby.
alGenSources has never given me any errors, and I can't imagine what would cause it to. Post the code?
While there's a convenience function in ALUT to load a WAV file into an AL buffer, OpenAL really just works with raw audio streams. If you don't mind doing a little bit of work to load and parse the audio files yourself, you can use any conceivable audio file format with it. As for Ogg Vorbis, there's an extension known as AL_EXT_vorbis, but I believe it's unsupported in Apple's current OpenAL implementation. I just use libvorbisfile (pretty simple and easy) to load the files myself, and pass the raw data to OpenAL with alBufferData.
- Alex Diener
While there's a convenience function in ALUT to load a WAV file into an AL buffer, OpenAL really just works with raw audio streams. If you don't mind doing a little bit of work to load and parse the audio files yourself, you can use any conceivable audio file format with it. As for Ogg Vorbis, there's an extension known as AL_EXT_vorbis, but I believe it's unsupported in Apple's current OpenAL implementation. I just use libvorbisfile (pretty simple and easy) to load the files myself, and pass the raw data to OpenAL with alBufferData.
- Alex Diener
Here is my code.
Code:
#define NUM_BUFFERS 1
#define NUM_SOURCES 1
#define NUM_ENVIRONMENTS 1
ALfloat listenerPos[] = {0.0, 0.0, 4.0};
ALfloat listenerVel[] = {0.0, 0.0, 0.0};
ALfloat listenerOri[] = {0.0, 0.0, 1.0, 0.0, 1.0, 0.0};
ALfloat source0Pos[] = {-2.0, 0.0, 0.0};
ALfloat source0Vel[] = {0.0, 0.0, 0.0};
ALuint buffer[NUM_BUFFERS];
ALuint source[NUM_SOURCES];
ALuint environment[NUM_ENVIRONMENTS];
ALsizei size, freq;
ALenum format;
ALvoid *data;
void init(void)
{
alListenerfv(AL_POSITION, listenerPos);
alListenerfv(AL_VELOCITY, listenerVel);
alListenerfv(AL_ORIENTATION, listenerOri);
alGetError();
alGenBuffers(NUM_BUFFERS, buffer);
if(alGetError() != AL_NO_ERROR)
{
printf("\n- Error creating buffers !!");
exit(1);
}
else
{
printf("\ninit() - No errors yet.");
}
alGetError();
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFStringRef name = CFSTR("music");
CFStringRef type = CFSTR("wav");
CFURLRef fileName = CFBundleCopyResourceURL(mainBundle, name, type,
NULL);
UInt8 completeFileName[100];
CFURLGetFileSystemRepresentation(fileName, true, completeFileName, 100);
/* CFStringRef stringName = CFURLGetString(fileName);
CFMutableStringRef modString =
CFStringCreateMutableCopy(kCFAllocatorDefault, 0, stringName);
CFStringRef appendString = CFSTR("\0");
CFStringAppend(modString, appendString);
stringName = modString;
const char *nameOfFile = CFStringGetCStringPtr(completeFileName,
kCFStringEncodingMacRoman); */
alutLoadWAVFile(completeFileName, &format, &data, &size, &freq);
if(alGetError() != AL_NO_ERROR)
{
printf("\n- Error loading WAV file -");
exit(2);
}
else
{
printf("\n- No errors loading WAV file -");
}
alGetError();
alBufferData(buffer[0], format, data, size, freq);
alutUnloadWAV(format, data, size, freq);
if(alGetError() != AL_NO_ERROR)
{
printf("\n- Error creating buffer data !!");
exit(3);
}
else
{
printf("\ninit - No errors after alBufferData().");
}
alGetError();
alGenSources(NUM_SOURCES, source);
if(alGetError() == AL_INVALID_OPERATION)
{
printf("\n- Error creating sources !!");
exit(4);
}
else
{
printf("\ninit - No errors after alGetSources.");
}
alSourcef(source[0], AL_PITCH, 1.0f);
alSourcef(source[0], AL_GAIN, 1.0f);
alSourcefv(source[0], AL_POSITION, source0Pos);
alSourcefv(source[0], AL_VELOCITY, source0Vel);
alSourcef(source[0], AL_BUFFER, buffer[0]);
alSourcef(source[0], AL_LOOPING, AL_TRUE);
}
Maybe this is blindingly obvious, but did you do alutInit() somewhere in your code? I didn't do that at first and got symptoms similar to what you're describing.
Ok, that was really stupid. I did forget to call alutInit(). Thanks a lot for the help everyone.

