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 <ardour/coreaudio_source.h>
24 using namespace ARDOUR;
26 CoreAudioSource::CoreAudioSource (const XMLNode& node)
27 : ExternalSource (node)
30 SourceCreated (this); /* EMIT SIGNAL */
33 CoreAudioSource::CoreAudioSource (const string& idstr, bool build_peak)
34 : ExternalSource(idstr, build_peak)
36 init (idstr, build_peak);
39 SourceCreated (this); /* EMIT SIGNAL */
44 CoreAudioSource::init (const string& idstr, bool build_peak)
46 string::size_type pos;
56 if ((pos = idstr.find_last_of (':')) == string::npos) {
60 channel = atoi (idstr.substr (pos+1).c_str());
61 file = idstr.substr (0, pos);
64 /* note that we temporarily truncated _id at the colon */
66 err = FSPathMakeRef ((UInt8*)file.c_str(), &ref, 0);
68 throw failed_constructor();
71 err = ExtAudioFileOpen (&ref, &af);
73 ExtAudioFileDispose (af);
74 throw failed_constructor();
77 AudioStreamBasicDescription absd;
78 memset(&absd, 0, sizeof(absd));
79 size_t absd_size = sizeof(absd);
80 err = ExtAudioFileGetProperty(af,
81 kExtAudioFileProperty_FileDataFormat, &absd_size, &absd);
83 ExtAudioFileDispose (af);
84 throw failed_constructor();
86 n_channels = absd.mChannelsPerFrame;
88 if (channel >= n_channels) {
89 error << string_compose(_("CoreAudioSource: file only contains %1 channels; %2 is invalid as a channel number"), n_channels, channel) << endmsg;
90 ExtAudioFileDispose (af);
91 throw failed_constructor();
95 size_t prop_size = sizeof(ca_frames);
97 err = ExtAudioFileGetProperty(af, kExtAudioFileProperty_FileLengthFrames, &prop_size, &ca_frames);
99 ExtAudioFileDispose (af);
100 throw failed_constructor();
107 if (initialize_peakfile (false, file)) {
108 ExtAudioFileDispose (af);
109 throw failed_constructor ();
114 CoreAudioSource::~CoreAudioSource ()
116 GoingAway (this); /* EMIT SIGNAL */
119 ExtAudioFileDispose (af);
128 CoreAudioSource::read (Sample *dst, jack_nframes_t start, jack_nframes_t cnt, char * workbuf) const
130 OSStatus err = noErr;
132 err = ExtAudioFileSeek(af, start);
134 error << string_compose(_("CoreAudioSource: could not seek to frame %1 within %2 (%3)"), start, _name.substr (1), err) << endmsg;
139 ab.mNumberChannels = n_channels;
140 ab.mDataByteSize = cnt;
144 abl.mNumberBuffers = 1;
145 abl.mBuffers[1] = ab;
147 if (n_channels == 1) {
148 err = ExtAudioFileRead(af, (UInt32*) &cnt, &abl);
149 _read_data_count = cnt * sizeof(float);
153 uint32_t real_cnt = cnt * n_channels;
156 LockMonitor lm (_tmpbuf_lock, __LINE__, __FILE__);
158 if (tmpbufsize < real_cnt) {
163 tmpbufsize = real_cnt;
164 tmpbuf = new float[tmpbufsize];
167 ab.mDataByteSize = real_cnt;
170 err = ExtAudioFileRead(af, (UInt32*) &real_cnt, &abl);
171 float *ptr = tmpbuf + channel;
172 real_cnt /= n_channels;
174 /* stride through the interleaved data */
176 for (uint32_t n = 0; n < real_cnt; ++n) {
182 _read_data_count = cnt * sizeof(float);