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 #include <AudioToolbox/AudioFormat.h>
28 using namespace ARDOUR;
30 CoreAudioSource::CoreAudioSource (const XMLNode& node)
31 : ExternalSource (node)
34 SourceCreated (this); /* EMIT SIGNAL */
37 CoreAudioSource::CoreAudioSource (const string& idstr, bool build_peak)
38 : ExternalSource(idstr, build_peak)
40 init (idstr, build_peak);
43 SourceCreated (this); /* EMIT SIGNAL */
48 CoreAudioSource::init (const string& idstr, bool build_peak)
50 string::size_type pos;
60 if ((pos = idstr.find_last_of (':')) == string::npos) {
64 channel = atoi (idstr.substr (pos+1).c_str());
65 file = idstr.substr (0, pos);
68 /* note that we temporarily truncated _id at the colon */
70 err = FSPathMakeRef ((UInt8*)file.c_str(), &ref, 0);
72 throw failed_constructor();
75 err = ExtAudioFileOpen (&ref, &af);
77 ExtAudioFileDispose (af);
78 throw failed_constructor();
81 AudioStreamBasicDescription file_asbd;
82 memset(&file_asbd, 0, sizeof(file_asbd));
83 size_t asbd_size = sizeof(file_asbd);
84 err = ExtAudioFileGetProperty(af,
85 kExtAudioFileProperty_FileDataFormat, &asbd_size, &file_asbd);
87 ExtAudioFileDispose (af);
88 throw failed_constructor();
90 n_channels = file_asbd.mChannelsPerFrame;
92 if (channel >= n_channels) {
93 error << string_compose(_("CoreAudioSource: file only contains %1 channels; %2 is invalid as a channel number"), n_channels, channel) << endmsg;
94 ExtAudioFileDispose (af);
95 throw failed_constructor();
99 size_t prop_size = sizeof(ca_frames);
101 err = ExtAudioFileGetProperty(af, kExtAudioFileProperty_FileLengthFrames, &prop_size, &ca_frames);
103 ExtAudioFileDispose (af);
104 throw failed_constructor();
111 if (initialize_peakfile (false, file)) {
112 error << "initialize peakfile failed" << endmsg;
113 ExtAudioFileDispose (af);
114 throw failed_constructor ();
118 AudioStreamBasicDescription client_asbd;
119 memset(&client_asbd, 0, sizeof(client_asbd));
120 client_asbd.mFormatID = kAudioFormatLinearPCM;
121 client_asbd.mFormatFlags = kLinearPCMFormatFlagIsFloat;
122 client_asbd.mSampleRate = file_asbd.mSampleRate;
124 err = AudioFormatGetProperty(kAudioFormatProperty_FormatInfo, 0, NULL, &asbd_size, &client_asbd);
126 ExtAudioFileDispose (af);
127 throw failed_constructor ();
130 err = ExtAudioFileSetProperty (af, kExtAudioFileProperty_ClientDataFormat, asbd_size, &client_asbd);
132 ExtAudioFileDispose (af);
133 throw failed_constructor ();
137 CoreAudioSource::~CoreAudioSource ()
139 GoingAway (this); /* EMIT SIGNAL */
142 ExtAudioFileDispose (af);
151 CoreAudioSource::read (Sample *dst, jack_nframes_t start, jack_nframes_t cnt, char * workbuf) const
153 OSStatus err = noErr;
155 err = ExtAudioFileSeek(af, start);
157 error << string_compose(_("CoreAudioSource: could not seek to frame %1 within %2 (%3)"), start, _name.substr (1), err) << endmsg;
162 abl.mNumberBuffers = 1;
163 abl.mBuffers[0].mNumberChannels = n_channels;
164 abl.mBuffers[0].mDataByteSize = cnt * sizeof(Sample);
165 abl.mBuffers[0].mData = dst;
167 if (n_channels == 1) {
168 err = ExtAudioFileRead(af, (UInt32*) &cnt, &abl);
169 _read_data_count = cnt * sizeof(float);
173 uint32_t real_cnt = cnt * n_channels;
176 LockMonitor lm (_tmpbuf_lock, __LINE__, __FILE__);
178 if (tmpbufsize < real_cnt) {
183 tmpbufsize = real_cnt;
184 tmpbuf = new float[tmpbufsize];
187 abl.mBuffers[0].mDataByteSize = real_cnt * sizeof(Sample);
188 abl.mBuffers[0].mData = tmpbuf;
190 err = ExtAudioFileRead(af, (UInt32*) &real_cnt, &abl);
191 float *ptr = tmpbuf + channel;
192 real_cnt /= n_channels;
194 /* stride through the interleaved data */
196 for (uint32_t n = 0; n < real_cnt; ++n) {
202 _read_data_count = cnt * sizeof(float);
208 CoreAudioSource::sample_rate() const
210 /* XXX taybin fill me in please */