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>
33 #include <AudioToolbox/AudioFormat.h>
36 using namespace ARDOUR;
39 CoreAudioSource::CoreAudioSource (Session& s, const XMLNode& node)
41 , AudioFileSource (s, node)
46 CoreAudioSource::CoreAudioSource (Session& s, const string& path, int chn, Flag flags)
47 /* files created this way are never writable or removable */
48 : Source (s, DataType::AUDIO, path, Source::Flag (flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy))),
49 AudioFileSource (s, path,
50 Source::Flag (flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy)))
57 CoreAudioSource::init_cafile ()
59 /* note that we temporarily truncated _id at the colon */
61 af.Open(_path.c_str());
63 CAStreamBasicDescription file_format (af.GetFileDataFormat());
64 n_channels = file_format.NumberChannels();
66 if (_channel >= n_channels) {
67 error << string_compose("CoreAudioSource: file only contains %1 channels; %2 is invalid as a channel number (%3)", n_channels, _channel, name()) << endmsg;
68 throw failed_constructor();
71 _length = af.GetNumberFrames();
73 CAStreamBasicDescription client_format (file_format);
75 /* set canonial form (PCM, native float packed, 32 bit, with the correct number of channels
76 and interleaved (since we plan to deinterleave ourselves)
79 client_format.SetCanonical(client_format.NumberChannels(), true);
80 af.SetClientFormat (client_format);
82 } catch (CAXException& cax) {
84 error << string_compose(_("CoreAudioSource: cannot open file \"%1\" for %2"),
85 _path, (writable() ? "read+write" : "reading")) << endmsg;
86 throw failed_constructor ();
90 CoreAudioSource::~CoreAudioSource ()
95 CoreAudioSource::safe_read (Sample* dst, framepos_t start, framecnt_t cnt, AudioBufferList& abl) const
102 af.Seek (start+nread);
103 } catch (CAXException& cax) {
104 error << string_compose("CoreAudioSource: %1 to %2 (%3)", cax.mOperation, start+nread, _name.val().substr (1)) << endmsg;
108 UInt32 new_cnt = cnt - nread;
110 abl.mBuffers[0].mDataByteSize = new_cnt * n_channels * sizeof(Sample);
111 abl.mBuffers[0].mData = dst + nread;
114 af.Read (new_cnt, &abl);
115 } catch (CAXException& cax) {
116 error << string_compose("CoreAudioSource: %1 (%2)", cax.mOperation, _name);
122 if (start+cnt == _length) {
123 /* we really did hit the end */
141 CoreAudioSource::read_unlocked (Sample *dst, framepos_t start, framecnt_t cnt) const
146 abl.mNumberBuffers = 1;
147 abl.mBuffers[0].mNumberChannels = n_channels;
149 if (start > _length) {
151 /* read starts beyond end of data, just memset to zero */
155 } else if (start + cnt > _length) {
157 /* read ends beyond end of data, read some, memset the rest */
159 file_cnt = _length - start;
163 /* read is entirely within data */
168 if (file_cnt != cnt) {
169 frameoffset_t delta = cnt - file_cnt;
170 memset (dst+file_cnt, 0, sizeof (Sample) * delta);
175 if (n_channels == 1) {
176 if (safe_read (dst, start, file_cnt, abl) == 0) {
183 Sample* interleave_buf = get_interleave_buffer (file_cnt * n_channels);
185 if (safe_read (interleave_buf, start, file_cnt, abl) != 0) {
189 Sample *ptr = interleave_buf + _channel;
191 /* stride through the interleaved data */
193 for (framecnt_t n = 0; n < file_cnt; ++n) {
202 CoreAudioSource::sample_rate() const
204 CAStreamBasicDescription client_asbd;
207 client_asbd = af.GetClientDataFormat ();
208 } catch (CAXException& cax) {
209 error << string_compose("CoreAudioSource: %1 (%2)", cax.mOperation, _name);
213 return client_asbd.mSampleRate;
217 CoreAudioSource::update_header (framepos_t, struct tm&, time_t)
223 CoreAudioSource::get_soundfile_info (string path, SoundFileInfo& _info, string&)
226 ExtAudioFileRef af = 0;
231 if (FSPathMakeRef ((UInt8*)path.c_str(), &ref, 0) != noErr) {
235 if (ExtAudioFileOpen(&ref, &af) != noErr) {
239 AudioStreamBasicDescription absd;
240 memset(&absd, 0, sizeof(absd));
241 size = sizeof(AudioStreamBasicDescription);
242 if (ExtAudioFileGetProperty (af, kExtAudioFileProperty_FileDataFormat, &size, &absd) != noErr) {
246 _info.samplerate = absd.mSampleRate;
247 _info.channels = absd.mChannelsPerFrame;
249 size = sizeof(_info.length);
250 if (ExtAudioFileGetProperty(af, kExtAudioFileProperty_FileLengthFrames, &size, &_info.length) != noErr) {
254 size = sizeof(CFStringRef);
255 if (AudioFormatGetProperty(kAudioFormatProperty_FormatName, sizeof(absd), &absd, &size, &name) != noErr) {
259 _info.format_name = "";
261 if (absd.mFormatID == kAudioFormatLinearPCM) {
262 if (absd.mFormatFlags & kAudioFormatFlagIsBigEndian) {
263 _info.format_name += "big-endian";
265 _info.format_name += "little-endian";
269 snprintf (buf, sizeof (buf), " %" PRIu32 " bit", absd.mBitsPerChannel);
270 _info.format_name += buf;
271 _info.format_name += '\n';
273 if (absd.mFormatFlags & kAudioFormatFlagIsFloat) {
274 _info.format_name += "float";
276 if (absd.mFormatFlags & kAudioFormatFlagIsSignedInteger) {
277 _info.format_name += "signed";
279 _info.format_name += "unsigned";
281 /* integer is typical, do not show it */
284 if (_info.channels > 1) {
285 if (absd.mFormatFlags & kAudioFormatFlagIsNonInterleaved) {
286 _info.format_name += " noninterleaved";
288 /* interleaved is the normal case, do not show it */
291 _info.format_name += ' ';
294 switch (absd.mFormatID) {
295 case kAudioFormatLinearPCM:
296 _info.format_name += "PCM";
299 case kAudioFormatAC3:
300 _info.format_name += "AC3";
303 case kAudioFormat60958AC3:
304 _info.format_name += "60958 AC3";
307 case kAudioFormatMPEGLayer1:
308 _info.format_name += "MPEG-1";
311 case kAudioFormatMPEGLayer2:
312 _info.format_name += "MPEG-2";
315 case kAudioFormatMPEGLayer3:
316 _info.format_name += "MPEG-3";
319 case kAudioFormatAppleIMA4:
320 _info.format_name += "IMA-4";
323 case kAudioFormatMPEG4AAC:
324 _info.format_name += "AAC";
327 case kAudioFormatMPEG4CELP:
328 _info.format_name += "CELP";
331 case kAudioFormatMPEG4HVXC:
332 _info.format_name += "HVXC";
335 case kAudioFormatMPEG4TwinVQ:
336 _info.format_name += "TwinVQ";
339 /* these really shouldn't show up, but we should do something
340 somewhere else to make sure that doesn't happen. until
341 that is guaranteed, print something anyway.
344 case kAudioFormatTimeCode:
345 _info.format_name += "timecode";
348 case kAudioFormatMIDIStream:
349 _info.format_name += "MIDI";
352 case kAudioFormatParameterValueStream:
353 _info.format_name += "parameter values";
357 // XXX it would be nice to find a way to get this information if it exists
363 ExtAudioFileDispose (af);
369 CoreAudioSource::set_path (const string& p)
371 FileSource::set_path (p);