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.
23 #include <pbd/error.h>
24 #include <ardour/coreaudiosource.h>
25 #include <ardour/utils.h>
27 #include <appleutility/CAAudioFile.h>
28 #include <appleutility/CAStreamBasicDescription.h>
32 #include <AudioToolbox/AudioFormat.h>
35 using namespace ARDOUR;
38 CoreAudioSource::CoreAudioSource (Session& s, const XMLNode& node)
39 : AudioFileSource (s, node)
44 CoreAudioSource::CoreAudioSource (Session& s, const string& path, int chn, Flag flags)
45 /* files created this way are never writable or removable */
46 : AudioFileSource (s, path, Flag (flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy)))
53 CoreAudioSource::init ()
55 /* note that we temporarily truncated _id at the colon */
57 af.Open(_path.c_str());
59 CAStreamBasicDescription file_format (af.GetFileDataFormat());
60 n_channels = file_format.NumberChannels();
62 if (_channel >= n_channels) {
63 error << string_compose("CoreAudioSource: file only contains %1 channels; %2 is invalid as a channel number (%3)", n_channels, _channel, name()) << endmsg;
64 throw failed_constructor();
67 _length = af.GetNumberFrames();
69 CAStreamBasicDescription client_format (file_format);
71 /* set canonial form (PCM, native float packed, 32 bit, with the correct number of channels
72 and interleaved (since we plan to deinterleave ourselves)
75 client_format.SetCanonical(client_format.NumberChannels(), true);
76 af.SetClientFormat (client_format);
78 } catch (CAXException& cax) {
80 error << string_compose(_("CoreAudioSource: cannot open file \"%1\" for %2"),
81 _path, (writable() ? "read+write" : "reading")) << endmsg;
82 throw failed_constructor ();
86 CoreAudioSource::~CoreAudioSource ()
88 GoingAway (); /* EMIT SIGNAL */
92 CoreAudioSource::safe_read (Sample* dst, nframes_t start, nframes_t cnt, AudioBufferList& abl) const
99 af.Seek (start+nread);
100 } catch (CAXException& cax) {
101 error << string_compose("CoreAudioSource: %1 to %2 (%3)", cax.mOperation, start+nread, _name.substr (1)) << endmsg;
105 UInt32 new_cnt = cnt - nread;
107 abl.mBuffers[0].mDataByteSize = new_cnt * n_channels * sizeof(Sample);
108 abl.mBuffers[0].mData = dst + nread;
111 af.Read (new_cnt, &abl);
112 } catch (CAXException& cax) {
113 error << string_compose("CoreAudioSource: %1 (%2)", cax.mOperation, _name);
119 if (start+cnt == _length) {
120 /* we really did hit the end */
138 CoreAudioSource::read_unlocked (Sample *dst, nframes_t start, nframes_t cnt) const
143 abl.mNumberBuffers = 1;
144 abl.mBuffers[0].mNumberChannels = n_channels;
146 if (start > _length) {
148 /* read starts beyond end of data, just memset to zero */
152 } else if (start + cnt > _length) {
154 /* read ends beyond end of data, read some, memset the rest */
156 file_cnt = _length - start;
160 /* read is entirely within data */
165 if (file_cnt != cnt) {
166 nframes_t delta = cnt - file_cnt;
167 memset (dst+file_cnt, 0, sizeof (Sample) * delta);
172 if (n_channels == 1) {
173 if (safe_read (dst, start, file_cnt, abl) == 0) {
174 _read_data_count = cnt * sizeof (Sample);
181 Sample* interleave_buf = get_interleave_buffer (file_cnt * n_channels);
183 if (safe_read (interleave_buf, start, file_cnt, abl) != 0) {
187 _read_data_count = cnt * sizeof(float);
189 Sample *ptr = interleave_buf + _channel;
191 /* stride through the interleaved data */
193 for (uint32_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 (nframes_t when, struct tm&, time_t)
223 CoreAudioSource::get_soundfile_info (string path, SoundFileInfo& _info, string& error_msg)
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 = CFStringRefToStdString(name);
261 // XXX it would be nice to find a way to get this information if it exists
267 ExtAudioFileDispose (af);