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