1bd7a837efe3516a96e176b132f25d2ff37825e5
[aubio.git] / src / io / utils_apple_audio.c
1 #ifdef __APPLE__
2
3 // CFURLRef, CFURLCreateWithFileSystemPath, ...
4 #include <CoreFoundation/CoreFoundation.h>
5 // ExtAudioFileRef, AudioStreamBasicDescription, AudioBufferList, ...
6 #include <AudioToolbox/AudioToolbox.h>
7 #include "aubio_priv.h"
8
9 int createAubioBufferList(AudioBufferList *bufferList, int channels, int segmentSize);
10 void freeAudioBufferList(AudioBufferList *bufferList);
11 CFURLRef getURLFromPath(const char * path);
12 char_t *getPrintableOSStatusError(char_t *str, OSStatus error);
13
14 int createAubioBufferList(AudioBufferList * bufferList, int channels, int max_source_samples) {
15   bufferList->mNumberBuffers = 1;
16   bufferList->mBuffers[0].mNumberChannels = channels;
17   bufferList->mBuffers[0].mData = AUBIO_ARRAY(short, max_source_samples);
18   bufferList->mBuffers[0].mDataByteSize = max_source_samples * sizeof(short);
19   return 0;
20 }
21
22 void freeAudioBufferList(AudioBufferList *bufferList) {
23   UInt32 i = 0;
24   if (!bufferList) return;
25   for (i = 0; i < bufferList->mNumberBuffers; i++) {
26     if (bufferList->mBuffers[i].mData) {
27       AUBIO_FREE(bufferList->mBuffers[i].mData);
28       bufferList->mBuffers[i].mData = NULL;
29     }
30   }
31   bufferList = NULL;
32 }
33
34 CFURLRef getURLFromPath(const char * path) {
35   CFStringRef cfTotalPath = CFStringCreateWithCString (kCFAllocatorDefault,
36       path, kCFStringEncodingUTF8);
37
38   return CFURLCreateWithFileSystemPath(kCFAllocatorDefault, cfTotalPath,
39       kCFURLPOSIXPathStyle, false);
40 }
41
42 char_t *getPrintableOSStatusError(char_t *str, OSStatus error)
43 {
44     // see if it appears to be a 4-char-code
45     *(UInt32 *)(str + 1) = CFSwapInt32HostToBig(error);
46     if (isprint(str[1]) && isprint(str[2]) && isprint(str[3]) && isprint(str[4])) {
47         str[0] = str[5] = '\'';
48         str[6] = '\0';
49     } else
50         // no, format it as an integer
51         sprintf(str, "%d", (int)error);
52     return str;
53 }
54
55 #endif /* __APPLE__ */