Add option to limit automatable control parmaters
[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 (samplepos_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 (samplepos_t cycle_start, samplecnt_t nframes)
58 {
59         vector<TempoMap::BBTPoint> points; // TODO use mempool allocator
60
61         if (_click_io == 0) {
62                 return;
63         }
64
65         /* transport_frame is audible-frame (what you hear,
66          * incl output latency). So internally we're ahead,
67          * we need to prepare frames that the user will hear
68          * in "output latency's" worth of time.
69          */
70         samplecnt_t offset = _click_io->connected_latency (true);
71
72         Glib::Threads::RWLock::WriterLock clickm (click_lock, Glib::Threads::TRY_LOCK);
73
74         /* how far have we moved since the last time the clicks got cleared */
75         const samplecnt_t click_distance = cycle_start + offset - _clicks_cleared;
76
77         if (!clickm.locked() || !_clicking || click_data == 0 || ((click_distance + nframes) < 0)) {
78                 _click_io->silence (nframes);
79                 return;
80         }
81
82         if (_click_rec_only && !actively_recording()) {
83                 return;
84         }
85
86         /* range to check for clicks */
87         samplepos_t start = cycle_start + offset;
88         const samplepos_t end = start + nframes;
89         /* correct start, potentially */
90         start = max (start, (samplepos_t) 0);
91
92         if (end > start) {
93                 _tempo_map->get_grid (points, start, end);
94         }
95
96         if (distance (points.begin(), points.end()) == 0) {
97                 goto run_clicks;
98         }
99
100         for (vector<TempoMap::BBTPoint>::iterator i = points.begin(); i != points.end(); ++i) {
101                 switch ((*i).beat) {
102                 case 1:
103                         add_click ((*i).sample, true);
104                         break;
105                 default:
106                         if (click_emphasis_data == 0 || (Config->get_use_click_emphasis () == false) || (click_emphasis_data && (*i).beat != 1)) { // XXX why is this check needed ??  (*i).beat !=1 must be true here
107                                 add_click ((*i).sample, false);
108                         }
109                         break;
110                 }
111         }
112
113   run_clicks:
114         clickm.release ();
115         run_click (cycle_start, nframes);
116 }
117
118 void
119 Session::run_click (samplepos_t start, samplepos_t nframes)
120 {
121         Glib::Threads::RWLock::ReaderLock clickm (click_lock, Glib::Threads::TRY_LOCK);
122
123         /* align to output */
124         start += _click_io->connected_latency (true);
125
126         if (!clickm.locked() || click_data == 0) {
127                 _click_io->silence (nframes);
128                 return;
129         }
130
131         Sample *buf;
132         BufferSet& bufs = get_scratch_buffers(ChanCount(DataType::AUDIO, 1));
133         buf = bufs.get_audio(0).data();
134         memset (buf, 0, sizeof (Sample) * nframes);
135
136         for (list<Click*>::iterator i = clicks.begin(); i != clicks.end(); ) {
137
138                 samplecnt_t copy;
139                 samplecnt_t internal_offset;
140                 Click *clk;
141
142                 clk = *i;
143
144                 if (clk->start < start) {
145                         internal_offset = 0;
146                 } else {
147                         internal_offset = clk->start - start;
148                 }
149
150                 if (nframes < internal_offset) {
151                          /* we've just located or something..
152                             effectively going backwards.
153                             lets get the flock out of here */
154                         break;
155                 }
156
157                 copy = min (clk->duration - clk->offset, nframes - internal_offset);
158
159                 memcpy (buf + internal_offset, &clk->data[clk->offset], copy * sizeof (Sample));
160
161                 clk->offset += copy;
162
163                 if (clk->offset >= clk->duration) {
164                         delete clk;
165                         i = clicks.erase (i);
166                 } else {
167                         ++i;
168                 }
169         }
170
171         _click_gain->run (bufs, 0, 0, 1.0, nframes, false);
172         _click_io->copy_to_outputs (bufs, DataType::AUDIO, nframes, 0);
173 }
174
175 void
176 Session::setup_click_sounds (Sample** data, Sample const * default_data, samplecnt_t* length, samplecnt_t default_length, string const & path)
177 {
178         if (*data != default_data) {
179                 delete[] *data;
180                 *data = 0;
181         }
182
183         if (path.empty ()) {
184
185                 *data = const_cast<Sample*> (default_data);
186                 *length = default_length;
187
188         } else {
189
190                 SF_INFO info;
191                 SNDFILE* sndfile;
192
193                 info.format = 0;
194                 if ((sndfile = sf_open (path.c_str(), SFM_READ, &info)) == 0) {
195                         char errbuf[256];
196                         sf_error_str (0, errbuf, sizeof (errbuf) - 1);
197                         warning << string_compose (_("cannot open click soundfile %1 (%2)"), path, errbuf) << endmsg;
198                         _clicking = false;
199                         return;
200                 }
201
202                 /* read the (possibly multi-channel) click data into a temporary buffer */
203
204                 sf_count_t const samples = info.frames * info.channels;
205
206                 Sample* tmp = new Sample[samples];
207
208                 if (sf_readf_float (sndfile, tmp, info.frames) != info.frames) {
209
210                         warning << _("cannot read data from click soundfile") << endmsg;
211                         *data = 0;
212                         _clicking = false;
213
214                 } else {
215
216                         *data = new Sample[info.frames];
217                         *length = info.frames;
218
219                         /* mix down to mono */
220
221                         for (int i = 0; i < info.frames; ++i) {
222                                 (*data)[i] = 0;
223                                 for (int j = 0; j < info.channels; ++j) {
224                                         (*data)[i] = tmp[i * info.channels + j];
225                                 }
226                                 (*data)[i] /= info.channels;
227                         }
228                 }
229
230                 delete[] tmp;
231                 sf_close (sndfile);
232         }
233 }
234
235 void
236 Session::setup_click_sounds (int which)
237 {
238         clear_clicks ();
239
240         if (which == 0 || which == 1) {
241                 setup_click_sounds (
242                         &click_data,
243                         default_click,
244                         &click_length,
245                         default_click_length,
246                         Config->get_click_sound ()
247                         );
248         }
249
250         if (which == 0 || which == -1) {
251                 setup_click_sounds (
252                         &click_emphasis_data,
253                         default_click_emphasis,
254                         &click_emphasis_length,
255                         default_click_emphasis_length,
256                         Config->get_click_emphasis_sound ()
257                         );
258         }
259 }
260
261 void
262 Session::clear_clicks ()
263 {
264         Glib::Threads::RWLock::WriterLock lm (click_lock);
265
266         for (Clicks::iterator i = clicks.begin(); i != clicks.end(); ++i) {
267                 delete *i;
268         }
269
270         clicks.clear ();
271         _clicks_cleared = _transport_sample;
272 }