Remove unused header.
[ardour.git] / libs / ardour / session_click.cc
1 /*
2     Copyright (C) 2002 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 */
19
20 #include <list>
21 #include <cerrno>
22
23 #include "ardour/ardour.h"
24 #include "ardour/audio_buffer.h"
25 #include "ardour/buffer_set.h"
26 #include "ardour/click.h"
27 #include "ardour/io.h"
28 #include "ardour/session.h"
29 #include "ardour/tempo.h"
30
31 #include <sndfile.h>
32
33 #include "i18n.h"
34
35 using namespace std;
36 using namespace ARDOUR;
37 using namespace PBD;
38
39 Pool Click::pool ("click", sizeof (Click), 128);
40
41 void
42 Session::click (framepos_t start, framecnt_t nframes)
43 {
44         TempoMap::BBTPointList *points;
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);
55                 return;
56         }
57
58         const framepos_t end = start + nframes;
59
60         BufferSet& bufs = get_scratch_buffers(ChanCount(DataType::AUDIO, 1));
61         buf = bufs.get_audio(0).data();
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                 framecnt_t copy;
95                 framecnt_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->copy_to_outputs (bufs, DataType::AUDIO, nframes, 0);
132 }
133
134 void
135 Session::setup_click_sounds (Sample** data, Sample const * default_data, framecnt_t* length, framecnt_t default_length, string const & path)
136 {
137         if (*data != default_data) {
138                 delete[] *data;
139                 *data = 0;
140         }
141
142         if (path.empty ()) {
143
144                 *data = const_cast<Sample*> (default_data);
145                 *length = default_length;
146
147         } else {
148
149                 SF_INFO info;
150                 SNDFILE* sndfile;
151
152                 info.format = 0;
153                 if ((sndfile = sf_open (path.c_str(), SFM_READ, &info)) == 0) {
154                         char errbuf[256];
155                         sf_error_str (0, errbuf, sizeof (errbuf) - 1);
156                         warning << string_compose (_("cannot open click soundfile %1 (%2)"), path, errbuf) << endmsg;
157                         _clicking = false;
158                         return;
159                 }
160
161                 /* read the (possibly multi-channel) click data into a temporary buffer */
162
163                 sf_count_t const samples = info.frames * info.channels;
164
165                 Sample* tmp = new Sample[samples];
166
167                 if (sf_readf_float (sndfile, tmp, info.frames) != info.frames) {
168
169                         warning << _("cannot read data from click soundfile") << endmsg;
170                         *data = 0;
171                         _clicking = false;
172
173                 } else {
174
175                         *data = new Sample[info.frames];
176                         *length = info.frames;
177
178                         /* mix down to mono */
179
180                         for (int i = 0; i < info.frames; ++i) {
181                                 (*data)[i] = 0;
182                                 for (int j = 0; j < info.channels; ++j) {
183                                         (*data)[i] = tmp[i * info.channels + j];
184                                 }
185                                 (*data)[i] /= info.channels;
186                         }
187                 }
188
189                 delete[] tmp;
190                 sf_close (sndfile);
191         }
192 }
193
194 void
195 Session::setup_click_sounds (int which)
196 {
197         clear_clicks ();
198
199         if (which == 0 || which == 1) {
200                 setup_click_sounds (
201                         &click_data,
202                         default_click,
203                         &click_length,
204                         default_click_length,
205                         Config->get_click_sound ()
206                         );
207         }
208
209         if (which == 0 || which == -1) {
210                 setup_click_sounds (
211                         &click_emphasis_data,
212                         default_click_emphasis,
213                         &click_emphasis_length,
214                         default_click_emphasis_length,
215                         Config->get_click_emphasis_sound ()
216                         );
217         }
218 }
219
220 void
221 Session::clear_clicks ()
222 {
223         Glib::RWLock::WriterLock lm (click_lock);
224
225         for (Clicks::iterator i = clicks.begin(); i != clicks.end(); ++i) {
226                 delete *i;
227         }
228
229         clicks.clear ();
230 }