"The In-Flight Hack, 2006"
[ardour.git] / libs / ardour / coreaudio_source.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
22 #include <ardour/coreaudio_source.h>
23
24 #include "i18n.h"
25
26 #include <AudioToolbox/AudioFormat.h>
27
28 using namespace ARDOUR;
29
30 CoreAudioSource::CoreAudioSource (const XMLNode& node)
31         : ExternalSource (node)
32 {
33         init (_name, true);
34         SourceCreated (this); /* EMIT SIGNAL */
35 }
36
37 CoreAudioSource::CoreAudioSource (const string& idstr, bool build_peak)
38         : ExternalSource(idstr, build_peak)
39 {
40         init (idstr, build_peak);
41
42         if (build_peak) {
43                  SourceCreated (this); /* EMIT SIGNAL */
44         }
45 }
46
47 void 
48 CoreAudioSource::init (const string& idstr, bool build_peak)
49 {
50         string::size_type pos;
51         string file;
52
53         tmpbuf = 0;
54         tmpbufsize = 0;
55         af = 0;
56         OSStatus err = noErr;
57
58         _name = idstr;
59
60         if ((pos = idstr.find_last_of (':')) == string::npos) {
61                 channel = 0;
62                 file = idstr;
63         } else {
64                 channel = atoi (idstr.substr (pos+1).c_str());
65                 file = idstr.substr (0, pos);
66         }
67
68         /* note that we temporarily truncated _id at the colon */
69         FSRef ref;
70         err = FSPathMakeRef ((UInt8*)file.c_str(), &ref, 0);
71         if (err != noErr) {
72                 throw failed_constructor();
73         }
74
75         err = ExtAudioFileOpen (&ref, &af);
76         if (err != noErr) {
77                 ExtAudioFileDispose (af);
78                 throw failed_constructor();
79         }
80
81         AudioStreamBasicDescription file_asbd;
82         memset(&file_asbd, 0, sizeof(file_asbd));
83         size_t asbd_size = sizeof(file_asbd);
84         err = ExtAudioFileGetProperty(af,
85                         kExtAudioFileProperty_FileDataFormat, &asbd_size, &file_asbd);
86         if (err != noErr) {
87                 ExtAudioFileDispose (af);
88                 throw failed_constructor();
89         }
90         n_channels = file_asbd.mChannelsPerFrame;
91
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();
96         }
97
98         int64_t ca_frames;
99         size_t prop_size = sizeof(ca_frames);
100
101         err = ExtAudioFileGetProperty(af, kExtAudioFileProperty_FileLengthFrames, &prop_size, &ca_frames);
102         if (err != noErr) {
103                 ExtAudioFileDispose (af);
104                 throw failed_constructor();
105         }
106         _length = ca_frames;
107
108         _path = file;
109
110         if (build_peak) {
111                 if (initialize_peakfile (false, file)) {
112                         error << "initialize peakfile failed" << endmsg;
113                         ExtAudioFileDispose (af);
114                         throw failed_constructor ();
115                 }
116         }
117         
118         AudioStreamBasicDescription client_asbd;
119         memset(&client_asbd, 0, sizeof(client_asbd));
120         client_asbd.mFormatID = kAudioFormatLinearPCM;
121         client_asbd.mFormatFlags = kLinearPCMFormatFlagIsFloat;
122         client_asbd.mSampleRate = file_asbd.mSampleRate;
123
124         err = AudioFormatGetProperty(kAudioFormatProperty_FormatInfo, 0, NULL, &asbd_size, &client_asbd);
125         if (err != noErr) {
126                 ExtAudioFileDispose (af);
127                 throw failed_constructor ();
128         }
129
130         err = ExtAudioFileSetProperty (af, kExtAudioFileProperty_ClientDataFormat, asbd_size, &client_asbd);
131         if (err != noErr) {
132                 ExtAudioFileDispose (af);
133                 throw failed_constructor ();
134         }
135 }
136
137 CoreAudioSource::~CoreAudioSource ()
138 {
139         GoingAway (this); /* EMIT SIGNAL */
140
141         if (af) {
142                 ExtAudioFileDispose (af);
143         }
144
145         if (tmpbuf) {
146                 delete [] tmpbuf;
147         }
148 }
149
150 jack_nframes_t
151 CoreAudioSource::read (Sample *dst, jack_nframes_t start, jack_nframes_t cnt, char * workbuf) const
152 {
153         OSStatus err = noErr;
154
155         err = ExtAudioFileSeek(af, start);
156         if (err != noErr) {
157                 error << string_compose(_("CoreAudioSource: could not seek to frame %1 within %2 (%3)"), start, _name.substr (1), err) << endmsg;
158                 return 0;
159         }
160
161         AudioBufferList abl;
162         abl.mNumberBuffers = 1;
163         abl.mBuffers[0].mNumberChannels = n_channels;
164         abl.mBuffers[0].mDataByteSize = cnt * sizeof(Sample);
165         abl.mBuffers[0].mData = dst;
166
167         if (n_channels == 1) {
168                 err = ExtAudioFileRead(af, (UInt32*) &cnt, &abl);
169                 _read_data_count = cnt * sizeof(float);
170                 return cnt;
171         }
172
173         uint32_t real_cnt = cnt * n_channels;
174
175         {
176                 LockMonitor lm (_tmpbuf_lock, __LINE__, __FILE__);
177                 
178                 if (tmpbufsize < real_cnt) {
179                         
180                         if (tmpbuf) {
181                                 delete [] tmpbuf;
182                         }
183                         tmpbufsize = real_cnt;
184                         tmpbuf = new float[tmpbufsize];
185                 }
186
187                 abl.mBuffers[0].mDataByteSize = real_cnt * sizeof(Sample);
188                 abl.mBuffers[0].mData = tmpbuf;
189                 
190                 err = ExtAudioFileRead(af, (UInt32*) &real_cnt, &abl);
191                 float *ptr = tmpbuf + channel;
192                 real_cnt /= n_channels;
193                 
194                 /* stride through the interleaved data */
195                 
196                 for (uint32_t n = 0; n < real_cnt; ++n) {
197                         dst[n] = *ptr;
198                         ptr += n_channels;
199                 }
200         }
201
202         _read_data_count = cnt * sizeof(float);
203                 
204         return real_cnt;
205 }
206
207 float
208 CoreAudioSource::sample_rate() const
209 {
210         /* XXX taybin fill me in please */
211
212         return 44100.0f;
213 }