From ab6ab082dd947a1a79fcc704968a6d1dc7701b66 Mon Sep 17 00:00:00 2001 From: Paul Davis Date: Fri, 4 Aug 2017 18:09:31 -0400 Subject: add tempo adjustment --- tools/bb/bb.cc | 38 +++++++++++++++++++++++++++++--------- tools/bb/bb.h | 4 ++++ tools/bb/gui.cc | 13 +++++++++++++ tools/bb/gui.h | 5 +++++ 4 files changed, 51 insertions(+), 9 deletions(-) diff --git a/tools/bb/bb.cc b/tools/bb/bb.cc index 0689b513f2..668630c0f9 100644 --- a/tools/bb/bb.cc +++ b/tools/bb/bb.cc @@ -144,6 +144,7 @@ BeatBox::BeatBox (int sr) , _running (false) , _measures (2) , _tempo (120) + , _tempo_request (0) , _meter_beats (4) , _meter_beat_type (4) , _input (0) @@ -183,19 +184,19 @@ BeatBox::register_ports (jack_client_t* jack) } void -BeatBox::start () +BeatBox::compute_tempo_clocks () { - /* compute tempo, beat steps etc. */ - - /* - superclocks_per_minute = superclock_ticks_per_second * 60; - beats_per_minute = _tempo; - whole_notes_per_minute = beats_per_minute / _meter_beat_type; - */ - whole_note_superclocks = (superclock_ticks_per_second * 60) / (_tempo / _meter_beat_type); beat_superclocks = whole_note_superclocks / _meter_beat_type; measure_superclocks = beat_superclocks * _meter_beats; +} + +void +BeatBox::start () +{ + /* compute tempo, beat steps etc. */ + + compute_tempo_clocks (); /* we can start */ @@ -208,6 +209,12 @@ BeatBox::stop () _start_requested = false; } +void +BeatBox::set_tempo (float bpm) +{ + _tempo_request = bpm; +} + int BeatBox::process (int nsamples) { @@ -230,6 +237,18 @@ BeatBox::process (int nsamples) return 0; } + if (_tempo_request) { + double ratio = _tempo / _tempo_request; + _tempo = _tempo_request; + _tempo_request = 0; + + compute_tempo_clocks (); + + for (Events::iterator ee = _current_events.begin(); ee != _current_events.end(); ++ee) { + (*ee)->time = llrintf ((*ee)->time * ratio); + } + } + superclock_t process_start = superclock_cnt - last_start; superclock_t process_end = process_start + superclocks; const superclock_t loop_length = _measures * measure_superclocks; @@ -324,6 +343,7 @@ BeatBox::process (int nsamples) event_pool.pop_back (); e->time = quantized_time; + e->whole_note_superclocks = whole_note_superclocks; e->size = in_event.size; memcpy (e->buf, in_event.buffer, in_event.size); diff --git a/tools/bb/bb.h b/tools/bb/bb.h index e16b9360a8..79a24d6929 100644 --- a/tools/bb/bb.h +++ b/tools/bb/bb.h @@ -44,6 +44,7 @@ class BeatBox { bool _running; int _measures; float _tempo; + float _tempo_request; int _meter_beats; int _meter_beat_type; jack_port_t* _input; @@ -60,6 +61,7 @@ class BeatBox { struct Event { superclock_t time; + superclock_t whole_note_superclocks; size_t size; unsigned char buf[24]; @@ -77,6 +79,8 @@ class BeatBox { typedef std::vector EventPool; EventPool event_pool; + + void compute_tempo_clocks (); }; diff --git a/tools/bb/gui.cc b/tools/bb/gui.cc index cdb34eb124..0bb894e3c2 100644 --- a/tools/bb/gui.cc +++ b/tools/bb/gui.cc @@ -14,6 +14,8 @@ BBGUI::BBGUI (int* argc, char** argv[], jack_client_t* j, BeatBox* bb) , quantize_whole (quantize_group, "Whole") , play_button ("Run") , clear_button ("Clear") + , tempo_adjustment (bb->tempo(), 1, 300, 1, 10) + , tempo_spinner (tempo_adjustment) { quantize_off.signal_toggled().connect (sigc::bind (sigc::mem_fun (*this, &BBGUI::set_quantize), 0)); quantize_32nd.signal_toggled().connect (sigc::bind (sigc::mem_fun (*this, &BBGUI::set_quantize), 32)); @@ -37,6 +39,10 @@ BBGUI::BBGUI (int* argc, char** argv[], jack_client_t* j, BeatBox* bb) misc_button_box.pack_start (play_button); misc_button_box.pack_start (clear_button); + tempo_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &BBGUI::tempo_changed)); + + misc_button_box.pack_start (tempo_spinner); + global_vbox.pack_start (misc_button_box); global_vbox.pack_start (quantize_button_box, true, true); window.add (global_vbox); @@ -54,6 +60,13 @@ BBGUI::run () main.run (); } +void +BBGUI::tempo_changed () +{ + float t = tempo_adjustment.get_value(); + bbox->set_tempo (t); +} + void BBGUI::set_quantize (int divisor) { diff --git a/tools/bb/gui.h b/tools/bb/gui.h index d3d9dc5e00..4c08d5e4ab 100644 --- a/tools/bb/gui.h +++ b/tools/bb/gui.h @@ -31,13 +31,18 @@ class BBGUI { Gtk::ToggleButton play_button; Gtk::Button clear_button; + Gtk::Adjustment tempo_adjustment; + Gtk::SpinButton tempo_spinner; + Gtk::VBox global_vbox; Gtk::VBox quantize_button_box; Gtk::HBox misc_button_box; + void set_quantize (int divisor); void toggle_play (); void clear (); + void tempo_changed (); }; #endif /* __bb_gui_h__ */ -- cgit v1.2.3