summaryrefslogtreecommitdiff
path: root/libs/ardour/session_click.cc
blob: 3a0d50ff65e9066a62d947f4f17b9f87ef7e0d3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
 * Copyright (C) 2002-2017 Paul Davis <paul@linuxaudiosystems.com>
 * Copyright (C) 2006-2012 David Robillard <d@drobilla.net>
 * Copyright (C) 2008-2011 Carl Hetherington <carl@carlh.net>
 * Copyright (C) 2015-2017 Robin Gareus <robin@gareus.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <list>
#include <cerrno>

#include "ardour/amp.h"
#include "ardour/audio_buffer.h"
#include "ardour/buffer_set.h"
#include "ardour/click.h"
#include "ardour/io.h"
#include "ardour/session.h"
#include "ardour/tempo.h"
#include "ardour/types.h"

#include <sndfile.h>

#include "pbd/i18n.h"

using namespace std;
using namespace ARDOUR;
using namespace PBD;

Pool Click::pool ("click", sizeof (Click), 1024);

/* pre-allocated vector for grid-point-lookup.
 *
 * Since Session::click() is never called concurrently
 * from different threads, this can be static-global.
 * (session.h does not include tempo.h so making this
 *  a Session member variable is tricky.)
 */
static vector<TempoMap::BBTPoint> _click_points;

void
Session::add_click (samplepos_t pos, bool emphasis)
{
	if (emphasis) {
		if (click_emphasis_data && Config->get_use_click_emphasis () == true) {
			clicks.push_back (new Click (pos, click_emphasis_length, click_emphasis_data));
		} else if (click_data && Config->get_use_click_emphasis () == false) {
			clicks.push_back (new Click (pos, click_length, click_data));
		}
	} else if (click_data) {
		clicks.push_back (new Click (pos, click_length, click_data));
	}
}

void
Session::click (samplepos_t cycle_start, samplecnt_t nframes)
{
	if (_click_io == 0) {
		return;
	}

	/* transport_frame is audible-frame (what you hear,
	 * incl output latency). So internally we're ahead,
	 * we need to prepare frames that the user will hear
	 * in "output latency's" worth of time.
	 */
	samplecnt_t offset = _click_io->connected_latency (true);

	Glib::Threads::RWLock::WriterLock clickm (click_lock, Glib::Threads::TRY_LOCK);

	/* how far have we moved since the last time the clicks got cleared */
	const samplecnt_t click_distance = cycle_start + offset - _clicks_cleared;

	if (!clickm.locked() || !_clicking || click_data == 0 || ((click_distance + nframes) < 0)) {
		_click_io->silence (nframes);
		return;
	}

	if (_click_rec_only && !actively_recording()) {
		return;
	}

	/* range to check for clicks */
	samplepos_t start = cycle_start + offset;
	/* correct start, potentially */
	start = max (start, (samplepos_t) 0);

	samplecnt_t remain = nframes;

	while (remain > 0) {
		samplecnt_t move = remain;

		Location* loop_location = get_play_loop () ? locations()->auto_loop_location () : NULL;
		if (loop_location) {
			const samplepos_t loop_start = loop_location->start ();
			const samplepos_t loop_end = loop_location->end ();
			if (start >= loop_end) {
				samplecnt_t off = (start - loop_end) % (loop_end - loop_start);
				start = loop_start + off;
				move = std::min (remain, loop_end - start);
			} else if (start + move >= loop_end) {
				move = std::min (remain, loop_end - start);
			}
			if (move == 0) {
				start = loop_start;
				const samplecnt_t looplen = loop_end - loop_start;
				move = std::min (remain, looplen);
			}
		}

		const samplepos_t end = start + move;

		_click_points.clear ();
		_tempo_map->get_grid (_click_points, start, end);

		if (distance (_click_points.begin(), _click_points.end()) == 0) {
			start += move;
			remain -= move;
			continue;
		}

		for (vector<TempoMap::BBTPoint>::iterator i = _click_points.begin(); i != _click_points.end(); ++i) {
			assert ((*i).sample >= start && (*i).sample < end);
			switch ((*i).beat) {
				case 1:
					add_click ((*i).sample, true);
					break;
				default:
					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
						add_click ((*i).sample, false);
					}
					break;
			}
		}

		start += move;
		remain -= move;
	}

	clickm.release ();
	run_click (cycle_start, nframes);
}

