do not offer combine operation for MIDI (see comment in libs/ardour/midi_playlist_sou...
[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/session.h"
25 #include "ardour/tempo.h"
26 #include "ardour/io.h"
27 #include "ardour/buffer_set.h"
28 #include "ardour/audio_buffer.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 Click::pool ("click", sizeof (Click), 128);
39
40 void
41 Session::click (framepos_t start, framecnt_t nframes)
42 {
43         TempoMap::BBTPointList *points;
44         Sample *buf;
45
46         if (_click_io == 0) {
47                 return;
48         }
49
50         Glib::RWLock::WriterLock clickm (click_lock, Glib::TRY_LOCK);
51
52         if (!clickm.locked() || _transport_speed != 1.0 || !_clicking || click_data == 0) {
53                 _click_io->silence (nframes);
54                 return;
55         }
56
57         const framepos_t end = start + nframes;
58
59         BufferSet& bufs = get_scratch_buffers(ChanCount(DataType::AUDIO, 1));
60         buf = bufs.get_audio(0).data();
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                 framecnt_t copy;
94                 framecnt_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->copy_to_outputs (bufs, DataType::AUDIO, nframes, 0);
131 }
132
133 void
134 Session::setup_click_sounds (Sample** data, Sample const * default_data, framecnt_t* length, framecnt_t default_length, string const & path)
135 {
136         if (*data != default_data) {
137                 delete[] *data;
138                 *data = 0;
139         }
140
141         if (path.empty ()) {
142
143                 *data = const_cast<Sample*> (default_data);
144                 *length = default_length;
145
146         } else {
147
148                 SF_INFO info;
149                 SNDFILE* sndfile;
150                 
151                 info.format = 0;
152                 if ((sndfile = sf_open (path.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)"), path, errbuf) << endmsg;
156                         _clicking = false;
157                         return;
158                 }
159
160                 /* read the (possibly multi-channel) click data into a temporary buffer */
161                 
162                 sf_count_t const samples = info.frames * info.channels;
163
164                 Sample* tmp = new Sample[samples];
165
166                 if (sf_readf_float (sndfile, tmp, info.frames) != info.frames) {
167
168                         warning << _("cannot read data from click soundfile") << endmsg;
169                         *data = 0;
170                         _clicking = false;
171                         
172                 } else {
173
174                         *data = new Sample[info.frames];
175                         *length = info.frames;
176                         
177                         /* mix down to mono */
178                         
179                         for (int i = 0; i < info.frames; ++i) {
180                                 (*data)[i] = 0;
181                                 for (int j = 0; j < info.channels; ++j) {
182                                         (*data)[i] = tmp[i * info.channels + j];
183                                 }
184                                 (*data)[i] /= info.channels;
185                         }
186                 }
187
188                 delete[] tmp;
189                 sf_close (sndfile);
190         }
191 }
192
193 void
194 Session::setup_click_sounds (int which)
195 {
196         clear_clicks ();
197
198         if (which == 0 || which == 1) {
199                 setup_click_sounds (
200                         &click_data,
201                         default_click,
202                         &click_length,
203                         default_click_length,
204                         Config->get_click_sound ()
205                         );
206         }
207
208         if (which == 0 || which == -1) {
209                 setup_click_sounds (
210                         &click_emphasis_data,
211                         default_click_emphasis,
212                         &click_emphasis_length,
213                         default_click_emphasis_length,
214                         Config->get_click_emphasis_sound ()
215                         );
216         }
217 }
218
219 void
220 Session::clear_clicks ()
221 {
222         Glib::RWLock::WriterLock lm (click_lock);
223
224         for (Clicks::iterator i = clicks.begin(); i != clicks.end(); ++i) {
225                 delete *i;
226         }
227
228         clicks.clear ();
229 }