Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tempo dialog : added an estimation of accuracy to the Tap Tempo function #774

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions gtk2_ardour/tempo_dialog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,17 @@ TempoDialog::init (const Temporal::BBT_Time& when, double bpm, double end_bpm, d
get_vbox ()->pack_end (*table, false, false);
table->show_all ();

table = manage (new Table (2, 2));
table = manage (new Table (3, 2));
table->set_spacings (6);
row = 0;

table->attach (_tap_source_label, 0, 1, row, row + 1);
table->attach (_midi_port_combo, 1, 2, row, row + 1);
++row;
table->attach (tap_tempo_button, 0, 2, row, row + 1);
++row;
pbar.hide();
table->attach (pbar, 0, 2, row, row + 1);

get_vbox()->pack_end (*table, false, false);
table->show_all ();
Expand Down Expand Up @@ -297,7 +300,7 @@ TempoDialog::init (const Temporal::BBT_Time& when, double bpm, double end_bpm, d

/* init state */
tempo_type_change ();
tapped = false;
toggle_tap(false);
#if 0
bpm_spinner.select_region (0, -1);
bpm_spinner.grab_focus ();
Expand All @@ -313,6 +316,17 @@ TempoDialog::~TempoDialog ()
AudioEngine::instance ()->unregister_port (_midi_tap_port);
}

void
TempoDialog::toggle_tap (bool active)
{
tapped = active;
if (active) {
pbar.show();
} else {
pbar.hide();
}
}

void
TempoDialog::midi_event (MIDI::byte* msg, size_t len, MIDI::samplecnt_t now)
{
Expand Down Expand Up @@ -387,7 +401,7 @@ void
TempoDialog::port_changed ()
{
bool rv = false;
tapped = false;
toggle_tap(false);
_midi_tap_port->disconnect_all ();
TreeModel::iterator r = _midi_port_combo.get_active ();
if (r) {
Expand Down Expand Up @@ -575,12 +589,21 @@ TempoDialog::tap_tempo (int64_t usec)
sum_x += n;
sum_xy += n * t;
sum_xx += n * n;

double T = (sum_xy / n - sum_x / n * sum_y / n) / (sum_xx / n - sum_x / n * sum_x / n);
/* Statistical error (95th percentile) is less relevant than considering the operator has the right
* number of pulses but an error of 1/2 period at the beggining and end so max error = 1 period */
double dT = T - (sum_xy / (n + 1) - sum_x / (n + 1) * sum_y / (n + 1)) / (sum_xx / (n + 1) - sum_x / (n + 1) * sum_x / (n + 1));

if (t - last_t < T / 1.2 || t - last_t > T * 1.2 || T <= .06 || T > 60) {
tapped = false;
toggle_tap(false);
} else {
bpm_spinner.set_value (60.0 / T);
if (T != 0) {
double accuracy = 1 - dT / T;
pbar.set_text(string_compose(_("Accuracy : %1 %% (+/- %2 BpM)"),(int)(accuracy * 100),60.0 * dT / (T * (T + dT))));
pbar.set_fraction (accuracy);
}
}
}
if (!tapped) {
Expand All @@ -592,7 +615,7 @@ TempoDialog::tap_tempo (int64_t usec)
sum_xx = 1.0;
tap_count = 1.0;

tapped = true;
toggle_tap(true);
}
tap_count++;
last_t = t;
Expand All @@ -601,7 +624,7 @@ TempoDialog::tap_tempo (int64_t usec)
bool
TempoDialog::tap_tempo_focus_out (GdkEventFocus* )
{
tapped = false;
toggle_tap(false);
return false;
}

Expand Down
3 changes: 3 additions & 0 deletions gtk2_ardour/tempo_dialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <gtkmm/spinbutton.h>
#include <gtkmm/combobox.h>
#include <gtkmm/comboboxtext.h>
#include <gtkmm/progressbar.h>

#include "ardour/types.h"
#include "ardour/tempo.h"
Expand Down Expand Up @@ -82,6 +83,7 @@ class TempoDialog : public ArdourDialog
void ports_changed ();

void tap_tempo (int64_t);
void toggle_tap (bool);
void midi_event (MIDI::byte*, size_t, MIDI::samplecnt_t);

typedef std::map<std::string,int> NoteTypes;
Expand Down Expand Up @@ -118,6 +120,7 @@ class TempoDialog : public ArdourDialog
Gtk::ComboBoxText tempo_type;
Gtk::ComboBoxText lock_style;
Gtk::ComboBox _midi_port_combo;
Gtk::ProgressBar pbar;

class MidiPortCols : public Gtk::TreeModelColumnRecord
{
Expand Down