Unloading a WAV in OpenAL - Printable Version
+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: Unloading a WAV in OpenAL (/thread-87.html)
Unloading a WAV in OpenAL - bmantzey - May 3, 2010 04:51 PM
I'm developing a sound engine for iPhone, which means the alut tools are not available to me (that I know of).
I want to be able to simply unload a WAV in OpenAL. I'm not doing anything fancy at all with the sounds, more than the possibility of multiple sounds playing simultaneously.
My game starts out with an intro sound, which I don't want to leave lingering in memory while the game plays. Also, when quitting the game to the main menu, I'd like to remove any and all game sounds. General cleanup is what I'm trying to achieve here.
OpenAL is supposed to be similar to OpenGL. In OpenGL, when I'm done with a texture, I simply call glDeleteTextures(n, texRef).
With OpenAL, we have a buffer and a source. I'm not really sure what the difference is between the two.
I've got a class that remembers the source and buffer that was generated when loading the WAV. When I tell my sound manager to unload the sound, it finds the object in a dictionary and unloads it.
This seems to work fine until the next (and only the next) sound I try to load. OpenAL gives me an error (a004, which translates is:
#define AL_INVALID_OPERATION 0xA004
When loading the file, there are no other errors until I try to release the fileURL.
I'm doing something wrong. All I want to be able to do is unload a WAV and remove it from memory. Any help is always greatly appreciated. Thanks in advance!
Unloading a WAV in OpenAL - bmantzey - May 3, 2010 05:27 PM
UPDATE: I found that the snippet I was using actually had an error in it. In the dealloc method, it loops through the sources dictionary and calls alDeleteBuffers on the sources and then through the dictionary of buffers and calls alDeleteBuffers on it too.
I thought I'd see if there was any such thing as alDeleteSources. Lo and behold, there it was and changing my code fixed my problem.
So anyway, this is all you need to unload a WAV:
Unloading a WAV in OpenAL - ThemsAllTook - May 3, 2010 10:49 PM
bmantzey Wrote:With OpenAL, we have a buffer and a source. I'm not really sure what the difference is between the two.
A source can be thought of like a virtual speaker in your app. A buffer contains data that can be sent to a source to play it audibly. If you want to unload buffered audio data, you needn't delete the source; in fact, you rarely need to delete sources at all. The games I've written that use OpenAL allocate a number of sources at startup (equal to the max number of sounds that can be playing at a time) and never deallocate them. It just attach the appropriate buffer to an idle source when it wants to play a sound.
Unloading a WAV in OpenAL - Frank C. - May 4, 2010 12:48 AM
To expand on ThemsAllTook's advice you should call alSourceStop then set the buffer to 0 for any sources using the buffer you're deleting.
Another gotcha on the iPhone is that the OpenAL mixer runs in a separate thread, so if you're using client buffers via alBufferDataStaticProc (as Apple recommends) you can crash if you free the data before AL has had a chance to update. So it's best to stop and unbind the buffer from the source(s) using it and delete the AL buffer object, but wait a few ticks before freeing the actual sound data - a PITA to be sure.