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