2 Copyright (C) 2002 Paul Davis
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.
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.
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.
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"
37 using namespace ARDOUR;
40 Pool Click::pool ("click", sizeof (Click), 1024);
43 Session::click (framepos_t start, framecnt_t nframes)
45 TempoMap::BBTPointList::const_iterator points_begin;
46 TempoMap::BBTPointList::const_iterator points_end;
48 framecnt_t click_distance;
54 Glib::Threads::RWLock::WriterLock clickm (click_lock, Glib::Threads::TRY_LOCK);
56 /* how far have we moved since the last time the clicks got cleared
59 click_distance = start - _clicks_cleared;
61 if (!clickm.locked() || !_clicking || click_data == 0 || ((click_distance + nframes) < _worst_track_latency)) {
62 _click_io->silence (nframes);
66 start -= _worst_track_latency;
67 /* start could be negative at this point */
68 const framepos_t end = start + nframes;
69 /* correct start, potentially */
70 start = max (start, (framepos_t) 0);
72 BufferSet& bufs = get_scratch_buffers(ChanCount(DataType::AUDIO, 1));
73 buf = bufs.get_audio(0).data();
75 _tempo_map->get_grid (points_begin, points_end, start, end);
77 if (distance (points_begin, points_end) == 0) {
81 for (TempoMap::BBTPointList::const_iterator i = points_begin; i != points_end; ++i) {
84 if (click_emphasis_data && Config->get_use_click_emphasis () == true) {
85 clicks.push_back (new Click ((*i).frame, click_emphasis_length, click_emphasis_data));
86 } else if (click_data && Config->get_use_click_emphasis () == false) {
87 clicks.push_back (new Click ((*i).frame, click_length, click_data));
92 if (click_emphasis_data == 0 || (Config->get_use_click_emphasis () == false) || (click_emphasis_data && (*i).beat != 1)) {
93 clicks.push_back (new Click ((*i).frame, click_length, click_data));
100 memset (buf, 0, sizeof (Sample) * nframes);
102 for (list<Click*>::iterator i = clicks.begin(); i != clicks.end(); ) {
105 framecnt_t internal_offset;
110 if (clk->start < start) {
113 internal_offset = clk->start - start;
116 if (nframes < internal_offset) {
117 /* we've just located or something..
118 effectively going backwards.
119 lets get the flock out of here */
123 copy = min (clk->duration - clk->offset, nframes - internal_offset);
125 memcpy (buf + internal_offset, &clk->data[clk->offset], copy * sizeof (Sample));
129 if (clk->offset >= clk->duration) {
131 i = clicks.erase (i);
137 _click_gain->run (bufs, 0, 0, nframes, false);
138 _click_io->copy_to_outputs (bufs, DataType::AUDIO, nframes, 0);
142 Session::setup_click_sounds (Sample** data, Sample const * default_data, framecnt_t* length, framecnt_t default_length, string const & path)
144 if (*data != default_data) {
151 *data = const_cast<Sample*> (default_data);
152 *length = default_length;
160 if ((sndfile = sf_open (path.c_str(), SFM_READ, &info)) == 0) {
162 sf_error_str (0, errbuf, sizeof (errbuf) - 1);
163 warning << string_compose (_("cannot open click soundfile %1 (%2)"), path, errbuf) << endmsg;
168 /* read the (possibly multi-channel) click data into a temporary buffer */
170 sf_count_t const samples = info.frames * info.channels;
172 Sample* tmp = new Sample[samples];
174 if (sf_readf_float (sndfile, tmp, info.frames) != info.frames) {
176 warning << _("cannot read data from click soundfile") << endmsg;
182 *data = new Sample[info.frames];
183 *length = info.frames;
185 /* mix down to mono */
187 for (int i = 0; i < info.frames; ++i) {
189 for (int j = 0; j < info.channels; ++j) {
190 (*data)[i] = tmp[i * info.channels + j];
192 (*data)[i] /= info.channels;
202 Session::setup_click_sounds (int which)
206 if (which == 0 || which == 1) {
211 default_click_length,
212 Config->get_click_sound ()
216 if (which == 0 || which == -1) {
218 &click_emphasis_data,
219 default_click_emphasis,
220 &click_emphasis_length,
221 default_click_emphasis_length,
222 Config->get_click_emphasis_sound ()
228 Session::clear_clicks ()
230 Glib::Threads::RWLock::WriterLock lm (click_lock);
232 for (Clicks::iterator i = clicks.begin(); i != clicks.end(); ++i) {
237 _clicks_cleared = _transport_frame;