Expose virtual-keyboard port as async-port
[ardour.git] / libs / ardour / session_click.cc
1 /*
2  * Copyright (C) 2002-2017 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2006-2012 David Robillard <d@drobilla.net>
4  * Copyright (C) 2008-2011 Carl Hetherington <carl@carlh.net>
5  * Copyright (C) 2015-2017 Robin Gareus <robin@gareus.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include <list>
23 #include <cerrno>
24
25 #include "ardour/amp.h"
26 #include "ardour/audio_buffer.h"
27 #include "ardour/buffer_set.h"
28 #include "ardour/click.h"
29 #include "ardour/io.h"
30 #include "ardour/session.h"
31 #include "ardour/tempo.h"
32 #include "ardour/types.h"
33
34 #include <sndfile.h>
35
36 #include "pbd/i18n.h"
37
38 using namespace std;
39 using namespace ARDOUR;
40 using namespace PBD;
41
42 Pool Click::pool ("click", sizeof (Click), 1024);
43
44 void
45 Session::add_click (samplepos_t pos, bool emphasis)
46 {
47         if (emphasis) {
48                 if (click_emphasis_data && Config->get_use_click_emphasis () == true) {
49                         clicks.push_back (new Click (pos, click_emphasis_length, click_emphasis_data));
50                 } else if (click_data && Config->get_use_click_emphasis () == false) {
51                         clicks.push_back (new Click (pos, click_length, click_data));
52                 }
53         } else if (click_data) {
54                 clicks.push_back (new Click (pos, click_length, click_data));
55         }
56 }
57
58 void
59 Session::click (samplepos_t cycle_start, samplecnt_t nframes)
60 {
61         vector<TempoMap::BBTPoint> points; // TODO use mempool allocator
62
63         if (_click_io == 0) {
64                 return;
65         }
66
67         /* transport_frame is audible-frame (what you hear,
68          * incl output latency). So internally we're ahead,
69          * we need to prepare frames that the user will hear
70          * in "output latency's" worth of time.
71          */
72         samplecnt_t offset = _click_io->connected_latency (true);
73
74         Glib::Threads::RWLock::WriterLock clickm (click_lock, Glib::Threads::TRY_LOCK);
75
76         /* how far have we moved since the last time the clicks got cleared */
77         const samplecnt_t click_distance = cycle_start + offset - _clicks_cleared;
78
79         if (!clickm.locked() || !_clicking || click_data == 0 || ((click_distance + nframes) < 0)) {
80                 _click_io->silence (nframes);
81                 return;
82         }
83
84         if (_click_rec_only && !actively_recording()) {
85                 return;
86         }
87
88         /* range to check for clicks */
89         samplepos_t start = cycle_start + offset;
90         const samplepos_t end = start + nframes;
91         /* correct start, potentially */
92         start = max (start, (samplepos_t) 0);
93
94         if (end > start) {
95                 _tempo_map->get_grid (points, start, end);
96         }
97
98         if (distance (points.begin(), points.end()) == 0) {
99                 goto run_clicks;
100         }
101
102         for (vector<TempoMap::BBTPoint>::iterator i = points.begin(); i != points.end(); ++i) {
103                 switch ((*i).beat) {
104                 case 1:
105                         add_click ((*i).sample, true);
106                         break;
107                 default:
108                         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
109                                 add_click ((*i).sample, false);
110                         }
111                         break;
112                 }
113         }
114
115   run_clicks:
116         clickm.release ();
117         run_click (cycle_start, nframes);
118 }
119
120 void
121 Session::run_click (samplepos_t start, samplepos_t nframes)
122 {
123         Glib::Threads::RWLock::ReaderLock clickm (click_lock, Glib::Threads::TRY_LOCK);
124
125         /* align to output */
126         start += _click_io->connected_latency (true);
127
128         if (!clickm.locked() || click_data == 0) {
129                 _click_io->silence (nframes);
130                 return;
131         }
132
133         Sample *buf;
134         BufferSet& bufs = get_scratch_buffers(ChanCount(DataType::AUDIO, 1));
135         buf = bufs.get_audio(0).data();
136         memset (buf, 0, sizeof (Sample) * nframes);
137
138         for (list<Click*>::iterator i = clicks.begin(); i != clicks.end(); ) {
139
140                 samplecnt_t copy;
141                 samplecnt_t internal_offset;
142                 Click *clk;
143
144                 clk = *i;
145
146                 if (clk->start < start) {
147                         internal_offset = 0;
148                 } else {
149                         internal_offset = clk->start - start;
150                 }
151
152                 if (nframes < internal_offset) {
153                          /* we've just located or something..
154                             effectively going backwards.
155                             lets get the flock out of here */
156                         break;
157                 }
158
159                 copy = min (clk->duration - clk->offset, nframes - internal_offset);
160
161                 memcpy (buf + internal_offset, &clk->data[clk->offset], copy * sizeof (Sample));
162
163                 clk->offset += copy;
164
165                 if (clk->offset >= clk->duration) {
166                         delete clk;
167                         i = clicks.erase (i);
168                 } else {
169                         ++i;
170                 }
171         }
172
173         _click_gain->run (bufs, 0, 0, 1.0, nframes, false);
174         _click_io->copy_to_outputs (bufs, DataType::AUDIO, nframes, 0);
175 }
176
177 void
178 Session::setup_click_sounds (Sample** data, Sample const * default_data, samplecnt_t* length, samplecnt_t default_length, string const & path)
179 {
180         if (*data != default_data) {
181                 delete[] *data;
182                 *data = 0;
183         }
184
185         if (path.empty ()) {
186
187                 *data = const_cast<Sample*> (default_data);
188                 *length = default_length;
189
190         } else {
191
192                 SF_INFO info;
193                 SNDFILE* sndfile;
194
195                 info.format = 0;
196                 if ((sndfile = sf_open (path.c_str(), SFM_READ, &info)) == 0) {
197                         char errbuf[256];
198                         sf_error_str (0, errbuf, sizeof (errbuf) - 1);
199                         warning << string_compose (_("cannot open click soundfile %1 (%2)"), path, errbuf) << endmsg;
200                         _clicking = false;
201                         return;
202                 }
203
204                 /* read the (possibly multi-channel) click data into a temporary buffer */
205
206                 sf_count_t const samples = info.frames * info.channels;
207
208                 Sample* tmp = new Sample[samples];
209
210                 if (sf_readf_float (sndfile, tmp, info.frames) != info.frames) {
211
212                         warning << _("cannot read data from click soundfile") << endmsg;
213                         *data = 0;
214                         _clicking = false;
215
216                 } else {
217
218                         *data = new Sample[info.frames];
219                         *length = info.frames;
220
221                         /* mix down to mono */
222
223                         for (int i = 0; i < info.frames; ++i) {
224                                 (*data)[i] = 0;
225                                 for (int j = 0; j < info.channels; ++j) {
226                                         (*data)[i] = tmp[i * info.channels + j];
227                                 }
228                                 (*data)[i] /= info.channels;
229                         }
230                 }
231
232                 delete[] tmp;
233                 sf_close (sndfile);
234         }
235 }
236
237 void
238 Session::setup_click_sounds (int which)
239 {
240         clear_clicks ();
241
242         if (which == 0 || which == 1) {
243                 setup_click_sounds (
244                         &click_data,
245                         default_click,
246                         &click_length,
247                         default_click_length,
248                         Config->get_click_sound ()
249                         );
250         }
251
252         if (which == 0 || which == -1) {
253                 setup_click_sounds (
254                         &click_emphasis_data,
255                         default_click_emphasis,
256                         &click_emphasis_length,
257                         default_click_emphasis_length,
258                         Config->get_click_emphasis_sound ()
259                         );
260         }
261 }
262
263 void
264 Session::clear_clicks ()
265 {
266         Glib::Threads::RWLock::WriterLock lm (click_lock);
267
268         for (Clicks::iterator i = clicks.begin(); i != clicks.end(); ++i) {
269                 delete *i;
270         }
271
272         clicks.clear ();
273         _clicks_cleared = _transport_sample;
274 }