AudioToolbox / OpenAL ExtAudioFile for playing compressed audio

I am currently using OpenAL to play game music. It works great, except that it does not work with anything other than raw WAV files. This means that I am finishing the soundtrack with a size of ~ 9 m.

I am new to OpenAL and I use the code directly from the Apple example ( https://developer.apple.com/library/ios/# samplecode / MusicCube / Listings / Classes_MyOpenALSupport_h.html% 23 // apple_ref / doc / uid / DTS40008978- Classes_MyOpenALSupport_h-DontLinkElementID_9 ) to get the buffer data.

Question: Is there a way to change this function so that it reads compressed sound and decodes it on the fly?
I don't care about the audio file format, as long as it can be played and compressed (e.g. mp3, aac, caf). The only reason I want to do this (obviously) is to reduce the file size.

Edit: It seems that the problem is not so much in OpenAL, but in the method that I use to get the buffer. Function https://developer.apple.com/library/ios/#samplecode/MusicCube/Listings/Classes_MyOpenALSupport_h.html%23//apple_ref/doc/uid/DTS40008978-Classes_MyOpenALSupport_h-DontLinkElementID_9 uses AudioFileOpenURL and AudioFileReadBytes . Is there a way to get the infrastructure to decode audio for me using ExtAudioFileOpenURL and ExtAudioFileRead ?

I tried the code here: https://devforums.apple.com/message/10678#10678 , but I don't know what to do with it. The function I use to get the buffer is in https://developer.apple.com/library/ios/#samplecode/MusicCube/Listings/Classes_MyOpenALSupport_h.html% 23 // apple_ref / doc / uid / DTS40008978-Classes_MyOpenALSupport_hment-Dent , and I did not modify it, so I need to do something.

I started generosity because I really need it, I hope someone can point me in the right direction.

+4
source share
5 answers

To download other formats, you need to use audio services. Keep in mind that OpenAL ONLY supports uncompressed PCM data, so any downloaded data must be uncompressed at boot time.

Here is the code that will download any format supported by iOS: https://github.com/kstenerud/ObjectAL-for-iPhone/blob/master/ObjectAL/ObjectAL/Support/OALAudioFile.m

If you want to transfer compressed sound to an audio track, use AVAudioPlayer, as it plays compressed sound directly from the disc.

+4
source

You do not need a third-party library to open archive files. With a little help from the AudioToolbox/AudioToolbox.h framework, you can open and read the data of a .caf file, which is a very good choice (better than mp3 or ogg) in terms of performance (minimal CPU impact during decompression). Thus, when the data falls into OpenAL, already PCM , it is ready to fill the buffers. Here is a sample code of how you can do this:

 -(void) prepareFiles:(NSString *) filePath{ // get the full path of the file NSString* fileName = [[NSBundle mainBundle] pathForResource:filePath ofType:@"caf"]; // open the file using the custom created methods (see below) AudioFileID fileID = [self openAudioFile:fileName]; preparedAudioFileSize = [self audioFileSize:fileID]; if (preparedAudioFile){ free(preparedAudioFile); preparedAudioFile = nil; } else{ ; } preparedAudioFile = malloc(preparedAudioFileSize); //read the data from the file into soundOutData var AudioFileReadBytes(fileID, false, 0, &preparedAudioFileSize, preparedAudioFile); //close the file AudioFileClose(fileID); } -(AudioFileID)openAudioFile:(NSString*)filePath { AudioFileID fileID; NSURL * url = [NSURL fileURLWithPath:filePath]; OSStatus result = AudioFileOpenURL((CFURLRef)url, kAudioFileReadPermission, 0, &fileID); if (result != noErr) { NSLog(@"fail to open: %@",filePath); } else { ; } return fileID; } -(UInt32)audioFileSize:(AudioFileID)fileDescriptor { UInt64 outDataSize = 0; UInt32 thePropSize = sizeof(UInt64); OSStatus result = AudioFileGetProperty(fileDescriptor, kAudioFilePropertyAudioDataByteCount, &thePropSize, &outDataSize); if(result != 0) NSLog(@"cannot find file size"); return (UInt32)outDataSize; } 
+3
source

Based on Karl's answer above, I created a minimal unified C ++ function that opens a file and returns you the pcm audio buffer (suitable for OpenAL) and all the information needed to create OpenAL sound (format, buffering, etc.).

two files are needed: https://gist.github.com/ofTheo/5171369

hope this helps! theor

+1
source

You can try using a third-party library to load mp3-ogg into the char * buffer, and then provide this openAL buffer. This will solve the file size problem.

For ogg you have to find libraries on your website

For mp3, I honestly don't know where to find a light library that could do this. But it must exist.

0
source

All Articles