#include <list>
#include <cerrno>
-#include "ardour/ardour.h"
+#include "ardour/amp.h"
+#include "ardour/audio_buffer.h"
+#include "ardour/buffer_set.h"
+#include "ardour/click.h"
+#include "ardour/io.h"
#include "ardour/session.h"
#include "ardour/tempo.h"
-#include "ardour/io.h"
-#include "ardour/buffer_set.h"
-#include "ardour/audio_buffer.h"
+#include "ardour/types.h"
#include <sndfile.h>
-#include "i18n.h"
+#include "pbd/i18n.h"
using namespace std;
using namespace ARDOUR;
using namespace PBD;
-Pool Click::pool ("click", sizeof (Click), 128);
+Pool Click::pool ("click", sizeof (Click), 1024);
void
-Session::click (framepos_t start, framecnt_t nframes)
+Session::add_click (samplepos_t pos, bool emphasis)
{
- TempoMap::BBTPointList *points;
- Sample *buf;
+ if (emphasis) {
+ if (click_emphasis_data && Config->get_use_click_emphasis () == true) {
+ clicks.push_back (new Click (pos, click_emphasis_length, click_emphasis_data));
+ } else if (click_data && Config->get_use_click_emphasis () == false) {
+ clicks.push_back (new Click (pos, click_length, click_data));
+ }
+ } else if (click_data) {
+ clicks.push_back (new Click (pos, click_length, click_data));
+ }
+}
+
+void
+Session::click (samplepos_t start, samplecnt_t nframes)
+{
+ vector<TempoMap::BBTPoint> points;
+ samplecnt_t click_distance;
if (_click_io == 0) {
return;
}
- Glib::RWLock::WriterLock clickm (click_lock, Glib::TRY_LOCK);
+ Glib::Threads::RWLock::WriterLock clickm (click_lock, Glib::Threads::TRY_LOCK);
+
+ /* how far have we moved since the last time the clicks got cleared
+ */
- if (!clickm.locked() || _transport_speed != 1.0 || !_clicking || click_data == 0) {
+ click_distance = start - _clicks_cleared;
+
+ if (!clickm.locked() || !_clicking || click_data == 0 || ((click_distance + nframes) < _worst_track_latency)) {
_click_io->silence (nframes);
return;
}
- const framepos_t end = start + nframes;
-
- BufferSet& bufs = get_scratch_buffers(ChanCount(DataType::AUDIO, 1));
- buf = bufs.get_audio(0).data();
- points = _tempo_map->get_points (start, end);
+ if (_click_rec_only && !actively_recording()) {
+ return;
+ }
- if (points == 0) {
- goto run_clicks;
+ start -= _worst_track_latency;
+#ifdef MIXBUS
+ if (_master_out) {
+ start -= _master_out->signal_latency (); // delay signal by mixbus' internal latency
}
+#endif
+ /* start could be negative at this point */
+ const samplepos_t end = start + nframes;
+ /* correct start, potentially */
+ start = max (start, (samplepos_t) 0);
+
+ _tempo_map->get_grid (points, start, end);
- if (points->empty()) {
- delete points;
+ if (distance (points.begin(), points.end()) == 0) {
goto run_clicks;
}
- for (TempoMap::BBTPointList::iterator i = points->begin(); i != points->end(); ++i) {
- switch ((*i).type) {
- case TempoMap::Beat:
- if (click_emphasis_data == 0 || (click_emphasis_data && (*i).beat != 1)) {
- clicks.push_back (new Click ((*i).frame, click_length, click_data));
- }
+ for (vector<TempoMap::BBTPoint>::iterator i = points.begin(); i != points.end(); ++i) {
+ switch ((*i).beat) {
+ case 1:
+ add_click ((*i).sample, true);
break;
-
- case TempoMap::Bar:
- if (click_emphasis_data) {
- clicks.push_back (new Click ((*i).frame, click_emphasis_length, click_emphasis_data));
+ default:
+ if (click_emphasis_data == 0 || (Config->get_use_click_emphasis () == false) || (click_emphasis_data && (*i).beat != 1)) { // XXX why is this check needed ??
+ add_click ((*i).sample, false);
}
break;
}
}
run_clicks:
+ clickm.release ();
+ run_click (start, nframes);
+}
+
+void
+Session::run_click (samplepos_t start, samplepos_t nframes)
+{
+ Glib::Threads::RWLock::ReaderLock clickm (click_lock, Glib::Threads::TRY_LOCK);
+
+ if (!clickm.locked() || click_data == 0) {
+ _click_io->silence (nframes);
+ return;
+ }
+
+ Sample *buf;
+ BufferSet& bufs = get_scratch_buffers(ChanCount(DataType::AUDIO, 1));
+ buf = bufs.get_audio(0).data();
memset (buf, 0, sizeof (Sample) * nframes);
for (list<Click*>::iterator i = clicks.begin(); i != clicks.end(); ) {
- framecnt_t copy;
- framecnt_t internal_offset;
+ samplecnt_t copy;
+ samplecnt_t internal_offset;
Click *clk;
- list<Click*>::iterator next;
clk = *i;
- next = i;
- ++next;
if (clk->start < start) {
internal_offset = 0;
if (clk->offset >= clk->duration) {
delete clk;
- clicks.erase (i);
+ i = clicks.erase (i);
+ } else {
+ ++i;
}
-
-
- i = next;
}
+ _click_gain->run (bufs, 0, 0, 1.0, nframes, false);
_click_io->copy_to_outputs (bufs, DataType::AUDIO, nframes, 0);
}
void
-Session::setup_click_sounds (int which)
+Session::setup_click_sounds (Sample** data, Sample const * default_data, samplecnt_t* length, samplecnt_t default_length, string const & path)
{
- SNDFILE *sndfile;
- SF_INFO info;
-
- clear_clicks();
+ if (*data != default_data) {
+ delete[] *data;
+ *data = 0;
+ }
- if ((which == 0 || which == 1)) {
+ if (path.empty ()) {
- if (click_data != default_click) {
- delete [] click_data;
- click_data = 0;
- }
+ *data = const_cast<Sample*> (default_data);
+ *length = default_length;
- string path = Config->get_click_sound();
+ } else {
- if (path.empty()) {
+ SF_INFO info;
+ SNDFILE* sndfile;
- click_data = const_cast<Sample*> (default_click);
- click_length = default_click_length;
+ info.format = 0;
+ if ((sndfile = sf_open (path.c_str(), SFM_READ, &info)) == 0) {
+ char errbuf[256];
+ sf_error_str (0, errbuf, sizeof (errbuf) - 1);
+ warning << string_compose (_("cannot open click soundfile %1 (%2)"), path, errbuf) << endmsg;
+ _clicking = false;
+ return;
+ }
- } else {
+ /* read the (possibly multi-channel) click data into a temporary buffer */
- info.format = 0;
- if ((sndfile = sf_open (path.c_str(), SFM_READ, &info)) == 0) {
- char errbuf[256];
- sf_error_str (0, errbuf, sizeof (errbuf) - 1);
- warning << string_compose (_("cannot open click soundfile %1 (%2)"), path, errbuf) << endmsg;
- _clicking = false;
- return;
- }
+ sf_count_t const samples = info.frames * info.channels;
- click_data = new Sample[info.frames];
- click_length = info.frames;
+ Sample* tmp = new Sample[samples];
- if (sf_read_float (sndfile, click_data, info.frames) != info.frames) {
- warning << _("cannot read data from click soundfile") << endmsg;
- delete click_data;
- click_data = 0;
- _clicking = false;
- }
+ if (sf_readf_float (sndfile, tmp, info.frames) != info.frames) {
- sf_close (sndfile);
+ warning << _("cannot read data from click soundfile") << endmsg;
+ *data = 0;
+ _clicking = false;
- }
- }
-
- if ((which == 0 || which == -1)) {
+ } else {
- if (click_emphasis_data != default_click_emphasis) {
- delete [] click_emphasis_data;
- click_emphasis_data = 0;
- }
+ *data = new Sample[info.frames];
+ *length = info.frames;
- string path = Config->get_click_emphasis_sound();
+ /* mix down to mono */
- if (path.empty()) {
- click_emphasis_data = const_cast<Sample*> (default_click_emphasis);
- click_emphasis_length = default_click_emphasis_length;
- } else {
- info.format = 0;
- if ((sndfile = sf_open (path.c_str(), SFM_READ, &info)) == 0) {
- char errbuf[256];
- sf_error_str (0, errbuf, sizeof (errbuf) - 1);
- warning << string_compose (_("cannot open click emphasis soundfile %1 (%2)"), path, errbuf) << endmsg;
- return;
+ for (int i = 0; i < info.frames; ++i) {
+ (*data)[i] = 0;
+ for (int j = 0; j < info.channels; ++j) {
+ (*data)[i] = tmp[i * info.channels + j];
+ }
+ (*data)[i] /= info.channels;
}
+ }
- click_emphasis_data = new Sample[info.frames];
- click_emphasis_length = info.frames;
+ delete[] tmp;
+ sf_close (sndfile);
+ }
+}
- if (sf_read_float (sndfile, click_emphasis_data, info.frames) != info.frames) {
- warning << _("cannot read data from click emphasis soundfile") << endmsg;
- delete click_emphasis_data;
- click_emphasis_data = 0;
- }
+void
+Session::setup_click_sounds (int which)
+{
+ clear_clicks ();
+
+ if (which == 0 || which == 1) {
+ setup_click_sounds (
+ &click_data,
+ default_click,
+ &click_length,
+ default_click_length,
+ Config->get_click_sound ()
+ );
+ }
- sf_close (sndfile);
- }
+ if (which == 0 || which == -1) {
+ setup_click_sounds (
+ &click_emphasis_data,
+ default_click_emphasis,
+ &click_emphasis_length,
+ default_click_emphasis_length,
+ Config->get_click_emphasis_sound ()
+ );
}
}
void
Session::clear_clicks ()
{
- Glib::RWLock::WriterLock lm (click_lock);
+ Glib::Threads::RWLock::WriterLock lm (click_lock);
for (Clicks::iterator i = clicks.begin(); i != clicks.end(); ++i) {
delete *i;
}
clicks.clear ();
+ _clicks_cleared = _transport_sample;
}