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)
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 : AudioFileSource (s, path, Flag (flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy)))
55 CoreAudioSource::init ()
57 /* note that we temporarily truncated _id at the colon */
59 af.Open(_path.c_str());
61 CAStreamBasicDescription file_format (af.GetFileDataFormat());
62 n_channels = file_format.NumberChannels();
64 if (_channel >= n_channels) {
65 error << string_compose("CoreAudioSource: file only contains %1 channels; %2 is invalid as a channel number (%3)", n_channels, _channel, name()) << endmsg;
66 throw failed_constructor();
69 _length = af.GetNumberFrames();
71 CAStreamBasicDescription client_format (file_format);
73 /* set canonial form (PCM, native float packed, 32 bit, with the correct number of channels
74 and interleaved (since we plan to deinterleave ourselves)
77 client_format.SetCanonical(client_format.NumberChannels(), true);
78 af.SetClientFormat (client_format);
80 } catch (CAXException& cax) {
82 error << string_compose(_("CoreAudioSource: cannot open file \"%1\" for %2"),
83 _path, (writable() ? "read+write" : "reading")) << endmsg;
84 throw failed_constructor ();
88 CoreAudioSource::~CoreAudioSource ()
90 GoingAway (); /* EMIT SIGNAL */
94 CoreAudioSource::safe_read (Sample* dst, nframes_t start, nframes_t cnt, AudioBufferList& abl) const
101 af.Seek (start+nread);
102 } catch (CAXException& cax) {
103 error << string_compose("CoreAudioSource: %1 to %2 (%3)", cax.mOperation, start+nread, _name.substr (1)) << endmsg;
107 UInt32 new_cnt = cnt - nread;
109 abl.mBuffers[0].mDataByteSize = new_cnt * n_channels * sizeof(Sample);
110 abl.mBuffers[0].mData = dst + nread;
113 af.Read (new_cnt, &abl);
114 } catch (CAXException& cax) {
115 error << string_compose("CoreAudioSource: %1 (%2)", cax.mOperation, _name);
121 if (start+cnt == _length) {
122 /* we really did hit the end */
140 CoreAudioSource::read_unlocked (Sample *dst, nframes_t start, nframes_t cnt) const
145 abl.mNumberBuffers = 1;
146 abl.mBuffers[0].mNumberChannels = n_channels;
148 if (start > _length) {
150 /* read starts beyond end of data, just memset to zero */
154 } else if (start + cnt > _length) {
156 /* read ends beyond end of data, read some, memset the rest */
158 file_cnt = _length - start;
162 /* read is entirely within data */
167 if (file_cnt != cnt) {
168 nframes_t delta = cnt - file_cnt;
169 memset (dst+file_cnt, 0, sizeof (Sample) * delta);
174 if (n_channels == 1) {
175 if (safe_read (dst, start, file_cnt, abl) == 0) {
176 _read_data_count = cnt * sizeof (Sample);
183 Sample* interleave_buf = get_interleave_buffer (file_cnt * n_channels);
185 if (safe_read (interleave_buf, start, file_cnt, abl) != 0) {
189 _read_data_count = cnt * sizeof(float);
191 Sample *ptr = interleave_buf + _channel;
193 /* stride through the interleaved data */
195 for (uint32_t n = 0; n < file_cnt; ++n) {
204 CoreAudioSource::sample_rate() const
206 CAStreamBasicDescription client_asbd;
209 client_asbd = af.GetClientDataFormat ();
210 } catch (CAXException& cax) {
211 error << string_compose("CoreAudioSource: %1 (%2)", cax.mOperation, _name);
215 return client_asbd.mSampleRate;
219 CoreAudioSource::update_header (nframes_t when, struct tm&, time_t)
225 CoreAudioSource::get_soundfile_info (string path, SoundFileInfo& _info, string& error_msg)
228 ExtAudioFileRef af = 0;
233 if (FSPathMakeRef ((UInt8*)path.c_str(), &ref, 0) != noErr) {
237 if (ExtAudioFileOpen(&ref, &af) != noErr) {
241 AudioStreamBasicDescription absd;
242 memset(&absd, 0, sizeof(absd));
243 size = sizeof(AudioStreamBasicDescription);
244 if (ExtAudioFileGetProperty (af, kExtAudioFileProperty_FileDataFormat, &size, &absd) != noErr) {
248 _info.samplerate = absd.mSampleRate;
249 _info.channels = absd.mChannelsPerFrame;
251 size = sizeof(_info.length);
252 if (ExtAudioFileGetProperty(af, kExtAudioFileProperty_FileLengthFrames, &size, &_info.length) != noErr) {
256 size = sizeof(CFStringRef);
257 if (AudioFormatGetProperty(kAudioFormatProperty_FormatName, sizeof(absd), &absd, &size, &name) != noErr) {
261 _info.format_name = "";
263 if (absd.mFormatID == kAudioFormatLinearPCM) {
264 if (absd.mFormatFlags & kAudioFormatFlagIsBigEndian) {
265 _info.format_name += "big-endian";
267 _info.format_name += "little-endian";
271 snprintf (buf, sizeof (buf), " %" PRIu32 " bit", absd.mBitsPerChannel);
272 _info.format_name += buf;
273 _info.format_name += '\n';
275 if (absd.mFormatFlags & kAudioFormatFlagIsFloat) {
276 _info.format_name += "float";
278 if (absd.mFormatFlags & kAudioFormatFlagIsSignedInteger) {
279 _info.format_name += "signed";
281 _info.format_name += "unsigned";
283 /* integer is typical, do not show it */
286 if (_info.channels > 1) {
287 if (absd.mFormatFlags & kAudioFormatFlagIsNonInterleaved) {
288 _info.format_name += " noninterleaved";
290 /* interleaved is the normal case, do not show it */
293 _info.format_name += ' ';
296 switch (absd.mFormatID) {
297 case kAudioFormatLinearPCM:
298 _info.format_name += "PCM";
301 case kAudioFormatAC3:
302 _info.format_name += "AC3";
305 case kAudioFormat60958AC3:
306 _info.format_name += "60958 AC3";
309 case kAudioFormatMPEGLayer1:
310 _info.format_name += "MPEG-1";
313 case kAudioFormatMPEGLayer2:
314 _info.format_name += "MPEG-2";
317 case kAudioFormatMPEGLayer3:
318 _info.format_name += "MPEG-3";
321 case kAudioFormatAppleIMA4:
322 _info.format_name += "IMA-4";
325 case kAudioFormatMPEG4AAC:
326 _info.format_name += "AAC";
329 case kAudioFormatMPEG4CELP:
330 _info.format_name += "CELP";
333 case kAudioFormatMPEG4HVXC:
334 _info.format_name += "HVXC";
337 case kAudioFormatMPEG4TwinVQ:
338 _info.format_name += "TwinVQ";
341 /* these really shouldn't show up, but we should do something
342 somewhere else to make sure that doesn't happen. until
343 that is guaranteed, print something anyway.
346 case kAudioFormatTimeCode:
347 _info.format_name += "timecode";
350 case kAudioFormatMIDIStream:
351 _info.format_name += "MIDI";
354 case kAudioFormatParameterValueStream:
355 _info.format_name += "parameter values";
359 // XXX it would be nice to find a way to get this information if it exists
365 ExtAudioFileDispose (af);