added mutable keyword to gints used in glib atomic operations to satisfy compiler...
[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 fsr;
70         err = FSPathMakeRef ((UInt8*)file.c_str(), &fsr, 0);
71         if (err != noErr) {
72                 cerr << "FSPathMakeRef " << err << endl;
73                 throw failed_constructor();
74         }
75
76         err = ExtAudioFileOpen (&fsr, &af);
77         if (err != noErr) {
78                 cerr << "ExtAudioFileOpen " << err << endl;
79                 ExtAudioFileDispose (af);
80                 throw failed_constructor();
81         }
82
83         AudioStreamBasicDescription file_asbd;
84         memset(&file_asbd, 0, sizeof(AudioStreamBasicDescription));
85         size_t asbd_size = sizeof(AudioStreamBasicDescription);
86         err = ExtAudioFileGetProperty(af,
87                         kExtAudioFileProperty_FileDataFormat, &asbd_size, &file_asbd);
88         if (err != noErr) {
89                 cerr << "ExtAudioFileGetProperty1 " << err << endl;
90                 ExtAudioFileDispose (af);
91                 throw failed_constructor();
92         }
93         n_channels = file_asbd.mChannelsPerFrame;
94
95         cerr << "number of channels: " << n_channels << endl;
96         
97         if (channel >= n_channels) {
98                 error << string_compose(_("CoreAudioSource: file only contains %1 channels; %2 is invalid as a channel number"), n_channels, channel) << endmsg;
99                 ExtAudioFileDispose (af);
100                 throw failed_constructor();
101         }
102
103         int64_t ca_frames;
104         size_t prop_size = sizeof(int64_t);
105
106         err = ExtAudioFileGetProperty(af, kExtAudioFileProperty_FileLengthFrames, &prop_size, &ca_frames);
107         if (err != noErr) {
108                 cerr << "ExtAudioFileGetProperty2 " << err << endl;
109                 ExtAudioFileDispose (af);
110                 throw failed_constructor();
111         }
112
113         _length = ca_frames;
114         _path = file;
115
116         AudioStreamBasicDescription client_asbd;
117         memset(&client_asbd, 0, sizeof(AudioStreamBasicDescription));
118         client_asbd.mSampleRate = file_asbd.mSampleRate;
119         client_asbd.mFormatID = kAudioFormatLinearPCM;
120         client_asbd.mFormatFlags = kLinearPCMFormatFlagIsFloat;
121         client_asbd.mBytesPerPacket = file_asbd.mChannelsPerFrame * 4;
122         client_asbd.mFramesPerPacket = 1;
123         client_asbd.mBytesPerFrame = client_asbd.mBytesPerPacket;
124         client_asbd.mChannelsPerFrame = file_asbd.mChannelsPerFrame;
125         client_asbd.mBitsPerChannel = 32;
126
127         err = ExtAudioFileSetProperty (af, kExtAudioFileProperty_ClientDataFormat, asbd_size, &client_asbd);
128         if (err != noErr) {
129                 cerr << "ExtAudioFileSetProperty3 " << err << endl;
130                 ExtAudioFileDispose (af);
131                 throw failed_constructor ();
132         }
133         
134         if (build_peak) {
135                 if (initialize_peakfile (false, file)) {
136                         error << "initialize peakfile failed" << endmsg;
137                         ExtAudioFileDispose (af);
138                         throw failed_constructor ();
139                 }
140         }
141 }
142
143 CoreAudioSource::~CoreAudioSource ()
144 {
145         GoingAway (this); /* EMIT SIGNAL */
146
147         if (af) {
148                 ExtAudioFileDispose (af);
149         }
150
151         if (tmpbuf) {
152                 delete [] tmpbuf;
153         }
154 }
155
156 jack_nframes_t
157 CoreAudioSource::read (Sample *dst, jack_nframes_t start, jack_nframes_t cnt, char * workbuf) const
158 {
159         OSStatus err = noErr;
160
161         err = ExtAudioFileSeek(af, start);
162         if (err != noErr) {
163                 error << string_compose(_("CoreAudioSource: could not seek to frame %1 within %2 (%3)"), start, _name.substr (1), err) << endmsg;
164                 return 0;
165         }
166
167         AudioBufferList abl;
168         abl.mNumberBuffers = 1;
169         abl.mBuffers[0].mNumberChannels = n_channels;
170         abl.mBuffers[0].mDataByteSize = cnt * sizeof(Sample);
171         abl.mBuffers[0].mData = dst;
172
173         if (n_channels == 1) {
174                 err = ExtAudioFileRead(af, (UInt32*) &cnt, &abl);
175                 _read_data_count = cnt * sizeof(float);
176                 return cnt;
177         }
178
179         uint32_t real_cnt = cnt * n_channels;
180
181         {
182                 Glib::Mutex::Lock lm (_tmpbuf_lock);
183                 
184                 if (tmpbufsize < real_cnt) {
185                         
186                         if (tmpbuf) {
187                                 delete [] tmpbuf;
188                         }
189                         tmpbufsize = real_cnt;
190                         tmpbuf = new float[tmpbufsize];
191                 }
192
193                 abl.mBuffers[0].mDataByteSize = real_cnt * sizeof(Sample);
194                 abl.mBuffers[0].mData = tmpbuf;
195                 
196                 err = ExtAudioFileRead(af, (UInt32*) &real_cnt, &abl);
197                 float *ptr = tmpbuf + channel;
198                 real_cnt /= n_channels;
199                 
200                 /* stride through the interleaved data */
201                 
202                 for (uint32_t n = 0; n < real_cnt; ++n) {
203                         dst[n] = *ptr;
204                         ptr += n_channels;
205                 }
206         }
207
208         _read_data_count = cnt * sizeof(float);
209                 
210         return real_cnt;
211 }
212
213 float
214 CoreAudioSource::sample_rate() const
215 {
216         /* XXX taybin fill me in please */
217
218         return 44100.0f;
219 }