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>
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.
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.
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.
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"
39 using namespace ARDOUR;
42 Pool Click::pool ("click", sizeof (Click), 1024);
45 Session::add_click (samplepos_t pos, bool 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));
53 } else if (click_data) {
54 clicks.push_back (new Click (pos, click_length, click_data));
59 Session::click (samplepos_t cycle_start, samplecnt_t nframes)
61 vector<TempoMap::BBTPoint> points; // TODO use mempool allocator
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.
72 samplecnt_t offset = _click_io->connected_latency (true);
74 Glib::Threads::RWLock::WriterLock clickm (click_lock, Glib::Threads::TRY_LOCK);
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;
79 if (!clickm.locked() || !_clicking || click_data == 0 || ((click_distance + nframes) < 0)) {
80 _click_io->silence (nframes);
84 if (_click_rec_only && !actively_recording()) {
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);
95 _tempo_map->get_grid (points, start, end);
98 if (distance (points.begin(), points.end()) == 0) {
102 for (vector<TempoMap::BBTPoint>::iterator i = points.begin(); i != points.end(); ++i) {
105 add_click ((*i).sample, true);
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);
117 run_click (cycle_start, nframes);
121 Session::run_click (samplepos_t start, samplepos_t nframes)
123 Glib::Threads::RWLock::ReaderLock clickm (click_lock, Glib::Threads::TRY_LOCK);
125 /* align to output */
126 start += _click_io->connected_latency (true);
128 if (!clickm.locked() || click_data == 0) {
129 _click_io->silence (nframes);
134 BufferSet& bufs = get_scratch_buffers(ChanCount(DataType::AUDIO, 1));
135 buf = bufs.get_audio(0).data();
136 memset (buf, 0, sizeof (Sample) * nframes);
138 for (list<Click*>::iterator i = clicks.begin(); i != clicks.end(); ) {
141 samplecnt_t internal_offset;
146 if (clk->start < start) {
149 internal_offset = clk->start - start;
152 if (nframes < internal_offset) {
153 /* we've just located or something..
154 effectively going backwards.
155 lets get the flock out of here */
159 copy = min (clk->duration - clk->offset, nframes - internal_offset);
161 memcpy (buf + internal_offset, &clk->data[clk->offset], copy * sizeof (Sample));
165 if (clk->offset >= clk->duration) {
167 i = clicks.erase (i);
173 _click_gain->run (bufs, 0, 0, 1.0, nframes, false);
174 _click_io->copy_to_outputs (bufs, DataType::AUDIO, nframes, 0);
178 Session::setup_click_sounds (Sample** data, Sample const * default_data, samplecnt_t* length, samplecnt_t default_length, string const & path)
180 if (*data != default_data) {
187 *data = const_cast<Sample*> (default_data);
188 *length = default_length;
196 if ((sndfile = sf_open (path.c_str(), SFM_READ, &info)) == 0) {
198 sf_error_str (0, errbuf, sizeof (errbuf) - 1);
199 warning << string_compose (_("cannot open click soundfile %1 (%2)"), path, errbuf) << endmsg;
204 /* read the (possibly multi-channel) click data into a temporary buffer */
206 sf_count_t const samples = info.frames * info.channels;
208 Sample* tmp = new Sample[samples];
210 if (sf_readf_float (sndfile, tmp, info.frames) != info.frames) {
212 warning << _("cannot read data from click soundfile") << endmsg;
218 *data = new Sample[info.frames];
219 *length = info.frames;
221 /* mix down to mono */
223 for (int i = 0; i < info.frames; ++i) {
225 for (int j = 0; j < info.channels; ++j) {
226 (*data)[i] = tmp[i * info.channels + j];
228 (*data)[i] /= info.channels;
238 Session::setup_click_sounds (int which)
242 if (which == 0 || which == 1) {
247 default_click_length,
248 Config->get_click_sound ()
252 if (which == 0 || which == -1) {
254 &click_emphasis_data,
255 default_click_emphasis,
256 &click_emphasis_length,
257 default_click_emphasis_length,
258 Config->get_click_emphasis_sound ()
264 Session::clear_clicks ()
266 Glib::Threads::RWLock::WriterLock lm (click_lock);
268 for (Clicks::iterator i = clicks.begin(); i != clicks.end(); ++i) {
273 _clicks_cleared = _transport_sample;