Unloading a WAV in OpenAL

Member
Posts: 241
Joined: 2008.07
Post: #1
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.

Code:
-(void)unloadSound:(NSString*)soundPath
{
    SoundObject* sound = [soundDictionary objectForKey:soundPath];
    
    if(sound)
    {
        NSUInteger sourceID = sound.sourceID;
        NSUInteger bufferID = sound.bufferID;
        alDeleteBuffers(1, &sourceID);
        alDeleteBuffers(1, &bufferID);
        [soundDictionary removeObjectForKey:soundPath];
    }
}

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
in al.h).

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!
Quote this message in a reply
Member
Posts: 241
Joined: 2008.07
Post: #2
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. Wink

So anyway, this is all you need to unload a WAV:

Code:
-(void)unloadSound:(NSString*)soundPath
{
    SoundObject* sound = [soundDictionary objectForKey:soundPath];
    
    if(sound)
    {
        NSUInteger sourceID = sound.sourceID;
        NSUInteger bufferID = sound.bufferID;
        alDeleteSources(1, &sourceID);
        alDeleteBuffers(1, &bufferID);
        [soundDictionary removeObjectForKey:soundPath];
    }
}
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #3
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.
Quote this message in a reply
Member
Posts: 440
Joined: 2002.09
Post: #4
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.
Quote this message in a reply
Post Reply