fc1f9c5e1d98e40a4bc8c086f51ba8116607ffbd
[ardour.git] / libs / ardour / session_click.cc
1 /*
2     Copyright (C) 2002 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 <list>
21 #include <cerrno>
22
23 #include "ardour/amp.h"
24 #include "ardour/audio_buffer.h"
25 #include "ardour/buffer_set.h"
26 #include "ardour/click.h"
27 #include "ardour/io.h"
28 #include "ardour/session.h"
29 #include "ardour/tempo.h"
30 #include "ardour/types.h"
31
32 #include <sndfile.h>
33
34 #include "pbd/i18n.h"
35
36 using namespace std;
37 using namespace ARDOUR;
38 using namespace PBD;
39
40 Pool Click::pool ("click", sizeof (Click), 1024);
41
42 void
43 Session::add_click (framepos_t pos, bool emphasis)
44 {
45         if (emphasis) {
46                 if (click_emphasis_data && Config->get_use_click_emphasis () == true) {
47                         clicks.push_back (new Click (pos, click_emphasis_length, click_emphasis_data));
48                 } else if (click_data && Config->get_use_click_emphasis () == false) {
49                         clicks.push_back (new Click (pos, click_length, click_data));
50                 }
51         } else if (click_data) {
52                 clicks.push_back (new Click (pos, click_length, click_data));
53         }
54 }
55
56 void
57 Session::click (framepos_t start, framecnt_t nframes)
58 {
59         vector<TempoMap::BBTPoint> points;
60         framecnt_t click_distance;
61
62         if (_click_io == 0) {
63                 return;
64         }
65
66         Glib::Threads::RWLock::WriterLock clickm (click_lock, Glib::Threads::TRY_LOCK);
67
68         /* how far have we moved since the last time the clicks got cleared
69          */
70
71         click_distance = start - _clicks_cleared;
72
73         if (!clickm.locked() || !_clicking || click_data == 0 || ((click_distance + nframes) < _worst_track_latency)) {
74                 _click_io->silence (nframes);
75                 return;
76         }
77
78         if (_click_rec_only && !actively_recording()) {
79                 return;
80         }
81
82         start -= _worst_track_latency;
83         /* start could be negative at this point */
84         const framepos_t end = start + nframes;
85         /* correct start, potentially */
86         start = max (start, (framepos_t) 0);
87
88         _tempo_map->get_grid (points, start, end);
89
90         if (distance (points.begin(), points.end()) == 0) {
91                 goto run_clicks;
92         }
93
94         for (vector<TempoMap::BBTPoint>::iterator i = points.begin(); i != points.end(); ++i) {
95                 switch ((*i).beat) {
96                 case 1:
97                         add_click ((*i).frame, true);
98                         break;
99                 default:
100                         if (click_emphasis_data == 0 || (Config->get_use_click_emphasis () == false) || (click_emphasis_data && (*i).beat != 1)) { // XXX why is this check needed ??
101                                 add_click ((*i).frame, false);
102                         }
103                         break;
104                 }
105         }
106
107   run_clicks:
108         clickm.release ();
109         run_click (start, nframes);
110 }
111
112 void
113 Session::run_click (framepos_t start, framepos_t nframes)
114 {
115         Glib::Threads::RWLock::ReaderLock clickm (click_lock, Glib::Threads::TRY_LOCK);
116
117         if (!clickm.locked() || click_data == 0) {
118                 _click_io->silence (nframes);
119                 return;
120         }
121
122         Sample *buf;
123         BufferSet& bufs = get_scratch_buffers(ChanCount(DataType::AUDIO, 1));
124         buf = bufs.get_audio(0).data();
125         memset (buf, 0, sizeof (Sample) * nframes);
126
127         for (list<Click*>::iterator i = clicks.begin(); i != clicks.end(); ) {
128
129                 framecnt_t copy;
130                 framecnt_t internal_offset;
131                 Click *clk;
132
133                 clk = *i;
134
135                 if (clk->start < start) {
136                         internal_offset = 0;
137                 } else {
138                         internal_offset = clk->start - start;
139                 }
140
141                 if (nframes < internal_offset) {
142                          /* we've just located or something..
143                             effectively going backwards.
144                             lets get the flock out of here */
145                         break;
146                 }
147
148                 copy = min (clk->duration - clk->offset, nframes - internal_offset);
149
150                 memcpy (buf + internal_offset, &clk->data[clk->offset], copy * sizeof (Sample));
151
152                 clk->offset += copy;
153
154                 if (clk->offset >= clk->duration) {
155                         delete clk;
156                         i = clicks.erase (i);
157                 } else {
158                         ++i;
159                 }
160         }
161
162         _click_gain->run (bufs, 0, 0, 1.0, nframes, false);
163         _click_io->copy_to_outputs (bufs, DataType::AUDIO, nframes, 0);
164 }
165
166 void
167 Session::setup_click_sounds (Sample** data, Sample const * default_data, framecnt_t* length, framecnt_t default_length, string const & path)
168 {
169         if (*data != default_data) {
170                 delete[] *data;
171                 *data = 0;
172         }
173
174         if (path.empty ()) {
175
176                 *data = const_cast<Sample*> (default_data);
177                 *length = default_length;
178
179         } else {
180
181                 SF_INFO info;
182                 SNDFILE* sndfile;
183
184                 info.format = 0;
185                 if ((sndfile = sf_open (path.c_str(), SFM_READ, &info)) == 0) {
186                         char errbuf[256];
187                         sf_error_str (0, errbuf, sizeof (errbuf) - 1);
188                         warning << string_compose (_("cannot open click soundfile %1 (%2)"), path, errbuf) << endmsg;
189                         _clicking = false;
190                         return;
191                 }
192
193                 /* read the (possibly multi-channel) click data into a temporary buffer */
194
195                 sf_count_t const samples = info.frames * info.channels;
196
197                 Sample* tmp = new Sample[samples];
198
199                 if (sf_readf_float (sndfile, tmp, info.frames) != info.frames) {
200
201                         warning << _("cannot read data from click soundfile") << endmsg;
202                         *data = 0;
203                         _clicking = false;
204
205                 } else {
206
207                         *data = new Sample[info.frames];
208                         *length = info.frames;
209
210                         /* mix down to mono */
211
212                         for (int i = 0; i < info.frames; ++i) {
213                                 (*data)[i] = 0;
214                                 for (int j = 0; j < info.channels; ++j) {
215                                         (*data)[i] = tmp[i * info.channels + j];
216                                 }
217                                 (*data)[i] /= info.channels;
218                         }
219                 }
220
221                 delete[] tmp;
222                 sf_close (sndfile);
223         }
224 }
225
226 void
227 Session::setup_click_sounds (int which)
228 {
229         clear_clicks ();
230
231         if (which == 0 || which == 1) {
232                 setup_click_sounds (
233                         &click_data,
234                         default_click,
235                         &click_length,
236                         default_click_length,
237                         Config->get_click_sound ()
238                         );
239         }
240
241         if (which == 0 || which == -1) {
242                 setup_click_sounds (
243                         &click_emphasis_data,
244                         default_click_emphasis,
245                         &click_emphasis_length,
246                         default_click_emphasis_length,
247                         Config->get_click_emphasis_sound ()
248                         );
249         }
250 }
251
252 void
253 Session::clear_clicks ()
254 {
255         Glib::Threads::RWLock::WriterLock lm (click_lock);
256
257         for (Clicks::iterator i = clicks.begin(); i != clicks.end(); ++i) {
258                 delete *i;
259         }
260
261         clicks.clear ();
262         _clicks_cleared = _transport_frame;
263 }