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