fix crash when copy'ing latent plugins
[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/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"
31
32 #include <sndfile.h>
33
34 #include "pbd/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         vector<TempoMap::BBTPoint> points;
46         Sample *buf;
47         framecnt_t click_distance;
48
49         if (_click_io == 0) {
50                 return;
51         }
52
53         Glib::Threads::RWLock::WriterLock clickm (click_lock, Glib::Threads::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() || !_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, start, end);
75
76         if (distance (points.begin(), points.end()) == 0) {
77                 goto run_clicks;
78         }
79
80         for (vector<TempoMap::BBTPoint>::iterator i = points.begin(); i != points.end(); ++i) {
81                 switch ((*i).beat) {
82                 case 1:
83                         if (click_emphasis_data && Config->get_use_click_emphasis () == true) {
84                                 clicks.push_back (new Click ((*i).frame, click_emphasis_length, click_emphasis_data));
85                         } else if (click_data && Config->get_use_click_emphasis () == false) {
86                                 clicks.push_back (new Click ((*i).frame, click_length, click_data));
87                         }
88                         break;
89
90                 default:
91                         if (click_emphasis_data == 0 || (Config->get_use_click_emphasis () == false) || (click_emphasis_data && (*i).beat != 1)) {
92                                 clicks.push_back (new Click ((*i).frame, click_length, click_data));
93                         }
94                         break;
95                 }
96         }
97
98   run_clicks:
99         memset (buf, 0, sizeof (Sample) * nframes);
100
101         for (list<Click*>::iterator i = clicks.begin(); i != clicks.end(); ) {
102
103                 framecnt_t copy;
104                 framecnt_t internal_offset;
105                 Click *clk;
106
107                 clk = *i;
108
109                 if (clk->start < start) {
110                         internal_offset = 0;
111                 } else {
112                         internal_offset = clk->start - start;
113                 }
114
115                 if (nframes < internal_offset) {
116                          /* we've just located or something..
117                             effectively going backwards.
118                             lets get the flock out of here */
119                         break;
120                 }
121
122                 copy = min (clk->duration - clk->offset, nframes - internal_offset);
123
124                 memcpy (buf + internal_offset, &clk->data[clk->offset], copy * sizeof (Sample));
125
126                 clk->offset += copy;
127
128                 if (clk->offset >= clk->duration) {
129                         delete clk;
130                         i = clicks.erase (i);
131                 } else {
132                         ++i;
133                 }
134         }
135
136         _click_gain->run (bufs, 0, 0, 1.0, nframes, false);
137         _click_io->copy_to_outputs (bufs, DataType::AUDIO, nframes, 0);
138 }
139
140 void
141 Session::setup_click_sounds (Sample** data, Sample const * default_data, framecnt_t* length, framecnt_t default_length, string const & path)
142 {
143         if (*data != default_data) {
144                 delete[] *data;
145                 *data = 0;
146         }
147
148         if (path.empty ()) {
149
150                 *data = const_cast<Sample*> (default_data);
151                 *length = default_length;
152
153         } else {
154
155                 SF_INFO info;
156                 SNDFILE* sndfile;
157
158                 info.format = 0;
159                 if ((sndfile = sf_open (path.c_str(), SFM_READ, &info)) == 0) {
160                         char errbuf[256];
161                         sf_error_str (0, errbuf, sizeof (errbuf) - 1);
162                         warning << string_compose (_("cannot open click soundfile %1 (%2)"), path, errbuf) << endmsg;
163                         _clicking = false;
164                         return;
165                 }
166
167                 /* read the (possibly multi-channel) click data into a temporary buffer */
168
169                 sf_count_t const samples = info.frames * info.channels;
170
171                 Sample* tmp = new Sample[samples];
172
173                 if (sf_readf_float (sndfile, tmp, info.frames) != info.frames) {
174
175                         warning << _("cannot read data from click soundfile") << endmsg;
176                         *data = 0;
177                         _clicking = false;
178
179                 } else {
180
181                         *data = new Sample[info.frames];
182                         *length = info.frames;
183
184                         /* mix down to mono */
185
186                         for (int i = 0; i < info.frames; ++i) {
187                                 (*data)[i] = 0;
188                                 for (int j = 0; j < info.channels; ++j) {
189                                         (*data)[i] = tmp[i * info.channels + j];
190                                 }
191                                 (*data)[i] /= info.channels;
192                         }
193                 }
194
195                 delete[] tmp;
196                 sf_close (sndfile);
197         }
198 }
199
200 void
201 Session::setup_click_sounds (int which)
202 {
203         clear_clicks ();
204
205         if (which == 0 || which == 1) {
206                 setup_click_sounds (
207                         &click_data,
208                         default_click,
209                         &click_length,
210                         default_click_length,
211                         Config->get_click_sound ()
212                         );
213         }
214
215         if (which == 0 || which == -1) {
216                 setup_click_sounds (
217                         &click_emphasis_data,
218                         default_click_emphasis,
219                         &click_emphasis_length,
220                         default_click_emphasis_length,
221                         Config->get_click_emphasis_sound ()
222                         );
223         }
224 }
225
226 void
227 Session::clear_clicks ()
228 {
229         Glib::Threads::RWLock::WriterLock lm (click_lock);
230
231         for (Clicks::iterator i = clicks.begin(); i != clicks.end(); ++i) {
232                 delete *i;
233         }
234
235         clicks.clear ();
236         _clicks_cleared = _transport_frame;
237 }