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