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>
21 #include <ardour/coreaudiosource.h>
25 #include <AudioToolbox/AudioFormat.h>
27 using namespace ARDOUR;
30 CoreAudioSource::CoreAudioSource (const XMLNode& node)
31 : AudioFileSource (node)
36 CoreAudioSource::CoreAudioSource (const string& idstr, Flag flags)
37 : AudioFileSource(idstr, flags)
43 CoreAudioSource::init (const string& idstr)
45 string::size_type pos;
55 if ((pos = idstr.find_last_of (':')) == string::npos) {
59 channel = atoi (idstr.substr (pos+1).c_str());
60 file = idstr.substr (0, pos);
63 /* note that we temporarily truncated _id at the colon */
65 err = FSPathMakeRef ((UInt8*)file.c_str(), &fsr, 0);
67 error << string_compose (_("Could not make reference to file: %1"), name()) << endmsg;
68 throw failed_constructor();
71 err = ExtAudioFileOpen (&fsr, &af);
73 error << string_compose (_("Could not open file: %1"), name()) << endmsg;
74 ExtAudioFileDispose (af);
75 throw failed_constructor();
78 AudioStreamBasicDescription file_asbd;
79 memset(&file_asbd, 0, sizeof(AudioStreamBasicDescription));
80 size_t asbd_size = sizeof(AudioStreamBasicDescription);
81 err = ExtAudioFileGetProperty(af,
82 kExtAudioFileProperty_FileDataFormat, &asbd_size, &file_asbd);
84 error << string_compose (_("Could not get file data format for file: %1"), name()) << endmsg;
85 ExtAudioFileDispose (af);
86 throw failed_constructor();
88 n_channels = file_asbd.mChannelsPerFrame;
90 cerr << "number of channels: " << n_channels << endl;
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(int64_t);
101 err = ExtAudioFileGetProperty(af, kExtAudioFileProperty_FileLengthFrames, &prop_size, &ca_frames);
103 error << string_compose (_("Could not get file length for file: %1"), name()) << endmsg;
104 ExtAudioFileDispose (af);
105 throw failed_constructor();
111 AudioStreamBasicDescription client_asbd;
112 memset(&client_asbd, 0, sizeof(AudioStreamBasicDescription));
113 client_asbd.mSampleRate = file_asbd.mSampleRate;
114 client_asbd.mFormatID = kAudioFormatLinearPCM;
115 client_asbd.mFormatFlags = kLinearPCMFormatFlagIsFloat;
116 client_asbd.mBytesPerPacket = file_asbd.mChannelsPerFrame * 4;
117 client_asbd.mFramesPerPacket = 1;
118 client_asbd.mBytesPerFrame = client_asbd.mBytesPerPacket;
119 client_asbd.mChannelsPerFrame = file_asbd.mChannelsPerFrame;
120 client_asbd.mBitsPerChannel = 32;
122 err = ExtAudioFileSetProperty (af, kExtAudioFileProperty_ClientDataFormat, asbd_size, &client_asbd);
124 error << string_compose (_("Could not set client data format for file: %1"), name()) << endmsg;
125 ExtAudioFileDispose (af);
126 throw failed_constructor ();
129 if (_build_peakfiles) {
130 if (initialize_peakfile (false, file)) {
131 error << string_compose(_("initialize peakfile failed for file %1"), name()) << endmsg;
132 ExtAudioFileDispose (af);
133 throw failed_constructor ();
138 CoreAudioSource::~CoreAudioSource ()
140 GoingAway (); /* EMIT SIGNAL */
143 ExtAudioFileDispose (af);
152 CoreAudioSource::read_unlocked (Sample *dst, jack_nframes_t start, jack_nframes_t cnt) const
154 OSStatus err = noErr;
156 err = ExtAudioFileSeek(af, start);
158 error << string_compose(_("CoreAudioSource: could not seek to frame %1 within %2 (%3)"), start, _name.substr (1), err) << endmsg;
163 abl.mNumberBuffers = 1;
164 abl.mBuffers[0].mNumberChannels = n_channels;
165 abl.mBuffers[0].mDataByteSize = cnt * sizeof(Sample);
166 abl.mBuffers[0].mData = dst;
168 if (n_channels == 1) {
169 err = ExtAudioFileRead(af, (UInt32*) &cnt, &abl);
170 _read_data_count = cnt * sizeof(float);
174 uint32_t real_cnt = cnt * n_channels;
177 Glib::Mutex::Lock lm (_tmpbuf_lock);
179 if (tmpbufsize < real_cnt) {
184 tmpbufsize = real_cnt;
185 tmpbuf = new float[tmpbufsize];
188 abl.mBuffers[0].mDataByteSize = real_cnt * sizeof(Sample);
189 abl.mBuffers[0].mData = tmpbuf;
191 err = ExtAudioFileRead(af, (UInt32*) &real_cnt, &abl);
192 float *ptr = tmpbuf + channel;
193 real_cnt /= n_channels;
195 /* stride through the interleaved data */
197 for (uint32_t n = 0; n < real_cnt; ++n) {
203 _read_data_count = cnt * sizeof(float);
209 CoreAudioSource::sample_rate() const
211 AudioStreamBasicDescription client_asbd;
212 memset(&client_asbd, 0, sizeof(AudioStreamBasicDescription));
214 OSStatus err = noErr;
215 size_t asbd_size = sizeof(AudioStreamBasicDescription);
217 err = ExtAudioFileSetProperty (af, kExtAudioFileProperty_ClientDataFormat, asbd_size, &client_asbd);
219 error << string_compose(_("Could not detect samplerate for: %1"), name()) << endmsg;
223 return client_asbd.mSampleRate;
227 CoreAudioSource::update_header (jack_nframes_t when, struct tm&, time_t)