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