CDOpenALSupport.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. Disclaimer: IMPORTANT: This Apple software is supplied to you by
  3. Apple Inc. ("Apple") in consideration of your agreement to the
  4. following terms, and your use, installation, modification or
  5. redistribution of this Apple software constitutes acceptance of these
  6. terms. If you do not agree with these terms, please do not use,
  7. install, modify or redistribute this Apple software.
  8. In consideration of your agreement to abide by the following terms, and
  9. subject to these terms, Apple grants you a personal, non-exclusive
  10. license, under Apple's copyrights in this original Apple software (the
  11. "Apple Software"), to use, reproduce, modify and redistribute the Apple
  12. Software, with or without modifications, in source and/or binary forms;
  13. provided that if you redistribute the Apple Software in its entirety and
  14. without modifications, you must retain this notice and the following
  15. text and disclaimers in all such redistributions of the Apple Software.
  16. Neither the name, trademarks, service marks or logos of Apple Inc.
  17. may be used to endorse or promote products derived from the Apple
  18. Software without specific prior written permission from Apple. Except
  19. as expressly stated in this notice, no other rights or licenses, express
  20. or implied, are granted by Apple herein, including but not limited to
  21. any patent rights that may be infringed by your derivative works or by
  22. other works in which the Apple Software may be incorporated.
  23. The Apple Software is provided by Apple on an "AS IS" basis. APPLE
  24. MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
  25. THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
  26. FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
  27. OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
  28. IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
  29. OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
  32. MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
  33. AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
  34. STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
  35. POSSIBILITY OF SUCH DAMAGE.
  36. Copyright (C) 2009 Apple Inc. All Rights Reserved.
  37. $Id: CDOpenALSupport.h 16 2010-03-11 06:22:10Z steveoldmeadow $
  38. */
  39. #import "audio/ios/CDOpenALSupport.h"
  40. #import "audio/ios/CocosDenshion.h"
  41. #import <AudioToolbox/AudioToolbox.h>
  42. #import <AudioToolbox/ExtendedAudioFile.h>
  43. //Taken from oalTouch MyOpenALSupport 1.1
  44. void* CDloadWaveAudioData(CFURLRef inFileURL, ALsizei *outDataSize, ALenum *outDataFormat, ALsizei* outSampleRate)
  45. {
  46. OSStatus err = noErr;
  47. UInt64 fileDataSize = 0;
  48. AudioStreamBasicDescription theFileFormat;
  49. UInt32 thePropertySize = sizeof(theFileFormat);
  50. AudioFileID afid = 0;
  51. void* theData = NULL;
  52. // Open a file with ExtAudioFileOpen()
  53. err = AudioFileOpenURL(inFileURL, kAudioFileReadPermission, 0, &afid);
  54. if(err) { CDLOG(@"MyGetOpenALAudioData: AudioFileOpenURL FAILED, Error = %ld\n", err); goto Exit; }
  55. // Get the audio data format
  56. err = AudioFileGetProperty(afid, kAudioFilePropertyDataFormat, &thePropertySize, &theFileFormat);
  57. if(err) { CDLOG(@"MyGetOpenALAudioData: AudioFileGetProperty(kAudioFileProperty_DataFormat) FAILED, Error = %ld\n", err); goto Exit; }
  58. if (theFileFormat.mChannelsPerFrame > 2) {
  59. CDLOG(@"MyGetOpenALAudioData - Unsupported Format, channel count is greater than stereo\n"); goto Exit;
  60. }
  61. if ((theFileFormat.mFormatID != kAudioFormatLinearPCM) || (!TestAudioFormatNativeEndian(theFileFormat))) {
  62. CDLOG(@"MyGetOpenALAudioData - Unsupported Format, must be little-endian PCM\n"); goto Exit;
  63. }
  64. if ((theFileFormat.mBitsPerChannel != 8) && (theFileFormat.mBitsPerChannel != 16)) {
  65. CDLOG(@"MyGetOpenALAudioData - Unsupported Format, must be 8 or 16 bit PCM\n"); goto Exit;
  66. }
  67. thePropertySize = sizeof(fileDataSize);
  68. err = AudioFileGetProperty(afid, kAudioFilePropertyAudioDataByteCount, &thePropertySize, &fileDataSize);
  69. if(err) { CDLOG(@"MyGetOpenALAudioData: AudioFileGetProperty(kAudioFilePropertyAudioDataByteCount) FAILED, Error = %ld\n", err); goto Exit; }
  70. // Read all the data into memory
  71. UInt32 dataSize = (UInt32)fileDataSize;
  72. theData = malloc(dataSize);
  73. if (theData)
  74. {
  75. memset(theData, 0, dataSize);
  76. AudioFileReadBytes(afid, false, 0, &dataSize, theData);
  77. if(err == noErr)
  78. {
  79. // success
  80. *outDataSize = (ALsizei)dataSize;
  81. //This fix was added by me, however, 8 bit sounds have a clipping sound at the end so aren't really usable (SO)
  82. if (theFileFormat.mBitsPerChannel == 16) {
  83. *outDataFormat = (theFileFormat.mChannelsPerFrame > 1) ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16;
  84. } else {
  85. *outDataFormat = (theFileFormat.mChannelsPerFrame > 1) ? AL_FORMAT_STEREO8 : AL_FORMAT_MONO8;
  86. }
  87. *outSampleRate = (ALsizei)theFileFormat.mSampleRate;
  88. }
  89. else
  90. {
  91. // failure
  92. free (theData);
  93. theData = NULL; // make sure to return NULL
  94. CDLOG(@"MyGetOpenALAudioData: ExtAudioFileRead FAILED, Error = %ld\n", err); goto Exit;
  95. }
  96. }
  97. Exit:
  98. // Dispose the ExtAudioFileRef, it is no longer needed
  99. if (afid) AudioFileClose(afid);
  100. return theData;
  101. }
  102. //Taken from oalTouch MyOpenALSupport 1.4
  103. void* CDloadCafAudioData(CFURLRef inFileURL, ALsizei *outDataSize, ALenum *outDataFormat, ALsizei* outSampleRate)
  104. {
  105. OSStatus status = noErr;
  106. BOOL abort = NO;
  107. SInt64 theFileLengthInFrames = 0;
  108. AudioStreamBasicDescription theFileFormat;
  109. UInt32 thePropertySize = sizeof(theFileFormat);
  110. ExtAudioFileRef extRef = NULL;
  111. void* theData = NULL;
  112. AudioStreamBasicDescription theOutputFormat;
  113. UInt32 dataSize = 0;
  114. // Open a file with ExtAudioFileOpen()
  115. status = ExtAudioFileOpenURL(inFileURL, &extRef);
  116. if (status != noErr)
  117. {
  118. CDLOG(@"MyGetOpenALAudioData: ExtAudioFileOpenURL FAILED, Error = %ld\n", status);
  119. abort = YES;
  120. }
  121. if (abort)
  122. goto Exit;
  123. // Get the audio data format
  124. status = ExtAudioFileGetProperty(extRef, kExtAudioFileProperty_FileDataFormat, &thePropertySize, &theFileFormat);
  125. if (status != noErr)
  126. {
  127. CDLOG(@"MyGetOpenALAudioData: ExtAudioFileGetProperty(kExtAudioFileProperty_FileDataFormat) FAILED, Error = %ld\n", status);
  128. abort = YES;
  129. }
  130. if (abort)
  131. goto Exit;
  132. if (theFileFormat.mChannelsPerFrame > 2)
  133. {
  134. CDLOG(@"MyGetOpenALAudioData - Unsupported Format, channel count is greater than stereo\n");
  135. abort = YES;
  136. }
  137. if (abort)
  138. goto Exit;
  139. // Set the client format to 16 bit signed integer (native-endian) data
  140. // Maintain the channel count and sample rate of the original source format
  141. theOutputFormat.mSampleRate = theFileFormat.mSampleRate;
  142. theOutputFormat.mChannelsPerFrame = theFileFormat.mChannelsPerFrame;
  143. theOutputFormat.mFormatID = kAudioFormatLinearPCM;
  144. theOutputFormat.mBytesPerPacket = 2 * theOutputFormat.mChannelsPerFrame;
  145. theOutputFormat.mFramesPerPacket = 1;
  146. theOutputFormat.mBytesPerFrame = 2 * theOutputFormat.mChannelsPerFrame;
  147. theOutputFormat.mBitsPerChannel = 16;
  148. theOutputFormat.mFormatFlags = kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsPacked | kAudioFormatFlagIsSignedInteger;
  149. // Set the desired client (output) data format
  150. status = ExtAudioFileSetProperty(extRef, kExtAudioFileProperty_ClientDataFormat, sizeof(theOutputFormat), &theOutputFormat);
  151. if (status != noErr)
  152. {
  153. CDLOG(@"MyGetOpenALAudioData: ExtAudioFileSetProperty(kExtAudioFileProperty_ClientDataFormat) FAILED, Error = %ld\n", status);
  154. abort = YES;
  155. }
  156. if (abort)
  157. goto Exit;
  158. // Get the total frame count
  159. thePropertySize = sizeof(theFileLengthInFrames);
  160. status = ExtAudioFileGetProperty(extRef, kExtAudioFileProperty_FileLengthFrames, &thePropertySize, &theFileLengthInFrames);
  161. if (status != noErr)
  162. {
  163. CDLOG(@"MyGetOpenALAudioData: ExtAudioFileGetProperty(kExtAudioFileProperty_FileLengthFrames) FAILED, Error = %ld\n", status);
  164. abort = YES;
  165. }
  166. if (abort)
  167. goto Exit;
  168. // Read all the data into memory
  169. dataSize = (UInt32) theFileLengthInFrames * theOutputFormat.mBytesPerFrame;
  170. theData = malloc(dataSize);
  171. if (theData)
  172. {
  173. memset(theData, 0, dataSize);
  174. AudioBufferList theDataBuffer;
  175. theDataBuffer.mNumberBuffers = 1;
  176. theDataBuffer.mBuffers[0].mDataByteSize = dataSize;
  177. theDataBuffer.mBuffers[0].mNumberChannels = theOutputFormat.mChannelsPerFrame;
  178. theDataBuffer.mBuffers[0].mData = theData;
  179. // Read the data into an AudioBufferList
  180. status = ExtAudioFileRead(extRef, (UInt32*)&theFileLengthInFrames, &theDataBuffer);
  181. if(status == noErr)
  182. {
  183. // success
  184. *outDataSize = (ALsizei)dataSize;
  185. *outDataFormat = (theOutputFormat.mChannelsPerFrame > 1) ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16;
  186. *outSampleRate = (ALsizei)theOutputFormat.mSampleRate;
  187. }
  188. else
  189. {
  190. // failure
  191. free (theData);
  192. theData = NULL; // make sure to return NULL
  193. CDLOG(@"MyGetOpenALAudioData: ExtAudioFileRead FAILED, Error = %ld\n", status);
  194. abort = YES;
  195. }
  196. }
  197. if (abort)
  198. goto Exit;
  199. Exit:
  200. // Dispose the ExtAudioFileRef, it is no longer needed
  201. if (extRef) ExtAudioFileDispose(extRef);
  202. return theData;
  203. }
  204. void* CDGetOpenALAudioData(CFURLRef inFileURL, ALsizei *outDataSize, ALenum *outDataFormat, ALsizei* outSampleRate) {
  205. CFStringRef extension = CFURLCopyPathExtension(inFileURL);
  206. CFComparisonResult isWavFile = 0;
  207. if (extension != NULL) {
  208. isWavFile = CFStringCompare (extension,(CFStringRef)@"wav", kCFCompareCaseInsensitive);
  209. CFRelease(extension);
  210. }
  211. if (isWavFile == kCFCompareEqualTo) {
  212. return CDloadWaveAudioData(inFileURL, outDataSize, outDataFormat, outSampleRate);
  213. } else {
  214. return CDloadCafAudioData(inFileURL, outDataSize, outDataFormat, outSampleRate);
  215. }
  216. }