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"))
39 , bpm_adjustment (60.0, 1.0, 999.9, 0.1, 1.0)
40 , bpm_spinner (bpm_adjustment)
41 , when_bar_label (_("bar:"), ALIGN_LEFT, ALIGN_CENTER)
42 , when_beat_label (_("beat:"), ALIGN_LEFT, ALIGN_CENTER)
43 , pulse_selector_label (_("Pulse note"), ALIGN_LEFT, ALIGN_CENTER)
44 , tap_tempo_button (_("Tap tempo"))
46 Timecode::BBT_Time when;
47 Tempo tempo (map.tempo_at (frame));
48 map.bbt_time (frame, when);
50 init (when, tempo.beats_per_minute(), tempo.note_type(), true);
53 TempoDialog::TempoDialog (TempoSection& section, const string&)
54 : ArdourDialog (_("Edit Tempo"))
55 , bpm_adjustment (60.0, 1.0, 999.9, 0.1, 1.0)
56 , bpm_spinner (bpm_adjustment)
57 , when_bar_label (_("bar:"), ALIGN_LEFT, ALIGN_CENTER)
58 , when_beat_label (_("beat:"), ALIGN_LEFT, ALIGN_CENTER)
59 , pulse_selector_label (_("Pulse note"), ALIGN_LEFT, ALIGN_CENTER)
60 , tap_tempo_button (_("Tap tempo"))
62 init (section.start(), section.beats_per_minute(), section.note_type(), section.movable());
66 TempoDialog::init (const Timecode::BBT_Time& when, double bpm, double note_type, bool movable)
68 vector<string> strings;
69 NoteTypes::iterator x;
71 bpm_spinner.set_numeric (true);
72 bpm_spinner.set_digits (2);
73 bpm_spinner.set_wrap (true);
74 bpm_spinner.set_value (bpm);
75 bpm_spinner.set_alignment (1.0);
77 note_types.insert (make_pair (_("whole"), 1.0));
78 strings.push_back (_("whole"));
79 note_types.insert (make_pair (_("second"), 2.0));
80 strings.push_back (_("second"));
81 note_types.insert (make_pair (_("third"), 3.0));
82 strings.push_back (_("third"));
83 note_types.insert (make_pair (_("quarter"), 4.0));
84 strings.push_back (_("quarter"));
85 note_types.insert (make_pair (_("eighth"), 8.0));
86 strings.push_back (_("eighth"));
87 note_types.insert (make_pair (_("sixteenth"), 16.0));
88 strings.push_back (_("sixteenth"));
89 note_types.insert (make_pair (_("thirty-second"), 32.0));
90 strings.push_back (_("thirty-second"));
91 note_types.insert (make_pair (_("sixty-fourth"), 64.0));
92 strings.push_back (_("sixty-fourth"));
93 note_types.insert (make_pair (_("one-hundred-twenty-eighth"), 128.0));
94 strings.push_back (_("one-hundred-twenty-eighth"));
96 set_popdown_strings (pulse_selector, strings);
98 for (x = note_types.begin(); x != note_types.end(); ++x) {
99 if (x->second == note_type) {
100 pulse_selector.set_active_text (x->first);
105 if (x == note_types.end()) {
106 pulse_selector.set_active_text (strings[3]); // "quarter"
111 if (UIConfiguration::instance().get_allow_non_quarter_pulse()) {
112 table = manage (new Table (5, 5));
114 table = manage (new Table (5, 4));
117 table->set_spacings (6);
118 table->set_homogeneous (false);
121 Label* bpm_label = manage (new Label(_("Beats per minute:"), ALIGN_LEFT, ALIGN_CENTER));
122 table->attach (*bpm_label, 0, 1, 0, 1);
123 table->attach (bpm_spinner, 1, 5, 0, 1);
125 if (UIConfiguration::instance().get_allow_non_quarter_pulse()) {
126 table->attach (pulse_selector_label, 0, 1, 1, 2);
127 table->attach (pulse_selector, 1, 5, 1, 2);
135 snprintf (buf, sizeof (buf), "%" PRIu32, when.bars);
136 when_bar_entry.set_text (buf);
137 snprintf (buf, sizeof (buf), "%" PRIu32, when.beats);
138 when_beat_entry.set_text (buf);
141 when_bar_entry.set_width_chars(4);
142 when_beat_entry.set_width_chars (4);
143 when_bar_entry.set_alignment (1.0);
144 when_beat_entry.set_alignment (1.0);
146 when_bar_label.set_name ("MetricLabel");
147 when_beat_label.set_name ("MetricLabel");
149 table->attach (when_bar_label, 1, 2, row, row+1, Gtk::AttachOptions(0), Gtk::AttachOptions(0));
150 table->attach (when_bar_entry, 2, 3, row, row+1, Gtk::AttachOptions(0), Gtk::AttachOptions(0));
152 table->attach (when_beat_label, 3, 4, row, row+1, Gtk::AttachOptions(0), Gtk::AttachOptions(0));
153 table->attach (when_beat_entry, 4, 5, row, row+1, Gtk::AttachOptions(0), Gtk::AttachOptions(0));
155 Label* when_label = manage (new Label(_("Tempo begins at"), ALIGN_LEFT, ALIGN_CENTER));
156 table->attach (*when_label, 0, 1, row, row+1);
159 get_vbox()->set_border_width (12);
160 get_vbox()->pack_end (*table);
163 add_button (Stock::CANCEL, RESPONSE_CANCEL);
164 add_button (Stock::APPLY, RESPONSE_ACCEPT);
165 set_response_sensitive (RESPONSE_ACCEPT, true);
166 set_default_response (RESPONSE_ACCEPT);
169 tap_tempo_button.show ();
170 get_vbox()->pack_end (tap_tempo_button);
171 bpm_spinner.grab_focus ();
173 set_name ("MetricDialog");
175 bpm_spinner.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &TempoDialog::response), RESPONSE_ACCEPT));
176 bpm_spinner.signal_button_press_event().connect (sigc::mem_fun (*this, &TempoDialog::bpm_button_press), false);
177 bpm_spinner.signal_button_release_event().connect (sigc::mem_fun (*this, &TempoDialog::bpm_button_release), false);
178 bpm_spinner.signal_changed().connect (sigc::mem_fun (*this, &TempoDialog::bpm_changed));
179 when_bar_entry.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &TempoDialog::response), RESPONSE_ACCEPT));
180 when_bar_entry.signal_key_release_event().connect (sigc::mem_fun (*this, &TempoDialog::entry_key_release), false);
181 when_beat_entry.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &TempoDialog::response), RESPONSE_ACCEPT));
182 when_beat_entry.signal_key_release_event().connect (sigc::mem_fun (*this, &TempoDialog::entry_key_release), false);
183 pulse_selector.signal_changed().connect (sigc::mem_fun (*this, &TempoDialog::pulse_change));
184 tap_tempo_button.signal_button_press_event().connect (sigc::mem_fun (*this, &TempoDialog::tap_tempo_button_press), false);
185 tap_tempo_button.signal_focus_out_event().connect (sigc::mem_fun (*this, &TempoDialog::tap_tempo_focus_out));
191 TempoDialog::is_user_input_valid() const
193 return (when_beat_entry.get_text() != "")
194 && (when_bar_entry.get_text() != "")
195 && (when_bar_entry.get_text() != "0");
199 TempoDialog::bpm_changed ()
201 set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
205 TempoDialog::bpm_button_press (GdkEventButton*)
211 TempoDialog::bpm_button_release (GdkEventButton*)
213 /* the value has been modified, accept should work now */
215 set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
220 TempoDialog::entry_key_release (GdkEventKey*)
222 set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
227 TempoDialog::get_bpm ()
229 return bpm_spinner.get_value ();
233 TempoDialog::get_bbt_time (Timecode::BBT_Time& requested)
235 if (sscanf (when_bar_entry.get_text().c_str(), "%" PRIu32, &requested.bars) != 1) {
239 if (sscanf (when_beat_entry.get_text().c_str(), "%" PRIu32, &requested.beats) != 1) {
249 TempoDialog::get_note_type ()
251 NoteTypes::iterator x = note_types.find (pulse_selector.get_active_text());
253 if (x == note_types.end()) {
254 error << string_compose(_("incomprehensible pulse note type (%1)"), pulse_selector.get_active_text()) << endmsg;
262 TempoDialog::pulse_change ()
264 set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
268 TempoDialog::tap_tempo_button_press (GdkEventButton *ev)
272 // Linear least-squares regression
274 t = 1e-6 * (g_get_monotonic_time () - first_t); // Subtract first_t to avoid precision problems
276 double n = tap_count;
281 double T = (sum_xy/n - sum_x/n * sum_y/n) / (sum_xx/n - sum_x/n * sum_x/n);
283 if (t - last_t < T / 1.2 || t - last_t > T * 1.2) {
286 bpm_spinner.set_value (60.0 / T);
290 first_t = g_get_monotonic_time ();
307 TempoDialog::tap_tempo_focus_out (GdkEventFocus* )
313 MeterDialog::MeterDialog (TempoMap& map, framepos_t frame, const string&)
314 : ArdourDialog (_("New Meter"))
316 Timecode::BBT_Time when;
317 frame = map.round_to_bar(frame, RoundNearest);
318 Meter meter (map.meter_at(frame));
320 map.bbt_time (frame, when);
321 init (when, meter.divisions_per_bar(), meter.note_divisor(), true);
324 MeterDialog::MeterDialog (MeterSection& section, const string&)
325 : ArdourDialog (_("Edit Meter"))
327 init (section.start(), section.divisions_per_bar(), section.note_divisor(), section.movable());
331 MeterDialog::init (const Timecode::BBT_Time& when, double bpb, double divisor, bool movable)
334 vector<string> strings;
335 NoteTypes::iterator x;
337 snprintf (buf, sizeof (buf), "%.2f", bpb);
338 bpb_entry.set_text (buf);
339 bpb_entry.select_region (0, -1);
340 bpb_entry.set_alignment (1.0);
342 note_types.insert (make_pair (_("whole"), 1.0));
343 strings.push_back (_("whole"));
344 note_types.insert (make_pair (_("second"), 2.0));
345 strings.push_back (_("second"));
346 note_types.insert (make_pair (_("third"), 3.0));
347 strings.push_back (_("third"));
348 note_types.insert (make_pair (_("quarter"), 4.0));
349 strings.push_back (_("quarter"));
350 note_types.insert (make_pair (_("eighth"), 8.0));
351 strings.push_back (_("eighth"));
352 note_types.insert (make_pair (_("sixteenth"), 16.0));
353 strings.push_back (_("sixteenth"));
354 note_types.insert (make_pair (_("thirty-second"), 32.0));
355 strings.push_back (_("thirty-second"));
356 note_types.insert (make_pair (_("sixty-fourth"), 64.0));
357 strings.push_back (_("sixty-fourth"));
358 note_types.insert (make_pair (_("one-hundred-twenty-eighth"), 128.0));
359 strings.push_back (_("one-hundred-twenty-eighth"));
361 set_popdown_strings (note_type, strings);
363 for (x = note_types.begin(); x != note_types.end(); ++x) {
364 if (x->second == divisor) {
365 note_type.set_active_text (x->first);
370 if (x == note_types.end()) {
371 note_type.set_active_text (strings[3]); // "quarter"
374 Label* note_label = manage (new Label (_("Note value:"), ALIGN_LEFT, ALIGN_CENTER));
375 Label* bpb_label = manage (new Label (_("Beats per bar:"), ALIGN_LEFT, ALIGN_CENTER));
376 Table* table = manage (new Table (3, 2));
377 table->set_spacings (6);
379 table->attach (*bpb_label, 0, 1, 0, 1, FILL|EXPAND, FILL|EXPAND);
380 table->attach (bpb_entry, 1, 2, 0, 1, FILL|EXPAND, FILL|EXPAND);
381 table->attach (*note_label, 0, 1, 1, 2, FILL|EXPAND, FILL|EXPAND);
382 table->attach (note_type, 1, 2, 1, 2, FILL|EXPAND, SHRINK);
384 snprintf (buf, sizeof (buf), "%" PRIu32, when.bars);
385 when_bar_entry.set_text (buf);
386 when_bar_entry.set_alignment (1.0);
389 Label* when_label = manage (new Label(_("Meter begins at bar:"), ALIGN_LEFT, ALIGN_CENTER));
391 table->attach (*when_label, 0, 1, 2, 3, FILL | EXPAND, FILL | EXPAND);
392 table->attach (when_bar_entry, 1, 2, 2, 3, FILL | EXPAND, FILL | EXPAND);
395 get_vbox()->set_border_width (12);
396 get_vbox()->pack_start (*table, false, false);
398 add_button (Stock::CANCEL, RESPONSE_CANCEL);
399 add_button (Stock::APPLY, RESPONSE_ACCEPT);
400 set_response_sensitive (RESPONSE_ACCEPT, true);
401 set_default_response (RESPONSE_ACCEPT);
403 get_vbox()->show_all ();
405 set_name ("MetricDialog");
406 bpb_entry.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &MeterDialog::response), RESPONSE_ACCEPT));
407 bpb_entry.signal_key_press_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_press), false);
408 bpb_entry.signal_key_release_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_release));
409 when_bar_entry.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &MeterDialog::response), RESPONSE_ACCEPT));
410 when_bar_entry.signal_key_press_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_press), false);
411 when_bar_entry.signal_key_release_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_release));
412 note_type.signal_changed().connect (sigc::mem_fun (*this, &MeterDialog::note_type_change));
416 MeterDialog::is_user_input_valid() const
418 return (when_bar_entry.get_text() != "")
419 && (when_bar_entry.get_text() != "0")
420 && (bpb_entry.get_text() != "");
424 MeterDialog::entry_key_press (GdkEventKey* ev)
427 switch (ev->keyval) {
472 MeterDialog::entry_key_release (GdkEventKey*)
474 set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
479 MeterDialog::note_type_change ()
481 set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
485 MeterDialog::get_bpb ()
489 if (sscanf (bpb_entry.get_text().c_str(), "%lf", &bpb) != 1) {
497 MeterDialog::get_note_type ()
499 NoteTypes::iterator x = note_types.find (note_type.get_active_text());
501 if (x == note_types.end()) {
502 error << string_compose(_("incomprehensible meter note type (%1)"), note_type.get_active_text()) << endmsg;
510 MeterDialog::get_bbt_time (Timecode::BBT_Time& requested)
512 if (sscanf (when_bar_entry.get_text().c_str(), "%" PRIu32, &requested.bars) != 1) {