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