Merge branch 'develop' into io
[aubio.git] / src / io / source_apple_audio.c
1 /*
2   Copyright (C) 2012 Paul Brossier <piem@aubio.org>
3
4   This file is part of aubio.
5
6   aubio 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 3 of the License, or
9   (at your option) any later version.
10
11   aubio 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 aubio.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #ifdef __APPLE__
22 #include "config.h"
23 #include "aubio_priv.h"
24 #include "fvec.h"
25 #include "io/source_apple_audio.h"
26
27 // ExtAudioFileRef, AudioStreamBasicDescription, AudioBufferList, ...
28 #include <AudioToolbox/AudioToolbox.h>
29
30 #define RT_BYTE1( a )      ( (a) & 0xff )
31 #define RT_BYTE2( a )      ( ((a) >> 8) & 0xff )
32 #define RT_BYTE3( a )      ( ((a) >> 16) & 0xff )
33 #define RT_BYTE4( a )      ( ((a) >> 24) & 0xff )
34
35 #define SHORT_TO_FLOAT(x) (smpl_t)(x * 3.0517578125e-05)
36
37 struct _aubio_source_apple_audio_t {
38   uint_t channels;
39   uint_t samplerate;
40   uint_t block_size;
41
42   char_t *path;
43
44   ExtAudioFileRef audioFile;
45   AudioBufferList bufferList;
46 };
47
48 extern int createAubioBufferList(AudioBufferList *bufferList, int channels, int segmentSize);
49 extern void freeAudioBufferList(AudioBufferList *bufferList);
50 extern CFURLRef getURLFromPath(const char * path);
51
52 aubio_source_apple_audio_t * new_aubio_source_apple_audio(char_t * path, uint_t samplerate, uint_t block_size)
53 {
54   aubio_source_apple_audio_t * s = AUBIO_NEW(aubio_source_apple_audio_t);
55
56   s->path = path;
57   s->samplerate = samplerate;
58   s->block_size = block_size;
59   s->channels = 1;
60
61   OSStatus err = noErr;
62   UInt32 propSize;
63
64   AudioStreamBasicDescription clientFormat;
65   propSize = sizeof(clientFormat);
66   memset(&clientFormat, 0, sizeof(AudioStreamBasicDescription));
67   clientFormat.mFormatID         = kAudioFormatLinearPCM;
68   clientFormat.mSampleRate       = (Float64)(s->samplerate);
69   clientFormat.mFormatFlags      = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
70   clientFormat.mChannelsPerFrame = s->channels;
71   clientFormat.mBitsPerChannel   = sizeof(short) * 8;
72   clientFormat.mFramesPerPacket  = 1;
73   clientFormat.mBytesPerFrame    = clientFormat.mBitsPerChannel * clientFormat.mChannelsPerFrame / 8;
74   clientFormat.mBytesPerPacket   = clientFormat.mFramesPerPacket * clientFormat.mBytesPerFrame;
75   clientFormat.mReserved         = 0;
76
77   // open the resource url
78   CFURLRef fileURL = getURLFromPath(path);
79   err = ExtAudioFileOpenURL(fileURL, &s->audioFile);
80   if (err) { AUBIO_ERR("error when trying to access %s, in ExtAudioFileOpenURL, %d\n", s->path, (int)err); goto beach;}
81
82   // create an empty AudioStreamBasicDescription
83   AudioStreamBasicDescription fileFormat;
84   propSize = sizeof(fileFormat);
85   memset(&fileFormat, 0, sizeof(AudioStreamBasicDescription));
86
87   // fill it with the file description
88   err = ExtAudioFileGetProperty(s->audioFile,
89       kExtAudioFileProperty_FileDataFormat, &propSize, &fileFormat);
90   if (err) { AUBIO_ERROR("error in ExtAudioFileGetProperty, %d\n", (int)err); goto beach;}
91
92   // set the client format description
93   err = ExtAudioFileSetProperty(s->audioFile, kExtAudioFileProperty_ClientDataFormat,
94       propSize, &clientFormat);
95   if (err) { AUBIO_ERROR("error in ExtAudioFileSetProperty, %d\n", (int)err); goto beach;}
96
97 #if 0
98   // print client and format descriptions
99   AUBIO_DBG("Opened %s\n", s->path);
100   AUBIO_DBG("file/client Format.mFormatID:        : %3c%c%c%c / %c%c%c%c\n",
101       (int)RT_BYTE4(fileFormat.mFormatID),   (int)RT_BYTE3(fileFormat.mFormatID),   (int)RT_BYTE2(fileFormat.mFormatID),   (int)RT_BYTE1(fileFormat.mFormatID),
102       (int)RT_BYTE4(clientFormat.mFormatID), (int)RT_BYTE3(clientFormat.mFormatID), (int)RT_BYTE2(clientFormat.mFormatID), (int)RT_BYTE1(clientFormat.mFormatID)
103       );
104
105   AUBIO_DBG("file/client Format.mSampleRate       : %6.0f / %.0f\n",     fileFormat.mSampleRate      ,      clientFormat.mSampleRate);
106   AUBIO_DBG("file/client Format.mFormatFlags      : %6d / %d\n",    (int)fileFormat.mFormatFlags     , (int)clientFormat.mFormatFlags);
107   AUBIO_DBG("file/client Format.mChannelsPerFrame : %6d / %d\n",    (int)fileFormat.mChannelsPerFrame, (int)clientFormat.mChannelsPerFrame);
108   AUBIO_DBG("file/client Format.mBitsPerChannel   : %6d / %d\n",    (int)fileFormat.mBitsPerChannel  , (int)clientFormat.mBitsPerChannel);
109   AUBIO_DBG("file/client Format.mFramesPerPacket  : %6d / %d\n",    (int)fileFormat.mFramesPerPacket , (int)clientFormat.mFramesPerPacket);
110   AUBIO_DBG("file/client Format.mBytesPerFrame    : %6d / %d\n",    (int)fileFormat.mBytesPerFrame   , (int)clientFormat.mBytesPerFrame);
111   AUBIO_DBG("file/client Format.mBytesPerPacket   : %6d / %d\n",    (int)fileFormat.mBytesPerPacket  , (int)clientFormat.mBytesPerPacket);
112   AUBIO_DBG("file/client Format.mReserved         : %6d / %d\n",    (int)fileFormat.mReserved        , (int)clientFormat.mReserved);
113 #endif
114
115   // compute the size of the segments needed to read the input file
116   UInt32 samples = s->block_size * clientFormat.mChannelsPerFrame;
117   Float64 rateRatio = clientFormat.mSampleRate / fileFormat.mSampleRate;
118   uint_t segmentSize= (uint_t)(samples * rateRatio + .5);
119   if (rateRatio < 1.) {
120     segmentSize = (uint_t)(samples / rateRatio + .5);
121   } else if (rateRatio > 1.) {
122     AUBIO_WRN("up-sampling %s from %0.2fHz to %0.2fHz\n", s->path, fileFormat.mSampleRate, clientFormat.mSampleRate);
123   } else {
124     assert (segmentSize == samples );
125     //AUBIO_DBG("not resampling, segmentSize %d, block_size %d\n", segmentSize, s->block_size);
126   }
127
128   // allocate the AudioBufferList
129   if (createAubioBufferList(&s->bufferList, s->channels, segmentSize)) err = -1;
130
131   return s;
132  
133 beach:
134   AUBIO_FREE(s);
135   return NULL;
136 }
137
138 void aubio_source_apple_audio_do(aubio_source_apple_audio_t *s, fvec_t * read_to, uint_t * read) {
139   UInt32 c, v, loadedPackets = s->block_size;
140   OSStatus err = ExtAudioFileRead(s->audioFile, &loadedPackets, &s->bufferList);
141   if (err) { AUBIO_ERROR("error in ExtAudioFileRead, %d\n", (int)err); goto beach;}
142
143   smpl_t *buf = read_to->data;
144
145   short *data = (short*)s->bufferList.mBuffers[0].mData;
146
147   if (buf) {
148       for (c = 0; c < s->channels; c++) {
149           for (v = 0; v < s->block_size; v++) {
150               if (v < loadedPackets) {
151                   buf[v * s->channels + c] =
152                       SHORT_TO_FLOAT(data[ v * s->channels + c]);
153               } else {
154                   buf[v * s->channels + c] = 0.f;
155               }
156           }
157       }
158   }
159   //if (loadedPackets < s->block_size) return EOF;
160   *read = (uint_t)loadedPackets;
161   return;
162 beach:
163   *read = 0;
164   return;
165 }
166
167 void del_aubio_source_apple_audio(aubio_source_apple_audio_t * s){
168   OSStatus err = noErr;
169   if (!s || !s->audioFile) { return; }
170   err = ExtAudioFileDispose(s->audioFile);
171   if (err) AUBIO_ERROR("error in ExtAudioFileDispose, %d\n", (int)err);
172   s->audioFile = NULL;
173   freeAudioBufferList(&s->bufferList);
174   AUBIO_FREE(s);
175   return;
176 }
177
178 #endif /* __APPLE__ */