2 * Copyright (C) 2005-2017 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2005 Karsten Wiese <fzuuzf@googlemail.com>
4 * Copyright (C) 2005 Taybin Rutkin <taybin@taybin.com>
5 * Copyright (C) 2008-2015 David Robillard <d@drobilla.net>
6 * Copyright (C) 2009-2010 Carl Hetherington <carl@carlh.net>
7 * Copyright (C) 2014-2015 Colin Fletcher <colin.m.fletcher@googlemail.com>
8 * Copyright (C) 2014-2017 Robin Gareus <robin@gareus.org>
9 * Copyright (C) 2015-2017 Ben Loftis <ben@harrisonconsoles.com>
10 * Copyright (C) 2015-2017 Nick Mainsbridge <mainsbridge@gmail.com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <cstdio> // for snprintf, grrr
29 #include <gtkmm/stock.h>
31 #include "gtkmm2ext/utils.h"
33 #include "tempo_dialog.h"
34 #include "ui_config.h"
40 using namespace Gtkmm2ext;
41 using namespace ARDOUR;
44 TempoDialog::TempoDialog (TempoMap& map, samplepos_t sample, const string&)
45 : ArdourDialog (_("New Tempo"))
48 , bpm_adjustment (60.0, 1.0, 999.9, 0.1, 1.0)
49 , bpm_spinner (bpm_adjustment)
50 , end_bpm_adjustment (60.0, 1.0, 999.9, 0.1, 1.0)
51 , end_bpm_spinner (end_bpm_adjustment)
52 , _end_bpm_label (_("End Beats per Minute:"), ALIGN_LEFT, ALIGN_CENTER)
53 , when_bar_label (_("bar:"), ALIGN_RIGHT, ALIGN_CENTER)
54 , when_beat_label (_("beat:"), ALIGN_RIGHT, ALIGN_CENTER)
55 , pulse_selector_label (_("Pulse:"), ALIGN_LEFT, ALIGN_CENTER)
56 , tap_tempo_button (_("Tap tempo"))
58 Tempo tempo (map.tempo_at_sample (sample));
59 Timecode::BBT_Time when (map.bbt_at_sample (sample));
61 init (when, tempo.note_types_per_minute(), tempo.end_note_types_per_minute(), tempo.note_type(), TempoSection::Constant, true, MusicTime);
64 TempoDialog::TempoDialog (TempoMap& map, TempoSection& section, const string&)
65 : ArdourDialog (_("Edit Tempo"))
68 , bpm_adjustment (60.0, 1.0, 999.9, 0.1, 1.0)
69 , bpm_spinner (bpm_adjustment)
70 , end_bpm_adjustment (60.0, 1.0, 999.9, 0.1, 1.0)
71 , end_bpm_spinner (end_bpm_adjustment)
72 , _end_bpm_label (_("End Beats per Minute:"), ALIGN_LEFT, ALIGN_CENTER)
73 , when_bar_label (_("bar:"), ALIGN_RIGHT, ALIGN_CENTER)
74 , when_beat_label (_("beat:"), ALIGN_RIGHT, ALIGN_CENTER)
75 , pulse_selector_label (_("Pulse:"), ALIGN_LEFT, ALIGN_CENTER)
76 , tap_tempo_button (_("Tap tempo"))
78 Timecode::BBT_Time when (map.bbt_at_sample (section.sample()));
79 init (when, section.note_types_per_minute(), section.end_note_types_per_minute(), section.note_type(), section.type()
80 , section.initial() || section.locked_to_meter(), section.position_lock_style());
84 TempoDialog::init (const Timecode::BBT_Time& when, double bpm, double end_bpm, double note_type, TempoSection::Type type, bool initial, PositionLockStyle style)
86 vector<string> strings;
87 NoteTypes::iterator x;
89 bpm_spinner.set_numeric (true);
90 bpm_spinner.set_digits (3);
91 bpm_spinner.set_wrap (true);
92 bpm_spinner.set_value (bpm);
93 bpm_spinner.set_alignment (1.0);
95 end_bpm_spinner.set_numeric (true);
96 end_bpm_spinner.set_digits (3);
97 end_bpm_spinner.set_wrap (true);
98 end_bpm_spinner.set_value (end_bpm);
99 end_bpm_spinner.set_alignment (1.0);
101 Gtkmm2ext::set_size_request_to_display_given_text (pulse_selector, _("one-hundred-twenty-eighth"), 3, 6);
103 note_types.insert (make_pair (_("whole"), 1.0));
104 strings.push_back (_("whole"));
105 note_types.insert (make_pair (_("second"), 2.0));
106 strings.push_back (_("second"));
107 note_types.insert (make_pair (_("third"), 3.0));
108 strings.push_back (_("third"));
109 note_types.insert (make_pair (_("quarter"), 4.0));
110 strings.push_back (_("quarter"));
111 note_types.insert (make_pair (_("eighth"), 8.0));
112 strings.push_back (_("eighth"));
113 note_types.insert (make_pair (_("sixteenth"), 16.0));
114 strings.push_back (_("sixteenth"));
115 note_types.insert (make_pair (_("thirty-second"), 32.0));
116 strings.push_back (_("thirty-second"));
117 note_types.insert (make_pair (_("sixty-fourth"), 64.0));
118 strings.push_back (_("sixty-fourth"));
119 note_types.insert (make_pair (_("one-hundred-twenty-eighth"), 128.0));
120 strings.push_back (_("one-hundred-twenty-eighth"));
122 set_popdown_strings (pulse_selector, strings);
124 for (x = note_types.begin(); x != note_types.end(); ++x) {
125 if (x->second == note_type) {
126 pulse_selector.set_active_text (x->first);
131 if (x == note_types.end()) {
132 pulse_selector.set_active_text (strings[3]); // "quarter"
137 tempo_types.insert (make_pair (_("ramped"), TempoSection::Ramp));
138 strings.push_back (_("ramped"));
139 tempo_types.insert (make_pair (_("constant"), TempoSection::Constant));
140 strings.push_back (_("constant"));
141 set_popdown_strings (tempo_type, strings);
142 TempoTypes::iterator tt;
143 for (tt = tempo_types.begin(); tt != tempo_types.end(); ++tt) {
144 if (tt->second == type) {
145 tempo_type.set_active_text (tt->first);
149 if (tt == tempo_types.end()) {
150 tempo_type.set_active_text (strings[1]); // "constant"
155 lock_styles.insert (make_pair (_("music"), MusicTime));
156 strings.push_back (_("music"));
157 lock_styles.insert (make_pair (_("audio"), AudioTime));
158 strings.push_back (_("audio"));
159 set_popdown_strings (lock_style, strings);
160 LockStyles::iterator ls;
161 for (ls = lock_styles.begin(); ls != lock_styles.end(); ++ls) {
162 if (ls->second == style) {
163 lock_style.set_active_text (ls->first);
167 if (ls == lock_styles.end()) {
168 lock_style.set_active_text (strings[0]); // "music"
173 if (UIConfiguration::instance().get_allow_non_quarter_pulse()) {
174 table = manage (new Table (5, 7));
176 table = manage (new Table (5, 6));
179 table->set_spacings (6);
180 table->set_homogeneous (false);
184 if (UIConfiguration::instance().get_allow_non_quarter_pulse()) {
185 table->attach (pulse_selector_label, 0, 1, row, row + 1);
186 table->attach (pulse_selector, 1, 5, row, row + 1);
191 Label* bpm_label = manage (new Label(_("Start Beats per Minute:"), ALIGN_LEFT, ALIGN_CENTER));
192 table->attach (*bpm_label, 0, 1, row, row + 1);
193 table->attach (bpm_spinner, 1, 5, row, row + 1);
196 table->attach (_end_bpm_label, 0, 1, row, row + 1);
197 table->attach (end_bpm_spinner, 1, 5, row, row + 1);
200 Label* tempo_type_label = manage (new Label(_("Tempo Type:"), ALIGN_LEFT, ALIGN_CENTER));
201 table->attach (*tempo_type_label, 0, 1, row, row + 1);
202 table->attach (tempo_type, 1, 5, row, row + 1);
208 snprintf (buf, sizeof (buf), "%" PRIu32, when.bars);
209 when_bar_entry.set_text (buf);
210 snprintf (buf, sizeof (buf), "%" PRIu32, when.beats);
211 when_beat_entry.set_text (buf);
214 when_bar_entry.set_width_chars(4);
215 when_beat_entry.set_width_chars (4);
216 when_bar_entry.set_alignment (1.0);
217 when_beat_entry.set_alignment (1.0);
219 when_bar_label.set_name ("MetricLabel");
220 when_beat_label.set_name ("MetricLabel");
222 table->attach (when_bar_label, 1, 2, row, row+1, Gtk::AttachOptions(0), Gtk::AttachOptions(0));
223 table->attach (when_bar_entry, 2, 3, row, row+1, Gtk::AttachOptions(0), Gtk::AttachOptions(0));
225 table->attach (when_beat_label, 3, 4, row, row+1, Gtk::AttachOptions(0), Gtk::AttachOptions(0));
226 table->attach (when_beat_entry, 4, 5, row, row+1, Gtk::AttachOptions(0), Gtk::AttachOptions(0));
228 Label* when_label = manage (new Label(_("Tempo begins at"), ALIGN_LEFT, ALIGN_CENTER));
229 table->attach (*when_label, 0, 1, row, row+1);
234 Label* lock_style_label = manage (new Label(_("Lock Style:"), ALIGN_LEFT, ALIGN_CENTER));
235 table->attach (*lock_style_label, 0, 1, row, row + 1);
236 table->attach (lock_style, 1, 5, row, row + 1);
241 get_vbox()->set_border_width (12);
242 get_vbox()->pack_end (*table);
246 add_button (Stock::CANCEL, RESPONSE_CANCEL);
247 add_button (Stock::APPLY, RESPONSE_ACCEPT);
248 set_response_sensitive (RESPONSE_ACCEPT, true);
249 set_default_response (RESPONSE_ACCEPT);
252 end_bpm_spinner.show ();
253 tap_tempo_button.show ();
254 get_vbox()->set_spacing (6);
255 get_vbox()->pack_end (tap_tempo_button);
256 tap_tempo_button.can_focus ();
257 tap_tempo_button.grab_focus ();
259 set_name ("MetricDialog");
261 bpm_spinner.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &TempoDialog::response), RESPONSE_ACCEPT));
262 bpm_spinner.signal_button_press_event().connect (sigc::mem_fun (*this, &TempoDialog::bpm_button_press), false);
263 bpm_spinner.signal_button_release_event().connect (sigc::mem_fun (*this, &TempoDialog::bpm_button_release), false);
264 bpm_spinner.signal_changed().connect (sigc::mem_fun (*this, &TempoDialog::bpm_changed));
265 end_bpm_spinner.signal_changed().connect (sigc::mem_fun (*this, &TempoDialog::bpm_changed));
266 when_bar_entry.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &TempoDialog::response), RESPONSE_ACCEPT));
267 when_bar_entry.signal_key_release_event().connect (sigc::mem_fun (*this, &TempoDialog::entry_key_release), false);
268 when_beat_entry.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &TempoDialog::response), RESPONSE_ACCEPT));
269 when_beat_entry.signal_key_release_event().connect (sigc::mem_fun (*this, &TempoDialog::entry_key_release), false);
270 pulse_selector.signal_changed().connect (sigc::mem_fun (*this, &TempoDialog::pulse_change));
271 tempo_type.signal_changed().connect (sigc::mem_fun (*this, &TempoDialog::tempo_type_change));
272 lock_style.signal_changed().connect (sigc::mem_fun (*this, &TempoDialog::lock_style_change));
273 tap_tempo_button.signal_button_press_event().connect (sigc::mem_fun (*this, &TempoDialog::tap_tempo_button_press), false);
274 tap_tempo_button.signal_key_press_event().connect (sigc::mem_fun (*this, &TempoDialog::tap_tempo_key_press), false);
275 tap_tempo_button.signal_focus_out_event().connect (sigc::mem_fun (*this, &TempoDialog::tap_tempo_focus_out));
283 TempoDialog::is_user_input_valid() const
285 return (when_beat_entry.get_text() != "")
286 && (when_bar_entry.get_text() != "")
287 && (when_bar_entry.get_text() != "0");
291 TempoDialog::bpm_changed ()
293 set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
297 TempoDialog::bpm_button_press (GdkEventButton*)
303 TempoDialog::bpm_button_release (GdkEventButton*)
305 /* the value has been modified, accept should work now */
307 set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
312 TempoDialog::entry_key_release (GdkEventKey*)
314 Timecode::BBT_Time bbt;
317 if (_section && is_user_input_valid()) {
318 set_response_sensitive (RESPONSE_ACCEPT, _map->can_solve_bbt (_section, bbt));
320 set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
327 TempoDialog::get_bpm ()
329 return bpm_spinner.get_value ();
333 TempoDialog::get_end_bpm ()
335 if (get_tempo_type() == TempoSection::Constant) {
336 return bpm_spinner.get_value ();
339 return end_bpm_spinner.get_value ();
343 TempoDialog::get_bbt_time (Timecode::BBT_Time& requested)
345 if (sscanf (when_bar_entry.get_text().c_str(), "%" PRIu32, &requested.bars) != 1) {
349 if (sscanf (when_beat_entry.get_text().c_str(), "%" PRIu32, &requested.beats) != 1) {
359 TempoDialog::get_note_type ()
361 NoteTypes::iterator x = note_types.find (pulse_selector.get_active_text());
363 if (x == note_types.end()) {
364 error << string_compose(_("incomprehensible pulse note type (%1)"), pulse_selector.get_active_text()) << endmsg;
372 TempoDialog::get_tempo_type ()
374 TempoTypes::iterator x = tempo_types.find (tempo_type.get_active_text());
376 if (x == tempo_types.end()) {
377 error << string_compose(_("incomprehensible tempo type (%1)"), tempo_type.get_active_text()) << endmsg;
378 return TempoSection::Constant;
385 TempoDialog::get_lock_style ()
387 LockStyles::iterator x = lock_styles.find (lock_style.get_active_text());
389 if (x == lock_styles.end()) {
390 error << string_compose(_("incomprehensible lock style (%1)"), lock_style.get_active_text()) << endmsg;
398 TempoDialog::pulse_change ()
400 set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
404 TempoDialog::tempo_type_change ()
406 if (get_tempo_type() == TempoSection::Constant) {
407 end_bpm_spinner.hide ();
408 _end_bpm_label.hide();
410 end_bpm_spinner.show ();
411 _end_bpm_label.show();
414 set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
418 TempoDialog::lock_style_change ()
420 set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
424 TempoDialog::tap_tempo_key_press (GdkEventKey*)
431 TempoDialog::tap_tempo_button_press (GdkEventButton* ev)
433 if (ev->type == GDK_2BUTTON_PRESS || ev->type == GDK_3BUTTON_PRESS) {
436 if (ev->button != 1) {
440 return false; // grab focus
444 TempoDialog::tap_tempo ()
448 // Linear least-squares regression
450 t = 1e-6 * (g_get_monotonic_time () - first_t); // Subtract first_t to avoid precision problems
452 double n = tap_count;
457 double T = (sum_xy/n - sum_x/n * sum_y/n) / (sum_xx/n - sum_x/n * sum_x/n);
459 if (t - last_t < T / 1.2 || t - last_t > T * 1.2) {
462 bpm_spinner.set_value (60.0 / T);
466 first_t = g_get_monotonic_time ();
481 TempoDialog::tap_tempo_focus_out (GdkEventFocus* )
487 MeterDialog::MeterDialog (TempoMap& map, samplepos_t sample, const string&)
488 : ArdourDialog (_("New Meter"))
490 sample = map.round_to_bar(sample, RoundNearest).sample;
491 Timecode::BBT_Time when (map.bbt_at_sample (sample));
492 Meter meter (map.meter_at_sample (sample));
494 init (when, meter.divisions_per_bar(), meter.note_divisor(), false, MusicTime);
497 MeterDialog::MeterDialog (TempoMap& map, MeterSection& section, const string&)
498 : ArdourDialog (_("Edit Meter"))
500 Timecode::BBT_Time when (map.bbt_at_sample (section.sample()));
502 init (when, section.divisions_per_bar(), section.note_divisor(), section.initial(), section.position_lock_style());
506 MeterDialog::init (const Timecode::BBT_Time& when, double bpb, double divisor, bool initial, PositionLockStyle style)
509 vector<string> strings;
510 NoteTypes::iterator x;
512 snprintf (buf, sizeof (buf), "%.2f", bpb);
513 bpb_entry.set_text (buf);
514 bpb_entry.select_region (0, -1);
515 bpb_entry.set_alignment (1.0);
517 note_types.insert (make_pair (_("whole"), 1.0));
518 strings.push_back (_("whole"));
519 note_types.insert (make_pair (_("second"), 2.0));
520 strings.push_back (_("second"));
521 note_types.insert (make_pair (_("third"), 3.0));
522 strings.push_back (_("third"));
523 note_types.insert (make_pair (_("quarter"), 4.0));
524 strings.push_back (_("quarter"));
525 note_types.insert (make_pair (_("eighth"), 8.0));
526 strings.push_back (_("eighth"));
527 note_types.insert (make_pair (_("sixteenth"), 16.0));
528 strings.push_back (_("sixteenth"));
529 note_types.insert (make_pair (_("thirty-second"), 32.0));
530 strings.push_back (_("thirty-second"));
531 note_types.insert (make_pair (_("sixty-fourth"), 64.0));
532 strings.push_back (_("sixty-fourth"));
533 note_types.insert (make_pair (_("one-hundred-twenty-eighth"), 128.0));
534 strings.push_back (_("one-hundred-twenty-eighth"));
536 set_popdown_strings (note_type, strings);
538 for (x = note_types.begin(); x != note_types.end(); ++x) {
539 if (x->second == divisor) {
540 note_type.set_active_text (x->first);
545 if (x == note_types.end()) {
546 note_type.set_active_text (strings[3]); // "quarter"
551 lock_styles.insert (make_pair (_("music"), MusicTime));
552 strings.push_back (_("music"));
553 lock_styles.insert (make_pair (_("audio"), AudioTime));
554 strings.push_back (_("audio"));
555 set_popdown_strings (lock_style, strings);
556 LockStyles::iterator ls;
557 for (ls = lock_styles.begin(); ls != lock_styles.end(); ++ls) {
558 if (ls->second == style) {
559 lock_style.set_active_text (ls->first);
563 if (ls == lock_styles.end()) {
564 lock_style.set_active_text (strings[0]); // "music"
567 Label* note_label = manage (new Label (_("Note value:"), ALIGN_RIGHT, ALIGN_CENTER));
568 Label* lock_label = manage (new Label (_("Lock style:"), ALIGN_RIGHT, ALIGN_CENTER));
569 Label* bpb_label = manage (new Label (_("Beats per bar:"), ALIGN_RIGHT, ALIGN_CENTER));
570 Table* table = manage (new Table (3, 3));
571 table->set_spacings (6);
573 table->attach (*bpb_label, 0, 1, 0, 1, FILL|EXPAND, FILL|EXPAND);
574 table->attach (bpb_entry, 1, 2, 0, 1, FILL|EXPAND, FILL|EXPAND);
575 table->attach (*note_label, 0, 1, 1, 2, FILL|EXPAND, FILL|EXPAND);
576 table->attach (note_type, 1, 2, 1, 2, FILL|EXPAND, FILL|EXPAND);
578 snprintf (buf, sizeof (buf), "%" PRIu32, when.bars);
579 when_bar_entry.set_text (buf);
580 when_bar_entry.set_alignment (1.0);
583 Label* when_label = manage (new Label(_("Meter begins at bar:"), ALIGN_LEFT, ALIGN_CENTER));
585 table->attach (*when_label, 0, 1, 2, 3, FILL | EXPAND, FILL | EXPAND);
586 table->attach (when_bar_entry, 1, 2, 2, 3, FILL | EXPAND, FILL | EXPAND);
588 table->attach (*lock_label, 0, 1, 3, 4, FILL|EXPAND, FILL|EXPAND);
589 table->attach (lock_style, 1, 2, 3, 4, FILL|EXPAND, SHRINK);
592 get_vbox()->set_border_width (12);
593 get_vbox()->pack_start (*table, false, false);
595 add_button (Stock::CANCEL, RESPONSE_CANCEL);
596 add_button (Stock::APPLY, RESPONSE_ACCEPT);
597 set_response_sensitive (RESPONSE_ACCEPT, true);
598 set_default_response (RESPONSE_ACCEPT);
600 get_vbox()->show_all ();
602 set_name ("MetricDialog");
603 bpb_entry.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &MeterDialog::response), RESPONSE_ACCEPT));
604 bpb_entry.signal_key_press_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_press), false);
605 bpb_entry.signal_key_release_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_release));
606 when_bar_entry.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &MeterDialog::response), RESPONSE_ACCEPT));
607 when_bar_entry.signal_key_press_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_press), false);
608 when_bar_entry.signal_key_release_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_release));
609 note_type.signal_changed().connect (sigc::mem_fun (*this, &MeterDialog::note_type_change));
610 lock_style.signal_changed().connect (sigc::mem_fun (*this, &MeterDialog::lock_style_change));
615 MeterDialog::is_user_input_valid() const
617 return (when_bar_entry.get_text() != "")
618 && (when_bar_entry.get_text() != "0")
619 && (bpb_entry.get_text() != "");
623 MeterDialog::entry_key_press (GdkEventKey* ev)
626 switch (ev->keyval) {
671 MeterDialog::entry_key_release (GdkEventKey*)
673 set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
678 MeterDialog::note_type_change ()
680 set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
684 MeterDialog::lock_style_change ()
686 set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
690 MeterDialog::get_bpb ()
694 if (sscanf (bpb_entry.get_text().c_str(), "%lf", &bpb) != 1) {
702 MeterDialog::get_note_type ()
704 NoteTypes::iterator x = note_types.find (note_type.get_active_text());
706 if (x == note_types.end()) {
707 error << string_compose(_("incomprehensible meter note type (%1)"), note_type.get_active_text()) << endmsg;
715 MeterDialog::get_lock_style ()
717 LockStyles::iterator x = lock_styles.find (lock_style.get_active_text());
719 if (x == lock_styles.end()) {
720 error << string_compose(_("incomprehensible meter lock style (%1)"), lock_style.get_active_text()) << endmsg;
728 MeterDialog::get_bbt_time (Timecode::BBT_Time& requested)
730 if (sscanf (when_bar_entry.get_text().c_str(), "%" PRIu32, &requested.bars) != 1) {