iDevGames Forums
OpenAL: looking for a simple example - Printable Version

+- iDevGames Forums (
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: OpenAL: looking for a simple example (/thread-2425.html)

Pages: 1 2

OpenAL: looking for a simple example - MoProductions - Sep 3, 2008 11:26 AM

Hello there. I've been looking everywhere I can find but can't find a single example of how to get sounds playing on the iPhone with openAL. I've found several examples, but they're all for platforms OTHER than iPhone because they use the "alutLoadWAVFile", which isn't supported on iPhone. I've looked at the SoundEngine stuff that came with the CrashLanding demo but it's so convoluted and full of stuff I don't need it's kinda useless. All I want is to be able to play sounds on a loop. I might look up the AudioQueueServicers package but everywhere I've read it says OpenAL is better for games (so I might as well get cracking). Any help is greatly appreciated.

OpenAL: looking for a simple example - OneSadCookie - Sep 3, 2008 02:52 PM

Writing an alutLoadWAVFile replacement for yourself is easy; the WAV file format is very straightforward.

On Mac OS X, CoreAudio has audio file loading functions you could use instead; I don't know if that's available on the phone.

OpenAL: looking for a simple example - kodex - Sep 3, 2008 09:36 PM

The iPhone remains under NDA and no one will be able to give you specific answers for the iPhone, the best we are allowed to do is provide tips for accomplishing things on the mac.

Please refrain from asking iPhone specific questions.

OpenAL: looking for a simple example - MoProductions - Sep 4, 2008 01:33 PM

Hey there OneSadCookie, thanx for the response. Unfortunately we need a bit more flexibility in our stuff due to all the kinds of formats we're going to need to deal with, hence the OpenAL. As far as the Core Audio, I did manage to get that to work but it's way too limiting because you can't have multiple sound files going at once, no looping, no volume control, etc.

And as far as not talking about the iPhone, I'm not sure I understand. If this is a forum for OSX and iPhone development, how are we not supposed to discuss the iPhone? There's an entire section called " iPhone & iPod Game Development: Game programming topics devoted to the Apple iPhone & iPod." I left this out of there because I assumed it should go into the audio specific forum.

OpenAL: looking for a simple example - AnotherJake - Sep 4, 2008 02:56 PM

Yes, the whole "can't talk about iPhone" thing is indeed confusing (and irritating, to say the least), but it's Apple's fault, not the forum's [although OSC is arguably correct in his comment below]. There's a section because it was assumed that Apple would release NDA, which they still haven't. It's still there because no one has heard that Apple won't be releasing NDA. Sorry, yes, it sucks, we've all complained about it, but the rules are the rules.

Yes, the stuff you need to load wav files with is in that code sample you mentioned. Yes it is convoluted, but it's all there. You can figure it out if you take it step at a time. I was able to piece together a loader in a couple hours from it.

[adding] You really should heed kodex's advice and implement your OpenAL stuff on the Mac first, so you might actually get some help with it.

OpenAL: looking for a simple example - OneSadCookie - Sep 4, 2008 02:57 PM

I didn't suggest you use CoreAudio directly for playback, only for loading files, rather than writing your own file loader.

The iPhone SDK is under NDA. The board is misnamed.

OpenAL: looking for a simple example - MoProductions - Sep 5, 2008 12:02 PM

Oh ok, I gotcha now. Seems strange since any yahoo (such as myself) can get the SDK but not allowed to talk about it. Hopefully that will end up soon.

As far as sound, I didn't know you could cross mix 'n match the different ways to play it but as you can see I'm new to audio on this platform. It took a bit but eventually was able to widdle down the SoundEngine stuff and get a simple .caf file (or any other file) loaded up and running with OpenAL (ok ok I had help from a friend at Apple) so once the NDA is done I can post it here if anyone wants it.


OpenAL: looking for a simple example - spuy767 - Sep 6, 2008 11:24 AM

idevgames just needs to lighten up. Apple isn't even moderating people talking about iPhone SDK out of their own boards. They could care less about developers sharing snippets of code. I've got a hunch that the NDA is in place to discourage things far more spurious than small time developers sharing code.

OpenAL: looking for a simple example - AnotherJake - Sep 6, 2008 12:07 PM

spuy767 Wrote:Apple isn't even moderating people talking about iPhone SDK out of their own boards.

I don't get that either, but they sure as heck have made it clear on their mailing lists, several times, that we still aren't supposed to talk about it anywhere -- "This includes other mailing lists, forums, and definitely blogs."

iDG falls under the "forums" category of course. Why Apple doesn't cover their own forums but makes it clear on their developer mailing list is a mystery. I'm hoping maybe they'll clear this mess up on Tuesday.

OpenAL: looking for a simple example - mediashock - Oct 1, 2008 01:37 PM

the NDA was lifted today

OpenAL: looking for a simple example - AnotherJake - Oct 1, 2008 01:44 PM

mediashock Wrote:the NDA was lifted today

Yes it was, but we're still under it until we get to sign the new one, whenever they get around to letting us do that... It never ends!

OpenAL: looking for a simple example - mediashock - Oct 6, 2008 08:22 AM

Yeah i dont think they are taking it as serious as people think.. there is tons of websites out there with tutorials and open discussions.

OpenAL: looking for a simple example - kordos - Jan 8, 2009 10:06 AM

... but not a lot of openAL simple examples, to the point of the original post Smile.

RE: OpenAL: looking for a simple example - kjregan - May 18, 2011 02:02 PM

If you're looking for a simple manager for openAL, check out

RE: OpenAL: looking for a simple example - Charybdis - Jun 28, 2012 03:56 PM

This is my loading code:

bool Sound::load(const std::string& _Name,const bool _Looping)
    std::string filename = DATAPATH+"sfx/"+_Name+".wav";
    FILE* fp = fopen(filename.c_str(),"rb");
    if( !fp ) { std::cout << "Failed to open sound:" << _Name << std::endl; return false; }

    char type[4];
    DWORD size,chunkSize;
    short formatType,channels;
    DWORD sampleRate,avgBytesPerSec;
    short bytesPerSample,bitsPerSample;
    DWORD dataSize;
    // *** check if WAV file is OK ***
    if( type[0]!='R' || type[1]!='I' || type[2]!='F' || type[3]!='F') { std::cout << "No RIFF: " << _Name << std::endl; return false; }

    if (type[0]!='W' || type[1]!='A' || type[2]!='V' || type[3]!='E') { std::cout << "not WAVE: " << _Name << std::endl; return false; }
    if (type[0]!='f' || type[1]!='m' || type[2]!='t' || type[3]!=' ') { std::cout << "not fmt: " << _Name << std::endl; return false; }

    // *** now data is read for the (valid) WAV file ***
    if (type[0]!='d' || type[1]!='a' || type[2]!='t' || type[3]!='a') { std::cout << "Missing DATA: " << _Name << std::endl; return false; }    
    fread(&dataSize,sizeof(DWORD),1,fp); // size of sound data
    std::cout << "Chunk Size: " << chunkSize << std::endl;
    std::cout << "Format Type: " << formatType << std::endl;
    std::cout << "Channels: " << channels << std::endl;
    std::cout << "Sample Rate: " << sampleRate << std::endl;
    std::cout << "Average Bytes Per Second: " << avgBytesPerSec << std::endl;
    std::cout << "Bytes Per Sample: " << bytesPerSample << std::endl;
    std::cout << "Bits Per Sample: " << bitsPerSample << std::endl;
    std::cout << "Data Size: " << dataSize << std::endl;

    _data_buffer = new unsigned char[dataSize]; // temp buffer
    std::cout << fread(_data_buffer,sizeof(BYTE),dataSize,fp) << " bytes loaded" << std::endl;

    ALuint frequency = sampleRate;
    ALenum format = 0;
    if(bitsPerSample == 8)
        if(channels == 1)
            format = AL_FORMAT_MONO8;
        else if(channels == 2)
            format = AL_FORMAT_STEREO8;
    else if(bitsPerSample == 16)
        if(channels == 1)
            format = AL_FORMAT_MONO16;
        else if(channels == 2)
            format = AL_FORMAT_STEREO16;
    if( !format ) { std::cout << "Wrong BitPerSample: " << _Name << std::endl; return false; }

    alBufferData(_buffer, format, _data_buffer, dataSize, frequency);
    if( alGetError()!=AL_NO_ERROR ) { std::cout << "Error loading ALBuffer: " << _Name << std::endl; return false; }

    ALfloat SourcePos[] = { 0.0, 0.0, 0.0 };  
    ALfloat SourceVel[] = { 0.0, 0.0, 0.0 };  
    alSourcei (_source, AL_BUFFER,   _buffer);
    alSourcef (_source, AL_PITCH,    1.0f     );
    alSourcef (_source, AL_GAIN,     1.0f     );
    alSourcefv(_source, AL_POSITION, SourcePos);
    alSourcefv(_source, AL_VELOCITY, SourceVel);
    alSourcei (_source, AL_LOOPING,  (_Looping) ? AL_TRUE : AL_FALSE );

    return true;

It is roughly based on code from somewhere, but I cant find the link. Anyway, hope it helps.