2 Copyright (C) 2006 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <pbd/error.h>
21 #include <ardour/coreaudiosource.h>
22 #include <ardour/utils.h>
24 #include <appleutility/CAAudioFile.h>
25 #include <appleutility/CAStreamBasicDescription.h>
29 #include <AudioToolbox/AudioFormat.h>
31 using namespace ARDOUR;
34 CoreAudioSource::CoreAudioSource (Session& s, const XMLNode& node)
35 : AudioFileSource (s, node)
40 CoreAudioSource::CoreAudioSource (Session& s, const string& path, int chn, Flag flags)
41 /* files created this way are never writable or removable */
42 : AudioFileSource (s, path, Flag (flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy)))
49 CoreAudioSource::init ()
54 cerr << "CoreAudioSource::init() " << name() << endl;
56 /* note that we temporarily truncated _id at the colon */
58 af.Open(_path.c_str());
60 CAStreamBasicDescription file_asbd (af.GetFileDataFormat());
61 n_channels = file_asbd.NumberChannels();
62 cerr << "number of channels: " << n_channels << endl;
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_asbd(file_asbd);
72 client_asbd.SetCanonical(client_asbd.NumberChannels(), false);
73 af.SetClientFormat (client_asbd);
74 } catch (CAXException& cax) {
75 error << string_compose ("CoreAudioSource: %1 (%2)", cax.mOperation, name()) << endmsg;
76 throw failed_constructor ();
80 CoreAudioSource::~CoreAudioSource ()
82 cerr << "CoreAudioSource::~CoreAudioSource() " << name() << endl;
83 GoingAway (); /* EMIT SIGNAL */
89 cerr << "deletion done" << endl;
93 CoreAudioSource::read_unlocked (Sample *dst, nframes_t start, nframes_t cnt) const
97 } catch (CAXException& cax) {
98 error << string_compose("CoreAudioSource: %1 to %2 (%3)", cax.mOperation, start, _name.substr (1)) << endmsg;
103 abl.mNumberBuffers = 1;
104 abl.mBuffers[0].mNumberChannels = n_channels;
106 UInt32 new_cnt = cnt;
107 if (n_channels == 1) {
108 abl.mBuffers[0].mDataByteSize = cnt * sizeof(Sample);
109 abl.mBuffers[0].mData = dst;
111 af.Read (new_cnt, &abl);
112 } catch (CAXException& cax) {
113 error << string_compose("CoreAudioSource: %1 (%2)", cax.mOperation, _name);
115 _read_data_count = new_cnt * sizeof(float);
119 UInt32 real_cnt = cnt * n_channels;
122 Glib::Mutex::Lock lm (_tmpbuf_lock);
124 if (tmpbufsize < real_cnt) {
129 tmpbufsize = real_cnt;
130 tmpbuf = new float[tmpbufsize];
133 abl.mBuffers[0].mDataByteSize = tmpbufsize * sizeof(Sample);
134 abl.mBuffers[0].mData = tmpbuf;
136 cerr << "channel: " << _channel << endl;
139 af.Read (real_cnt, &abl);
140 } catch (CAXException& cax) {
141 error << string_compose("CoreAudioSource: %1 (%2)", cax.mOperation, _name);
143 float *ptr = tmpbuf + _channel;
144 real_cnt /= n_channels;
146 /* stride through the interleaved data */
148 for (uint32_t n = 0; n < real_cnt; ++n) {
154 _read_data_count = cnt * sizeof(float);
160 CoreAudioSource::sample_rate() const
162 CAStreamBasicDescription client_asbd;
165 client_asbd = af.GetClientDataFormat ();
166 } catch (CAXException& cax) {
167 error << string_compose("CoreAudioSource: %1 (%2)", cax.mOperation, _name);
171 return client_asbd.mSampleRate;
175 CoreAudioSource::update_header (nframes_t when, struct tm&, time_t)
181 CoreAudioSource::get_soundfile_info (string path, SoundFileInfo& _info, string& error_msg)
184 ExtAudioFileRef af = 0;
189 if (FSPathMakeRef ((UInt8*)path.c_str(), &ref, 0) != noErr) {
193 if (ExtAudioFileOpen(&ref, &af) != noErr) {
197 AudioStreamBasicDescription absd;
198 memset(&absd, 0, sizeof(absd));
199 size = sizeof(AudioStreamBasicDescription);
200 if (ExtAudioFileGetProperty (af, kExtAudioFileProperty_FileDataFormat, &size, &absd) != noErr) {
204 _info.samplerate = absd.mSampleRate;
205 _info.channels = absd.mChannelsPerFrame;
207 size = sizeof(_info.length);
208 if (ExtAudioFileGetProperty(af, kExtAudioFileProperty_FileLengthFrames, &size, &_info.length) != noErr) {
212 size = sizeof(CFStringRef);
213 if (AudioFormatGetProperty(kAudioFormatProperty_FormatName, sizeof(absd), &absd, &size, &name) != noErr) {
217 _info.format_name = CFStringRefToStdString(name);
219 // XXX it would be nice to find a way to get this information if it exists
225 ExtAudioFileDispose (af);