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) {
79 error << string_compose ("CoreAudioSource: %1 (%2)", cax.mOperation, name()) << endmsg;
80 throw failed_constructor ();
84 CoreAudioSource::~CoreAudioSource ()
86 GoingAway (); /* EMIT SIGNAL */
90 CoreAudioSource::safe_read (Sample* dst, nframes_t start, nframes_t cnt, AudioBufferList& abl) const
97 af.Seek (start+nread);
98 } catch (CAXException& cax) {
99 error << string_compose("CoreAudioSource: %1 to %2 (%3)", cax.mOperation, start+nread, _name.substr (1)) << endmsg;
103 UInt32 new_cnt = cnt - nread;
105 abl.mBuffers[0].mDataByteSize = new_cnt * n_channels * sizeof(Sample);
106 abl.mBuffers[0].mData = dst + nread;
109 af.Read (new_cnt, &abl);
110 } catch (CAXException& cax) {
111 error << string_compose("CoreAudioSource: %1 (%2)", cax.mOperation, _name);
117 if (start+cnt == _length) {
118 /* we really did hit the end */
136 CoreAudioSource::read_unlocked (Sample *dst, nframes_t start, nframes_t cnt) const
141 abl.mNumberBuffers = 1;
142 abl.mBuffers[0].mNumberChannels = n_channels;
144 if (start > _length) {
146 /* read starts beyond end of data, just memset to zero */
150 } else if (start + cnt > _length) {
152 /* read ends beyond end of data, read some, memset the rest */
154 file_cnt = _length - start;
158 /* read is entirely within data */
163 if (file_cnt != cnt) {
164 nframes_t delta = cnt - file_cnt;
165 memset (dst+file_cnt, 0, sizeof (Sample) * delta);
170 if (n_channels == 1) {
171 if (safe_read (dst, start, file_cnt, abl) == 0) {
172 _read_data_count = cnt * sizeof (Sample);
179 Sample* interleave_buf = get_interleave_buffer (file_cnt * n_channels);
181 if (safe_read (interleave_buf, start, file_cnt, abl) != 0) {
185 _read_data_count = cnt * sizeof(float);
187 Sample *ptr = interleave_buf + _channel;
189 /* stride through the interleaved data */
191 for (uint32_t n = 0; n < file_cnt; ++n) {
200 CoreAudioSource::sample_rate() const
202 CAStreamBasicDescription client_asbd;
205 client_asbd = af.GetClientDataFormat ();
206 } catch (CAXException& cax) {
207 error << string_compose("CoreAudioSource: %1 (%2)", cax.mOperation, _name);
211 return client_asbd.mSampleRate;
215 CoreAudioSource::update_header (nframes_t when, struct tm&, time_t)
221 CoreAudioSource::get_soundfile_info (string path, SoundFileInfo& _info, string& error_msg)
224 ExtAudioFileRef af = 0;
229 if (FSPathMakeRef ((UInt8*)path.c_str(), &ref, 0) != noErr) {
233 if (ExtAudioFileOpen(&ref, &af) != noErr) {
237 AudioStreamBasicDescription absd;
238 memset(&absd, 0, sizeof(absd));
239 size = sizeof(AudioStreamBasicDescription);
240 if (ExtAudioFileGetProperty (af, kExtAudioFileProperty_FileDataFormat, &size, &absd) != noErr) {
244 _info.samplerate = absd.mSampleRate;
245 _info.channels = absd.mChannelsPerFrame;
247 size = sizeof(_info.length);
248 if (ExtAudioFileGetProperty(af, kExtAudioFileProperty_FileLengthFrames, &size, &_info.length) != noErr) {
252 size = sizeof(CFStringRef);
253 if (AudioFormatGetProperty(kAudioFormatProperty_FormatName, sizeof(absd), &absd, &size, &name) != noErr) {
257 _info.format_name = CFStringRefToStdString(name);
259 // XXX it would be nice to find a way to get this information if it exists
265 ExtAudioFileDispose (af);