Strip trailing whitespace and fix other whitespace errors (e.g. space/tab mixing...
[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         : Source (s, node)
42         , AudioFileSource (s, node)
43 {
44         init ();
45 }
46
47 CoreAudioSource::CoreAudioSource (Session& s, const string& path, bool, int chn, Flag flags)
48         /* files created this way are never writable or removable */
49         : Source (s, DataType::AUDIO, path, Source::Flag (flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy))),
50                 AudioFileSource (s, path,
51                         Source::Flag (flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy)))
52 {
53         _channel = chn;
54         init ();
55 }
56
57 void
58 CoreAudioSource::init ()
59 {
60         /* note that we temporarily truncated _id at the colon */
61         try {
62                 af.Open(_path.c_str());
63
64                 CAStreamBasicDescription file_format (af.GetFileDataFormat());
65                 n_channels = file_format.NumberChannels();
66
67                 if (_channel >= n_channels) {
68                         error << string_compose("CoreAudioSource: file only contains %1 channels; %2 is invalid as a channel number (%3)", n_channels, _channel, name()) << endmsg;
69                         throw failed_constructor();
70                 }
71
72                 _length = af.GetNumberFrames();
73
74                 CAStreamBasicDescription client_format (file_format);
75
76                 /* set canonial form (PCM, native float packed, 32 bit, with the correct number of channels
77                    and interleaved (since we plan to deinterleave ourselves)
78                 */
79
80                 client_format.SetCanonical(client_format.NumberChannels(), true);
81                 af.SetClientFormat (client_format);
82
83         } catch (CAXException& cax) {
84
85                 error << string_compose(_("CoreAudioSource: cannot open file \"%1\" for %2"),
86                                         _path, (writable() ? "read+write" : "reading")) << endmsg;
87                 throw failed_constructor ();
88         }
89 }
90
91 CoreAudioSource::~CoreAudioSource ()
92 {
93         GoingAway (); /* EMIT SIGNAL */
94 }
95
96 int
97 CoreAudioSource::safe_read (Sample* dst, nframes_t start, nframes_t cnt, AudioBufferList& abl) const
98 {
99         nframes_t nread = 0;
100
101         while (nread < cnt) {
102
103                 try {
104                         af.Seek (start+nread);
105                 } catch (CAXException& cax) {
106                         error << string_compose("CoreAudioSource: %1 to %2 (%3)", cax.mOperation, start+nread, _name.substr (1)) << endmsg;
107                         return -1;
108                 }
109
110                 UInt32 new_cnt = cnt - nread;
111
112                 abl.mBuffers[0].mDataByteSize = new_cnt * n_channels * sizeof(Sample);
113                 abl.mBuffers[0].mData = dst + nread;
114
115                 try {
116                         af.Read (new_cnt, &abl);
117                 } catch (CAXException& cax) {
118                         error << string_compose("CoreAudioSource: %1 (%2)", cax.mOperation, _name);
119                         return -1;
120                 }
121
122                 if (new_cnt == 0) {
123                         /* EOF */
124                         if (start+cnt == _length) {
125                                 /* we really did hit the end */
126                                 nread = cnt;
127                         }
128                         break;
129                 }
130
131                 nread += new_cnt;
132         }
133
134         if (nread < cnt) {
135                 return -1;
136         } else {
137                 return 0;
138         }
139 }
140
141
142 nframes_t
143 CoreAudioSource::read_unlocked (Sample *dst, sframes_t start, nframes_t cnt) const
144 {
145         nframes_t file_cnt;
146         AudioBufferList abl;
147
148         abl.mNumberBuffers = 1;
149         abl.mBuffers[0].mNumberChannels = n_channels;
150
151         if (start > _length) {
152
153                 /* read starts beyond end of data, just memset to zero */
154
155                 file_cnt = 0;
156
157         } else if (start + cnt > _length) {
158
159                 /* read ends beyond end of data, read some, memset the rest */
160
161                 file_cnt = _length - start;
162
163         } else {
164
165                 /* read is entirely within data */
166
167                 file_cnt = cnt;
168         }
169
170         if (file_cnt != cnt) {
171                 nframes_t delta = cnt - file_cnt;
172                 memset (dst+file_cnt, 0, sizeof (Sample) * delta);
173         }
174
175         if (file_cnt) {
176
177                 if (n_channels == 1) {
178                         if (safe_read (dst, start, file_cnt, abl) == 0) {
179                                 _read_data_count = cnt * sizeof (Sample);
180                                 return cnt;
181                         }
182                         return 0;
183                 }
184         }
185
186         Sample* interleave_buf = get_interleave_buffer (file_cnt * n_channels);
187
188         if (safe_read (interleave_buf, start, file_cnt, abl) != 0) {
189                 return 0;
190         }
191
192         _read_data_count = cnt * sizeof(float);
193
194         Sample *ptr = interleave_buf + _channel;
195
196         /* stride through the interleaved data */
197
198         for (uint32_t n = 0; n < file_cnt; ++n) {
199                 dst[n] = *ptr;
200                 ptr += n_channels;
201         }
202
203         return cnt;
204 }
205
206 float
207 CoreAudioSource::sample_rate() const
208 {
209         CAStreamBasicDescription client_asbd;
210
211         try {
212                 client_asbd = af.GetClientDataFormat ();
213         } catch (CAXException& cax) {
214                 error << string_compose("CoreAudioSource: %1 (%2)", cax.mOperation, _name);
215                 return 0.0;
216         }
217
218         return client_asbd.mSampleRate;
219 }
220
221 int
222 CoreAudioSource::update_header (sframes_t when, struct tm&, time_t)
223 {
224         return 0;
225 }
226
227 int
228 CoreAudioSource::get_soundfile_info (string path, SoundFileInfo& _info, string& error_msg)
229 {
230         FSRef ref;
231         ExtAudioFileRef af = 0;
232         size_t size;
233         CFStringRef name;
234         int ret = -1;
235
236         if (FSPathMakeRef ((UInt8*)path.c_str(), &ref, 0) != noErr) {
237                 goto out;
238         }
239
240         if (ExtAudioFileOpen(&ref, &af) != noErr) {
241                 goto out;
242         }
243
244         AudioStreamBasicDescription absd;
245         memset(&absd, 0, sizeof(absd));
246         size = sizeof(AudioStreamBasicDescription);
247         if (ExtAudioFileGetProperty (af, kExtAudioFileProperty_FileDataFormat, &size, &absd) != noErr) {
248                 goto out;
249         }
250
251         _info.samplerate = absd.mSampleRate;
252         _info.channels   = absd.mChannelsPerFrame;
253
254         size = sizeof(_info.length);
255         if (ExtAudioFileGetProperty(af, kExtAudioFileProperty_FileLengthFrames, &size, &_info.length) != noErr) {
256                 goto out;
257         }
258
259         size = sizeof(CFStringRef);
260         if (AudioFormatGetProperty(kAudioFormatProperty_FormatName, sizeof(absd), &absd, &size, &name) != noErr) {
261                 goto out;
262         }
263
264         _info.format_name = "";
265
266         if (absd.mFormatID == kAudioFormatLinearPCM) {
267                 if (absd.mFormatFlags & kAudioFormatFlagIsBigEndian) {
268                         _info.format_name += "big-endian";
269                 } else {
270                         _info.format_name += "little-endian";
271                 }
272
273                 char buf[32];
274                 snprintf (buf, sizeof (buf), " %" PRIu32 " bit", absd.mBitsPerChannel);
275                 _info.format_name += buf;
276                 _info.format_name += '\n';
277
278                 if (absd.mFormatFlags & kAudioFormatFlagIsFloat) {
279                         _info.format_name += "float";
280                 } else {
281                         if (absd.mFormatFlags & kAudioFormatFlagIsSignedInteger) {
282                                 _info.format_name += "signed";
283                         } else {
284                                 _info.format_name += "unsigned";
285                         }
286                         /* integer is typical, do not show it */
287                 }
288
289                 if (_info.channels > 1) {
290                         if (absd.mFormatFlags & kAudioFormatFlagIsNonInterleaved) {
291                                 _info.format_name += " noninterleaved";
292                         }
293                         /* interleaved is the normal case, do not show it */
294                 }
295
296                 _info.format_name += ' ';
297         }
298
299         switch (absd.mFormatID) {
300         case kAudioFormatLinearPCM:
301                 _info.format_name += "PCM";
302                 break;
303
304         case kAudioFormatAC3:
305                 _info.format_name += "AC3";
306                 break;
307
308         case kAudioFormat60958AC3:
309                 _info.format_name += "60958 AC3";
310                 break;
311
312         case kAudioFormatMPEGLayer1:
313                 _info.format_name += "MPEG-1";
314                 break;
315
316         case kAudioFormatMPEGLayer2:
317                 _info.format_name += "MPEG-2";
318                 break;
319
320         case kAudioFormatMPEGLayer3:
321                 _info.format_name += "MPEG-3";
322                 break;
323
324         case kAudioFormatAppleIMA4:
325                 _info.format_name += "IMA-4";
326                 break;
327
328         case kAudioFormatMPEG4AAC:
329                 _info.format_name += "AAC";
330                 break;
331
332         case kAudioFormatMPEG4CELP:
333                 _info.format_name += "CELP";
334                 break;
335
336         case kAudioFormatMPEG4HVXC:
337                 _info.format_name += "HVXC";
338                 break;
339
340         case kAudioFormatMPEG4TwinVQ:
341                 _info.format_name += "TwinVQ";
342                 break;
343
344         /* these really shouldn't show up, but we should do something
345            somewhere else to make sure that doesn't happen. until
346            that is guaranteed, print something anyway.
347         */
348
349         case kAudioFormatTimeCode:
350                 _info.format_name += "timecode";
351                 break;
352
353         case kAudioFormatMIDIStream:
354                 _info.format_name += "MIDI";
355                 break;
356
357         case kAudioFormatParameterValueStream:
358                 _info.format_name += "parameter values";
359                 break;
360         }
361
362         // XXX it would be nice to find a way to get this information if it exists
363
364         _info.timecode = 0;
365         ret = 0;
366
367   out:
368         ExtAudioFileDispose (af);
369         return ret;
370
371 }