2 Copyright (C) 2006 Paul Davis
3 Written by Taybin Rutkin
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.
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.
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.
24 #include "pbd/error.h"
25 #include "ardour/coreaudiosource.h"
26 #include "ardour/utils.h"
28 #include <appleutility/CAAudioFile.h>
29 #include <appleutility/CAStreamBasicDescription.h>
31 #include <glibmm/fileutils.h>
35 #include <AudioToolbox/AudioFormat.h>
38 using namespace ARDOUR;
41 /** Create a new CoreAudioSource using session state, which implies that the
42 * file must already exist.
44 CoreAudioSource::CoreAudioSource (Session& s, const XMLNode& node)
46 , AudioFileSource (s, node)
50 assert (Glib::file_test (_path, Glib::FILE_TEST_EXISTS));
54 /** Create a new CoreAudioSource from an existing file. Sources created with this
55 * method are never writable or removable.
57 CoreAudioSource::CoreAudioSource (Session& s, const string& path, int chn, Flag flags)
58 : Source (s, DataType::AUDIO, path, Source::Flag (flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy))),
59 AudioFileSource (s, path,
60 Source::Flag (flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy)))
65 assert (Glib::file_test (_path, Glib::FILE_TEST_EXISTS));
70 CoreAudioSource::init_cafile ()
72 /* note that we temporarily truncated _id at the colon */
74 af.Open(_path.c_str());
76 CAStreamBasicDescription file_format (af.GetFileDataFormat());
77 n_channels = file_format.NumberChannels();
79 if (_channel >= n_channels) {
80 error << string_compose("CoreAudioSource: file only contains %1 channels; %2 is invalid as a channel number (%3)", n_channels, _channel, name()) << endmsg;
81 throw failed_constructor();
84 _length = af.GetNumberFrames();
86 CAStreamBasicDescription client_format (file_format);
88 /* set canonial form (PCM, native float packed, 32 bit, with the correct number of channels
89 and interleaved (since we plan to deinterleave ourselves)
92 client_format.SetCanonical(client_format.NumberChannels(), true);
93 af.SetClientFormat (client_format);
95 } catch (CAXException& cax) {
97 error << string_compose(_("CoreAudioSource: cannot open file \"%1\" for %2"),
98 _path, (writable() ? "read+write" : "reading")) << endmsg;
99 throw failed_constructor ();
103 CoreAudioSource::~CoreAudioSource ()
108 CoreAudioSource::safe_read (Sample* dst, framepos_t start, framecnt_t cnt, AudioBufferList& abl) const
110 framecnt_t nread = 0;
112 while (nread < cnt) {
115 af.Seek (start+nread);
116 } catch (CAXException& cax) {
117 error << string_compose("CoreAudioSource: %1 to %2 (%3)", cax.mOperation, start+nread, _name.val().substr (1)) << endmsg;
121 UInt32 new_cnt = cnt - nread;
123 abl.mBuffers[0].mDataByteSize = new_cnt * n_channels * sizeof(Sample);
124 abl.mBuffers[0].mData = dst + nread;
127 af.Read (new_cnt, &abl);
128 } catch (CAXException& cax) {
129 error << string_compose("CoreAudioSource: %1 (%2)", cax.mOperation, _name);
135 if (start+cnt == _length) {
136 /* we really did hit the end */
154 CoreAudioSource::read_unlocked (Sample *dst, framepos_t start, framecnt_t cnt) const
159 abl.mNumberBuffers = 1;
160 abl.mBuffers[0].mNumberChannels = n_channels;
162 if (start > _length) {
164 /* read starts beyond end of data, just memset to zero */
168 } else if (start + cnt > _length) {
170 /* read ends beyond end of data, read some, memset the rest */
172 file_cnt = _length - start;
176 /* read is entirely within data */
181 if (file_cnt != cnt) {
182 frameoffset_t delta = cnt - file_cnt;
183 memset (dst+file_cnt, 0, sizeof (Sample) * delta);
188 if (n_channels == 1) {
189 if (safe_read (dst, start, file_cnt, abl) == 0) {
196 Sample* interleave_buf = get_interleave_buffer (file_cnt * n_channels);
198 if (safe_read (interleave_buf, start, file_cnt, abl) != 0) {
202 Sample *ptr = interleave_buf + _channel;
204 /* stride through the interleaved data */
206 for (framecnt_t n = 0; n < file_cnt; ++n) {
215 CoreAudioSource::sample_rate() const
217 CAStreamBasicDescription client_asbd;
220 client_asbd = af.GetClientDataFormat ();
221 } catch (CAXException& cax) {
222 error << string_compose("CoreAudioSource: %1 (%2)", cax.mOperation, _name);
226 return client_asbd.mSampleRate;
230 CoreAudioSource::update_header (framepos_t, struct tm&, time_t)
236 CoreAudioSource::get_soundfile_info (string path, SoundFileInfo& _info, string&)
239 ExtAudioFileRef af = 0;
244 if (FSPathMakeRef ((UInt8*)path.c_str(), &ref, 0) != noErr) {
248 if (ExtAudioFileOpen(&ref, &af) != noErr) {
252 AudioStreamBasicDescription absd;
253 memset(&absd, 0, sizeof(absd));
254 size = sizeof(AudioStreamBasicDescription);
255 if (ExtAudioFileGetProperty (af, kExtAudioFileProperty_FileDataFormat, &size, &absd) != noErr) {
259 _info.samplerate = absd.mSampleRate;
260 _info.channels = absd.mChannelsPerFrame;
262 size = sizeof(_info.length);
263 if (ExtAudioFileGetProperty(af, kExtAudioFileProperty_FileLengthFrames, &size, &_info.length) != noErr) {
267 size = sizeof(CFStringRef);
268 if (AudioFormatGetProperty(kAudioFormatProperty_FormatName, sizeof(absd), &absd, &size, &name) != noErr) {
272 _info.format_name = "";
274 if (absd.mFormatID == kAudioFormatLinearPCM) {
275 if (absd.mFormatFlags & kAudioFormatFlagIsBigEndian) {
276 _info.format_name += "big-endian";
278 _info.format_name += "little-endian";
282 snprintf (buf, sizeof (buf), " %" PRIu32 " bit", absd.mBitsPerChannel);
283 _info.format_name += buf;
284 _info.format_name += '\n';
286 if (absd.mFormatFlags & kAudioFormatFlagIsFloat) {
287 _info.format_name += "float";
289 if (absd.mFormatFlags & kAudioFormatFlagIsSignedInteger) {
290 _info.format_name += "signed";
292 _info.format_name += "unsigned";
294 /* integer is typical, do not show it */
297 if (_info.channels > 1) {
298 if (absd.mFormatFlags & kAudioFormatFlagIsNonInterleaved) {
299 _info.format_name += " noninterleaved";
301 /* interleaved is the normal case, do not show it */
304 _info.format_name += ' ';
307 switch (absd.mFormatID) {
308 case kAudioFormatLinearPCM:
309 _info.format_name += "PCM";
312 case kAudioFormatAC3:
313 _info.format_name += "AC3";
316 case kAudioFormat60958AC3:
317 _info.format_name += "60958 AC3";
320 case kAudioFormatMPEGLayer1:
321 _info.format_name += "MPEG-1";
324 case kAudioFormatMPEGLayer2:
325 _info.format_name += "MPEG-2";
328 case kAudioFormatMPEGLayer3:
329 _info.format_name += "MPEG-3";
332 case kAudioFormatAppleIMA4:
333 _info.format_name += "IMA-4";
336 case kAudioFormatMPEG4AAC:
337 _info.format_name += "AAC";
340 case kAudioFormatMPEG4CELP:
341 _info.format_name += "CELP";
344 case kAudioFormatMPEG4HVXC:
345 _info.format_name += "HVXC";
348 case kAudioFormatMPEG4TwinVQ:
349 _info.format_name += "TwinVQ";
352 /* these really shouldn't show up, but we should do something
353 somewhere else to make sure that doesn't happen. until
354 that is guaranteed, print something anyway.
357 case kAudioFormatTimeCode:
358 _info.format_name += "timecode";
361 case kAudioFormatMIDIStream:
362 _info.format_name += "MIDI";
365 case kAudioFormatParameterValueStream:
366 _info.format_name += "parameter values";
370 // XXX it would be nice to find a way to get this information if it exists
376 ExtAudioFileDispose (af);
382 CoreAudioSource::set_path (const string& p)
384 FileSource::set_path (p);