250adfe4cc0c6e94f706e1657b3cc9501fab32d4
[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         if (_transport_speed != 1.0 || !_clicking || click_data == 0) {
51                 _click_io->silence (nframes, offset);
52                 return;
53         } 
54
55         end = start + nframes;
56
57         buf = _passthru_buffers[0];
58         points = _tempo_map->get_points (start, end);
59
60         if (points == 0) {
61                 goto run_clicks;
62         }
63
64         if (points->empty()) {
65                 delete points;
66                 goto run_clicks;
67         }
68
69         for (TempoMap::BBTPointList::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                 jack_nframes_t copy;
91                 jack_nframes_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->deliver_output (_passthru_buffers, 1, nframes, offset);
128 }
129
130 void
131 Session::setup_click_sounds (int which)
132 {
133         SNDFILE *sndfile;
134         SF_INFO info;
135
136         clear_clicks();
137
138         if ((which == 0 || which == 1)) {
139                 
140                 if (click_data && click_data != default_click) {
141                         delete [] click_data;
142                         click_data = 0;
143                 }
144
145                 if (click_sound.length() == 0) {
146
147                         click_data = const_cast<Sample*> (default_click);
148                         click_length = default_click_length;
149
150                 } else {
151
152                         if ((sndfile = sf_open (click_sound.c_str(), SFM_READ, &info)) == 0) {
153                                 char errbuf[256];
154                                 sf_error_str (0, errbuf, sizeof (errbuf) - 1);
155                                 warning << string_compose (_("cannot open click soundfile %1 (%2)"), click_sound, errbuf) << endmsg;
156                                 _clicking = false;
157                                 return;
158                         }
159                         
160                         click_data = new Sample[info.frames];
161                         click_length = info.frames;
162                         
163                         if (sf_read_float (sndfile, click_data, info.frames) != info.frames) {
164                                 warning << _("cannot read data from click soundfile") << endmsg;                        
165                                 delete click_data;
166                                 click_data = 0;
167                                 _clicking = false;
168                         }
169                         
170                         sf_close (sndfile);
171
172                 }
173         }
174                 
175         if ((which == 0 || which == -1)) {
176
177                 if (click_emphasis_data && click_emphasis_data != default_click_emphasis) {
178                         delete [] click_emphasis_data;
179                         click_emphasis_data = 0;
180                 }
181
182                 if (click_emphasis_sound.length() == 0) {
183                         click_emphasis_data = const_cast<Sample*> (default_click_emphasis);
184                         click_emphasis_length = default_click_emphasis_length;
185                 } else {
186                         if ((sndfile = sf_open (click_emphasis_sound.c_str(), SFM_READ, &info)) == 0) {
187                                 char errbuf[256];
188                                 sf_error_str (0, errbuf, sizeof (errbuf) - 1);
189                                 warning << string_compose (_("cannot open click emphasis soundfile %1 (%2)"), click_emphasis_sound, errbuf) << endmsg;
190                                 return;
191                         }
192                         
193                         click_emphasis_data = new Sample[info.frames];
194                         click_emphasis_length = info.frames;
195                         
196                         if (sf_read_float (sndfile, click_emphasis_data, info.frames) != info.frames) {
197                                 warning << _("cannot read data from click emphasis soundfile") << endmsg;                       
198                                 delete click_emphasis_data;
199                                 click_emphasis_data = 0;
200                         }
201                         
202                         sf_close (sndfile);
203                 }
204         }
205 }               
206
207 void
208 Session::clear_clicks ()
209 {
210         LockMonitor lm (route_lock, __LINE__, __FILE__);
211
212         for (Clicks::iterator i = clicks.begin(); i != clicks.end(); ++i) {
213                 delete *i;
214         }
215
216         clicks.clear ();
217 }
218
219 void
220 Session::set_click_sound (string path)
221 {
222         if (path != click_sound) {
223                 click_sound = path;
224                 setup_click_sounds (1);
225         }
226 }
227
228 void
229 Session::set_click_emphasis_sound (string path)
230 {
231         if (path != click_emphasis_sound) {
232                 click_emphasis_sound = path;
233                 setup_click_sounds (-1);
234         }
235 }
236
237 void
238 Session::set_clicking (bool yn)
239 {
240         if (click_requested != yn) {
241                 click_requested = yn;
242                 
243                 if (yn) {
244                         if (_click_io && click_data) {
245                                 _clicking = true;
246                         }
247                 } else {
248                         _clicking = false;
249                 }
250
251                  ControlChanged (Clicking); /* EMIT SIGNAL */
252         }
253 }
254
255 bool
256 Session::get_clicking () const
257 {
258         return click_requested;
259 }
260