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"
29 #include "CAAudioFile.h"
31 #include "CAExtAudioFile.h"
33 #include "CAStreamBasicDescription.h"
35 #include <glibmm/fileutils.h>
39 #include <AudioToolbox/AudioFormat.h>
42 using namespace ARDOUR;
45 /** Create a new CoreAudioSource using session state, which implies that the
46 * file must already exist.
48 CoreAudioSource::CoreAudioSource (Session& s, const XMLNode& node)
50 , AudioFileSource (s, node)
54 assert (Glib::file_test (_path, Glib::FILE_TEST_EXISTS));
58 /** Create a new CoreAudioSource from an existing file. Sources created with this
59 * method are never writable or removable.
61 CoreAudioSource::CoreAudioSource (Session& s, const string& path, int chn, Flag flags)
62 : Source (s, DataType::AUDIO, path, Source::Flag (flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy))),
63 AudioFileSource (s, path,
64 Source::Flag (flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy)))
69 assert (Glib::file_test (_path, Glib::FILE_TEST_EXISTS));
74 CoreAudioSource::init_cafile ()
76 /* note that we temporarily truncated _id at the colon */
78 af.Open(_path.c_str());
80 CAStreamBasicDescription file_format (af.GetFileDataFormat());
81 n_channels = file_format.NumberChannels();
83 if (_channel >= n_channels) {
84 error << string_compose("CoreAudioSource: file only contains %1 channels; %2 is invalid as a channel number (%3)", n_channels, _channel, name()) << endmsg;
85 throw failed_constructor();
88 _length = af.GetNumberSamples();
90 CAStreamBasicDescription client_format (file_format);
92 /* set canonial form (PCM, native float packed, 32 bit, with the correct number of channels
93 and interleaved (since we plan to deinterleave ourselves)
96 client_format.SetCanonical(client_format.NumberChannels(), true);
97 af.SetClientFormat (client_format);
99 } catch (CAXException& cax) {
101 error << string_compose(_("CoreAudioSource: cannot open file \"%1\" for %2"),
102 _path, (writable() ? "read+write" : "reading")) << endmsg;
103 throw failed_constructor ();
107 CoreAudioSource::~CoreAudioSource ()
112 CoreAudioSource::close ()
118 CoreAudioSource::safe_read (Sample* dst, samplepos_t start, samplecnt_t cnt, AudioBufferList& abl) const
120 samplecnt_t nread = 0;
122 while (nread < cnt) {
125 af.Seek (start+nread);
126 } catch (CAXException& cax) {
127 error << string_compose("CoreAudioSource: %1 to %2 (%3)", cax.mOperation, start+nread, _name.val().substr (1)) << endmsg;
131 UInt32 new_cnt = cnt - nread;
133 abl.mBuffers[0].mDataByteSize = new_cnt * n_channels * sizeof(Sample);
134 abl.mBuffers[0].mData = dst + nread;
137 af.Read (new_cnt, &abl);
138 } catch (CAXException& cax) {
139 error << string_compose("CoreAudioSource: %1 (%2)", cax.mOperation, _name);
145 if (start+cnt == _length) {
146 /* we really did hit the end */
164 CoreAudioSource::read_unlocked (Sample *dst, samplepos_t start, samplecnt_t cnt) const
166 samplecnt_t file_cnt;
169 abl.mNumberBuffers = 1;
170 abl.mBuffers[0].mNumberChannels = n_channels;
172 if (start > _length) {
174 /* read starts beyond end of data, just memset to zero */
178 } else if (start + cnt > _length) {
180 /* read ends beyond end of data, read some, memset the rest */
182 file_cnt = _length - start;
186 /* read is entirely within data */
191 if (file_cnt != cnt) {
192 sampleoffset_t delta = cnt - file_cnt;
193 memset (dst+file_cnt, 0, sizeof (Sample) * delta);
198 if (n_channels == 1) {
199 if (safe_read (dst, start, file_cnt, abl) == 0) {
206 Sample* interleave_buf = get_interleave_buffer (file_cnt * n_channels);
208 if (safe_read (interleave_buf, start, file_cnt, abl) != 0) {
212 Sample *ptr = interleave_buf + _channel;
214 /* stride through the interleaved data */
216 for (samplecnt_t n = 0; n < file_cnt; ++n) {
225 CoreAudioSource::sample_rate() const
227 CAStreamBasicDescription client_asbd;
230 client_asbd = af.GetClientDataFormat ();
231 } catch (CAXException& cax) {
232 error << string_compose("CoreAudioSource: %1 (%2)", cax.mOperation, _name);
236 return client_asbd.mSampleRate;
240 CoreAudioSource::update_header (samplepos_t, struct tm&, time_t)
246 CoreAudioSource::get_soundfile_info (string path, SoundFileInfo& _info, string&)
251 ExtAudioFileRef af = 0;
257 if (FSPathMakeRef ((UInt8*)path.c_str(), &ref, 0) != noErr) {
261 if (ExtAudioFileOpen(&ref, &af) != noErr) {
265 CFURLRef url = CFURLCreateFromFileSystemRepresentation (kCFAllocatorDefault, (const UInt8*)path.c_str (), strlen (path.c_str ()), false);
266 OSStatus res = ExtAudioFileOpenURL(url, &af);
267 if (url) CFRelease (url);
274 AudioStreamBasicDescription absd;
275 memset(&absd, 0, sizeof(absd));
276 size = sizeof(AudioStreamBasicDescription);
277 if (ExtAudioFileGetProperty (af, kExtAudioFileProperty_FileDataFormat, &size, &absd) != noErr) {
281 _info.samplerate = absd.mSampleRate;
282 _info.channels = absd.mChannelsPerFrame;
284 size = sizeof(_info.length);
285 if (ExtAudioFileGetProperty(af, kExtAudioFileProperty_FileLengthSamples, &size, &_info.length) != noErr) {
289 size = sizeof(CFStringRef);
290 if (AudioFormatGetProperty(kAudioFormatProperty_FormatName, sizeof(absd), &absd, &size, &name) != noErr) {
294 _info.format_name = "";
296 if (absd.mFormatID == kAudioFormatLinearPCM) {
297 if (absd.mFormatFlags & kAudioFormatFlagIsBigEndian) {
298 _info.format_name += "big-endian";
300 _info.format_name += "little-endian";
304 snprintf (buf, sizeof (buf), " %" PRIu32 " bit", absd.mBitsPerChannel);
305 _info.format_name += buf;
306 _info.format_name += '\n';
308 if (absd.mFormatFlags & kAudioFormatFlagIsFloat) {
309 _info.format_name += "float";
311 if (absd.mFormatFlags & kAudioFormatFlagIsSignedInteger) {
312 _info.format_name += "signed";
314 _info.format_name += "unsigned";
316 /* integer is typical, do not show it */
319 if (_info.channels > 1) {
320 if (absd.mFormatFlags & kAudioFormatFlagIsNonInterleaved) {
321 _info.format_name += " noninterleaved";
323 /* interleaved is the normal case, do not show it */
326 _info.format_name += ' ';
329 switch (absd.mFormatID) {
330 case kAudioFormatLinearPCM:
331 _info.format_name += "PCM";
334 case kAudioFormatAC3:
335 _info.format_name += "AC3";
338 case kAudioFormat60958AC3:
339 _info.format_name += "60958 AC3";
342 case kAudioFormatMPEGLayer1:
343 _info.format_name += "MPEG-1";
346 case kAudioFormatMPEGLayer2:
347 _info.format_name += "MPEG-2";
350 case kAudioFormatMPEGLayer3:
351 _info.format_name += "MPEG-3";
354 case kAudioFormatAppleIMA4:
355 _info.format_name += "IMA-4";
358 case kAudioFormatMPEG4AAC:
359 _info.format_name += "AAC";
362 case kAudioFormatMPEG4CELP:
363 _info.format_name += "CELP";
366 case kAudioFormatMPEG4HVXC:
367 _info.format_name += "HVXC";
370 case kAudioFormatMPEG4TwinVQ:
371 _info.format_name += "TwinVQ";
374 /* these really shouldn't show up, but we should do something
375 somewhere else to make sure that doesn't happen. until
376 that is guaranteed, print something anyway.
379 case kAudioFormatTimeCode:
380 _info.format_name += "timecode";
383 case kAudioFormatMIDIStream:
384 _info.format_name += "MIDI";
387 case kAudioFormatParameterValueStream:
388 _info.format_name += "parameter values";
392 // XXX it would be nice to find a way to get this information if it exists
398 ExtAudioFileDispose (af);
404 CoreAudioSource::set_path (const string& p)
406 FileSource::set_path (p);