Rect fix
[ardour.git] / libs / ardour / coreaudiosource.cc
1 /*
2     Copyright (C) 2006 Paul Davis 
3
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.
8
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.
13
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.
17
18 */
19
20 #include <pbd/error.h>
21 #include <ardour/coreaudiosource.h>
22
23 #include "i18n.h"
24
25 #include <AudioToolbox/AudioFormat.h>
26
27 using namespace ARDOUR;
28 using namespace PBD;
29
30 CoreAudioSource::CoreAudioSource (const XMLNode& node)
31         : AudioFileSource (node)
32 {
33         init (_name);
34         
35         AudioSourceCreated (this); /* EMIT SIGNAL */
36 }
37
38 CoreAudioSource::CoreAudioSource (const string& idstr, Flag flags)
39         : AudioFileSource(idstr, flags)
40 {
41         init (idstr);
42
43         AudioSourceCreated (this); /* EMIT SIGNAL */
44 }
45
46 void 
47 CoreAudioSource::init (const string& idstr)
48 {
49         string::size_type pos;
50         string file;
51
52         tmpbuf = 0;
53         tmpbufsize = 0;
54         af = 0;
55         OSStatus err = noErr;
56
57         _name = idstr;
58
59         if ((pos = idstr.find_last_of (':')) == string::npos) {
60                 channel = 0;
61                 file = idstr;
62         } else {
63                 channel = atoi (idstr.substr (pos+1).c_str());
64                 file = idstr.substr (0, pos);
65         }
66
67         /* note that we temporarily truncated _id at the colon */
68         FSRef fsr;
69         err = FSPathMakeRef ((UInt8*)file.c_str(), &fsr, 0);
70         if (err != noErr) {
71                 error << string_compose (_("Could not make reference to file: %1"), name()) << endmsg;
72                 throw failed_constructor();
73         }
74
75         err = ExtAudioFileOpen (&fsr, &af);
76         if (err != noErr) {
77                 error << string_compose (_("Could not open file: %1"), name()) << endmsg;
78                 ExtAudioFileDispose (af);
79                 throw failed_constructor();
80         }
81
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);
87         if (err != noErr) {
88                 error << string_compose (_("Could not get file data format for file: %1"), name()) << endmsg;
89                 ExtAudioFileDispose (af);
90                 throw failed_constructor();
91         }
92         n_channels = file_asbd.mChannelsPerFrame;
93
94         cerr << "number of channels: " << n_channels << endl;
95         
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();
100         }
101
102         int64_t ca_frames;
103         size_t prop_size = sizeof(int64_t);
104
105         err = ExtAudioFileGetProperty(af, kExtAudioFileProperty_FileLengthFrames, &prop_size, &ca_frames);
106         if (err != noErr) {
107                 error << string_compose (_("Could not get file length for file: %1"), name()) << endmsg;
108                 ExtAudioFileDispose (af);
109                 throw failed_constructor();
110         }
111
112         _length = ca_frames;
113         _path = file;
114
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;
125
126         err = ExtAudioFileSetProperty (af, kExtAudioFileProperty_ClientDataFormat, asbd_size, &client_asbd);
127         if (err != noErr) {
128                 error << string_compose (_("Could not set client data format for file: %1"), name()) << endmsg;
129                 ExtAudioFileDispose (af);
130                 throw failed_constructor ();
131         }
132         
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 ();
138                 }
139         }
140 }
141
142 CoreAudioSource::~CoreAudioSource ()
143 {
144         GoingAway (this); /* EMIT SIGNAL */
145
146         if (af) {
147                 ExtAudioFileDispose (af);
148         }
149
150         if (tmpbuf) {
151                 delete [] tmpbuf;
152         }
153 }
154
155 jack_nframes_t
156 CoreAudioSource::read_unlocked (Sample *dst, jack_nframes_t start, jack_nframes_t cnt) const
157 {
158         OSStatus err = noErr;
159
160         err = ExtAudioFileSeek(af, start);
161         if (err != noErr) {
162                 error << string_compose(_("CoreAudioSource: could not seek to frame %1 within %2 (%3)"), start, _name.substr (1), err) << endmsg;
163                 return 0;
164         }
165
166         AudioBufferList abl;
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;
171
172         if (n_channels == 1) {
173                 err = ExtAudioFileRead(af, (UInt32*) &cnt, &abl);
174                 _read_data_count = cnt * sizeof(float);
175                 return cnt;
176         }
177
178         uint32_t real_cnt = cnt * n_channels;
179
180         {
181                 Glib::Mutex::Lock lm (_tmpbuf_lock);
182                 
183                 if (tmpbufsize < real_cnt) {
184                         
185                         if (tmpbuf) {
186                                 delete [] tmpbuf;
187                         }
188                         tmpbufsize = real_cnt;
189                         tmpbuf = new float[tmpbufsize];
190                 }
191
192                 abl.mBuffers[0].mDataByteSize = real_cnt * sizeof(Sample);
193                 abl.mBuffers[0].mData = tmpbuf;
194                 
195                 err = ExtAudioFileRead(af, (UInt32*) &real_cnt, &abl);
196                 float *ptr = tmpbuf + channel;
197                 real_cnt /= n_channels;
198                 
199                 /* stride through the interleaved data */
200                 
201                 for (uint32_t n = 0; n < real_cnt; ++n) {
202                         dst[n] = *ptr;
203                         ptr += n_channels;
204                 }
205         }
206
207         _read_data_count = cnt * sizeof(float);
208                 
209         return real_cnt;
210 }
211
212 float
213 CoreAudioSource::sample_rate() const
214 {
215         AudioStreamBasicDescription client_asbd;
216         memset(&client_asbd, 0, sizeof(AudioStreamBasicDescription));
217
218         OSStatus err = noErr;
219         size_t asbd_size = sizeof(AudioStreamBasicDescription);
220
221         err = ExtAudioFileSetProperty (af, kExtAudioFileProperty_ClientDataFormat, asbd_size, &client_asbd);
222         if (err != noErr) {
223                 error << string_compose(_("Could not detect samplerate for: %1"), name()) << endmsg;
224                 return 0.0;
225         }
226
227         return client_asbd.mSampleRate;
228 }
229
230 int
231 CoreAudioSource::update_header (jack_nframes_t when, struct tm&, time_t)
232 {
233         return 0;
234 }
235