|
| 1 | +/* |
| 2 | +
|
| 3 | + File: MyOpenALSupport.c |
| 4 | +Abstract: OpenAL-related support functions |
| 5 | + Version: 1.9 |
| 6 | +
|
| 7 | +Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple |
| 8 | +Inc. ("Apple") in consideration of your agreement to the following |
| 9 | +terms, and your use, installation, modification or redistribution of |
| 10 | +this Apple software constitutes acceptance of these terms. If you do |
| 11 | +not agree with these terms, please do not use, install, modify or |
| 12 | +redistribute this Apple software. |
| 13 | +
|
| 14 | +In consideration of your agreement to abide by the following terms, and |
| 15 | +subject to these terms, Apple grants you a personal, non-exclusive |
| 16 | +license, under Apple's copyrights in this original Apple software (the |
| 17 | +"Apple Software"), to use, reproduce, modify and redistribute the Apple |
| 18 | +Software, with or without modifications, in source and/or binary forms; |
| 19 | +provided that if you redistribute the Apple Software in its entirety and |
| 20 | +without modifications, you must retain this notice and the following |
| 21 | +text and disclaimers in all such redistributions of the Apple Software. |
| 22 | +Neither the name, trademarks, service marks or logos of Apple Inc. may |
| 23 | +be used to endorse or promote products derived from the Apple Software |
| 24 | +without specific prior written permission from Apple. Except as |
| 25 | +expressly stated in this notice, no other rights or licenses, express or |
| 26 | +implied, are granted by Apple herein, including but not limited to any |
| 27 | +patent rights that may be infringed by your derivative works or by other |
| 28 | +works in which the Apple Software may be incorporated. |
| 29 | +
|
| 30 | +The Apple Software is provided by Apple on an "AS IS" basis. APPLE |
| 31 | +MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION |
| 32 | +THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS |
| 33 | +FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND |
| 34 | +OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. |
| 35 | +
|
| 36 | +IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL |
| 37 | +OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 38 | +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 39 | +INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, |
| 40 | +MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED |
| 41 | +AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), |
| 42 | +STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE |
| 43 | +POSSIBILITY OF SUCH DAMAGE. |
| 44 | +
|
| 45 | +Copyright (C) 2010 Apple Inc. All Rights Reserved. |
| 46 | +
|
| 47 | +
|
| 48 | +*/ |
| 49 | + |
| 50 | +#include "MyOpenALSupport.h" |
| 51 | + |
| 52 | +ALvoid alBufferDataStaticProc(const ALint bid, ALenum format, ALvoid* data, ALsizei size, ALsizei freq) |
| 53 | +{ |
| 54 | + static alBufferDataStaticProcPtr proc = NULL; |
| 55 | + |
| 56 | + if (proc == NULL) { |
| 57 | + proc = (alBufferDataStaticProcPtr) alcGetProcAddress(NULL, (const ALCchar*) "alBufferDataStatic"); |
| 58 | + } |
| 59 | + |
| 60 | + if (proc) |
| 61 | + proc(bid, format, data, size, freq); |
| 62 | + |
| 63 | + return; |
| 64 | +} |
| 65 | + |
| 66 | +void* MyGetOpenALAudioData(CFURLRef inFileURL, ALsizei *outDataSize, ALenum *outDataFormat, ALsizei* outSampleRate) |
| 67 | +{ |
| 68 | + OSStatus err = noErr; |
| 69 | + SInt64 theFileLengthInFrames = 0; |
| 70 | + AudioStreamBasicDescription theFileFormat; |
| 71 | + UInt32 thePropertySize = sizeof(theFileFormat); |
| 72 | + ExtAudioFileRef extRef = NULL; |
| 73 | + void* theData = NULL; |
| 74 | + AudioStreamBasicDescription theOutputFormat; |
| 75 | + |
| 76 | + // Open a file with ExtAudioFileOpen() |
| 77 | + err = ExtAudioFileOpenURL(inFileURL, &extRef); |
| 78 | + if(err) { printf("MyGetOpenALAudioData: ExtAudioFileOpenURL FAILED, Error = %ld\n", err); goto Exit; } |
| 79 | + |
| 80 | + // Get the audio data format |
| 81 | + err = ExtAudioFileGetProperty(extRef, kExtAudioFileProperty_FileDataFormat, &thePropertySize, &theFileFormat); |
| 82 | + if(err) { printf("MyGetOpenALAudioData: ExtAudioFileGetProperty(kExtAudioFileProperty_FileDataFormat) FAILED, Error = %ld\n", err); goto Exit; } |
| 83 | + if (theFileFormat.mChannelsPerFrame > 2) { printf("MyGetOpenALAudioData - Unsupported Format, channel count is greater than stereo\n"); goto Exit;} |
| 84 | + |
| 85 | + // Set the client format to 16 bit signed integer (native-endian) data |
| 86 | + // Maintain the channel count and sample rate of the original source format |
| 87 | + theOutputFormat.mSampleRate = theFileFormat.mSampleRate; |
| 88 | + theOutputFormat.mChannelsPerFrame = theFileFormat.mChannelsPerFrame; |
| 89 | + |
| 90 | + theOutputFormat.mFormatID = kAudioFormatLinearPCM; |
| 91 | + theOutputFormat.mBytesPerPacket = 2 * theOutputFormat.mChannelsPerFrame; |
| 92 | + theOutputFormat.mFramesPerPacket = 1; |
| 93 | + theOutputFormat.mBytesPerFrame = 2 * theOutputFormat.mChannelsPerFrame; |
| 94 | + theOutputFormat.mBitsPerChannel = 16; |
| 95 | + theOutputFormat.mFormatFlags = kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsPacked | kAudioFormatFlagIsSignedInteger; |
| 96 | + |
| 97 | + // Set the desired client (output) data format |
| 98 | + err = ExtAudioFileSetProperty(extRef, kExtAudioFileProperty_ClientDataFormat, sizeof(theOutputFormat), &theOutputFormat); |
| 99 | + if(err) { printf("MyGetOpenALAudioData: ExtAudioFileSetProperty(kExtAudioFileProperty_ClientDataFormat) FAILED, Error = %ld\n", err); goto Exit; } |
| 100 | + |
| 101 | + // Get the total frame count |
| 102 | + thePropertySize = sizeof(theFileLengthInFrames); |
| 103 | + err = ExtAudioFileGetProperty(extRef, kExtAudioFileProperty_FileLengthFrames, &thePropertySize, &theFileLengthInFrames); |
| 104 | + if(err) { printf("MyGetOpenALAudioData: ExtAudioFileGetProperty(kExtAudioFileProperty_FileLengthFrames) FAILED, Error = %ld\n", err); goto Exit; } |
| 105 | + |
| 106 | + // Read all the data into memory |
| 107 | + UInt32 dataSize = theFileLengthInFrames * theOutputFormat.mBytesPerFrame;; |
| 108 | + theData = malloc(dataSize); |
| 109 | + if (theData) |
| 110 | + { |
| 111 | + AudioBufferList theDataBuffer; |
| 112 | + theDataBuffer.mNumberBuffers = 1; |
| 113 | + theDataBuffer.mBuffers[0].mDataByteSize = dataSize; |
| 114 | + theDataBuffer.mBuffers[0].mNumberChannels = theOutputFormat.mChannelsPerFrame; |
| 115 | + theDataBuffer.mBuffers[0].mData = theData; |
| 116 | + |
| 117 | + // Read the data into an AudioBufferList |
| 118 | + err = ExtAudioFileRead(extRef, (UInt32*)&theFileLengthInFrames, &theDataBuffer); |
| 119 | + if(err == noErr) |
| 120 | + { |
| 121 | + // success |
| 122 | + *outDataSize = (ALsizei)dataSize; |
| 123 | + *outDataFormat = (theOutputFormat.mChannelsPerFrame > 1) ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16; |
| 124 | + *outSampleRate = (ALsizei)theOutputFormat.mSampleRate; |
| 125 | + } |
| 126 | + else |
| 127 | + { |
| 128 | + // failure |
| 129 | + free (theData); |
| 130 | + theData = NULL; // make sure to return NULL |
| 131 | + printf("MyGetOpenALAudioData: ExtAudioFileRead FAILED, Error = %ld\n", err); goto Exit; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | +Exit: |
| 136 | + // Dispose the ExtAudioFileRef, it is no longer needed |
| 137 | + if (extRef) ExtAudioFileDispose(extRef); |
| 138 | + return theData; |
| 139 | +} |
| 140 | + |
0 commit comments