c9777500a26a8aaea1f09331236b42690bc17074
[ardour.git] / libs / ardour / session_click.cc
1 /*
2     Copyright (C) 20002 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     $Id$
19 */
20
21 #include <list>
22 #include <cerrno>
23
24 #include <ardour/ardour.h>
25 #include <ardour/session.h>
26 #include <ardour/tempo.h>
27 #include <ardour/io.h>
28
29 #include <sndfile.h>
30
31 #include "i18n.h"
32
33 using namespace std;
34 using namespace ARDOUR;
35
36 Pool Session::Click::pool ("click", sizeof (Click), 128);
37
38 void
39 Session::click (jack_nframes_t start, jack_nframes_t nframes, jack_nframes_t offset)
40 {
41         TempoMap::BBTPointList *points;
42         jack_nframes_t end;
43         Sample *buf;
44         vector<Sample*> bufs;
45
46         if (_click_io == 0) {
47                 return;
48         }
49
50         TentativeRWLockMonitor clickm (click_lock, true, __LINE__, __FILE__);
51         
52         if (!clickm.locked() || _transport_speed != 1.0 || !_clicking || click_data == 0) {
53                 _click_io->silence (nframes, offset);
54                 return;
55         } 
56
57         end = start + nframes;
58
59         buf = _passthru_buffers[0];
60         points = _tempo_map->get_points (start, end);
61
62         if (points == 0) {
63                 goto run_clicks;
64         }
65
66         if (points->empty()) {
67                 delete points;
68                 goto run_clicks;
69         }
70
71         for (TempoMap::BBTPointList::iterator i = points->begin(); i != points->end(); ++i) {
72                 switch ((*i).type) {
73                 case TempoMap::Beat:
74                         if (click_emphasis_data == 0 || (click_emphasis_data && (*i).beat != 1)) {
75                                 clicks.push_back (new Click ((*i).frame, click_length, click_data));
76                         }
77                         break;
78
79                 case TempoMap::Bar:
80                         if (click_emphasis_data) {
81                                 clicks.push_back (new Click ((*i).frame, click_emphasis_length, click_emphasis_data));
82                         } 
83                         break;
84                 }
85         }
86
87   run_clicks:
88         memset (buf, 0, sizeof (Sample) * nframes);
89
90         for (list<Click*>::iterator i = clicks.begin(); i != clicks.end(); ) {
91
92                 jack_nframes_t copy;
93                 jack_nframes_t internal_offset;
94                 Click *clk;
95                 list<Click*>::iterator next;
96
97                 clk = *i;
98                 next = i;
99                 ++next;
100         
101                 if (clk->start < start) {
102                         internal_offset = 0;
103                 } else {
104                         internal_offset = clk->start - start;
105                 }
106
107                 if (nframes < internal_offset) {
108                          /* we've just located or something.. 
109                             effectively going backwards.
110                             lets get the flock out of here */
111                         break;
112                 }
113
114                 copy = min (clk->duration - clk->offset, nframes - internal_offset);
115
116                 memcpy (buf + internal_offset, &clk->data[clk->offset], copy * sizeof (Sample));
117
118                 clk->offset += copy;
119
120                 if (clk->offset >= clk->duration) {
121                         delete clk;
122                         clicks.erase (i);
123                 }
124
125
126                 i = next;
127         }
128
129         _click_io->deliver_output (_passthru_buffers, 1, nframes, offset);
130 }
131
132 void
133 Session::setup_click_sounds (int which)
134 {
135         SNDFILE *sndfile;
136         SF_INFO info;
137
138         clear_clicks();
139
140         if ((which == 0 || which == 1)) {
141                 
142                 if (click_data && click_data != default_click) {
143                         delete [] click_data;
144                         click_data = 0;
145                 }
146
147                 if (click_sound.length() == 0) {
148
149                         click_data = const_cast<Sample*> (default_click);
150                         click_length = default_click_length;
151
152                 } else {
153
154                         if ((sndfile = sf_open (click_sound.c_str(), SFM_READ, &info)) == 0) {
155                                 char errbuf[256];
156                                 sf_error_str (0, errbuf, sizeof (errbuf) - 1);
157                                 warning << string_compose (_("cannot open click soundfile %1 (%2)"), click_sound, errbuf) << endmsg;
158                                 _clicking = false;
159                                 return;
160                         }
161                         
162                         click_data = new Sample[info.frames];
163                         click_length = info.frames;
164                         
165                         if (sf_read_float (sndfile, click_data, info.frames) != info.frames) {
166                                 warning << _("cannot read data from click soundfile") << endmsg;                        
167                                 delete click_data;
168                                 click_data = 0;
169                                 _clicking = false;
170                         }
171                         
172                         sf_close (sndfile);
173
174                 }
175         }
176                 
177         if ((which == 0 || which == -1)) {
178
179                 if (click_emphasis_data && click_emphasis_data != default_click_emphasis) {
180                         delete [] click_emphasis_data;
181                         click_emphasis_data = 0;
182                 }
183
184                 if (click_emphasis_sound.length() == 0) {
185                         click_emphasis_data = const_cast<Sample*> (default_click_emphasis);
186                         click_emphasis_length = default_click_emphasis_length;
187                 } else {
188                         if ((sndfile = sf_open (click_emphasis_sound.c_str(), SFM_READ, &info)) == 0) {
189                                 char errbuf[256];
190                                 sf_error_str (0, errbuf, sizeof (errbuf) - 1);
191                                 warning << string_compose (_("cannot open click emphasis soundfile %1 (%2)"), click_emphasis_sound, errbuf) << endmsg;
192                                 return;
193                         }
194                         
195                         click_emphasis_data = new Sample[info.frames];
196                         click_emphasis_length = info.frames;
197                         
198                         if (sf_read_float (sndfile, click_emphasis_data, info.frames) != info.frames) {
199                                 warning << _("cannot read data from click emphasis soundfile") << endmsg;                       
200                                 delete click_emphasis_data;
201                                 click_emphasis_data = 0;
202                         }
203                         
204                         sf_close (sndfile);
205                 }
206         }
207 }               
208
209 void
210 Session::clear_clicks ()
211 {
212         RWLockMonitor lm (click_lock, true, __LINE__, __FILE__);
213
214         for (Clicks::iterator i = clicks.begin(); i != clicks.end(); ++i) {
215                 delete *i;
216         }
217
218         clicks.clear ();
219 }
220
221 void
222 Session::set_click_sound (string path)
223 {
224         if (path != click_sound) {
225                 click_sound = path;
226                 setup_click_sounds (1);
227         }
228 }
229
230 void
231 Session::set_click_emphasis_sound (string path)
232 {
233         if (path != click_emphasis_sound) {
234                 click_emphasis_sound = path;
235                 setup_click_sounds (-1);
236         }
237 }
238
239 void
240 Session::set_clicking (bool yn)
241 {
242         if (click_requested != yn) {
243                 click_requested = yn;
244                 
245                 if (yn) {
246                         if (_click_io && click_data) {
247                                 _clicking = true;
248                         }
249                 } else {
250                         _clicking = false;
251                 }
252
253                  ControlChanged (Clicking); /* EMIT SIGNAL */
254         }
255 }
256
257 bool
258 Session::get_clicking () const
259 {
260         return click_requested;
261 }
262