OpenAL: looking for a simple example

Apprentice
Posts: 11
Joined: 2008.09
Post: #1
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.
Mike
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
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.
Quote this message in a reply
Moderator
Posts: 613
Joined: 2004.09
Post: #3
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.

Kyle Richter
DragonForged.com
Twitter: @kylerichter
Quote this message in a reply
Apprentice
Posts: 11
Joined: 2008.09
Post: #4
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.
Thanx
MoP
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #5
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.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #6
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.
Quote this message in a reply
Apprentice
Posts: 11
Joined: 2008.09
Post: #7
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.

Thanx
Mo
Quote this message in a reply
Nibbie
Posts: 4
Joined: 2008.09
Post: #8
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.
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #9
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.
Quote this message in a reply
Apprentice
Posts: 8
Joined: 2008.09
Post: #10
the NDA was lifted today
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #11
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!
Quote this message in a reply
Apprentice
Posts: 8
Joined: 2008.09
Post: #12
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.
Quote this message in a reply
Apprentice
Posts: 6
Joined: 2009.01
Post: #13
... but not a lot of openAL simple examples, to the point of the original post Smile.
Quote this message in a reply
Nibbie
Posts: 1
Joined: 2011.05
Post: #14
If you're looking for a simple manager for openAL, check out http://elevatedpixels.com/?p=117
Quote this message in a reply
Apprentice
Posts: 6
Joined: 2011.06
Post: #15
This is my loading code:

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 ***
    fread(type,sizeof(char),4,fp);
    if( type[0]!='R' || type[1]!='I' || type[2]!='F' || type[3]!='F') { std::cout << "No RIFF: " << _Name << std::endl; return false; }

    fread(&size,sizeof(DWORD),1,fp);
    fread(type,sizeof(char),4,fp);
    if (type[0]!='W' || type[1]!='A' || type[2]!='V' || type[3]!='E') { std::cout << "not WAVE: " << _Name << std::endl; return false; }
    
    fread(type,sizeof(char),4,fp);
    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 ***
    fread(&chunkSize,sizeof(DWORD),1,fp);
    fread(&formatType,sizeof(short),1,fp);
    fread(&channels,sizeof(short),1,fp);
    fread(&sampleRate,sizeof(DWORD),1,fp);
    fread(&avgBytesPerSec,sizeof(DWORD),1,fp);
    fread(&bytesPerSample,sizeof(short),1,fp);
    fread(&bitsPerSample,sizeof(short),1,fp);
    
    fread(type,sizeof(char),4,fp);
    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;
    fclose(fp);

    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.

"I just want to program!"
Quote this message in a reply
Post Reply