2 * Copyright (C) 2006-2009 David Robillard <d@drobilla.net>
3 * Copyright (C) 2006-2009 Taybin Rutkin <taybin@taybin.com>
4 * Copyright (C) 2006-2017 Paul Davis <paul@linuxaudiosystems.com>
5 * Copyright (C) 2010-2011 Carl Hetherington <carl@carlh.net>
6 * Copyright (C) 2015-2017 Robin Gareus <robin@gareus.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "pbd/error.h"
27 #include "ardour/coreaudiosource.h"
28 #include "ardour/utils.h"
31 #include "CAAudioFile.h"
33 #include "CAExtAudioFile.h"
35 #include "CAStreamBasicDescription.h"
37 #include <glibmm/fileutils.h>
41 #include <AudioToolbox/AudioFormat.h>
44 using namespace ARDOUR;
47 /** Create a new CoreAudioSource using session state, which implies that the
48 * file must already exist.
50 CoreAudioSource::CoreAudioSource (Session& s, const XMLNode& node)
52 , AudioFileSource (s, node)
56 assert (Glib::file_test (_path, Glib::FILE_TEST_EXISTS));
60 /** Create a new CoreAudioSource from an existing file. Sources created with this
61 * method are never writable or removable.
63 CoreAudioSource::CoreAudioSource (Session& s, const string& path, int chn, Flag flags)
64 : Source (s, DataType::AUDIO, path, Source::Flag (flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy))),
65 AudioFileSource (s, path,
66 Source::Flag (flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy)))
71 assert (Glib::file_test (_path, Glib::FILE_TEST_EXISTS));
76 CoreAudioSource::init_cafile ()
78 /* note that we temporarily truncated _id at the colon */
80 af.Open(_path.c_str());
82 CAStreamBasicDescription file_format (af.GetFileDataFormat());
83 n_channels = file_format.NumberChannels();
85 if (_channel >= n_channels) {
86 error << string_compose("CoreAudioSource: file only contains %1 channels; %2 is invalid as a channel number (%3)", n_channels, _channel, name()) << endmsg;
87 throw failed_constructor();
90 _length = af.GetNumberFrames();
92 CAStreamBasicDescription client_format (file_format);
94 /* set canonial form (PCM, native float packed, 32 bit, with the correct number of channels
95 and interleaved (since we plan to deinterleave ourselves)
98 client_format.SetCanonical(client_format.NumberChannels(), true);
99 af.SetClientFormat (client_format);
101 } catch (CAXException& cax) {
103 error << string_compose(_("CoreAudioSource: cannot open file \"%1\" for %2"),
104 _path, (writable() ? "read+write" : "reading")) << endmsg;
105 throw failed_constructor ();
109 CoreAudioSource::~CoreAudioSource ()
114 CoreAudioSource::close ()
120 CoreAudioSource::safe_read (Sample* dst, samplepos_t start, samplecnt_t cnt, AudioBufferList& abl) const
122 samplecnt_t nread = 0;
124 while (nread < cnt) {
127 af.Seek (start+nread);
128 } catch (CAXException& cax) {
129 error << string_compose("CoreAudioSource: %1 to %2 (%3)", cax.mOperation, start+nread, _name.val().substr (1)) << endmsg;
133 UInt32 new_cnt = cnt - nread;
135 abl.mBuffers[0].mDataByteSize = new_cnt * n_channels * sizeof(Sample);
136 abl.mBuffers[0].mData = dst + nread;
139 af.Read (new_cnt, &abl);
140 } catch (CAXException& cax) {
141 error << string_compose("CoreAudioSource: %1 (%2)", cax.mOperation, _name);
147 if (start+cnt == _length) {
148 /* we really did hit the end */
166 CoreAudioSource::read_unlocked (Sample *dst, samplepos_t start, samplecnt_t cnt) const
168 samplecnt_t file_cnt;
171 abl.mNumberBuffers = 1;
172 abl.mBuffers[0].mNumberChannels = n_channels;
174 if (start > _length) {
176 /* read starts beyond end of data, just memset to zero */
180 } else if (start + cnt > _length) {
182 /* read ends beyond end of data, read some, memset the rest */
184 file_cnt = _length - start;
188 /* read is entirely within data */
193 if (file_cnt != cnt) {
194 sampleoffset_t delta = cnt - file_cnt;
195 memset (dst+file_cnt, 0, sizeof (Sample) * delta);
200 if (n_channels == 1) {
201 if (safe_read (dst, start, file_cnt, abl) == 0) {
208 Sample* interleave_buf = get_interleave_buffer (file_cnt * n_channels);
210 if (safe_read (interleave_buf, start, file_cnt, abl) != 0) {
214 Sample *ptr = interleave_buf + _channel;
216 /* stride through the interleaved data */
218 for (samplecnt_t n = 0; n < file_cnt; ++n) {
227 CoreAudioSource::sample_rate() const
229 CAStreamBasicDescription client_asbd;
232 client_asbd = af.GetClientDataFormat ();
233 } catch (CAXException& cax) {
234 error << string_compose("CoreAudioSource: %1 (%2)", cax.mOperation, _name);
238 return client_asbd.mSampleRate;
242 CoreAudioSource::update_header (samplepos_t, struct tm&, time_t)
248 CoreAudioSource::get_soundfile_info (string path, SoundFileInfo& _info, string&)
253 ExtAudioFileRef af = 0;
259 if (FSPathMakeRef ((UInt8*)path.c_str(), &ref, 0) != noErr) {
263 if (ExtAudioFileOpen(&ref, &af) != noErr) {
267 CFURLRef url = CFURLCreateFromFileSystemRepresentation (kCFAllocatorDefault, (const UInt8*)path.c_str (), strlen (path.c_str ()), false);
268 OSStatus res = ExtAudioFileOpenURL(url, &af);
269 if (url) CFRelease (url);
276 AudioStreamBasicDescription absd;
277 memset(&absd, 0, sizeof(absd));
278 size = sizeof(AudioStreamBasicDescription);
279 if (ExtAudioFileGetProperty (af, kExtAudioFileProperty_FileDataFormat, &size, &absd) != noErr) {
283 _info.samplerate = absd.mSampleRate;
284 _info.channels = absd.mChannelsPerFrame;
286 size = sizeof(_info.length);
287 if (ExtAudioFileGetProperty(af, kExtAudioFileProperty_FileLengthFrames, &size, &_info.length) != noErr) {
291 size = sizeof(CFStringRef);
292 if (AudioFormatGetProperty(kAudioFormatProperty_FormatName, sizeof(absd), &absd, &size, &name) != noErr) {
296 _info.format_name = "";
298 if (absd.mFormatID == kAudioFormatLinearPCM) {
299 if (absd.mFormatFlags & kAudioFormatFlagIsBigEndian) {
300 _info.format_name += "big-endian";
302 _info.format_name += "little-endian";
306 snprintf (buf, sizeof (buf), " %" PRIu32 " bit", absd.mBitsPerChannel);
307 _info.format_name += buf;
308 _info.format_name += '\n';
310 if (absd.mFormatFlags & kAudioFormatFlagIsFloat) {
311 _info.format_name += "float";
313 if (absd.mFormatFlags & kAudioFormatFlagIsSignedInteger) {
314 _info.format_name += "signed";
316 _info.format_name += "unsigned";
318 /* integer is typical, do not show it */
321 if (_info.channels > 1) {
322 if (absd.mFormatFlags & kAudioFormatFlagIsNonInterleaved) {
323 _info.format_name += " noninterleaved";
325 /* interleaved is the normal case, do not show it */
328 _info.format_name += ' ';
331 switch (absd.mFormatID) {
332 case kAudioFormatLinearPCM:
333 _info.format_name += "PCM";
336 case kAudioFormatAC3:
337 _info.format_name += "AC3";
340 case kAudioFormat60958AC3:
341 _info.format_name += "60958 AC3";
344 case kAudioFormatMPEGLayer1:
345 _info.format_name += "MPEG-1";
348 case kAudioFormatMPEGLayer2:
349 _info.format_name += "MPEG-2";
352 case kAudioFormatMPEGLayer3:
353 _info.format_name += "MPEG-3";
356 case kAudioFormatAppleIMA4:
357 _info.format_name += "IMA-4";
360 case kAudioFormatMPEG4AAC:
361 _info.format_name += "AAC";
364 case kAudioFormatMPEG4CELP:
365 _info.format_name += "CELP";
368 case kAudioFormatMPEG4HVXC:
369 _info.format_name += "HVXC";
372 case kAudioFormatMPEG4TwinVQ:
373 _info.format_name += "TwinVQ";
376 /* these really shouldn't show up, but we should do something
377 somewhere else to make sure that doesn't happen. until
378 that is guaranteed, print something anyway.
381 case kAudioFormatTimeCode:
382 _info.format_name += "timecode";
385 case kAudioFormatMIDIStream:
386 _info.format_name += "MIDI";
389 case kAudioFormatParameterValueStream:
390 _info.format_name += "parameter values";
394 // XXX it would be nice to find a way to get this information if it exists
400 ExtAudioFileDispose (af);
406 CoreAudioSource::set_path (const string& p)
408 FileSource::set_path (p);