OpenAL - Ogg files vs Caf files

Member
Posts: 34
Joined: 2009.01
Post: #1
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

My first game on AppStore : here
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #2
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

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #3
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).
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #4
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.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Member
Posts: 34
Joined: 2009.01
Post: #5
[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

My first game on AppStore : here
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #6
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

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Member
Posts: 34
Joined: 2009.01
Post: #7
(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 ?

My first game on AppStore : here
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #8
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.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Member
Posts: 96
Joined: 2011.07
Post: #9
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.)

respect,
pk

iFrog is coming.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  How To Make .des Files? hariszaman 2 4,563 Mar 11, 2012 02:17 PM
Last Post: SethWillits
  Best way to generate png files kalamantina 2 5,396 Dec 7, 2010 02:15 PM
Last Post: SethWillits
  how do i make .xm music files? mac_girl 8 11,849 Mar 6, 2007 06:01 AM
Last Post: Leisure Suit Lurie
  Linking multiple glsl source files into one program TomorrowPlusX 5 7,051 Nov 2, 2006 02:18 PM
Last Post: OneSadCookie
  Matching sound files sameersbn 1 3,246 Sep 27, 2005 03:01 AM
Last Post: Fenris