3c81b18fd4fb2ee129a8d230d4fb180f75e234b6
[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 <appleutility/CAAudioFile.h>
24 #include <appleutility/CAStreamBasicDescription.h>
25
26 #include "i18n.h"
27
28 #include <AudioToolbox/AudioFormat.h>
29
30 using namespace ARDOUR;
31 using namespace PBD;
32
33 CoreAudioSource::CoreAudioSource (Session& s, const XMLNode& node)
34         : AudioFileSource (s, node)
35 {
36         init (_name);
37 }
38
39 CoreAudioSource::CoreAudioSource (Session& s, const string& idstr, Flag flags)
40         : AudioFileSource(s, idstr, flags)
41 {
42         init (idstr);
43 }
44
45 void 
46 CoreAudioSource::init (string idstr)
47 {
48         string::size_type pos;
49
50         tmpbuf = 0;
51         tmpbufsize = 0;
52
53         _name = idstr;
54
55         if ((pos = idstr.find_last_of (':')) == string::npos) {
56                 channel = 0;
57                 _path = idstr;
58         } else {
59                 channel = atoi (idstr.substr (pos+1).c_str());
60                 _path = idstr.substr (0, pos);
61         }
62
63         cerr << "CoreAudioSource::init() " << name() << endl;
64         
65         /* note that we temporarily truncated _id at the colon */
66         try {
67                 af.Open(_path.c_str());
68
69                 CAStreamBasicDescription file_asbd (af.GetFileDataFormat());
70                 n_channels = file_asbd.NumberChannels();
71                 cerr << "number of channels: " << n_channels << endl;
72                 
73                 if (channel >= n_channels) {
74                         error << string_compose("CoreAudioSource: file only contains %1 channels; %2 is invalid as a channel number (%3)", n_channels, channel, name()) << endmsg;
75                         throw failed_constructor();
76                 }
77
78                 _length = af.GetNumberFrames();
79
80                 CAStreamBasicDescription client_asbd(file_asbd);
81                 client_asbd.SetCanonical(client_asbd.NumberChannels(), false);
82                 af.SetClientFormat (client_asbd);
83         } catch (CAXException& cax) {
84                 error << string_compose ("CoreAudioSource: %1 (%2)", cax.mOperation, name()) << endmsg;
85                 throw failed_constructor ();
86         }
87 }
88
89 CoreAudioSource::~CoreAudioSource ()
90 {
91         cerr << "CoreAudioSource::~CoreAudioSource() " << name() << endl;
92         GoingAway (); /* EMIT SIGNAL */
93
94         if (tmpbuf) {
95                 delete [] tmpbuf;
96         }
97         
98         cerr << "deletion done" << endl;
99 }
100
101 nframes_t
102 CoreAudioSource::read_unlocked (Sample *dst, nframes_t start, nframes_t cnt) const
103 {
104         try {
105                 af.Seek (start);
106         } catch (CAXException& cax) {
107                 error << string_compose("CoreAudioSource: %1 to %2 (%3)", cax.mOperation, start, _name.substr (1)) << endmsg;
108                 return 0;
109         }
110
111         AudioBufferList abl;
112         abl.mNumberBuffers = 1;
113         abl.mBuffers[0].mNumberChannels = n_channels;
114
115         UInt32 new_cnt = cnt;
116         if (n_channels == 1) {
117                 abl.mBuffers[0].mDataByteSize = cnt * sizeof(Sample);
118                 abl.mBuffers[0].mData = dst;
119                 try {
120                         af.Read (new_cnt, &abl);
121                 } catch (CAXException& cax) {
122                         error << string_compose("CoreAudioSource: %1 (%2)", cax.mOperation, _name);
123                 }
124                 _read_data_count = new_cnt * sizeof(float);
125                 return new_cnt;
126         }
127
128         UInt32 real_cnt = cnt * n_channels;
129
130         {
131                 Glib::Mutex::Lock lm (_tmpbuf_lock);
132                 
133                 if (tmpbufsize < real_cnt) {
134                         
135                         if (tmpbuf) {
136                                 delete [] tmpbuf;
137                         }
138                         tmpbufsize = real_cnt;
139                         tmpbuf = new float[tmpbufsize];
140                 }
141
142                 abl.mBuffers[0].mDataByteSize = tmpbufsize * sizeof(Sample);
143                 abl.mBuffers[0].mData = tmpbuf;
144
145                 cerr << "channel: " << channel << endl;
146                 
147                 try {
148                         af.Read (real_cnt, &abl);
149                 } catch (CAXException& cax) {
150                         error << string_compose("CoreAudioSource: %1 (%2)", cax.mOperation, _name);
151                 }
152                 float *ptr = tmpbuf + channel;
153                 real_cnt /= n_channels;
154                 
155                 /* stride through the interleaved data */
156                 
157                 for (uint32_t n = 0; n < real_cnt; ++n) {
158                         dst[n] = *ptr;
159                         ptr += n_channels;
160                 }
161         }
162
163         _read_data_count = cnt * sizeof(float);
164                 
165         return real_cnt;
166 }
167
168 float
169 CoreAudioSource::sample_rate() const
170 {
171         CAStreamBasicDescription client_asbd;
172
173         try {
174                 client_asbd = af.GetClientDataFormat ();
175         } catch (CAXException& cax) {
176                 error << string_compose("CoreAudioSource: %1 (%2)", cax.mOperation, _name);
177                 return 0.0;
178         }
179
180         return client_asbd.mSampleRate;
181 }
182
183 int
184 CoreAudioSource::update_header (nframes_t when, struct tm&, time_t)
185 {
186         return 0;
187 }
188
189 int
190 CoreAudioSource::get_soundfile_info (string path, SoundFileInfo& _info, string& error_msg)
191 {
192         FSRef ref; 
193         ExtAudioFileRef af = 0;
194         size_t size;
195         CFStringRef name;
196         int ret = -1;
197
198         if (FSPathMakeRef ((UInt8*)path.c_str(), &ref, 0) != noErr) {
199                 goto out;
200         }
201         
202         if (ExtAudioFileOpen(&ref, &af) != noErr) {
203                 goto out;
204         }
205         
206         AudioStreamBasicDescription absd;
207         memset(&absd, 0, sizeof(absd));
208         size = sizeof(AudioStreamBasicDescription);
209         if (ExtAudioFileGetProperty (af, kExtAudioFileProperty_FileDataFormat, &size, &absd) != noErr) {
210                 goto out;
211         }
212         
213         _info.samplerate = absd.mSampleRate;
214         _info.channels   = absd.mChannelsPerFrame;
215
216         size = sizeof(_info.length);
217         if (ExtAudioFileGetProperty(af, kExtAudioFileProperty_FileLengthFrames, &size, &_info.length) != noErr) {
218                 goto out;
219         }
220         
221         size = sizeof(CFStringRef);
222         if (AudioFormatGetProperty(kAudioFormatProperty_FormatName, sizeof(absd), &absd, &size, &name) != noErr) {
223                 goto out;
224         }
225
226         _info.format_name = CFStringRefToStdString(name);
227
228         // XXX it would be nice to find a way to get this information if it exists
229
230         _info.timecode = 0;
231         ret = 0;
232         
233   out:
234         ExtAudioFileDispose (af);
235         return ret;
236         
237 }