new approach to tempo/meter: compute and store the entire map (every bar|beat point...
[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), 128);
40
41 void
42 Session::click (framepos_t start, framecnt_t nframes)
43 {
44         TempoMap::BBTPointList points;
45         Sample *buf;
46
47         if (_click_io == 0) {
48                 return;
49         }
50
51         Glib::RWLock::WriterLock clickm (click_lock, Glib::TRY_LOCK);
52
53         if (!clickm.locked() || _transport_speed != 1.0 || !_clicking || click_data == 0) {
54                 _click_io->silence (nframes);
55                 return;
56         }
57
58         const framepos_t end = start + nframes;
59
60         BufferSet& bufs = get_scratch_buffers(ChanCount(DataType::AUDIO, 1));
61         buf = bufs.get_audio(0).data();
62         _tempo_map->map (points, start, end);
63
64         if (points.empty()) {
65                 goto run_clicks;
66         }
67
68         for (TempoMap::BBTPointList::iterator i = points.begin(); i != points.end(); ++i) {
69                 switch ((*i).type) {
70                 case TempoMap::Beat:
71                         if (click_emphasis_data == 0 || (click_emphasis_data && (*i).beat != 1)) {
72                                 clicks.push_back (new Click ((*i).frame, click_length, click_data));
73                         }
74                         break;
75
76                 case TempoMap::Bar:
77                         if (click_emphasis_data) {
78                                 clicks.push_back (new Click ((*i).frame, click_emphasis_length, click_emphasis_data));
79                         }
80                         break;
81                 }
82         }
83
84   run_clicks:
85         memset (buf, 0, sizeof (Sample) * nframes);
86
87         for (list<Click*>::iterator i = clicks.begin(); i != clicks.end(); ) {
88
89                 framecnt_t copy;
90                 framecnt_t internal_offset;
91                 Click *clk;
92                 list<Click*>::iterator next;
93
94                 clk = *i;
95                 next = i;
96                 ++next;
97
98                 if (clk->start < start) {
99                         internal_offset = 0;
100                 } else {
101                         internal_offset = clk->start - start;
102                 }
103
104                 if (nframes < internal_offset) {
105                          /* we've just located or something..
106                             effectively going backwards.
107                             lets get the flock out of here */
108                         break;
109                 }
110
111                 copy = min (clk->duration - clk->offset, nframes - internal_offset);
112
113                 memcpy (buf + internal_offset, &clk->data[clk->offset], copy * sizeof (Sample));
114
115                 clk->offset += copy;
116
117                 if (clk->offset >= clk->duration) {
118                         delete clk;
119                         clicks.erase (i);
120                 }
121
122
123                 i = next;
124         }
125
126         _click_io->copy_to_outputs (bufs, DataType::AUDIO, nframes, 0);
127 }
128
129 void
130 Session::setup_click_sounds (Sample** data, Sample const * default_data, framecnt_t* length, framecnt_t default_length, string const & path)
131 {
132         if (*data != default_data) {
133                 delete[] *data;
134                 *data = 0;
135         }
136
137         if (path.empty ()) {
138
139                 *data = const_cast<Sample*> (default_data);
140                 *length = default_length;
141
142         } else {
143
144                 SF_INFO info;
145                 SNDFILE* sndfile;
146
147                 info.format = 0;
148                 if ((sndfile = sf_open (path.c_str(), SFM_READ, &info)) == 0) {
149                         char errbuf[256];
150                         sf_error_str (0, errbuf, sizeof (errbuf) - 1);
151                         warning << string_compose (_("cannot open click soundfile %1 (%2)"), path, errbuf) << endmsg;
152                         _clicking = false;
153                         return;
154                 }
155
156                 /* read the (possibly multi-channel) click data into a temporary buffer */
157
158                 sf_count_t const samples = info.frames * info.channels;
159
160                 Sample* tmp = new Sample[samples];
161
162                 if (sf_readf_float (sndfile, tmp, info.frames) != info.frames) {
163
164                         warning << _("cannot read data from click soundfile") << endmsg;
165                         *data = 0;
166                         _clicking = false;
167
168                 } else {
169
170                         *data = new Sample[info.frames];
171                         *length = info.frames;
172
173                         /* mix down to mono */
174
175                         for (int i = 0; i < info.frames; ++i) {
176                                 (*data)[i] = 0;
177                                 for (int j = 0; j < info.channels; ++j) {
178                                         (*data)[i] = tmp[i * info.channels + j];
179                                 }
180                                 (*data)[i] /= info.channels;
181                         }
182                 }
183
184                 delete[] tmp;
185                 sf_close (sndfile);
186         }
187 }
188
189 void
190 Session::setup_click_sounds (int which)
191 {
192         clear_clicks ();
193
194         if (which == 0 || which == 1) {
195                 setup_click_sounds (
196                         &click_data,
197                         default_click,
198                         &click_length,
199                         default_click_length,
200                         Config->get_click_sound ()
201                         );
202         }
203
204         if (which == 0 || which == -1) {
205                 setup_click_sounds (
206                         &click_emphasis_data,
207                         default_click_emphasis,
208                         &click_emphasis_length,
209                         default_click_emphasis_length,
210                         Config->get_click_emphasis_sound ()
211                         );
212         }
213 }
214
215 void
216 Session::clear_clicks ()
217 {
218         Glib::RWLock::WriterLock lm (click_lock);
219
220         for (Clicks::iterator i = clicks.begin(); i != clicks.end(); ++i) {
221                 delete *i;
222         }
223
224         clicks.clear ();
225 }