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>
23 #include <appleutility/CAAudioFile.h>
24 #include <appleutility/CAStreamBasicDescription.h>
28 #include <AudioToolbox/AudioFormat.h>
30 using namespace ARDOUR;
33 CoreAudioSource::CoreAudioSource (Session& s, const XMLNode& node)
34 : AudioFileSource (s, node)
39 CoreAudioSource::CoreAudioSource (Session& s, const string& idstr, Flag flags)
40 : AudioFileSource(s, idstr, flags)
46 CoreAudioSource::init (const string& idstr)
48 string::size_type pos;
55 if ((pos = idstr.find_last_of (':')) == string::npos) {
59 channel = atoi (idstr.substr (pos+1).c_str());
60 _path = idstr.substr (0, pos);
63 cerr << "CoreAudioSource::init() " << name() << endl;
65 /* note that we temporarily truncated _id at the colon */
67 af.Open(_path.c_str());
69 CAStreamBasicDescription file_asbd (af.GetFileDataFormat());
70 n_channels = file_asbd.NumberChannels();
71 cerr << "number of channels: " << n_channels << endl;
73 if (channel >= n_channels) {
74 error << string_compose("CoreAudioSource: file only contains %1 channels; %2 is invalid as a channel number (%3)", n_channels, channel, name()) << endmsg;
75 throw failed_constructor();
78 _length = af.GetNumberFrames();
80 CAStreamBasicDescription client_asbd(file_asbd);
81 client_asbd.SetCanonical(client_asbd.NumberChannels(), false);
82 af.SetClientFormat (client_asbd);
83 } catch (CAXException& cax) {
84 error << string_compose ("CoreAudioSource: %1 (%2)", cax.mOperation, name()) << endmsg;
85 throw failed_constructor ();
88 if (_build_peakfiles) {
89 if (initialize_peakfile (false, _path)) {
90 error << string_compose("CoreAudioSource: initialize peakfile failed (%1)", name()) << endmsg;
91 throw failed_constructor ();
96 CoreAudioSource::~CoreAudioSource ()
98 cerr << "CoreAudioSource::~CoreAudioSource() " << name() << endl;
99 GoingAway (); /* EMIT SIGNAL */
105 cerr << "deletion done" << endl;
109 CoreAudioSource::read_unlocked (Sample *dst, nframes_t start, nframes_t cnt) const
113 } catch (CAXException& cax) {
114 error << string_compose("CoreAudioSource: %1 to %2 (%3)", cax.mOperation, start, _name.substr (1)) << endmsg;
119 abl.mNumberBuffers = 1;
120 abl.mBuffers[0].mNumberChannels = n_channels;
122 UInt32 new_cnt = cnt;
123 if (n_channels == 1) {
124 abl.mBuffers[0].mDataByteSize = cnt * sizeof(Sample);
125 abl.mBuffers[0].mData = dst;
127 af.Read (new_cnt, &abl);
128 } catch (CAXException& cax) {
129 error << string_compose("CoreAudioSource: %1 (%2)", cax.mOperation, _name);
131 _read_data_count = new_cnt * sizeof(float);
135 UInt32 real_cnt = cnt * n_channels;
138 Glib::Mutex::Lock lm (_tmpbuf_lock);
140 if (tmpbufsize < real_cnt) {
145 tmpbufsize = real_cnt;
146 tmpbuf = new float[tmpbufsize];
149 abl.mBuffers[0].mDataByteSize = tmpbufsize * sizeof(Sample);
150 abl.mBuffers[0].mData = tmpbuf;
152 cerr << "channel: " << channel << endl;
155 af.Read (real_cnt, &abl);
156 } catch (CAXException& cax) {
157 error << string_compose("CoreAudioSource: %1 (%2)", cax.mOperation, _name);
159 float *ptr = tmpbuf + channel;
160 real_cnt /= n_channels;
162 /* stride through the interleaved data */
164 for (uint32_t n = 0; n < real_cnt; ++n) {
170 _read_data_count = cnt * sizeof(float);
176 CoreAudioSource::sample_rate() const
178 CAStreamBasicDescription client_asbd;
181 client_asbd = af.GetClientDataFormat ();
182 } catch (CAXException& cax) {
183 error << string_compose("CoreAudioSource: %1 (%2)", cax.mOperation, _name);
187 return client_asbd.mSampleRate;
191 CoreAudioSource::update_header (nframes_t when, struct tm&, time_t)