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.
22 #define __STDC_FORMAT_MACROS
25 #include "pbd/error.h"
26 #include "ardour/coreaudiosource.h"
27 #include "ardour/utils.h"
29 #include <appleutility/CAAudioFile.h>
30 #include <appleutility/CAStreamBasicDescription.h>
34 #include <AudioToolbox/AudioFormat.h>
37 using namespace ARDOUR;
40 CoreAudioSource::CoreAudioSource (Session& s, const XMLNode& node)
42 , AudioFileSource (s, node)
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)))
58 CoreAudioSource::init ()
60 /* note that we temporarily truncated _id at the colon */
62 af.Open(_path.c_str());
64 CAStreamBasicDescription file_format (af.GetFileDataFormat());
65 n_channels = file_format.NumberChannels();
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();
72 _length = af.GetNumberFrames();
74 CAStreamBasicDescription client_format (file_format);
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)
80 client_format.SetCanonical(client_format.NumberChannels(), true);
81 af.SetClientFormat (client_format);
83 } catch (CAXException& cax) {
85 error << string_compose(_("CoreAudioSource: cannot open file \"%1\" for %2"),
86 _path, (writable() ? "read+write" : "reading")) << endmsg;
87 throw failed_constructor ();
91 CoreAudioSource::~CoreAudioSource ()
93 GoingAway (); /* EMIT SIGNAL */
97 CoreAudioSource::safe_read (Sample* dst, nframes_t start, nframes_t cnt, AudioBufferList& abl) const
101 while (nread < cnt) {
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;
110 UInt32 new_cnt = cnt - nread;
112 abl.mBuffers[0].mDataByteSize = new_cnt * n_channels * sizeof(Sample);
113 abl.mBuffers[0].mData = dst + nread;
116 af.Read (new_cnt, &abl);
117 } catch (CAXException& cax) {
118 error << string_compose("CoreAudioSource: %1 (%2)", cax.mOperation, _name);
124 if (start+cnt == _length) {
125 /* we really did hit the end */
143 CoreAudioSource::read_unlocked (Sample *dst, sframes_t start, nframes_t cnt) const
148 abl.mNumberBuffers = 1;
149 abl.mBuffers[0].mNumberChannels = n_channels;
151 if (start > _length) {
153 /* read starts beyond end of data, just memset to zero */
157 } else if (start + cnt > _length) {
159 /* read ends beyond end of data, read some, memset the rest */
161 file_cnt = _length - start;
165 /* read is entirely within data */
170 if (file_cnt != cnt) {
171 nframes_t delta = cnt - file_cnt;
172 memset (dst+file_cnt, 0, sizeof (Sample) * delta);
177 if (n_channels == 1) {
178 if (safe_read (dst, start, file_cnt, abl) == 0) {
179 _read_data_count = cnt * sizeof (Sample);
186 Sample* interleave_buf = get_interleave_buffer (file_cnt * n_channels);
188 if (safe_read (interleave_buf, start, file_cnt, abl) != 0) {
192 _read_data_count = cnt * sizeof(float);
194 Sample *ptr = interleave_buf + _channel;
196 /* stride through the interleaved data */
198 for (uint32_t n = 0; n < file_cnt; ++n) {
207 CoreAudioSource::sample_rate() const
209 CAStreamBasicDescription client_asbd;
212 client_asbd = af.GetClientDataFormat ();
213 } catch (CAXException& cax) {
214 error << string_compose("CoreAudioSource: %1 (%2)", cax.mOperation, _name);
218 return client_asbd.mSampleRate;
222 CoreAudioSource::update_header (sframes_t when, struct tm&, time_t)
228 CoreAudioSource::get_soundfile_info (string path, SoundFileInfo& _info, string& error_msg)
231 ExtAudioFileRef af = 0;
236 if (FSPathMakeRef ((UInt8*)path.c_str(), &ref, 0) != noErr) {
240 if (ExtAudioFileOpen(&ref, &af) != noErr) {
244 AudioStreamBasicDescription absd;
245 memset(&absd, 0, sizeof(absd));
246 size = sizeof(AudioStreamBasicDescription);
247 if (ExtAudioFileGetProperty (af, kExtAudioFileProperty_FileDataFormat, &size, &absd) != noErr) {
251 _info.samplerate = absd.mSampleRate;
252 _info.channels = absd.mChannelsPerFrame;
254 size = sizeof(_info.length);
255 if (ExtAudioFileGetProperty(af, kExtAudioFileProperty_FileLengthFrames, &size, &_info.length) != noErr) {
259 size = sizeof(CFStringRef);
260 if (AudioFormatGetProperty(kAudioFormatProperty_FormatName, sizeof(absd), &absd, &size, &name) != noErr) {
264 _info.format_name = "";
266 if (absd.mFormatID == kAudioFormatLinearPCM) {
267 if (absd.mFormatFlags & kAudioFormatFlagIsBigEndian) {
268 _info.format_name += "big-endian";
270 _info.format_name += "little-endian";
274 snprintf (buf, sizeof (buf), " %" PRIu32 " bit", absd.mBitsPerChannel);
275 _info.format_name += buf;
276 _info.format_name += '\n';
278 if (absd.mFormatFlags & kAudioFormatFlagIsFloat) {
279 _info.format_name += "float";
281 if (absd.mFormatFlags & kAudioFormatFlagIsSignedInteger) {
282 _info.format_name += "signed";
284 _info.format_name += "unsigned";
286 /* integer is typical, do not show it */
289 if (_info.channels > 1) {
290 if (absd.mFormatFlags & kAudioFormatFlagIsNonInterleaved) {
291 _info.format_name += " noninterleaved";
293 /* interleaved is the normal case, do not show it */
296 _info.format_name += ' ';
299 switch (absd.mFormatID) {
300 case kAudioFormatLinearPCM:
301 _info.format_name += "PCM";
304 case kAudioFormatAC3:
305 _info.format_name += "AC3";
308 case kAudioFormat60958AC3:
309 _info.format_name += "60958 AC3";
312 case kAudioFormatMPEGLayer1:
313 _info.format_name += "MPEG-1";
316 case kAudioFormatMPEGLayer2:
317 _info.format_name += "MPEG-2";
320 case kAudioFormatMPEGLayer3:
321 _info.format_name += "MPEG-3";
324 case kAudioFormatAppleIMA4:
325 _info.format_name += "IMA-4";
328 case kAudioFormatMPEG4AAC:
329 _info.format_name += "AAC";
332 case kAudioFormatMPEG4CELP:
333 _info.format_name += "CELP";
336 case kAudioFormatMPEG4HVXC:
337 _info.format_name += "HVXC";
340 case kAudioFormatMPEG4TwinVQ:
341 _info.format_name += "TwinVQ";
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.
349 case kAudioFormatTimeCode:
350 _info.format_name += "timecode";
353 case kAudioFormatMIDIStream:
354 _info.format_name += "MIDI";
357 case kAudioFormatParameterValueStream:
358 _info.format_name += "parameter values";
362 // XXX it would be nice to find a way to get this information if it exists
368 ExtAudioFileDispose (af);