2 Copyright (C) 2000-2007 Paul Davis
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.
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.
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.
20 #include <cstdio> // for snprintf, grrr
22 #include <gtkmm/stock.h>
24 #include "gtkmm2ext/utils.h"
26 #include "tempo_dialog.h"
27 #include "ui_config.h"
33 using namespace Gtkmm2ext;
34 using namespace ARDOUR;
37 TempoDialog::TempoDialog (TempoMap& map, framepos_t frame, const string&)
38 : ArdourDialog (_("New Tempo"))
41 , bpm_adjustment (60.0, 1.0, 999.9, 0.1, 1.0)
42 , bpm_spinner (bpm_adjustment)
43 , when_bar_label (_("bar:"), ALIGN_RIGHT, ALIGN_CENTER)
44 , when_beat_label (_("beat:"), ALIGN_RIGHT, ALIGN_CENTER)
45 , pulse_selector_label (_("Pulse:"), ALIGN_RIGHT, ALIGN_CENTER)
46 , tap_tempo_button (_("Tap tempo"))
48 Tempo tempo (map.tempo_at_frame (frame));
49 Timecode::BBT_Time when (map.bbt_at_frame (frame));
51 init (when, tempo.note_types_per_minute(), tempo.note_type(), TempoSection::Constant, true, MusicTime);
54 TempoDialog::TempoDialog (TempoMap& map, TempoSection& section, const string&)
55 : ArdourDialog (_("Edit Tempo"))
58 , bpm_adjustment (60.0, 1.0, 999.9, 0.1, 1.0)
59 , bpm_spinner (bpm_adjustment)
60 , when_bar_label (_("bar:"), ALIGN_RIGHT, ALIGN_CENTER)
61 , when_beat_label (_("beat:"), ALIGN_RIGHT, ALIGN_CENTER)
62 , pulse_selector_label (_("Pulse:"), ALIGN_RIGHT, ALIGN_CENTER)
63 , tap_tempo_button (_("Tap tempo"))
65 Timecode::BBT_Time when (map.bbt_at_frame (section.frame()));
66 init (when, section.note_types_per_minute(), section.note_type(), section.type()
67 , section.initial() || section.locked_to_meter(), section.position_lock_style());
71 TempoDialog::init (const Timecode::BBT_Time& when, double bpm, double note_type, TempoSection::Type type, bool initial, PositionLockStyle style)
73 vector<string> strings;
74 NoteTypes::iterator x;
76 bpm_spinner.set_numeric (true);
77 bpm_spinner.set_digits (3);
78 bpm_spinner.set_wrap (true);
79 bpm_spinner.set_value (bpm);
80 bpm_spinner.set_alignment (1.0);
82 note_types.insert (make_pair (_("whole"), 1.0));
83 strings.push_back (_("whole"));
84 note_types.insert (make_pair (_("second"), 2.0));
85 strings.push_back (_("second"));
86 note_types.insert (make_pair (_("third"), 3.0));
87 strings.push_back (_("third"));
88 note_types.insert (make_pair (_("quarter"), 4.0));
89 strings.push_back (_("quarter"));
90 note_types.insert (make_pair (_("eighth"), 8.0));
91 strings.push_back (_("eighth"));
92 note_types.insert (make_pair (_("sixteenth"), 16.0));
93 strings.push_back (_("sixteenth"));
94 note_types.insert (make_pair (_("thirty-second"), 32.0));
95 strings.push_back (_("thirty-second"));
96 note_types.insert (make_pair (_("sixty-fourth"), 64.0));
97 strings.push_back (_("sixty-fourth"));
98 note_types.insert (make_pair (_("one-hundred-twenty-eighth"), 128.0));
99 strings.push_back (_("one-hundred-twenty-eighth"));
101 set_popdown_strings (pulse_selector, strings);
103 for (x = note_types.begin(); x != note_types.end(); ++x) {
104 if (x->second == note_type) {
105 pulse_selector.set_active_text (x->first);
110 if (x == note_types.end()) {
111 pulse_selector.set_active_text (strings[3]); // "quarter"
116 tempo_types.insert (make_pair (_("ramped"), TempoSection::Ramp));
117 strings.push_back (_("ramped"));
118 tempo_types.insert (make_pair (_("constant"), TempoSection::Constant));
119 strings.push_back (_("constant"));
120 set_popdown_strings (tempo_type, strings);
121 TempoTypes::iterator tt;
122 for (tt = tempo_types.begin(); tt != tempo_types.end(); ++tt) {
123 if (tt->second == type) {
124 tempo_type.set_active_text (tt->first);
128 if (tt == tempo_types.end()) {
129 tempo_type.set_active_text (strings[1]); // "constant"
134 lock_styles.insert (make_pair (_("music"), MusicTime));
135 strings.push_back (_("music"));
136 lock_styles.insert (make_pair (_("audio"), AudioTime));
137 strings.push_back (_("audio"));
138 set_popdown_strings (lock_style, strings);
139 LockStyles::iterator ls;
140 for (ls = lock_styles.begin(); ls != lock_styles.end(); ++ls) {
141 if (ls->second == style) {
142 lock_style.set_active_text (ls->first);
146 if (ls == lock_styles.end()) {
147 lock_style.set_active_text (strings[0]); // "music"
152 if (UIConfiguration::instance().get_allow_non_quarter_pulse()) {
153 table = manage (new Table (5, 7));
155 table = manage (new Table (5, 6));
158 table->set_spacings (6);
159 table->set_homogeneous (false);
163 if (UIConfiguration::instance().get_allow_non_quarter_pulse()) {
164 table->attach (pulse_selector_label, 0, 1, 0, 1);
165 table->attach (pulse_selector, 1, 5, 0, 1);
172 Label* bpm_label = manage (new Label(_("Beats per Minute:"), ALIGN_LEFT, ALIGN_CENTER));
173 table->attach (*bpm_label, 0, 1, row, row + 1);
174 table->attach (bpm_spinner, 1, 5, row, row + 1);
179 snprintf (buf, sizeof (buf), "%" PRIu32, when.bars);
180 when_bar_entry.set_text (buf);
181 snprintf (buf, sizeof (buf), "%" PRIu32, when.beats);
182 when_beat_entry.set_text (buf);
185 when_bar_entry.set_width_chars(4);
186 when_beat_entry.set_width_chars (4);
187 when_bar_entry.set_alignment (1.0);
188 when_beat_entry.set_alignment (1.0);
190 when_bar_label.set_name ("MetricLabel");
191 when_beat_label.set_name ("MetricLabel");
193 table->attach (when_bar_label, 1, 2, row, row+1, Gtk::AttachOptions(0), Gtk::AttachOptions(0));
194 table->attach (when_bar_entry, 2, 3, row, row+1, Gtk::AttachOptions(0), Gtk::AttachOptions(0));
196 table->attach (when_beat_label, 3, 4, row, row+1, Gtk::AttachOptions(0), Gtk::AttachOptions(0));
197 table->attach (when_beat_entry, 4, 5, row, row+1, Gtk::AttachOptions(0), Gtk::AttachOptions(0));
199 Label* when_label = manage (new Label(_("Tempo begins at"), ALIGN_LEFT, ALIGN_CENTER));
200 table->attach (*when_label, 0, 1, row, row+1);
205 Label* lock_style_label = manage (new Label(_("Lock Style:"), ALIGN_RIGHT, ALIGN_CENTER));
206 table->attach (*lock_style_label, 0, 1, row, row + 1);
207 table->attach (lock_style, 1, 5, row, row + 1);
213 Label* tempo_type_label = manage (new Label(_("Tempo Type:"), ALIGN_RIGHT, ALIGN_CENTER));
214 table->attach (*tempo_type_label, 0, 1, row, row + 1);
215 table->attach (tempo_type, 1, 5, row, row + 1);
219 get_vbox()->set_border_width (12);
220 get_vbox()->pack_end (*table);
224 add_button (Stock::CANCEL, RESPONSE_CANCEL);
225 add_button (Stock::APPLY, RESPONSE_ACCEPT);
226 set_response_sensitive (RESPONSE_ACCEPT, true);
227 set_default_response (RESPONSE_ACCEPT);
230 tap_tempo_button.show ();
231 get_vbox()->set_spacing (6);
232 get_vbox()->pack_end (tap_tempo_button);
233 bpm_spinner.grab_focus ();
235 set_name ("MetricDialog");
237 bpm_spinner.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &TempoDialog::response), RESPONSE_ACCEPT));
238 bpm_spinner.signal_button_press_event().connect (sigc::mem_fun (*this, &TempoDialog::bpm_button_press), false);
239 bpm_spinner.signal_button_release_event().connect (sigc::mem_fun (*this, &TempoDialog::bpm_button_release), false);
240 bpm_spinner.signal_changed().connect (sigc::mem_fun (*this, &TempoDialog::bpm_changed));
241 when_bar_entry.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &TempoDialog::response), RESPONSE_ACCEPT));
242 when_bar_entry.signal_key_release_event().connect (sigc::mem_fun (*this, &TempoDialog::entry_key_release), false);
243 when_beat_entry.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &TempoDialog::response), RESPONSE_ACCEPT));
244 when_beat_entry.signal_key_release_event().connect (sigc::mem_fun (*this, &TempoDialog::entry_key_release), false);
245 pulse_selector.signal_changed().connect (sigc::mem_fun (*this, &TempoDialog::pulse_change));
246 tempo_type.signal_changed().connect (sigc::mem_fun (*this, &TempoDialog::tempo_type_change));
247 lock_style.signal_changed().connect (sigc::mem_fun (*this, &TempoDialog::lock_style_change));
248 tap_tempo_button.signal_button_press_event().connect (sigc::mem_fun (*this, &TempoDialog::tap_tempo_button_press), false);
249 tap_tempo_button.signal_focus_out_event().connect (sigc::mem_fun (*this, &TempoDialog::tap_tempo_focus_out));
255 TempoDialog::is_user_input_valid() const
257 return (when_beat_entry.get_text() != "")
258 && (when_bar_entry.get_text() != "")
259 && (when_bar_entry.get_text() != "0");
263 TempoDialog::bpm_changed ()
265 set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
269 TempoDialog::bpm_button_press (GdkEventButton*)
275 TempoDialog::bpm_button_release (GdkEventButton*)
277 /* the value has been modified, accept should work now */
279 set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
284 TempoDialog::entry_key_release (GdkEventKey*)
286 Timecode::BBT_Time bbt;
289 if (_section && is_user_input_valid()) {
290 set_response_sensitive (RESPONSE_ACCEPT, _map->can_solve_bbt (_section, bbt));
292 set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
299 TempoDialog::get_bpm ()
301 return bpm_spinner.get_value ();
305 TempoDialog::get_bbt_time (Timecode::BBT_Time& requested)
307 if (sscanf (when_bar_entry.get_text().c_str(), "%" PRIu32, &requested.bars) != 1) {
311 if (sscanf (when_beat_entry.get_text().c_str(), "%" PRIu32, &requested.beats) != 1) {
321 TempoDialog::get_note_type ()
323 NoteTypes::iterator x = note_types.find (pulse_selector.get_active_text());
325 if (x == note_types.end()) {
326 error << string_compose(_("incomprehensible pulse note type (%1)"), pulse_selector.get_active_text()) << endmsg;
334 TempoDialog::get_tempo_type ()
336 TempoTypes::iterator x = tempo_types.find (tempo_type.get_active_text());
338 if (x == tempo_types.end()) {
339 error << string_compose(_("incomprehensible tempo type (%1)"), tempo_type.get_active_text()) << endmsg;
340 return TempoSection::Constant;
347 TempoDialog::get_lock_style ()
349 LockStyles::iterator x = lock_styles.find (lock_style.get_active_text());
351 if (x == lock_styles.end()) {
352 error << string_compose(_("incomprehensible lock style (%1)"), lock_style.get_active_text()) << endmsg;
360 TempoDialog::pulse_change ()
362 set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
366 TempoDialog::tempo_type_change ()
368 set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
372 TempoDialog::lock_style_change ()
374 set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
378 TempoDialog::tap_tempo_button_press (GdkEventButton *ev)
382 // Linear least-squares regression
384 t = 1e-6 * (g_get_monotonic_time () - first_t); // Subtract first_t to avoid precision problems
386 double n = tap_count;
391 double T = (sum_xy/n - sum_x/n * sum_y/n) / (sum_xx/n - sum_x/n * sum_x/n);
393 if (t - last_t < T / 1.2 || t - last_t > T * 1.2) {
396 bpm_spinner.set_value (60.0 / T);
400 first_t = g_get_monotonic_time ();
417 TempoDialog::tap_tempo_focus_out (GdkEventFocus* )
423 MeterDialog::MeterDialog (TempoMap& map, framepos_t frame, const string&)
424 : ArdourDialog (_("New Meter"))
426 frame = map.round_to_bar(frame, RoundNearest).frame;
427 Timecode::BBT_Time when (map.bbt_at_frame (frame));
428 Meter meter (map.meter_at_frame (frame));
430 init (when, meter.divisions_per_bar(), meter.note_divisor(), false, MusicTime);
433 MeterDialog::MeterDialog (TempoMap& map, MeterSection& section, const string&)
434 : ArdourDialog (_("Edit Meter"))
436 Timecode::BBT_Time when (map.bbt_at_frame (section.frame()));
438 init (when, section.divisions_per_bar(), section.note_divisor(), section.initial(), section.position_lock_style());
442 MeterDialog::init (const Timecode::BBT_Time& when, double bpb, double divisor, bool initial, PositionLockStyle style)
445 vector<string> strings;
446 NoteTypes::iterator x;
448 snprintf (buf, sizeof (buf), "%.2f", bpb);
449 bpb_entry.set_text (buf);
450 bpb_entry.select_region (0, -1);
451 bpb_entry.set_alignment (1.0);
453 note_types.insert (make_pair (_("whole"), 1.0));
454 strings.push_back (_("whole"));
455 note_types.insert (make_pair (_("second"), 2.0));
456 strings.push_back (_("second"));
457 note_types.insert (make_pair (_("third"), 3.0));
458 strings.push_back (_("third"));
459 note_types.insert (make_pair (_("quarter"), 4.0));
460 strings.push_back (_("quarter"));
461 note_types.insert (make_pair (_("eighth"), 8.0));
462 strings.push_back (_("eighth"));
463 note_types.insert (make_pair (_("sixteenth"), 16.0));
464 strings.push_back (_("sixteenth"));
465 note_types.insert (make_pair (_("thirty-second"), 32.0));
466 strings.push_back (_("thirty-second"));
467 note_types.insert (make_pair (_("sixty-fourth"), 64.0));
468 strings.push_back (_("sixty-fourth"));
469 note_types.insert (make_pair (_("one-hundred-twenty-eighth"), 128.0));
470 strings.push_back (_("one-hundred-twenty-eighth"));
472 set_popdown_strings (note_type, strings);
474 for (x = note_types.begin(); x != note_types.end(); ++x) {
475 if (x->second == divisor) {
476 note_type.set_active_text (x->first);
481 if (x == note_types.end()) {
482 note_type.set_active_text (strings[3]); // "quarter"
487 lock_styles.insert (make_pair (_("music"), MusicTime));
488 strings.push_back (_("music"));
489 lock_styles.insert (make_pair (_("audio"), AudioTime));
490 strings.push_back (_("audio"));
491 set_popdown_strings (lock_style, strings);
492 LockStyles::iterator ls;
493 for (ls = lock_styles.begin(); ls != lock_styles.end(); ++ls) {
494 if (ls->second == style) {
495 lock_style.set_active_text (ls->first);
499 if (ls == lock_styles.end()) {
500 lock_style.set_active_text (strings[0]); // "music"
503 Label* note_label = manage (new Label (_("Note value:"), ALIGN_RIGHT, ALIGN_CENTER));
504 Label* lock_label = manage (new Label (_("Lock style:"), ALIGN_RIGHT, ALIGN_CENTER));
505 Label* bpb_label = manage (new Label (_("Beats per bar:"), ALIGN_RIGHT, ALIGN_CENTER));
506 Table* table = manage (new Table (3, 3));
507 table->set_spacings (6);
509 table->attach (*bpb_label, 0, 1, 0, 1, FILL|EXPAND, FILL|EXPAND);
510 table->attach (bpb_entry, 1, 2, 0, 1, FILL|EXPAND, FILL|EXPAND);
511 table->attach (*note_label, 0, 1, 1, 2, FILL|EXPAND, FILL|EXPAND);
512 table->attach (note_type, 1, 2, 1, 2, FILL|EXPAND, FILL|EXPAND);
514 snprintf (buf, sizeof (buf), "%" PRIu32, when.bars);
515 when_bar_entry.set_text (buf);
516 when_bar_entry.set_alignment (1.0);
519 Label* when_label = manage (new Label(_("Meter begins at bar:"), ALIGN_LEFT, ALIGN_CENTER));
521 table->attach (*when_label, 0, 1, 2, 3, FILL | EXPAND, FILL | EXPAND);
522 table->attach (when_bar_entry, 1, 2, 2, 3, FILL | EXPAND, FILL | EXPAND);
524 table->attach (*lock_label, 0, 1, 3, 4, FILL|EXPAND, FILL|EXPAND);
525 table->attach (lock_style, 1, 2, 3, 4, FILL|EXPAND, SHRINK);
528 get_vbox()->set_border_width (12);
529 get_vbox()->pack_start (*table, false, false);
531 add_button (Stock::CANCEL, RESPONSE_CANCEL);
532 add_button (Stock::APPLY, RESPONSE_ACCEPT);
533 set_response_sensitive (RESPONSE_ACCEPT, true);
534 set_default_response (RESPONSE_ACCEPT);
536 get_vbox()->show_all ();
538 set_name ("MetricDialog");
539 bpb_entry.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &MeterDialog::response), RESPONSE_ACCEPT));
540 bpb_entry.signal_key_press_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_press), false);
541 bpb_entry.signal_key_release_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_release));
542 when_bar_entry.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &MeterDialog::response), RESPONSE_ACCEPT));
543 when_bar_entry.signal_key_press_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_press), false);
544 when_bar_entry.signal_key_release_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_release));
545 note_type.signal_changed().connect (sigc::mem_fun (*this, &MeterDialog::note_type_change));
546 lock_style.signal_changed().connect (sigc::mem_fun (*this, &MeterDialog::lock_style_change));
551 MeterDialog::is_user_input_valid() const
553 return (when_bar_entry.get_text() != "")
554 && (when_bar_entry.get_text() != "0")
555 && (bpb_entry.get_text() != "");
559 MeterDialog::entry_key_press (GdkEventKey* ev)
562 switch (ev->keyval) {
607 MeterDialog::entry_key_release (GdkEventKey*)
609 set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
614 MeterDialog::note_type_change ()
616 set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
620 MeterDialog::lock_style_change ()
622 set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
626 MeterDialog::get_bpb ()
630 if (sscanf (bpb_entry.get_text().c_str(), "%lf", &bpb) != 1) {
638 MeterDialog::get_note_type ()
640 NoteTypes::iterator x = note_types.find (note_type.get_active_text());
642 if (x == note_types.end()) {
643 error << string_compose(_("incomprehensible meter note type (%1)"), note_type.get_active_text()) << endmsg;
651 MeterDialog::get_lock_style ()
653 LockStyles::iterator x = lock_styles.find (lock_style.get_active_text());
655 if (x == lock_styles.end()) {
656 error << string_compose(_("incomprehensible meter lock style (%1)"), lock_style.get_active_text()) << endmsg;
664 MeterDialog::get_bbt_time (Timecode::BBT_Time& requested)
666 if (sscanf (when_bar_entry.get_text().c_str(), "%" PRIu32, &requested.bars) != 1) {