void
Session::run_click (samplepos_t start, samplepos_t nframes)
{
	Glib::Threads::RWLock::ReaderLock clickm (click_lock, Glib::Threads::TRY_LOCK);

	/* align to output */
	start += _click_io->connected_latency (true);

	if (!clickm.locked() || click_data == 0) {
		_click_io->silence (nframes);
		return;
	}

	Sample *buf;
	BufferSet& bufs = get_scratch_buffers(ChanCount(DataType::AUDIO, 1));
	buf = bufs.get_audio(0).data();
	memset (buf, 0, sizeof (Sample) * nframes);

	/* given a large output latency, `start' can be offset by by > 1 cycle.
	 * and needs to be mapped back into the loop-range */
	Location* loop_location = get_play_loop () ? locations()->auto_loop_location () : NULL;
	if (_count_in_samples > 0) {
		loop_location = NULL;
	}
	bool crossloop = false;
	samplecnt_t span = nframes;
	if (loop_location) {
		const samplepos_t loop_start = loop_location->start ();
		const samplepos_t loop_end = loop_location->end ();
		if (start >= loop_end) {
			samplecnt_t off = (start - loop_end) % (loop_end - loop_start);
			start = loop_start + off;
			span = std::min (nframes, loop_end - start);
		} else if (start + nframes >= loop_end) {
			crossloop = true;
			span = std::min (nframes, loop_end - start);
		}
	}

	for (list<Click*>::iterator i = clicks.begin(); i != clicks.end(); ) {

		Click *clk = *i;

		if (loop_location) {
			const samplepos_t loop_start = loop_location->start ();
			const samplepos_t loop_end = loop_location->end ();
			/* remove any clicks that are outside loop location, and not currently playing */
			if ((clk->start < loop_start || clk->start >= loop_end) && clk->offset == 0) {
				delete clk;
				i = clicks.erase (i);
				continue;
			}
		}

		samplecnt_t internal_offset;

		if (clk->start <= start || clk->offset > 0) {
			internal_offset = 0;
		} else if (clk->start < start + span) {
			/* queue click at offset in current cycle */
			internal_offset = clk->start - start;
		} else if (crossloop) {
			/* When loop wraps around in current cycle, take
			 * clicks at loop-start into account */
			const samplepos_t loop_start = loop_location->start ();
			internal_offset = clk->start - loop_start + span;
		} else if (_count_in_samples > 0) {
			++i;
			continue;
		} else {
			/* this can happen when locating
			 * with an active click */
			delete clk;
			i = clicks.erase (i);
			continue;
		}

		if (internal_offset >= nframes) {
			break;
		}

		samplecnt_t copy = min (clk->duration - clk->offset, nframes - internal_offset);
		memcpy (buf + internal_offset, &clk->data[clk->offset], copy * sizeof (Sample));
		clk->offset += copy;

		if (clk->offset >= clk->duration) {
			delete clk;
			i = clicks.erase (i);
		} else {
			++i;
		}
	}

	_click_gain->run (bufs, 0, 0, 1.0, nframes, false);
	_click_io->copy_to_outputs (bufs, DataType::AUDIO, nframes, 0);
}

void
Session::setup_click_sounds (Sample** data, Sample const * default_data, samplecnt_t* length, samplecnt_t default_length, string const & path)
{
	if (*data != default_data) {
		delete[] *data;
		*data = 0;
	}

	if (path.empty ()) {

		*data = const_cast<Sample*> (default_data);
		*length = default_length;

	} else {

		SF_INFO info;
		SNDFILE* sndfile;

		info.format = 0;
		if ((sndfile = sf_open (path.c_str(), SFM_READ, &info)) == 0) {
			char errbuf[256];
			sf_error_str (0, errbuf, sizeof (errbuf) - 1);
			warning << string_compose (_("cannot open click soundfile %1 (%2)"), path, errbuf) << endmsg;
			_clicking = false;
			return;
		}

		/* read the (possibly multi-channel) click data into a temporary buffer */

		sf_count_t const samples = info.frames * info.channels;

		Sample* tmp = new Sample[samples];

		if (sf_readf_float (sndfile, tmp, info.frames) != info.frames) {

			warning << _("cannot read data from click soundfile") << endmsg;
			*data = 0;
			_clicking = false;

		} else {

			*data = new Sample[info.frames];
			*length = info.frames;

			/* mix down to mono */

			for (int i = 0; i < info.frames; ++i) {
				(*data)[i] = 0;
				for (int j = 0; j < info.channels; ++j) {
					(*data)[i] = tmp[i * info.channels + j];
				}
				(*data)[i] /= info.channels;
			}
		}

		delete[] tmp;
		sf_close (sndfile);
	}
}

void
Session::setup_click_sounds (int which)
{
	_click_points.reserve (8);
	clear_clicks ();

	if (which == 0 || which == 1) {
		setup_click_sounds (
			&click_data,
			default_click,
			&click_length,
			default_click_length,
			Config->get_click_sound ()
			);
	}

	if (which == 0 || which == -1) {
		setup_click_sounds (
			&click_emphasis_data,
			default_click_emphasis,
			&click_emphasis_length,
			default_click_emphasis_length,
			Config->get_click_emphasis_sound ()
			);
	}
}

void
Session::clear_clicks ()
{
	Glib::Threads::RWLock::WriterLock lm (click_lock);

	for (Clicks::iterator i = clicks.begin(); i != clicks.end(); ++i) {
		delete *i;
	}

	clicks.clear ();
	_clicks_cleared = _transport_sample;
}