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>
22 #include <ardour/coreaudio_source.h>
26 using namespace ARDOUR;
28 CoreAudioSource::CoreAudioSource (const XMLNode& node)
29 : ExternalSource (node)
32 SourceCreated (this); /* EMIT SIGNAL */
35 CoreAudioSource::CoreAudioSource (const string& idstr, bool build_peak)
36 : ExternalSource(idstr, build_peak)
38 init (idstr, build_peak);
41 SourceCreated (this); /* EMIT SIGNAL */
46 CoreAudioSource::init (const string& idstr, bool build_peak)
48 string::size_type pos;
58 if ((pos = idstr.find_last_of (':')) == string::npos) {
62 channel = atoi (idstr.substr (pos+1).c_str());
63 file = idstr.substr (0, pos);
66 /* note that we temporarily truncated _id at the colon */
68 err = FSPathMakeRef ((UInt8*)file.c_str(), &ref, 0);
70 error << err << endmsg;
71 throw failed_constructor();
74 err = ExtAudioFileOpen (&ref, &af);
76 error << err << endmsg;
77 ExtAudioFileDispose (af);
78 throw failed_constructor();
81 AudioStreamBasicDescription absd;
82 memset(&absd, 0, sizeof(absd));
83 size_t absd_size = sizeof(absd);
84 err = ExtAudioFileGetProperty(af,
85 kExtAudioFileProperty_FileDataFormat, &absd_size, &absd);
87 error << err << endmsg;
88 ExtAudioFileDispose (af);
89 throw failed_constructor();
91 n_channels = absd.mChannelsPerFrame;
93 if (channel >= n_channels) {
94 error << string_compose(_("CoreAudioSource: file only contains %1 channels; %2 is invalid as a channel number"), n_channels, channel) << endmsg;
95 error << err << endmsg;
96 ExtAudioFileDispose (af);
97 throw failed_constructor();
101 size_t prop_size = sizeof(ca_frames);
103 err = ExtAudioFileGetProperty(af, kExtAudioFileProperty_FileLengthFrames, &prop_size, &ca_frames);
105 error << err << endmsg;
106 ExtAudioFileDispose (af);
107 throw failed_constructor();
114 if (initialize_peakfile (false, file)) {
115 error << "initialize peakfile failed" << endmsg;
116 ExtAudioFileDispose (af);
117 throw failed_constructor ();
122 CoreAudioSource::~CoreAudioSource ()
124 GoingAway (this); /* EMIT SIGNAL */
127 ExtAudioFileDispose (af);
136 CoreAudioSource::read (Sample *dst, jack_nframes_t start, jack_nframes_t cnt, char * workbuf) const
138 OSStatus err = noErr;
140 err = ExtAudioFileSeek(af, start);
142 error << string_compose(_("CoreAudioSource: could not seek to frame %1 within %2 (%3)"), start, _name.substr (1), err) << endmsg;
147 abl.mNumberBuffers = 1;
148 abl.mBuffers[0].mNumberChannels = n_channels;
149 abl.mBuffers[0].mDataByteSize = cnt;
150 abl.mBuffers[0].mData = dst;
152 if (n_channels == 1) {
153 err = ExtAudioFileRead(af, (UInt32*) &cnt, &abl);
154 _read_data_count = cnt * sizeof(float);
158 uint32_t real_cnt = cnt * n_channels;
161 LockMonitor lm (_tmpbuf_lock, __LINE__, __FILE__);
163 if (tmpbufsize < real_cnt) {
168 tmpbufsize = real_cnt;
169 tmpbuf = new float[tmpbufsize];
172 abl.mBuffers[0].mDataByteSize = real_cnt;
173 abl.mBuffers[0].mData = tmpbuf;
175 err = ExtAudioFileRead(af, (UInt32*) &real_cnt, &abl);
176 float *ptr = tmpbuf + channel;
177 real_cnt /= n_channels;
179 /* stride through the interleaved data */
181 for (uint32_t n = 0; n < real_cnt; ++n) {
187 _read_data_count = cnt * sizeof(float);