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;
31 CoreAudioSource::CoreAudioSource (const XMLNode& node)
32 : AudioFileSource (node)
35 AudioSourceCreated (this); /* EMIT SIGNAL */
38 CoreAudioSource::CoreAudioSource (const string& idstr, bool build_peak)
39 : AudioFileSource(idstr, build_peak)
41 init (idstr, build_peak);
44 AudioSourceCreated (this); /* EMIT SIGNAL */
49 CoreAudioSource::init (const string& idstr, bool build_peak)
51 string::size_type pos;
61 if ((pos = idstr.find_last_of (':')) == string::npos) {
65 channel = atoi (idstr.substr (pos+1).c_str());
66 file = idstr.substr (0, pos);
69 /* note that we temporarily truncated _id at the colon */
71 err = FSPathMakeRef ((UInt8*)file.c_str(), &fsr, 0);
73 cerr << "FSPathMakeRef " << err << endl;
74 throw failed_constructor();
77 err = ExtAudioFileOpen (&fsr, &af);
79 cerr << "ExtAudioFileOpen " << err << endl;
80 ExtAudioFileDispose (af);
81 throw failed_constructor();
84 AudioStreamBasicDescription file_asbd;
85 memset(&file_asbd, 0, sizeof(AudioStreamBasicDescription));
86 size_t asbd_size = sizeof(AudioStreamBasicDescription);
87 err = ExtAudioFileGetProperty(af,
88 kExtAudioFileProperty_FileDataFormat, &asbd_size, &file_asbd);
90 cerr << "ExtAudioFileGetProperty1 " << err << endl;
91 ExtAudioFileDispose (af);
92 throw failed_constructor();
94 n_channels = file_asbd.mChannelsPerFrame;
96 cerr << "number of channels: " << n_channels << endl;
98 if (channel >= n_channels) {
99 error << string_compose(_("CoreAudioSource: file only contains %1 channels; %2 is invalid as a channel number"), n_channels, channel) << endmsg;
100 ExtAudioFileDispose (af);
101 throw failed_constructor();
105 size_t prop_size = sizeof(int64_t);
107 err = ExtAudioFileGetProperty(af, kExtAudioFileProperty_FileLengthFrames, &prop_size, &ca_frames);
109 cerr << "ExtAudioFileGetProperty2 " << err << endl;
110 ExtAudioFileDispose (af);
111 throw failed_constructor();
117 AudioStreamBasicDescription client_asbd;
118 memset(&client_asbd, 0, sizeof(AudioStreamBasicDescription));
119 client_asbd.mSampleRate = file_asbd.mSampleRate;
120 client_asbd.mFormatID = kAudioFormatLinearPCM;
121 client_asbd.mFormatFlags = kLinearPCMFormatFlagIsFloat;
122 client_asbd.mBytesPerPacket = file_asbd.mChannelsPerFrame * 4;
123 client_asbd.mFramesPerPacket = 1;
124 client_asbd.mBytesPerFrame = client_asbd.mBytesPerPacket;
125 client_asbd.mChannelsPerFrame = file_asbd.mChannelsPerFrame;
126 client_asbd.mBitsPerChannel = 32;
128 err = ExtAudioFileSetProperty (af, kExtAudioFileProperty_ClientDataFormat, asbd_size, &client_asbd);
130 cerr << "ExtAudioFileSetProperty3 " << err << endl;
131 ExtAudioFileDispose (af);
132 throw failed_constructor ();
136 if (initialize_peakfile (false, file)) {
137 error << "initialize peakfile failed" << endmsg;
138 ExtAudioFileDispose (af);
139 throw failed_constructor ();
144 CoreAudioSource::~CoreAudioSource ()
146 GoingAway (this); /* EMIT SIGNAL */
149 ExtAudioFileDispose (af);
158 CoreAudioSource::read (Sample *dst, jack_nframes_t start, jack_nframes_t cnt, char * workbuf) const
160 OSStatus err = noErr;
162 err = ExtAudioFileSeek(af, start);
164 error << string_compose(_("CoreAudioSource: could not seek to frame %1 within %2 (%3)"), start, _name.substr (1), err) << endmsg;
169 abl.mNumberBuffers = 1;
170 abl.mBuffers[0].mNumberChannels = n_channels;
171 abl.mBuffers[0].mDataByteSize = cnt * sizeof(Sample);
172 abl.mBuffers[0].mData = dst;
174 if (n_channels == 1) {
175 err = ExtAudioFileRead(af, (UInt32*) &cnt, &abl);
176 _read_data_count = cnt * sizeof(float);
180 uint32_t real_cnt = cnt * n_channels;
183 Glib::Mutex::Lock lm (_tmpbuf_lock);
185 if (tmpbufsize < real_cnt) {
190 tmpbufsize = real_cnt;
191 tmpbuf = new float[tmpbufsize];
194 abl.mBuffers[0].mDataByteSize = real_cnt * sizeof(Sample);
195 abl.mBuffers[0].mData = tmpbuf;
197 err = ExtAudioFileRead(af, (UInt32*) &real_cnt, &abl);
198 float *ptr = tmpbuf + channel;
199 real_cnt /= n_channels;
201 /* stride through the interleaved data */
203 for (uint32_t n = 0; n < real_cnt; ++n) {
209 _read_data_count = cnt * sizeof(float);
215 CoreAudioSource::sample_rate() const
217 /* XXX taybin fill me in please */