fix compose mess, and a number of 64 bit printf specs
[ardour.git] / libs / ardour / sndfilesource.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/sndfilesource.h>
28
29 #include "i18n.h"
30
31 using namespace ARDOUR;
32
33 string SndFileSource::peak_dir = "";
34
35 SndFileSource::SndFileSource (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 SndFileSource::SndFileSource (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 SndFileSource::init (const string& idstr, bool build_peak)
58 {
59         string::size_type pos;
60         string file;
61
62         tmpbuf = 0;
63         tmpbufsize = 0;
64         sf = 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         /* although libsndfile says we don't need to set this,
77            valgrind and source code shows us that we do.
78         */
79
80         memset (&_info, 0, sizeof(_info));
81
82         /* note that we temporarily truncated _id at the colon */
83         
84         if ((sf = sf_open (file.c_str(), SFM_READ, &_info)) == 0) {
85                 char errbuf[256];
86                 sf_error_str (0, errbuf, sizeof (errbuf) - 1);
87                 error << string_compose(_("SndFileSource: cannot open file \"%1\" (%2)"), file, errbuf) << endmsg;
88                 throw failed_constructor();
89         }
90
91         if (channel >= _info.channels) {
92                 error << string_compose(_("SndFileSource: file only contains %1 channels; %2 is invalid as a channel number"), _info.channels, channel) << endmsg;
93                 sf_close (sf);
94                 sf = 0;
95                 throw failed_constructor();
96         }
97
98         _length = _info.frames;
99         _path = file;
100
101         if (build_peak) {
102                 if (initialize_peakfile (false, file)) {
103                         sf_close (sf);
104                         sf = 0;
105                         throw failed_constructor ();
106                 }
107         }
108 }
109
110 SndFileSource::~SndFileSource ()
111
112 {
113          GoingAway (this); /* EMIT SIGNAL */
114
115         if (sf) {
116                 sf_close (sf);
117         }
118
119         if (tmpbuf) {
120                 delete [] tmpbuf;
121         }
122 }
123
124 jack_nframes_t
125 SndFileSource::read_unlocked (Sample *dst, jack_nframes_t start, jack_nframes_t cnt) const
126 {
127         return read (dst, start, cnt);
128 }
129
130 jack_nframes_t
131 SndFileSource::read (Sample *dst, jack_nframes_t start, jack_nframes_t cnt) const
132 {
133         int32_t nread;
134         float *ptr;
135         uint32_t real_cnt;
136
137         if (sf_seek (sf, (off_t) start, SEEK_SET) < 0) {
138                 char errbuf[256];
139                 sf_error_str (0, errbuf, sizeof (errbuf) - 1);
140                 error << string_compose(_("SndFileSource: could not seek to frame %1 within %2 (%3)"), start, _name.substr (1), errbuf) << endmsg;
141                 return 0;
142         }
143
144         if (_info.channels == 1) {
145                 jack_nframes_t ret = sf_read_float (sf, dst, cnt);
146                 _read_data_count = cnt * sizeof(float);
147                 return ret;
148         }
149
150         real_cnt = cnt * _info.channels;
151
152         {
153                 LockMonitor lm (_tmpbuf_lock, __LINE__, __FILE__);
154                 
155                 if (tmpbufsize < real_cnt) {
156                         
157                         if (tmpbuf) {
158                                 delete [] tmpbuf;
159                         }
160                         tmpbufsize = real_cnt;
161                         tmpbuf = new float[tmpbufsize];
162                 }
163                 
164                 nread = sf_read_float (sf, tmpbuf, real_cnt);
165                 ptr = tmpbuf + channel;
166                 nread /= _info.channels;
167                 
168                 /* stride through the interleaved data */
169                 
170                 for (int32_t n = 0; n < nread; ++n) {
171                         dst[n] = *ptr;
172                         ptr += _info.channels;
173                 }
174         }
175
176         _read_data_count = cnt * sizeof(float);
177                 
178         return nread;
179 }
180
181 string
182 SndFileSource::peak_path (string audio_path)
183 {
184         /* XXX hardly bombproof! fix me */
185
186         struct stat stat_file;
187         struct stat stat_mount;
188
189         string mp = mountpoint (audio_path);
190
191         stat (audio_path.c_str(), &stat_file);
192         stat (mp.c_str(), &stat_mount);
193
194         char buf[32];
195         snprintf (buf, sizeof (buf), "%ld-%ld-%d.peak", stat_mount.st_ino, stat_file.st_ino, channel);
196
197         string res = peak_dir;
198         res += buf;
199
200         return res;
201 }
202
203 string
204 SndFileSource::old_peak_path (string audio_path)
205 {
206         return peak_path (audio_path);
207 }