Remove unnecessary 0 checks before delete; see http://www.parashift.com/c++-faq-lite...
[ardour.git] / libs / ardour / coreaudiosource.cc
1 /*
2     Copyright (C) 2006 Paul Davis
3     Written by Taybin Rutkin
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #include <algorithm>
22 #define __STDC_FORMAT_MACROS
23 #include <inttypes.h>
24
25 #include <pbd/error.h>
26 #include <ardour/coreaudiosource.h>
27 #include <ardour/utils.h>
28
29 #include <appleutility/CAAudioFile.h>
30 #include <appleutility/CAStreamBasicDescription.h>
31
32 #include "i18n.h"
33
34 #include <AudioToolbox/AudioFormat.h>
35
36 using namespace std;
37 using namespace ARDOUR;
38 using namespace PBD;
39
40 CoreAudioSource::CoreAudioSource (Session& s, const XMLNode& node)
41         : AudioFileSource (s, node)
42 {
43         init ();
44 }
45
46 CoreAudioSource::CoreAudioSource (Session& s, const string& path, int chn, Flag flags)
47         /* files created this way are never writable or removable */
48         : AudioFileSource (s, path, Flag (flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy)))
49 {
50         _channel = chn;
51         init ();
52 }
53
54 void 
55 CoreAudioSource::init ()
56 {
57         /* note that we temporarily truncated _id at the colon */
58         try {
59                 af.Open(_path.c_str());
60
61                 CAStreamBasicDescription file_format (af.GetFileDataFormat());
62                 n_channels = file_format.NumberChannels();
63                 
64                 if (_channel >= n_channels) {
65                         error << string_compose("CoreAudioSource: file only contains %1 channels; %2 is invalid as a channel number (%3)", n_channels, _channel, name()) << endmsg;
66                         throw failed_constructor();
67                 }
68
69                 _length = af.GetNumberFrames();
70
71                 CAStreamBasicDescription client_format (file_format);
72
73                 /* set canonial form (PCM, native float packed, 32 bit, with the correct number of channels
74                    and interleaved (since we plan to deinterleave ourselves)
75                 */
76
77                 client_format.SetCanonical(client_format.NumberChannels(), true);
78                 af.SetClientFormat (client_format);
79
80         } catch (CAXException& cax) {
81                 
82                 error << string_compose(_("CoreAudioSource: cannot open file \"%1\" for %2"), 
83                                         _path, (writable() ? "read+write" : "reading")) << endmsg;
84                 throw failed_constructor ();
85         }
86 }
87
88 CoreAudioSource::~CoreAudioSource ()
89 {
90         GoingAway (); /* EMIT SIGNAL */
91 }
92
93 int
94 CoreAudioSource::safe_read (Sample* dst, nframes_t start, nframes_t cnt, AudioBufferList& abl) const
95 {
96         nframes_t nread = 0;
97
98         while (nread < cnt) {
99                 
100                 try {
101                         af.Seek (start+nread);
102                 } catch (CAXException& cax) {
103                         error << string_compose("CoreAudioSource: %1 to %2 (%3)", cax.mOperation, start+nread, _name.substr (1)) << endmsg;
104                         return -1;
105                 }
106                 
107                 UInt32 new_cnt = cnt - nread;
108                 
109                 abl.mBuffers[0].mDataByteSize = new_cnt * n_channels * sizeof(Sample);
110                 abl.mBuffers[0].mData = dst + nread;
111                         
112                 try {
113                         af.Read (new_cnt, &abl);
114                 } catch (CAXException& cax) {
115                         error << string_compose("CoreAudioSource: %1 (%2)", cax.mOperation, _name);
116                         return -1;
117                 }
118
119                 if (new_cnt == 0) {
120                         /* EOF */
121                         if (start+cnt == _length) {
122                                 /* we really did hit the end */
123                                 nread = cnt;
124                         }
125                         break;
126                 }
127
128                 nread += new_cnt;
129         }
130
131         if (nread < cnt) {
132                 return -1;
133         } else {
134                 return 0;
135         }
136 }
137         
138
139 nframes_t
140 CoreAudioSource::read_unlocked (Sample *dst, nframes_t start, nframes_t cnt) const
141 {
142         nframes_t file_cnt;
143         AudioBufferList abl;
144
145         abl.mNumberBuffers = 1;
146         abl.mBuffers[0].mNumberChannels = n_channels;
147
148         if (start > _length) {
149
150                 /* read starts beyond end of data, just memset to zero */
151                 
152                 file_cnt = 0;
153
154         } else if (start + cnt > _length) {
155                 
156                 /* read ends beyond end of data, read some, memset the rest */
157                 
158                 file_cnt = _length - start;
159
160         } else {
161                 
162                 /* read is entirely within data */
163
164                 file_cnt = cnt;
165         }
166
167         if (file_cnt != cnt) {
168                 nframes_t delta = cnt - file_cnt;
169                 memset (dst+file_cnt, 0, sizeof (Sample) * delta);
170         }
171
172         if (file_cnt) {
173
174                 if (n_channels == 1) {
175                         if (safe_read (dst, start, file_cnt, abl) == 0) {
176                                 _read_data_count = cnt * sizeof (Sample);
177                                 return cnt;
178                         }
179                         return 0;
180                 }
181         }
182
183         Sample* interleave_buf = get_interleave_buffer (file_cnt * n_channels);
184         
185         if (safe_read (interleave_buf, start, file_cnt, abl) != 0) {
186                 return 0;
187         }
188
189         _read_data_count = cnt * sizeof(float);
190
191         Sample *ptr = interleave_buf + _channel;
192         
193         /* stride through the interleaved data */
194         
195         for (uint32_t n = 0; n < file_cnt; ++n) {
196                 dst[n] = *ptr;
197                 ptr += n_channels;
198         }
199
200         return cnt;
201 }
202
203 float
204 CoreAudioSource::sample_rate() const
205 {
206         CAStreamBasicDescription client_asbd;
207
208         try {
209                 client_asbd = af.GetClientDataFormat ();
210         } catch (CAXException& cax) {
211                 error << string_compose("CoreAudioSource: %1 (%2)", cax.mOperation, _name);
212                 return 0.0;
213         }
214
215         return client_asbd.mSampleRate;
216 }
217
218 int
219 CoreAudioSource::update_header (nframes_t when, struct tm&, time_t)
220 {
221         return 0;
222 }
223
224 int
225 CoreAudioSource::get_soundfile_info (string path, SoundFileInfo& _info, string& error_msg)
226 {
227         FSRef ref; 
228         ExtAudioFileRef af = 0;
229         size_t size;
230         CFStringRef name;
231         int ret = -1;
232
233         if (FSPathMakeRef ((UInt8*)path.c_str(), &ref, 0) != noErr) {
234                 goto out;
235         }
236         
237         if (ExtAudioFileOpen(&ref, &af) != noErr) {
238                 goto out;
239         }
240         
241         AudioStreamBasicDescription absd;
242         memset(&absd, 0, sizeof(absd));
243         size = sizeof(AudioStreamBasicDescription);
244         if (ExtAudioFileGetProperty (af, kExtAudioFileProperty_FileDataFormat, &size, &absd) != noErr) {
245                 goto out;
246         }
247         
248         _info.samplerate = absd.mSampleRate;
249         _info.channels   = absd.mChannelsPerFrame;
250
251         size = sizeof(_info.length);
252         if (ExtAudioFileGetProperty(af, kExtAudioFileProperty_FileLengthFrames, &size, &_info.length) != noErr) {
253                 goto out;
254         }
255         
256         size = sizeof(CFStringRef);
257         if (AudioFormatGetProperty(kAudioFormatProperty_FormatName, sizeof(absd), &absd, &size, &name) != noErr) {
258                 goto out;
259         }
260
261         _info.format_name = "";
262
263         if (absd.mFormatID == kAudioFormatLinearPCM) {
264                 if (absd.mFormatFlags & kAudioFormatFlagIsBigEndian) {
265                         _info.format_name += "big-endian";
266                 } else {
267                         _info.format_name += "little-endian";
268                 }
269                 
270                 char buf[32];
271                 snprintf (buf, sizeof (buf), " %" PRIu32 " bit", absd.mBitsPerChannel);
272                 _info.format_name += buf;
273                 _info.format_name += '\n';
274                 
275                 if (absd.mFormatFlags & kAudioFormatFlagIsFloat) {
276                         _info.format_name += "float";
277                 } else {
278                         if (absd.mFormatFlags & kAudioFormatFlagIsSignedInteger) {
279                                 _info.format_name += "signed";
280                         } else {
281                                 _info.format_name += "unsigned";
282                         }
283                         /* integer is typical, do not show it */
284                 }
285                 
286                 if (_info.channels > 1) {
287                         if (absd.mFormatFlags & kAudioFormatFlagIsNonInterleaved) {
288                                 _info.format_name += " noninterleaved";
289                         }
290                         /* interleaved is the normal case, do not show it */
291                 }
292                 
293                 _info.format_name += ' ';
294         }
295
296         switch (absd.mFormatID) {
297         case kAudioFormatLinearPCM:
298                 _info.format_name += "PCM";
299                 break;
300
301         case kAudioFormatAC3:
302                 _info.format_name += "AC3";
303                 break;
304
305         case kAudioFormat60958AC3:
306                 _info.format_name += "60958 AC3";
307                 break;
308
309         case kAudioFormatMPEGLayer1:
310                 _info.format_name += "MPEG-1";
311                 break;
312
313         case kAudioFormatMPEGLayer2:
314                 _info.format_name += "MPEG-2";
315                 break;
316
317         case kAudioFormatMPEGLayer3:
318                 _info.format_name += "MPEG-3";
319                 break;
320
321         case kAudioFormatAppleIMA4:
322                 _info.format_name += "IMA-4";
323                 break;
324
325         case kAudioFormatMPEG4AAC:
326                 _info.format_name += "AAC";
327                 break;
328
329         case kAudioFormatMPEG4CELP:
330                 _info.format_name += "CELP";
331                 break;
332
333         case kAudioFormatMPEG4HVXC:
334                 _info.format_name += "HVXC";
335                 break;
336
337         case kAudioFormatMPEG4TwinVQ:
338                 _info.format_name += "TwinVQ";
339                 break;
340
341         /* these really shouldn't show up, but we should do something
342            somewhere else to make sure that doesn't happen. until
343            that is guaranteed, print something anyway.
344         */
345
346         case kAudioFormatTimeCode:
347                 _info.format_name += "timecode";
348                 break;
349
350         case kAudioFormatMIDIStream:
351                 _info.format_name += "MIDI";
352                 break;
353
354         case kAudioFormatParameterValueStream:
355                 _info.format_name += "parameter values";
356                 break;
357         }
358
359         // XXX it would be nice to find a way to get this information if it exists
360
361         _info.timecode = 0;
362         ret = 0;
363         
364   out:
365         ExtAudioFileDispose (af);
366         return ret;
367         
368 }