iDevGames Forums
OpenAL - Ogg files vs Caf files - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: OpenAL - Ogg files vs Caf files (/thread-8173.html)



OpenAL - Ogg files vs Caf files - Fred9000 - Oct 5, 2010 06:37 AM

Hello all

in my games, I'm currently playing music that is in ima4 format in .caf files. The Ogg format gives smaller files than Caf but from what I understand Ogg files need to be decoded just like Caf files, and then need the same buffer size as the original wav file would (just like Caf do).

Is it true ?
I can't find a simple working sample of Ogg player, it seems there's always something missing.
Sorry if this has already been discussed here, but the search feature of the forum always return an empty list...

Fred


RE: OpenAL - Ogg files vs Caf files - Skorche - Oct 5, 2010 07:19 AM

Ogg loading is actually pretty easy.

Code:
static VALUE
rbas_load_vorbis(VALUE self, VALUE filename)
{
    FILE *file = fopen(StringValuePtr(filename), "rb");
    if(!file) rb_raise(rb_eIOError, "File not found: %s", filename);
    
    OggVorbis_File vorbis;
    if(ov_open(file, &vorbis, NULL, 0))
        rb_raise(rb_eIOError, "Could not read %s", filename);
    
    vorbis_info *info = ov_info(&vorbis, -1);
    int samples = ov_pcm_total(&vorbis, -1);
    int rate = info->rate;
    int channels = info->channels;
    
    // Breaks for non mono/stereo
    ALenum ALformat = (channels == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
    int bytes = 2*samples*channels;
    
    VALUE data = rb_str_new(NULL, 0);
    rb_str_resize(data, bytes);
    
    int bytes_read = 0;
    while(bytes_read < bytes){
        int remain = bytes - bytes_read;
        char *cursor = StringValuePtr(data) + bytes_read;
        
        long value = ov_read(&vorbis, cursor, remain, (SDL_BYTEORDER==SDL_BIG_ENDIAN), 2, 1, NULL);
        if(value < 0) rb_raise(rb_eIOError, "Could not read %s", filename);
        
        bytes_read += value;
    }
    
    ov_clear(&vorbis);
    return rb_struct_new(rb_eval_string("AS::SoundStruct"), data, INT2NUM(rate), INT2NUM(ALformat));
}

Is all you need to do to decompress an entire Ogg Vorbis. There is some Ruby extension code mixed in, but I think you get the idea.

However, for music you really want to stream the music. Meaning you only decompress a little at a time. I have some code for doing that as well: http://gist.github.com/611624


RE: OpenAL - Ogg files vs Caf files - OneSadCookie - Oct 5, 2010 07:42 AM

Since you're talking about iPhone, be aware that the iPhone can decompress one stream of certain audio formats in hardware. You should take advantage of that where possible (eg. background music).


RE: OpenAL - Ogg files vs Caf files - Skorche - Oct 5, 2010 09:12 AM

Oh iPhone? Yeah, don't decompress things in software if you can avoid it and Vorbis is not supported by the hardware. You'll be doing yourself and your player's batteries favor by converting it to AAC and using AVAuidioPlayer. Playing the music as a stream is a half a dozen lines of code.


RE: OpenAL - Ogg files vs Caf files - Fred9000 - Oct 6, 2010 01:53 AM

[OneSadCookie] not only iPhone but iPhone/iPod/iPad actually Smile

[Skorche] I just download Switch Audio Converter to convert the wav to aac, and I give a try to AVAudioPlayer.

Is it still possible to play other sounds (caf or wav) while streaming AAC background music ?

Thank you all


RE: OpenAL - Ogg files vs Caf files - Skorche - Oct 6, 2010 06:11 AM

Yes, you can use an AVAudioPlayer to play music and still use OpenAL.

For all intents and purposes, iPhone is still used as an umbrella term for all the iOS devices as it was called the iPhone OS up until a couple months ago. Rasp


RE: OpenAL - Ogg files vs Caf files - Fred9000 - Oct 6, 2010 09:15 AM

(Oct 6, 2010 06:11 AM)Skorche Wrote:  Yes, you can use an AVAudioPlayer to play music and still use OpenAL.

Yes I can. The AVAudioPlayer code is exactly half a dozen lines Smile

The problem is now if I wants to hear sounds but not the music and if I release, stop or even pause the AVaudioPlayer, OpenAL doesn't make any sound anymore.
That's probably the reason why I stopped using AVaudioplayer.
I think I will just set the volume to 0.0 instead of pausing Cool

[edit] oh oh... setting the volume to 0.0 on the AVAudioPlayer turns off OpenAL volume too Smile

Anyone knows how to do ?


RE: OpenAL - Ogg files vs Caf files - Skorche - Oct 6, 2010 10:46 AM

Never had that problem.

I'm guessing that the AVAudioPlayer is probably controlling your app's audio session category because you never set one up. When you stop the music, it disables the app's audio on you.


RE: OpenAL - Ogg files vs Caf files - ipeku - Aug 23, 2011 08:01 PM

One thing to note that caf is only a wrapper. It can hold audio coded in different formats such as PCM or AAC. AAC is supposed to be a good format because, as in post #3 OSC mentioned iOS devices can decode some formats with hardware and AAC is one of them. 128kbps AAC sounds really good already but I'm not aware how it compares with ogg in terms of audio quality and size. At least it's better than mp3.

I thought of sharing 2 file dropper convertors that I've created and used for iOS games:

aif2caf

It comes with two converters. One to convert an aif to a caf that holds 128kbps AAC and the other that converts an aif to a caf with 16 bit PCM.

The first is for background music and the second for sound effects.

(Two known bugs: you can't have spaces in the name of the aif and I also believe you have to have these on your desktop when dropping the aif on top. Also, only tested on snow leopard.)