Get the number of channels in the file.
[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 <string>
21 #include <sys/stat.h>
22 #include <unistd.h>
23 #include <sys/time.h>
24
25 #include <pbd/mountpoint.h>
26 #include <ardour/coreaudio_source.h>
27
28 #include "i18n.h"
29
30 using namespace ARDOUR;
31
32 string CoreAudioSource::peak_dir = "";
33
34 CoreAudioSource::CoreAudioSource (const XMLNode& node)
35         : Source (node)
36 {
37         if (set_state (node)) {
38                 throw failed_constructor();
39         }
40
41         init (_name, true);
42          SourceCreated (this); /* EMIT SIGNAL */
43 }
44
45 CoreAudioSource::CoreAudioSource (const string& idstr, bool build_peak)
46         : Source(build_peak)
47 {
48         init (idstr, build_peak);
49
50         if (build_peak) {
51                  SourceCreated (this); /* EMIT SIGNAL */
52         }
53 }
54
55 void 
56 CoreAudioSource::init (const string& idstr, bool build_peak)
57 {
58         string::size_type pos;
59         string file;
60
61         tmpbuf = 0;
62         tmpbufsize = 0;
63         af = 0;
64         OSStatus err = noErr;
65
66         _name = idstr;
67
68         if ((pos = idstr.find_last_of (':')) == string::npos) {
69                 channel = 0;
70                 file = idstr;
71         } else {
72                 channel = atoi (idstr.substr (pos+1).c_str());
73                 file = idstr.substr (0, pos);
74         }
75
76
77         /* note that we temporarily truncated _id at the colon */
78         FSRef* ref;
79         err = FSPathMakeRef ((UInt8*)file.c_str(), ref, 0);
80         if (err != noErr) {
81                 throw failed_constructor();
82         }
83
84         err = ExtAudioFileOpen (ref, &af);
85         if (err != noErr) {
86                 ExtAudioFileDispose (af);
87                 throw failed_constructor();
88         }
89
90         AudioStreamBasicDescription absd;
91         memset(&absd, 0, sizeof(absd));
92         size_t absd_size = sizeof(absd);
93         err = ExtAudioFileGetProperty(af,
94                         kExtAudioFileProperty_FileDataFormat, &absd_size, &absd);
95         if (err != noErr) {
96                 ExtAudioFileDispose (af);
97                 throw failed_constructor();
98         }
99         n_channels = absd.mChannelsPerFrame;
100
101         if (channel >= n_channels) {
102                 error << string_compose(_("CoreAudioSource: file only contains %1 channels; %2 is invalid as a channel number"), n_channels, channel) << endmsg;
103                 ExtAudioFileDispose (af);
104                 throw failed_constructor();
105         }
106
107         int64_t ca_frames;
108         size_t prop_size = sizeof(ca_frames);
109
110         err = ExtAudioFileGetProperty(af, kExtAudioFileProperty_FileLengthFrames, &prop_size, &ca_frames);
111         if (err != noErr) {
112                 ExtAudioFileDispose (af);
113                 throw failed_constructor();
114         }
115         _length = ca_frames;
116
117         _path = file;
118
119         if (build_peak) {
120                 if (initialize_peakfile (false, file)) {
121                         ExtAudioFileDispose (af);
122                         throw failed_constructor ();
123                 }
124         }
125 }
126
127 CoreAudioSource::~CoreAudioSource ()
128
129 {
130          GoingAway (this); /* EMIT SIGNAL */
131
132         if (af) {
133                 ExtAudioFileDispose (af);
134         }
135
136         if (tmpbuf) {
137                 delete [] tmpbuf;
138         }
139 }
140
141 jack_nframes_t
142 CoreAudioSource::read_unlocked (Sample *dst, jack_nframes_t start, jack_nframes_t cnt, char * workbuf) const
143 {
144         return read (dst, start, cnt, workbuf);
145 }
146
147 jack_nframes_t
148 CoreAudioSource::read (Sample *dst, jack_nframes_t start, jack_nframes_t cnt, char * workbuf) const
149 {
150         OSStatus err = noErr;
151
152         err = ExtAudioFileSeek(af, start);
153         if (err != noErr) {
154                 error << string_compose(_("CoreAudioSource: could not seek to frame %1 within %2 (%3)"), start, _name.substr (1), err) << endmsg;
155                 return 0;
156         }
157
158         AudioBuffer ab;
159         ab.mNumberChannels = n_channels;
160         ab.mDataByteSize = cnt;
161         ab.mData = dst;
162
163         AudioBufferList abl;
164         abl.mNumberBuffers = 1;
165         abl.mBuffers[1] = ab;
166
167         if (n_channels == 1) {
168                 err = ExtAudioFileRead(af, (UInt32*) &cnt, &abl);
169                 _read_data_count = cnt * sizeof(float);
170                 return cnt;
171         }
172
173         uint32_t real_cnt = cnt * n_channels;
174
175         {
176                 LockMonitor lm (_tmpbuf_lock, __LINE__, __FILE__);
177                 
178                 if (tmpbufsize < real_cnt) {
179                         
180                         if (tmpbuf) {
181                                 delete [] tmpbuf;
182                         }
183                         tmpbufsize = real_cnt;
184                         tmpbuf = new float[tmpbufsize];
185                 }
186
187                 ab.mDataByteSize = real_cnt;
188                 ab.mData = tmpbuf;
189                 
190                 err = ExtAudioFileRead(af, (UInt32*) &real_cnt, &abl);
191                 float *ptr = tmpbuf + channel;
192                 real_cnt /= n_channels;
193                 
194                 /* stride through the interleaved data */
195                 
196                 for (uint32_t n = 0; n < real_cnt; ++n) {
197                         dst[n] = *ptr;
198                         ptr += n_channels;
199                 }
200         }
201
202         _read_data_count = cnt * sizeof(float);
203                 
204         return real_cnt;
205 }
206
207 string
208 CoreAudioSource::peak_path (string audio_path)
209 {
210         /* XXX hardly bombproof! fix me */
211
212         struct stat stat_file;
213         struct stat stat_mount;
214
215         string mp = mountpoint (audio_path);
216
217         stat (audio_path.c_str(), &stat_file);
218         stat (mp.c_str(), &stat_mount);
219
220         char buf[32];
221         snprintf (buf, sizeof (buf), "%u-%u-%d.peak", stat_mount.st_ino, stat_file.st_ino, channel);
222
223         string res = peak_dir;
224         res += buf;
225
226         return res;
227 }
228
229 string
230 CoreAudioSource::old_peak_path (string audio_path)
231 {
232         return peak_path (audio_path);
233 }
234