Starting OpenAL & alBufferData()?

Apprentice
Posts: 9
Joined: 2008.10
Post: #1
Hi, I'm new here, so please forgive any mistakes of etiquette (seems to be usually NDA related?).

--------------

Trying to get started introducing OpenAL sound into my iPhone App. Currently just running things in the simulator. I've done a bit of simple OpenAL stuff on PC before.

Just throwing together some test code, I've successfully created a device and context, buffer and source, but when I call alBufferData to fill a buffer with some noise, the call fails with an AL error message of: AL_INVALID_OPERATION

All the calls pass error checking without problems, except alBufferData().

Any help would be much appreciated, thank you.

--------------

Here's the code, pardon the mess.
Code:
MainViewController.mm :

    ALCdevice* alDevice;
    ALuint testALBuffer;
    ALuint testALSource;

    alDevice = alcOpenDevice(NULL);
    if((alGetError()) != AL_NO_ERROR) {
        printf("Error!");
    }

    alContext = alcCreateContext(alDevice, NULL);
    if((alGetError()) != AL_NO_ERROR) {
        printf("Error!");
    }
    
    alGenBuffers(1, &testALBuffer);
    alGenSources(1, &testALSource);

    ALCenum eError = AL_NO_ERROR;

/*    //I've actually got other test code loading a real sample, which also
       //didnt work (I get the same error).  
       //This is commented out stuff relating to that.

    ALsizei nSoundSizeInBytes = sizeof(sampleData);
    ALsizei nSampleRate = desc.mSampleRate;


    alBufferData(testALBuffer, AL_FORMAT_MONO16, sampleData, nSoundSizeInBytes, nSampleRate);
*/

// Here's my attempt to just fill a buffer with -anything-, just to try to get it working.
    // temp
    ALchar *tempArr = new ALchar[4];
    tempArr[0] = 128;
    tempArr[1] = 255;
    tempArr[2] = 128;
    tempArr[3] = 0;
    alBufferData(testALBuffer, AL_FORMAT_MONO8, tempArr, 4, 100);
    delete tempArr;
    //end temp

    if((eError = alGetError()) != AL_NO_ERROR) {
                // I get the error here
        printf("Error!");
    }

I've made sure to compile against IPhone OS 2.1 since I've noticed OpenAL doesnt work in the simulator in 2.0.

What am I missing?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
It looks like you're missing an alcMakeContextCurrent?

The OpenAL spec doesn't say that alBufferData can throw INVALID_OPERATION... but your code as pasted doesn't prove it does.
Quote this message in a reply
Apprentice
Posts: 9
Joined: 2008.10
Post: #3
OneSadCookie Wrote:It looks like you're missing an alcMakeContextCurrent?

The OpenAL spec doesn't say that alBufferData can throw INVALID_OPERATION... but your code as pasted doesn't prove it does.

