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