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)
35 AudioSourceCreated (this); /* EMIT SIGNAL */
38 CoreAudioSource::CoreAudioSource (const string& idstr, Flag flags)
39 : AudioFileSource(idstr, flags)
43 AudioSourceCreated (this); /* EMIT SIGNAL */
47 CoreAudioSource::init (const string& idstr)
49 string::size_type pos;
59 if ((pos = idstr.find_last_of (':')) == string::npos) {
63 channel = atoi (idstr.substr (pos+1).c_str());
64 file = idstr.substr (0, pos);
67 /* note that we temporarily truncated _id at the colon */
69 err = FSPathMakeRef ((UInt8*)file.c_str(), &fsr, 0);
71 error << string_compose (_("Could not make reference to file: %1"), name()) << endmsg;
72 throw failed_constructor();
75 err = ExtAudioFileOpen (&fsr, &af);
77 error << string_compose (_("Could not open file: %1"), name()) << endmsg;
78 ExtAudioFileDispose (af);
79 throw failed_constructor();
82 AudioStreamBasicDescription file_asbd;
83 memset(&file_asbd, 0, sizeof(AudioStreamBasicDescription));
84 size_t asbd_size = sizeof(AudioStreamBasicDescription);
85 err = ExtAudioFileGetProperty(af,
86 kExtAudioFileProperty_FileDataFormat, &asbd_size, &file_asbd);
88 error << string_compose (_("Could not get file data format for file: %1"), name()) << endmsg;
89 ExtAudioFileDispose (af);
90 throw failed_constructor();
92 n_channels = file_asbd.mChannelsPerFrame;
94 cerr << "number of channels: " << n_channels << endl;
96 if (channel >= n_channels) {
97 error << string_compose(_("CoreAudioSource: file only contains %1 channels; %2 is invalid as a channel number"), n_channels, channel) << endmsg;
98 ExtAudioFileDispose (af);
99 throw failed_constructor();
103 size_t prop_size = sizeof(int64_t);
105 err = ExtAudioFileGetProperty(af, kExtAudioFileProperty_FileLengthFrames, &prop_size, &ca_frames);
107 error << string_compose (_("Could not get file length for file: %1"), name()) << endmsg;
108 ExtAudioFileDispose (af);
109 throw failed_constructor();
115 AudioStreamBasicDescription client_asbd;
116 memset(&client_asbd, 0, sizeof(AudioStreamBasicDescription));
117 client_asbd.mSampleRate = file_asbd.mSampleRate;
118 client_asbd.mFormatID = kAudioFormatLinearPCM;
119 client_asbd.mFormatFlags = kLinearPCMFormatFlagIsFloat;
120 client_asbd.mBytesPerPacket = file_asbd.mChannelsPerFrame * 4;
121 client_asbd.mFramesPerPacket = 1;
122 client_asbd.mBytesPerFrame = client_asbd.mBytesPerPacket;
123 client_asbd.mChannelsPerFrame = file_asbd.mChannelsPerFrame;
124 client_asbd.mBitsPerChannel = 32;
126 err = ExtAudioFileSetProperty (af, kExtAudioFileProperty_ClientDataFormat, asbd_size, &client_asbd);
128 error << string_compose (_("Could not set client data format for file: %1"), name()) << endmsg;
129 ExtAudioFileDispose (af);
130 throw failed_constructor ();
133 if (_build_peakfiles) {
134 if (initialize_peakfile (false, file)) {
135 error << string_compose(_("initialize peakfile failed for file %1"), name()) << endmsg;
136 ExtAudioFileDispose (af);
137 throw failed_constructor ();
142 CoreAudioSource::~CoreAudioSource ()
144 GoingAway (this); /* EMIT SIGNAL */
147 ExtAudioFileDispose (af);
156 CoreAudioSource::read_unlocked (Sample *dst, jack_nframes_t start, jack_nframes_t cnt) const
158 OSStatus err = noErr;
160 err = ExtAudioFileSeek(af, start);
162 error << string_compose(_("CoreAudioSource: could not seek to frame %1 within %2 (%3)"), start, _name.substr (1), err) << endmsg;
167 abl.mNumberBuffers = 1;
168 abl.mBuffers[0].mNumberChannels = n_channels;
169 abl.mBuffers[0].mDataByteSize = cnt * sizeof(Sample);
170 abl.mBuffers[0].mData = dst;
172 if (n_channels == 1) {
173 err = ExtAudioFileRead(af, (UInt32*) &cnt, &abl);
174 _read_data_count = cnt * sizeof(float);
178 uint32_t real_cnt = cnt * n_channels;
181 Glib::Mutex::Lock lm (_tmpbuf_lock);
183 if (tmpbufsize < real_cnt) {
188 tmpbufsize = real_cnt;
189 tmpbuf = new float[tmpbufsize];
192 abl.mBuffers[0].mDataByteSize = real_cnt * sizeof(Sample);
193 abl.mBuffers[0].mData = tmpbuf;
195 err = ExtAudioFileRead(af, (UInt32*) &real_cnt, &abl);
196 float *ptr = tmpbuf + channel;
197 real_cnt /= n_channels;
199 /* stride through the interleaved data */
201 for (uint32_t n = 0; n < real_cnt; ++n) {
207 _read_data_count = cnt * sizeof(float);
213 CoreAudioSource::sample_rate() const
215 AudioStreamBasicDescription client_asbd;
216 memset(&client_asbd, 0, sizeof(AudioStreamBasicDescription));
218 OSStatus err = noErr;
219 size_t asbd_size = sizeof(AudioStreamBasicDescription);
221 err = ExtAudioFileSetProperty (af, kExtAudioFileProperty_ClientDataFormat, asbd_size, &client_asbd);
223 error << string_compose(_("Could not detect samplerate for: %1"), name()) << endmsg;
227 return client_asbd.mSampleRate;
231 CoreAudioSource::update_header (jack_nframes_t when, struct tm&, time_t)