Success! Thank you Grin
(grr, the OpenAL programming guide didnt mention that in the initialisation section text, it's only in the code excerpt in that section. I should read more carefully I guess >_<)


As for the INVALID_OPERATION, I probably should have explained, in the debugger the value of eError was xA004, which according to al.h is that error.


Now I shall proceed to try to play a source..... I hope it goes smoothly.


Update:
Loading a wave file with AudioFileOpenURL, adding it to a buffer then source, and playing the source, all proceeded smoothly without problems Smile

I can post the code if anyone wants it, although maybe noone other than me has problems with such a simple thing Smile
Quote this message in a reply
Member
Posts: 269
Joined: 2005.04
Post: #4
technocrat9000 Wrote:Loading a wave file with AudioFileOpenURL, adding it to a buffer then source, and playing the source, all proceeded smoothly without problems Smile

I can post the code if anyone wants it, although maybe noone other than me has problems with such a simple thing Smile

I'd love to see it. I haven't been able to get OpenAL working on the simulator at all, despite having used OAL for years now. My kingdom for alutLoadWAVFile.

Does OAL even work on the simulator? The CrashLanding sample code doesn't have sound for me either on the sim. And Apple is being a pain in the ass and apparently doesn't want my $99, so I haven't been able to test on a real iPhone yet.
Quote this message in a reply
Apprentice
Posts: 9
Joined: 2008.10
Post: #5
Bachus Wrote:Does OAL even work on the simulator? The CrashLanding sample code doesn't have sound for me either on the sim.

To get OpenAL (such as CrashLanding) to work in the sim, click on the top left of your xcode window (where it says Simulator | Debug), and make sure you select simulator 2.1 on both options. (there should be two places to set to 2.1).

I've found that the bottom setting randomly sets itself back to 2.0, so if your sound ever stops working check that first.

That's my trial-and-error solution, maybe someone can explain in more detail what these settings actually are and how to permanently edit them.
Quote this message in a reply
Apprentice
Posts: 9
Joined: 2008.10
Post: #6
Here's my beginner code for loading a wave file and then adding to a OpenAL buffer and playing it.

Might need to massage it a bit since I cut and paste it out of a larger class file.

Code:
#import <OpenAL/al.h>
#import <OpenAL/alc.h>


// from the .h file:
ALCdevice* myALDevice;
ALCcontext* myALContext;
ALuint testALBuffer;
ALuint testALSource;


// from the .m file:
-(void)initialiseEngine
{
    myALDevice = NULL;
    myALDevice = alcOpenDevice(NULL);

    myALContext = NULL;
    myALContext = alcCreateContext(myALDevice, NULL);

    alcMakeContextCurrent(myALContext);
    
    alGenBuffers(1, &testALBuffer);
    alGenSources(1, &testALSource);

    [self loadWave:CFSTR("bass_loop_a") toBuffer:testALBuffer];
    alSourcei(testALSource, AL_BUFFER, testALBuffer);
    alSourcePlay(testALSource);
}

- (BOOL)loadWave:(CFStringRef)filename toBuffer:(ALuint)bufferID
{
    CFURLRef fileURL =
        CFBundleCopyResourceURL(CFBundleGetMainBundle(), filename, CFSTR("wav"), NULL);

    AudioFileID    audioFileID;
    AudioFileOpenURL (    
        fileURL,
        0x01, //fsRdPerm,                        // read only
        kAudioFileWAVEType,
        &audioFileID
    );

    UInt32 nPropertySize = 0;
    UInt32 nPropertyWritable = 0;
    AudioFileGetPropertyInfo (audioFileID,
                              kAudioFilePropertyAudioDataPacketCount,
                              &nPropertySize,
                              &nPropertyWritable);

    UInt64 sampleLength;
    if (sizeof(sampleLength) == nPropertySize)    {
        AudioFileGetProperty(audioFileID, kAudioFilePropertyAudioDataPacketCount, &nPropertySize, &sampleLength);
    }

    AudioStreamBasicDescription desc;
    UInt32 nPropSize = sizeof(desc);
    AudioFileGetProperty(audioFileID, kAudioFilePropertyDataFormat, &nPropSize, &desc);

    UInt32 nOutNumBytes, nOutNumPackets;
    nOutNumPackets = sampleLength;
    SInt16 sampleData[sampleLength];

    AudioFileReadPackets (
                          audioFileID,
                          false,
                          &nOutNumBytes,
                          NULL,
                          0,
                          &nOutNumPackets,
                          sampleData
                          );


    ALsizei nSoundSizeInBytes = sizeof(sampleData);
    ALsizei nSampleRate = desc.mSampleRate;

    alBufferData(bufferID, AL_FORMAT_MONO16, sampleData, nSoundSizeInBytes, nSampleRate);

    return YES;
}

-(void)tearDownEngine
{
    alcMakeContextCurrent(NULL);
    alcDestroyContext(myALContext);
    alcCloseDevice(myALDevice);
}
Quote this message in a reply
Member
Posts: 269
Joined: 2005.04
Post: #7
technocrat9000 Wrote:To get OpenAL (such as CrashLanding) to work in the sim, click on the top left of your xcode window (where it says Simulator | Debug), and make sure you select simulator 2.1 on both options. (there should be two places to set to 2.1).

I've found that the bottom setting randomly sets itself back to 2.0, so if your sound ever stops working check that first.

That's my trial-and-error solution, maybe someone can explain in more detail what these settings actually are and how to permanently edit them.

Hmm, weird. I set to iPhone OS 2.1 and iPhone Simulator 2.1 and I still can't get any sound in my code or CrashLanding. Metronome that uses AudioServices instead of OAL works fine. Sad
Quote this message in a reply
Member
Posts: 32
Joined: 2008.10
Post: #8
I've got OpenAL working just fine on the actual device (didn't have to change a single line of code from my cross platform rendering engine), however it doesn't play any sound in the simulator. Not that I care that much, since it works on the actual hardware, and I do testing on desktop target anyway.
Quote this message in a reply
Member
Posts: 241
Joined: 2008.07
Post: #9
Make sure you have the target set to Simulator - iPhone OS 2.1. For some reason 2.0 doesn't work.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  alBufferDataStatic instead of alBufferData monteboyd 6 6,784 Feb 16, 2009 04:12 AM
Last Post: monteboyd