remove debug output; increase size of click pool, to cover long latency settings...
[ardour.git] / libs / ardour / session_click.cc
1 /*
2     Copyright (C) 2002 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #include <list>
21 #include <cerrno>
22
23 #include "ardour/ardour.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
31 #include <sndfile.h>
32
33 #include "i18n.h"
34
35 using namespace std;
36 using namespace ARDOUR;
37 using namespace PBD;
38
39 Pool Click::pool ("click", sizeof (Click), 1024);
40
41 void
42 Session::click (framepos_t start, framecnt_t nframes)
43 {
44         TempoMap::BBTPointList::const_iterator points_begin;
45         TempoMap::BBTPointList::const_iterator points_end;
46         Sample *buf;
47         framecnt_t click_distance;
48
49         if (_click_io == 0) {
50                 return;
51         }
52
53         Glib::RWLock::WriterLock clickm (click_lock, Glib::TRY_LOCK);
54
55         /* how far have we moved since the last time the clicks got cleared
56          */
57
58         click_distance = start - _clicks_cleared;
59
60         if (!clickm.locked() || _transport_speed != 1.0 || !_clicking || click_data == 0 || ((click_distance + nframes) < _worst_track_latency)) {
61                 _click_io->silence (nframes);
62                 return;
63         }
64
65         start -= _worst_track_latency;
66         /* start could be negative at this point */
67         const framepos_t end = start + nframes;
68         /* correct start, potentially */
69         start = max (start, (framepos_t) 0);
70
71         BufferSet& bufs = get_scratch_buffers(ChanCount(DataType::AUDIO, 1));
72         buf = bufs.get_audio(0).data();
73
74         _tempo_map->get_grid (points_begin, points_end, start, end);
75
76         if (distance (points_begin, points_end) == 0) {
77                 goto run_clicks;
78         }
79
80         for (TempoMap::BBTPointList::const_iterator i = points_begin; i != points_end; ++i) {
81                 switch ((*i).beat) {
82                 case 1:
83                         if (click_emphasis_data) {
84                                 clicks.push_back (new Click ((*i).frame, click_emphasis_length, click_emphasis_data));
85                         }
86                         break;
87
88                 default:
89                         if (click_emphasis_data == 0 || (click_emphasis_data && (*i).beat != 1)) {
90                                 clicks.push_back (new Click ((*i).frame, click_length, click_data));
91                         }
92                         break;
93                 }
94         }
95
96   run_clicks:
97         memset (buf, 0, sizeof (Sample) * nframes);
98
99         for (list<Click*>::iterator i = clicks.begin(); i != clicks.end(); ) {
100
101                 framecnt_t copy;
102                 framecnt_t internal_offset;
103                 Click *clk;
104
105                 clk = *i;
106
107                 if (clk->start < start) {
108                         internal_offset = 0;
109                 } else {
110                         internal_offset = clk->start - start;
111                 }
112
113                 if (nframes < internal_offset) {
114                          /* we've just located or something..
115                             effectively going backwards.
116                             lets get the flock out of here */
117                         break;
118                 }
119
120                 copy = min (clk->duration - clk->offset, nframes - internal_offset);
121
122                 memcpy (buf + internal_offset, &clk->data[clk->offset], copy * sizeof (Sample));
123
124                 clk->offset += copy;
125
126                 if (clk->offset >= clk->duration) {
127                         delete clk;
128                         i = clicks.erase (i);
129                 } else {
130                         ++i;
131                 }
132         }
133
134         _click_io->copy_to_outputs (bufs, DataType::AUDIO, nframes, 0);
135 }
136
137 void
138 Session::setup_click_sounds (Sample** data, Sample const * default_data, framecnt_t* length, framecnt_t default_length, string const & path)
139 {
140         if (*data != default_data) {
141                 delete[] *data;
142                 *data = 0;
143         }
144
145         if (path.empty ()) {
146
147                 *data = const_cast<Sample*> (default_data);
148                 *length = default_length;
149
150         } else {
151
152                 SF_INFO info;
153                 SNDFILE* sndfile;
154
155                 info.format = 0;
156                 if ((sndfile = sf_open (path.c_str(), SFM_READ, &info)) == 0) {
157                         char errbuf[256];
158                         sf_error_str (0, errbuf, sizeof (errbuf) - 1);
159                         warning << string_compose (_("cannot open click soundfile %1 (%2)"), path, errbuf) << endmsg;
160                         _clicking = false;
161                         return;
162                 }
163
164                 /* read the (possibly multi-channel) click data into a temporary buffer */
165
166                 sf_count_t const samples = info.frames * info.channels;
167
168                 Sample* tmp = new Sample[samples];
169
170                 if (sf_readf_float (sndfile, tmp, info.frames) != info.frames) {
171
172                         warning << _("cannot read data from click soundfile") << endmsg;
173                         *data = 0;
174                         _clicking = false;
175
176                 } else {
177
178                         *data = new Sample[info.frames];
179                         *length = info.frames;
180
181                         /* mix down to mono */
182
183                         for (int i = 0; i < info.frames; ++i) {
184                                 (*data)[i] = 0;
185                                 for (int j = 0; j < info.channels; ++j) {
186                                         (*data)[i] = tmp[i * info.channels + j];
187                                 }
188                                 (*data)[i] /= info.channels;
189                         }
190                 }
191
192                 delete[] tmp;
193                 sf_close (sndfile);
194         }
195 }
196
197 void
198 Session::setup_click_sounds (int which)
199 {
200         clear_clicks ();
201
202         if (which == 0 || which == 1) {
203                 setup_click_sounds (
204                         &click_data,
205                         default_click,
206                         &click_length,
207                         default_click_length,
208                         Config->get_click_sound ()
209                         );
210         }
211
212         if (which == 0 || which == -1) {
213                 setup_click_sounds (
214                         &click_emphasis_data,
215                         default_click_emphasis,
216                         &click_emphasis_length,
217                         default_click_emphasis_length,
218                         Config->get_click_emphasis_sound ()
219                         );
220         }
221 }
222
223 void
224 Session::clear_clicks ()
225 {
226         Glib::RWLock::WriterLock lm (click_lock);
227
228         for (Clicks::iterator i = clicks.begin(); i != clicks.end(); ++i) {
229                 delete *i;
230         }
231
232         clicks.clear ();
233         _clicks_cleared = _transport_frame;
234 }