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