Fixed compilation of CoreAudioSource
[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
36 CoreAudioSource::CoreAudioSource (const string& idstr, Flag flags)
37         : AudioFileSource(idstr, flags)
38 {
39         init (idstr);
40 }
41
42 void 
43 CoreAudioSource::init (const string& idstr)
44 {
45         string::size_type pos;
46         string file;
47
48         tmpbuf = 0;
49         tmpbufsize = 0;
50         af = 0;
51         OSStatus err = noErr;
52
53         _name = idstr;
54
55         if ((pos = idstr.find_last_of (':')) == string::npos) {
56                 channel = 0;
57                 file = idstr;
58         } else {
59                 channel = atoi (idstr.substr (pos+1).c_str());
60                 file = idstr.substr (0, pos);
61         }
62
63         /* note that we temporarily truncated _id at the colon */
64         FSRef fsr;
65         err = FSPathMakeRef ((UInt8*)file.c_str(), &fsr, 0);
66         if (err != noErr) {
67                 error << string_compose (_("Could not make reference to file: %1"), name()) << endmsg;
68                 throw failed_constructor();
69         }
70
71         err = ExtAudioFileOpen (&fsr, &af);
72         if (err != noErr) {
73                 error << string_compose (_("Could not open file: %1"), name()) << endmsg;
74                 ExtAudioFileDispose (af);
75                 throw failed_constructor();
76         }
77
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);
83         if (err != noErr) {
84                 error << string_compose (_("Could not get file data format for file: %1"), name()) << endmsg;
85                 ExtAudioFileDispose (af);
86                 throw failed_constructor();
87         }
88         n_channels = file_asbd.mChannelsPerFrame;
89
90         cerr << "number of channels: " << n_channels << endl;
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(int64_t);
100
101         err = ExtAudioFileGetProperty(af, kExtAudioFileProperty_FileLengthFrames, &prop_size, &ca_frames);
102         if (err != noErr) {
103                 error << string_compose (_("Could not get file length for file: %1"), name()) << endmsg;
104                 ExtAudioFileDispose (af);
105                 throw failed_constructor();
106         }
107
108         _length = ca_frames;
109         _path = file;
110
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;
121
122         err = ExtAudioFileSetProperty (af, kExtAudioFileProperty_ClientDataFormat, asbd_size, &client_asbd);
123         if (err != noErr) {
124                 error << string_compose (_("Could not set client data format for file: %1"), name()) << endmsg;
125                 ExtAudioFileDispose (af);
126                 throw failed_constructor ();
127         }
128         
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 ();
134                 }
135         }
136 }
137
138 CoreAudioSource::~CoreAudioSource ()
139 {
140         GoingAway (); /* EMIT SIGNAL */
141
142         if (af) {
143                 ExtAudioFileDispose (af);
144         }
145
146         if (tmpbuf) {
147                 delete [] tmpbuf;
148         }
149 }
150
151 jack_nframes_t
152 CoreAudioSource::read_unlocked (Sample *dst, jack_nframes_t start, jack_nframes_t cnt) const
153 {
154         OSStatus err = noErr;
155
156         err = ExtAudioFileSeek(af, start);
157         if (err != noErr) {
158                 error << string_compose(_("CoreAudioSource: could not seek to frame %1 within %2 (%3)"), start, _name.substr (1), err) << endmsg;
159                 return 0;
160         }
161
162         AudioBufferList abl;
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;
167
168         if (n_channels == 1) {
169                 err = ExtAudioFileRead(af, (UInt32*) &cnt, &abl);
170                 _read_data_count = cnt * sizeof(float);
171                 return cnt;
172         }
173
174         uint32_t real_cnt = cnt * n_channels;
175
176         {
177                 Glib::Mutex::Lock lm (_tmpbuf_lock);
178                 
179                 if (tmpbufsize < real_cnt) {
180                         
181                         if (tmpbuf) {
182                                 delete [] tmpbuf;
183                         }
184                         tmpbufsize = real_cnt;
185                         tmpbuf = new float[tmpbufsize];
186                 }
187
188                 abl.mBuffers[0].mDataByteSize = real_cnt * sizeof(Sample);
189                 abl.mBuffers[0].mData = tmpbuf;
190                 
191                 err = ExtAudioFileRead(af, (UInt32*) &real_cnt, &abl);
192                 float *ptr = tmpbuf + channel;
193                 real_cnt /= n_channels;
194                 
195                 /* stride through the interleaved data */
196                 
197                 for (uint32_t n = 0; n < real_cnt; ++n) {
198                         dst[n] = *ptr;
199                         ptr += n_channels;
200                 }
201         }
202
203         _read_data_count = cnt * sizeof(float);
204                 
205         return real_cnt;
206 }
207
208 float
209 CoreAudioSource::sample_rate() const
210 {
211         AudioStreamBasicDescription client_asbd;
212         memset(&client_asbd, 0, sizeof(AudioStreamBasicDescription));
213
214         OSStatus err = noErr;
215         size_t asbd_size = sizeof(AudioStreamBasicDescription);
216
217         err = ExtAudioFileSetProperty (af, kExtAudioFileProperty_ClientDataFormat, asbd_size, &client_asbd);
218         if (err != noErr) {
219                 error << string_compose(_("Could not detect samplerate for: %1"), name()) << endmsg;
220                 return 0.0;
221         }
222
223         return client_asbd.mSampleRate;
224 }
225
226 int
227 CoreAudioSource::update_header (jack_nframes_t when, struct tm&, time_t)
228 {
229         return 0;
230 }
231