fixes for endemic (compiler?) issues with virtual inheritance of sigc::trackable...
[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 (nframes_t start, nframes_t nframes, nframes_t offset)
41 {
42         TempoMap::BBTPointList *points;
43         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
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         
86         delete points;
87
88         delete points;
89         
90   run_clicks:
91         
92         memset (buf, 0, sizeof (Sample) * nframes);
93
94         for (list<Click*>::iterator i = clicks.begin(); i != clicks.end(); ) {
95
96                 nframes_t copy;
97                 nframes_t internal_offset;
98                 Click *clk;
99                 list<Click*>::iterator next;
100
101                 clk = *i;
102                 next = i;
103                 ++next;
104         
105                 if (clk->start < start) {
106                         internal_offset = 0;
107                 } else {
108                         internal_offset = clk->start - start;
109                 }
110
111                 if (nframes < internal_offset) {
112                          /* we've just located or something.. 
113                             effectively going backwards.
114                             lets get the flock out of here */
115                         break;
116                 }
117
118                 copy = min (clk->duration - clk->offset, nframes - internal_offset);
119
120                 memcpy (buf + internal_offset, &clk->data[clk->offset], copy * sizeof (Sample));
121
122                 clk->offset += copy;
123
124                 if (clk->offset >= clk->duration) {
125                         delete clk;
126                         clicks.erase (i);
127                 }
128
129
130                 i = next;
131         }
132
133         _click_io->deliver_output (_passthru_buffers, 1, nframes, offset);
134 }
135
136 void
137 Session::setup_click_sounds (int which)
138 {
139         SNDFILE *sndfile;
140         SF_INFO info;
141
142         clear_clicks();
143
144         if ((which == 0 || which == 1)) {
145                 
146                 if (click_data && click_data != default_click) {
147                         delete [] click_data;
148                         click_data = 0;
149                 }
150
151                 string path = Config->get_click_emphasis_sound();
152
153                 if (path.empty()) {
154
155                         click_data = const_cast<Sample*> (default_click);
156                         click_length = default_click_length;
157
158                 } else {
159
160                         if ((sndfile = sf_open (path.c_str(), SFM_READ, &info)) == 0) {
161                                 char errbuf[256];
162                                 sf_error_str (0, errbuf, sizeof (errbuf) - 1);
163                                 warning << string_compose (_("cannot open click soundfile %1 (%2)"), path, errbuf) << endmsg;
164                                 _clicking = false;
165                                 return;
166                         }
167                         
168                         click_data = new Sample[info.frames];
169                         click_length = info.frames;
170                         
171                         if (sf_read_float (sndfile, click_data, info.frames) != info.frames) {
172                                 warning << _("cannot read data from click soundfile") << endmsg;                        
173                                 delete click_data;
174                                 click_data = 0;
175                                 _clicking = false;
176                         }
177                         
178                         sf_close (sndfile);
179
180                 }
181         }
182                 
183         if ((which == 0 || which == -1)) {
184
185                 if (click_emphasis_data && click_emphasis_data != default_click_emphasis) {
186                         delete [] click_emphasis_data;
187                         click_emphasis_data = 0;
188                 }
189
190                 string path = Config->get_click_emphasis_sound();
191
192                 if (path.empty()) {
193                         click_emphasis_data = const_cast<Sample*> (default_click_emphasis);
194                         click_emphasis_length = default_click_emphasis_length;
195                 } else {
196                         if ((sndfile = sf_open (path.c_str(), SFM_READ, &info)) == 0) {
197                                 char errbuf[256];
198                                 sf_error_str (0, errbuf, sizeof (errbuf) - 1);
199                                 warning << string_compose (_("cannot open click emphasis soundfile %1 (%2)"), path, errbuf) << endmsg;
200                                 return;
201                         }
202                         
203                         click_emphasis_data = new Sample[info.frames];
204                         click_emphasis_length = info.frames;
205                         
206                         if (sf_read_float (sndfile, click_emphasis_data, info.frames) != info.frames) {
207                                 warning << _("cannot read data from click emphasis soundfile") << endmsg;                       
208                                 delete click_emphasis_data;
209                                 click_emphasis_data = 0;
210                         }
211                         
212                         sf_close (sndfile);
213                 }
214         }
215 }               
216
217 void
218 Session::clear_clicks ()
219 {
220         Glib::RWLock::WriterLock lm (click_lock);
221
222         for (Clicks::iterator i = clicks.begin(); i != clicks.end(); ++i) {
223                 delete *i;
224         }
225
226         clicks.clear ();
227 }