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,
49 Source::Flag (flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy)))
56 CoreAudioSource::init ()
58 /* note that we temporarily truncated _id at the colon */
60 af.Open(_path.c_str());
62 CAStreamBasicDescription file_format (af.GetFileDataFormat());
63 n_channels = file_format.NumberChannels();
65 if (_channel >= n_channels) {
66 error << string_compose("CoreAudioSource: file only contains %1 channels; %2 is invalid as a channel number (%3)", n_channels, _channel, name()) << endmsg;
67 throw failed_constructor();
70 _length = af.GetNumberFrames();
72 CAStreamBasicDescription client_format (file_format);
74 /* set canonial form (PCM, native float packed, 32 bit, with the correct number of channels
75 and interleaved (since we plan to deinterleave ourselves)
78 client_format.SetCanonical(client_format.NumberChannels(), true);
79 af.SetClientFormat (client_format);
81 } catch (CAXException& cax) {
83 error << string_compose(_("CoreAudioSource: cannot open file \"%1\" for %2"),
84 _path, (writable() ? "read+write" : "reading")) << endmsg;
85 throw failed_constructor ();
89 CoreAudioSource::~CoreAudioSource ()
91 GoingAway (); /* EMIT SIGNAL */
95 CoreAudioSource::safe_read (Sample* dst, nframes_t start, nframes_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.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, nframes_t start, nframes_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 nframes_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) {
177 _read_data_count = cnt * sizeof (Sample);
184 Sample* interleave_buf = get_interleave_buffer (file_cnt * n_channels);
186 if (safe_read (interleave_buf, start, file_cnt, abl) != 0) {
190 _read_data_count = cnt * sizeof(float);
192 Sample *ptr = interleave_buf + _channel;
194 /* stride through the interleaved data */
196 for (uint32_t n = 0; n < file_cnt; ++n) {
205 CoreAudioSource::sample_rate() const
207 CAStreamBasicDescription client_asbd;
210 client_asbd = af.GetClientDataFormat ();
211 } catch (CAXException& cax) {
212 error << string_compose("CoreAudioSource: %1 (%2)", cax.mOperation, _name);
216 return client_asbd.mSampleRate;
220 CoreAudioSource::update_header (nframes_t when, struct tm&, time_t)
226 CoreAudioSource::get_soundfile_info (string path, SoundFileInfo& _info, string& error_msg)
229 ExtAudioFileRef af = 0;
234 if (FSPathMakeRef ((UInt8*)path.c_str(), &ref, 0) != noErr) {
238 if (ExtAudioFileOpen(&ref, &af) != noErr) {
242 AudioStreamBasicDescription absd;
243 memset(&absd, 0, sizeof(absd));
244 size = sizeof(AudioStreamBasicDescription);
245 if (ExtAudioFileGetProperty (af, kExtAudioFileProperty_FileDataFormat, &size, &absd) != noErr) {
249 _info.samplerate = absd.mSampleRate;
250 _info.channels = absd.mChannelsPerFrame;
252 size = sizeof(_info.length);
253 if (ExtAudioFileGetProperty(af, kExtAudioFileProperty_FileLengthFrames, &size, &_info.length) != noErr) {
257 size = sizeof(CFStringRef);
258 if (AudioFormatGetProperty(kAudioFormatProperty_FormatName, sizeof(absd), &absd, &size, &name) != noErr) {
262 _info.format_name = "";
264 if (absd.mFormatID == kAudioFormatLinearPCM) {
265 if (absd.mFormatFlags & kAudioFormatFlagIsBigEndian) {
266 _info.format_name += "big-endian";
268 _info.format_name += "little-endian";
272 snprintf (buf, sizeof (buf), " %" PRIu32 " bit", absd.mBitsPerChannel);
273 _info.format_name += buf;
274 _info.format_name += '\n';
276 if (absd.mFormatFlags & kAudioFormatFlagIsFloat) {
277 _info.format_name += "float";
279 if (absd.mFormatFlags & kAudioFormatFlagIsSignedInteger) {
280 _info.format_name += "signed";
282 _info.format_name += "unsigned";
284 /* integer is typical, do not show it */
287 if (_info.channels > 1) {
288 if (absd.mFormatFlags & kAudioFormatFlagIsNonInterleaved) {
289 _info.format_name += " noninterleaved";
291 /* interleaved is the normal case, do not show it */
294 _info.format_name += ' ';
297 switch (absd.mFormatID) {
298 case kAudioFormatLinearPCM:
299 _info.format_name += "PCM";
302 case kAudioFormatAC3:
303 _info.format_name += "AC3";
306 case kAudioFormat60958AC3:
307 _info.format_name += "60958 AC3";
310 case kAudioFormatMPEGLayer1:
311 _info.format_name += "MPEG-1";
314 case kAudioFormatMPEGLayer2:
315 _info.format_name += "MPEG-2";
318 case kAudioFormatMPEGLayer3:
319 _info.format_name += "MPEG-3";
322 case kAudioFormatAppleIMA4:
323 _info.format_name += "IMA-4";
326 case kAudioFormatMPEG4AAC:
327 _info.format_name += "AAC";
330 case kAudioFormatMPEG4CELP:
331 _info.format_name += "CELP";
334 case kAudioFormatMPEG4HVXC:
335 _info.format_name += "HVXC";
338 case kAudioFormatMPEG4TwinVQ:
339 _info.format_name += "TwinVQ";
342 /* these really shouldn't show up, but we should do something
343 somewhere else to make sure that doesn't happen. until
344 that is guaranteed, print something anyway.
347 case kAudioFormatTimeCode:
348 _info.format_name += "timecode";
351 case kAudioFormatMIDIStream:
352 _info.format_name += "MIDI";
355 case kAudioFormatParameterValueStream:
356 _info.format_name += "parameter values";
360 // XXX it would be nice to find a way to get this information if it exists
366 ExtAudioFileDispose (af);