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(), &fsr, 0);
72 cerr << "FSPathMakeRef " << err << endl;
73 throw failed_constructor();
76 err = ExtAudioFileOpen (&fsr, &af);
78 cerr << "ExtAudioFileOpen " << err << endl;
79 ExtAudioFileDispose (af);
80 throw failed_constructor();
83 AudioStreamBasicDescription file_asbd;
84 memset(&file_asbd, 0, sizeof(AudioStreamBasicDescription));
85 size_t asbd_size = sizeof(AudioStreamBasicDescription);
86 err = ExtAudioFileGetProperty(af,
87 kExtAudioFileProperty_FileDataFormat, &asbd_size, &file_asbd);
89 cerr << "ExtAudioFileGetProperty1 " << err << endl;
90 ExtAudioFileDispose (af);
91 throw failed_constructor();
93 n_channels = file_asbd.mChannelsPerFrame;
95 cerr << "number of channels: " << n_channels << endl;
97 if (channel >= n_channels) {
98 error << string_compose(_("CoreAudioSource: file only contains %1 channels; %2 is invalid as a channel number"), n_channels, channel) << endmsg;
99 ExtAudioFileDispose (af);
100 throw failed_constructor();
104 size_t prop_size = sizeof(int64_t);
106 err = ExtAudioFileGetProperty(af, kExtAudioFileProperty_FileLengthFrames, &prop_size, &ca_frames);
108 cerr << "ExtAudioFileGetProperty2 " << err << endl;
109 ExtAudioFileDispose (af);
110 throw failed_constructor();
116 AudioStreamBasicDescription client_asbd;
117 memset(&client_asbd, 0, sizeof(AudioStreamBasicDescription));
118 client_asbd.mSampleRate = file_asbd.mSampleRate;
119 client_asbd.mFormatID = kAudioFormatLinearPCM;
120 client_asbd.mFormatFlags = kLinearPCMFormatFlagIsFloat;
121 client_asbd.mBytesPerPacket = file_asbd.mChannelsPerFrame * 4;
122 client_asbd.mFramesPerPacket = 1;
123 client_asbd.mBytesPerFrame = client_asbd.mBytesPerPacket;
124 client_asbd.mChannelsPerFrame = file_asbd.mChannelsPerFrame;
125 client_asbd.mBitsPerChannel = 32;
127 err = ExtAudioFileSetProperty (af, kExtAudioFileProperty_ClientDataFormat, asbd_size, &client_asbd);
129 cerr << "ExtAudioFileSetProperty3 " << err << endl;
130 ExtAudioFileDispose (af);
131 throw failed_constructor ();
135 if (initialize_peakfile (false, file)) {
136 error << "initialize peakfile failed" << endmsg;
137 ExtAudioFileDispose (af);
138 throw failed_constructor ();
143 CoreAudioSource::~CoreAudioSource ()
145 GoingAway (this); /* EMIT SIGNAL */
148 ExtAudioFileDispose (af);
157 CoreAudioSource::read (Sample *dst, jack_nframes_t start, jack_nframes_t cnt, char * workbuf) const
159 OSStatus err = noErr;
161 err = ExtAudioFileSeek(af, start);
163 error << string_compose(_("CoreAudioSource: could not seek to frame %1 within %2 (%3)"), start, _name.substr (1), err) << endmsg;
168 abl.mNumberBuffers = 1;
169 abl.mBuffers[0].mNumberChannels = n_channels;
170 abl.mBuffers[0].mDataByteSize = cnt * sizeof(Sample);
171 abl.mBuffers[0].mData = dst;
173 if (n_channels == 1) {
174 err = ExtAudioFileRead(af, (UInt32*) &cnt, &abl);
175 _read_data_count = cnt * sizeof(float);
179 uint32_t real_cnt = cnt * n_channels;
182 Glib::Mutex::Lock lm (_tmpbuf_lock);
184 if (tmpbufsize < real_cnt) {
189 tmpbufsize = real_cnt;
190 tmpbuf = new float[tmpbufsize];
193 abl.mBuffers[0].mDataByteSize = real_cnt * sizeof(Sample);
194 abl.mBuffers[0].mData = tmpbuf;
196 err = ExtAudioFileRead(af, (UInt32*) &real_cnt, &abl);
197 float *ptr = tmpbuf + channel;
198 real_cnt /= n_channels;
200 /* stride through the interleaved data */
202 for (uint32_t n = 0; n < real_cnt; ++n) {
208 _read_data_count = cnt * sizeof(float);
214 CoreAudioSource::sample_rate() const
216 /* XXX taybin fill me in please */