2 Copyright (C) 20002 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/ardour.h>
24 #include <ardour/session.h>
25 #include <ardour/tempo.h>
26 #include <ardour/io.h>
27 #include <ardour/buffer_set.h>
34 using namespace ARDOUR;
37 Pool Session::Click::pool ("click", sizeof (Click), 128);
40 Session::click (nframes_t start, nframes_t nframes, nframes_t offset)
42 TempoMap::BBTPointList *points;
49 Glib::RWLock::WriterLock clickm (click_lock, Glib::TRY_LOCK);
51 if (!clickm.locked() || _transport_speed != 1.0 || !_clicking || click_data == 0) {
52 _click_io->silence (nframes, offset);
56 const nframes_t end = start + (nframes_t)floor(nframes * _transport_speed);
58 BufferSet& bufs = get_scratch_buffers(ChanCount(DataType::AUDIO, 1));
59 buf = bufs.get_audio(0).data();
60 points = _tempo_map->get_points (start, end);
66 if (points->empty()) {
71 for (TempoMap::BBTPointList::iterator i = points->begin(); i != points->end(); ++i) {
74 if (click_emphasis_data == 0 || (click_emphasis_data && (*i).beat != 1)) {
75 clicks.push_back (new Click ((*i).frame, click_length, click_data));
80 if (click_emphasis_data) {
81 clicks.push_back (new Click ((*i).frame, click_emphasis_length, click_emphasis_data));
88 memset (buf, 0, sizeof (Sample) * nframes);
90 for (list<Click*>::iterator i = clicks.begin(); i != clicks.end(); ) {
93 nframes_t internal_offset;
95 list<Click*>::iterator next;
101 if (clk->start < start) {
104 internal_offset = clk->start - start;
107 if (nframes < internal_offset) {
108 /* we've just located or something..
109 effectively going backwards.
110 lets get the flock out of here */
114 copy = min (clk->duration - clk->offset, nframes - internal_offset);
116 memcpy (buf + internal_offset, &clk->data[clk->offset], copy * sizeof (Sample));
120 if (clk->offset >= clk->duration) {
129 _click_io->deliver_output (bufs, start, end, nframes, offset);
133 Session::setup_click_sounds (int which)
140 if ((which == 0 || which == 1)) {
142 if (click_data && click_data != default_click) {
143 delete [] click_data;
147 string path = Config->get_click_sound();
151 click_data = const_cast<Sample*> (default_click);
152 click_length = default_click_length;
156 if ((sndfile = sf_open (path.c_str(), SFM_READ, &info)) == 0) {
158 sf_error_str (0, errbuf, sizeof (errbuf) - 1);
159 warning << string_compose (_("cannot open click soundfile %1 (%2)"), path, errbuf) << endmsg;
164 click_data = new Sample[info.frames];
165 click_length = info.frames;
167 if (sf_read_float (sndfile, click_data, info.frames) != info.frames) {
168 warning << _("cannot read data from click soundfile") << endmsg;
179 if ((which == 0 || which == -1)) {
181 if (click_emphasis_data && click_emphasis_data != default_click_emphasis) {
182 delete [] click_emphasis_data;
183 click_emphasis_data = 0;
186 string path = Config->get_click_emphasis_sound();
189 click_emphasis_data = const_cast<Sample*> (default_click_emphasis);
190 click_emphasis_length = default_click_emphasis_length;
192 if ((sndfile = sf_open (path.c_str(), SFM_READ, &info)) == 0) {
194 sf_error_str (0, errbuf, sizeof (errbuf) - 1);
195 warning << string_compose (_("cannot open click emphasis soundfile %1 (%2)"), path, errbuf) << endmsg;
199 click_emphasis_data = new Sample[info.frames];
200 click_emphasis_length = info.frames;
202 if (sf_read_float (sndfile, click_emphasis_data, info.frames) != info.frames) {
203 warning << _("cannot read data from click emphasis soundfile") << endmsg;
204 delete click_emphasis_data;
205 click_emphasis_data = 0;
214 Session::clear_clicks ()
216 Glib::RWLock::WriterLock lm (click_lock);
218 for (Clicks::iterator i = clicks.begin(); i != clicks.end(); ++i) {