An audio player for macOS 10.8 and newer. https://kode54.net/cog
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

319 lines
8.1KB

  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2006 Stephen F. Booth <me@sbooth.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <unistd.h>
  21. #import "CoreAudioDecoder.h"
  22. #import "Logging.h"
  23. #define REWIND_SIZE 131072
  24. @interface CoreAudioDecoder (Private)
  25. - (BOOL) readInfoFromExtAudioFileRef;
  26. @end
  27. static OSStatus readProc(void* clientData,
  28. SInt64 position,
  29. UInt32 requestCount,
  30. void* buffer,
  31. UInt32* actualCount)
  32. {
  33. NSObject* _handle = (__bridge NSObject *)(clientData);
  34. CoreAudioDecoder * __unsafe_unretained pSelf = (id) _handle;
  35. id<CogSource> source = pSelf->_audioSource;
  36. if (position != pSelf->_lastPosition) {
  37. if ([source seekable])
  38. [source seek:position whence:SEEK_SET];
  39. else if (position < pSelf->rewindStart)
  40. return seekErr;
  41. }
  42. size_t copyMax = 0;
  43. size_t bytesRead = 0;
  44. size_t rewindBytes = 0;
  45. if (![source seekable]) {
  46. long rewindStart = pSelf->rewindStart;
  47. rewindBytes = [pSelf->rewindBuffer length];
  48. if ( position < rewindStart + rewindBytes ) {
  49. const uint8_t * rewindBuffer = (const uint8_t *)([pSelf->rewindBuffer bytes]);
  50. copyMax = rewindStart + rewindBytes - position;
  51. if (copyMax > requestCount)
  52. copyMax = requestCount;
  53. memcpy(buffer, rewindBuffer + (position - rewindStart), copyMax);
  54. requestCount -= copyMax;
  55. position += copyMax;
  56. }
  57. }
  58. if ( requestCount )
  59. bytesRead = [source read:(((uint8_t *)buffer) + copyMax) amount:requestCount];
  60. if (![source seekable]) {
  61. size_t copyBytes = bytesRead;
  62. if (copyBytes) {
  63. ssize_t removeBytes = ((rewindBytes + copyBytes) - REWIND_SIZE);
  64. if (removeBytes > 0) {
  65. NSRange range = NSMakeRange(0, removeBytes);
  66. [pSelf->rewindBuffer replaceBytesInRange:range withBytes:NULL length:0];
  67. pSelf->rewindStart += removeBytes;
  68. }
  69. [pSelf->rewindBuffer appendBytes:buffer length:copyBytes];
  70. }
  71. }
  72. pSelf->_lastPosition = position + bytesRead;
  73. if(actualCount)
  74. *actualCount = (UInt32) (copyMax + bytesRead);
  75. return noErr;
  76. }
  77. static SInt64 getSizeProc(void* clientData) {
  78. NSObject* _handle = (__bridge NSObject *)(clientData);
  79. CoreAudioDecoder * __unsafe_unretained pSelf = (id) _handle;
  80. id<CogSource> source = pSelf->_audioSource;
  81. SInt64 size;
  82. if ([source seekable]) {
  83. [source seek:0 whence:SEEK_END];
  84. size = [source tell];
  85. [source seek:pSelf->_lastPosition whence:SEEK_SET];
  86. }
  87. else {
  88. size = INT64_MAX;
  89. }
  90. return size;
  91. }
  92. @implementation CoreAudioDecoder
  93. - (void) close
  94. {
  95. OSStatus err;
  96. err = ExtAudioFileDispose(_in);
  97. if(noErr != err) {
  98. DLog(@"Error closing ExtAudioFile");
  99. }
  100. err = AudioFileClose(_audioFile);
  101. if(noErr != err) {
  102. DLog(@"Error closing AudioFile");
  103. }
  104. _audioSource = nil;
  105. }
  106. - (void) dealloc
  107. {
  108. [self close];
  109. }
  110. - (BOOL)open:(id<CogSource>)source;
  111. {
  112. OSStatus err;
  113. _audioSource = source;
  114. _lastPosition = [source tell];
  115. rewindStart = _lastPosition;
  116. rewindBuffer = [[NSMutableData alloc] init];
  117. err = AudioFileOpenWithCallbacks((__bridge void *)self, readProc, 0, getSizeProc, 0, 0, &_audioFile);
  118. if(noErr != err) {
  119. ALog(@"Error opening callback interface to file: %d", err);
  120. return NO;
  121. }
  122. err = ExtAudioFileWrapAudioFileID(_audioFile, false, &_in);
  123. if(noErr != err) {
  124. ALog(@"Error opening file: %d", err);
  125. return NO;
  126. }
  127. return [self readInfoFromExtAudioFileRef];
  128. }
  129. - (BOOL) readInfoFromExtAudioFileRef
  130. {
  131. OSStatus err;
  132. UInt32 size;
  133. AudioStreamBasicDescription asbd;
  134. // Get input file information
  135. size = sizeof(asbd);
  136. err = ExtAudioFileGetProperty(_in, kExtAudioFileProperty_FileDataFormat, &size, &asbd);
  137. if(err != noErr) {
  138. err = ExtAudioFileDispose(_in);
  139. return NO;
  140. }
  141. SInt64 total;
  142. size = sizeof(total);
  143. err = ExtAudioFileGetProperty(_in, kExtAudioFileProperty_FileLengthFrames, &size, &total);
  144. if(err != noErr) {
  145. err = ExtAudioFileDispose(_in);
  146. return NO;
  147. }
  148. totalFrames = total;
  149. //Is there a way to get bitrate with extAudioFile?
  150. bitrate = 0;
  151. // Set our properties
  152. bitsPerSample = asbd.mBitsPerChannel;
  153. channels = asbd.mChannelsPerFrame;
  154. frequency = asbd.mSampleRate;
  155. floatingPoint = NO;
  156. // mBitsPerChannel will only be set for lpcm formats
  157. if(0 == bitsPerSample) {
  158. bitsPerSample = 32;
  159. floatingPoint = YES;
  160. }
  161. // Set output format
  162. AudioStreamBasicDescription result;
  163. bzero(&result, sizeof(AudioStreamBasicDescription));
  164. result.mFormatID = kAudioFormatLinearPCM;
  165. if (floatingPoint) {
  166. result.mFormatFlags = kAudioFormatFlagIsFloat | kAudioFormatFlagIsPacked;
  167. }
  168. else {
  169. result.mFormatFlags = kAudioFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsBigEndian;
  170. }
  171. result.mSampleRate = frequency;
  172. result.mChannelsPerFrame = channels;
  173. result.mBitsPerChannel = bitsPerSample;
  174. result.mBytesPerPacket = channels * (bitsPerSample / 8);
  175. result.mFramesPerPacket = 1;
  176. result.mBytesPerFrame = channels * (bitsPerSample / 8);
  177. err = ExtAudioFileSetProperty(_in, kExtAudioFileProperty_ClientDataFormat, sizeof(result), &result);
  178. if(noErr != err) {
  179. err = ExtAudioFileDispose(_in);
  180. return NO;
  181. }
  182. [self willChangeValueForKey:@"properties"];
  183. [self didChangeValueForKey:@"properties"];
  184. return YES;
  185. }
  186. - (int) readAudio:(void *)buf frames:(UInt32)frames
  187. {
  188. OSStatus err;
  189. AudioBufferList bufferList;
  190. UInt32 frameCount;
  191. // Set up the AudioBufferList
  192. bufferList.mNumberBuffers = 1;
  193. bufferList.mBuffers[0].mNumberChannels = channels;
  194. bufferList.mBuffers[0].mData = buf;
  195. bufferList.mBuffers[0].mDataByteSize = frames * channels * (bitsPerSample/8);
  196. // Read a chunk of PCM input (converted from whatever format)
  197. frameCount = frames;
  198. err = ExtAudioFileRead(_in, &frameCount, &bufferList);
  199. if(err != noErr) {
  200. return 0;
  201. }
  202. return frameCount;
  203. }
  204. - (long) seek:(long)frame
  205. {
  206. OSStatus err;
  207. err = ExtAudioFileSeek(_in, frame);
  208. if(noErr != err) {
  209. return -1;
  210. }
  211. return frame;
  212. }
  213. + (NSArray *)fileTypes
  214. {
  215. OSStatus err;
  216. UInt32 size;
  217. NSArray *sAudioExtensions;
  218. size = sizeof(sAudioExtensions);
  219. err = AudioFileGetGlobalInfo(kAudioFileGlobalInfo_AllExtensions, 0, NULL, &size, &sAudioExtensions);
  220. if(noErr != err) {
  221. return nil;
  222. }
  223. return sAudioExtensions;
  224. }
  225. + (NSArray *)mimeTypes
  226. {
  227. OSStatus err;
  228. UInt32 size;
  229. NSArray *sAudioMIMETypes;
  230. size = sizeof(sAudioMIMETypes);
  231. err = AudioFileGetGlobalInfo(kAudioFileGlobalInfo_AllMIMETypes, 0, NULL, &size, &sAudioMIMETypes);
  232. if(noErr != err) {
  233. return nil;
  234. }
  235. return sAudioMIMETypes;
  236. }
  237. + (float)priority
  238. {
  239. return 1.0;
  240. }
  241. - (NSDictionary *)properties
  242. {
  243. return [NSDictionary dictionaryWithObjectsAndKeys:
  244. [NSNumber numberWithInt:channels],@"channels",
  245. [NSNumber numberWithInt:bitsPerSample],@"bitsPerSample",
  246. [NSNumber numberWithBool:floatingPoint],@"floatingPoint",
  247. [NSNumber numberWithInt:bitrate],@"bitrate",
  248. [NSNumber numberWithFloat:frequency],@"sampleRate",
  249. [NSNumber numberWithLong:totalFrames],@"totalFrames",
  250. [NSNumber numberWithBool:[_audioSource seekable]], @"seekable",
  251. floatingPoint ? @"host" : @"big", @"endian",
  252. nil];
  253. }
  254. @end