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 "ardour_ui.h"
27 #include "tempo_dialog.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);
76 note_types.insert (make_pair (_("whole"), 1.0));
77 strings.push_back (_("whole"));
78 note_types.insert (make_pair (_("second"), 2.0));
79 strings.push_back (_("second"));
80 note_types.insert (make_pair (_("third"), 3.0));
81 strings.push_back (_("third"));
82 note_types.insert (make_pair (_("quarter"), 4.0));
83 strings.push_back (_("quarter"));
84 note_types.insert (make_pair (_("eighth"), 8.0));
85 strings.push_back (_("eighth"));
86 note_types.insert (make_pair (_("sixteenth"), 16.0));
87 strings.push_back (_("sixteenth"));
88 note_types.insert (make_pair (_("thirty-second"), 32.0));
89 strings.push_back (_("thirty-second"));
90 note_types.insert (make_pair (_("sixty-fourth"), 64.0));
91 strings.push_back (_("sixty-fourth"));
92 note_types.insert (make_pair (_("one-hundred-twenty-eighth"), 128.0));
93 strings.push_back (_("one-hundred-twenty-eighth"));
95 set_popdown_strings (pulse_selector, strings);
97 for (x = note_types.begin(); x != note_types.end(); ++x) {
98 if (x->second == note_type) {
99 pulse_selector.set_active_text (x->first);
104 if (x == note_types.end()) {
105 pulse_selector.set_active_text (strings[3]); // "quarter"
110 if (ARDOUR_UI::config()->get_allow_non_quarter_pulse()) {
111 table = manage (new Table (5, 5));
113 table = manage (new Table (5, 4));
116 table->set_spacings (6);
117 table->set_homogeneous (false);
120 Label* bpm_label = manage (new Label(_("Beats per minute:"), ALIGN_LEFT, ALIGN_CENTER));
121 table->attach (*bpm_label, 0, 1, 0, 1);
122 table->attach (bpm_spinner, 1, 5, 0, 1);
124 if (ARDOUR_UI::config()->get_allow_non_quarter_pulse()) {
125 table->attach (pulse_selector_label, 0, 1, 1, 2);
126 table->attach (pulse_selector, 1, 5, 1, 2);
134 snprintf (buf, sizeof (buf), "%" PRIu32, when.bars);
135 when_bar_entry.set_text (buf);
136 snprintf (buf, sizeof (buf), "%" PRIu32, when.beats);
137 when_beat_entry.set_text (buf);
140 when_bar_entry.set_width_chars(4);
141 when_beat_entry.set_width_chars (4);
143 when_bar_label.set_name ("MetricLabel");
144 when_beat_label.set_name ("MetricLabel");
146 table->attach (when_bar_label, 1, 2, row, row+1, Gtk::AttachOptions(0), Gtk::AttachOptions(0));
147 table->attach (when_bar_entry, 2, 3, row, row+1, Gtk::AttachOptions(0), Gtk::AttachOptions(0));
149 table->attach (when_beat_label, 3, 4, row, row+1, Gtk::AttachOptions(0), Gtk::AttachOptions(0));
150 table->attach (when_beat_entry, 4, 5, row, row+1, Gtk::AttachOptions(0), Gtk::AttachOptions(0));
152 Label* when_label = manage (new Label(_("Tempo begins at"), ALIGN_LEFT, ALIGN_CENTER));
153 table->attach (*when_label, 0, 1, row, row+1);
156 get_vbox()->set_border_width (12);
157 get_vbox()->pack_end (*table);
160 add_button (Stock::CANCEL, RESPONSE_CANCEL);
161 add_button (Stock::APPLY, RESPONSE_ACCEPT);
162 set_response_sensitive (RESPONSE_ACCEPT, true);
163 set_default_response (RESPONSE_ACCEPT);
166 tap_tempo_button.show ();
167 get_vbox()->pack_end (tap_tempo_button);
168 bpm_spinner.grab_focus ();
170 set_name ("MetricDialog");
172 bpm_spinner.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &TempoDialog::response), RESPONSE_ACCEPT));
173 bpm_spinner.signal_button_press_event().connect (sigc::mem_fun (*this, &TempoDialog::bpm_button_press), false);
174 bpm_spinner.signal_button_release_event().connect (sigc::mem_fun (*this, &TempoDialog::bpm_button_release), false);
175 bpm_spinner.signal_changed().connect (sigc::mem_fun (*this, &TempoDialog::bpm_changed));
176 when_bar_entry.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &TempoDialog::response), RESPONSE_ACCEPT));
177 when_bar_entry.signal_key_release_event().connect (sigc::mem_fun (*this, &TempoDialog::entry_key_release), false);
178 when_beat_entry.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &TempoDialog::response), RESPONSE_ACCEPT));
179 when_beat_entry.signal_key_release_event().connect (sigc::mem_fun (*this, &TempoDialog::entry_key_release), false);
180 pulse_selector.signal_changed().connect (sigc::mem_fun (*this, &TempoDialog::pulse_change));
181 tap_tempo_button.signal_button_press_event().connect (sigc::mem_fun (*this, &TempoDialog::tap_tempo_button_press), false);
182 tap_tempo_button.signal_focus_out_event().connect (sigc::mem_fun (*this, &TempoDialog::tap_tempo_focus_out));
188 TempoDialog::is_user_input_valid() const
190 return (when_beat_entry.get_text() != "")
191 && (when_bar_entry.get_text() != "")
192 && (when_bar_entry.get_text() != "0");
196 TempoDialog::bpm_changed ()
198 set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
202 TempoDialog::bpm_button_press (GdkEventButton*)
208 TempoDialog::bpm_button_release (GdkEventButton*)
210 /* the value has been modified, accept should work now */
212 set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
217 TempoDialog::entry_key_release (GdkEventKey*)
219 set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
224 TempoDialog::get_bpm ()
226 return bpm_spinner.get_value ();
230 TempoDialog::get_bbt_time (Timecode::BBT_Time& requested)
232 if (sscanf (when_bar_entry.get_text().c_str(), "%" PRIu32, &requested.bars) != 1) {
236 if (sscanf (when_beat_entry.get_text().c_str(), "%" PRIu32, &requested.beats) != 1) {
246 TempoDialog::get_note_type ()
248 NoteTypes::iterator x = note_types.find (pulse_selector.get_active_text());
250 if (x == note_types.end()) {
251 error << string_compose(_("incomprehensible pulse note type (%1)"), pulse_selector.get_active_text()) << endmsg;
259 TempoDialog::pulse_change ()
261 set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
265 TempoDialog::tap_tempo_button_press (GdkEventButton *ev)
268 now = g_get_monotonic_time (); // microseconds
271 double interval, bpm;
272 static const double decay = 0.5;
274 interval = (now - last_tap) * 1.0e-6;
275 if (interval <= 6.0) {
276 // <= 6 seconds (say): >= 10 bpm
277 if (average_interval > 0) {
278 if (average_interval > interval / 1.2 && average_interval < interval * 1.2) {
279 average_interval = interval * decay
280 + average_interval * (1.0-decay);
282 average_interval = 0;
285 average_interval = interval;
288 if (average_interval > 0) {
289 bpm = 60.0 / average_interval;
290 bpm_spinner.set_value (bpm);
293 average_interval = 0;
296 average_interval = 0;
303 TempoDialog::tap_tempo_focus_out (GdkEventFocus* )
309 MeterDialog::MeterDialog (TempoMap& map, framepos_t frame, const string&)
310 : ArdourDialog (_("New Meter"))
312 Timecode::BBT_Time when;
313 frame = map.round_to_bar(frame, RoundNearest);
314 Meter meter (map.meter_at(frame));
316 map.bbt_time (frame, when);
317 init (when, meter.divisions_per_bar(), meter.note_divisor(), true);
320 MeterDialog::MeterDialog (MeterSection& section, const string&)
321 : ArdourDialog (_("Edit Meter"))
323 init (section.start(), section.divisions_per_bar(), section.note_divisor(), section.movable());
327 MeterDialog::init (const Timecode::BBT_Time& when, double bpb, double divisor, bool movable)
330 vector<string> strings;
331 NoteTypes::iterator x;
333 snprintf (buf, sizeof (buf), "%.2f", bpb);
334 bpb_entry.set_text (buf);
335 bpb_entry.select_region (0, -1);
337 note_types.insert (make_pair (_("whole"), 1.0));
338 strings.push_back (_("whole"));
339 note_types.insert (make_pair (_("second"), 2.0));
340 strings.push_back (_("second"));
341 note_types.insert (make_pair (_("third"), 3.0));
342 strings.push_back (_("third"));
343 note_types.insert (make_pair (_("quarter"), 4.0));
344 strings.push_back (_("quarter"));
345 note_types.insert (make_pair (_("eighth"), 8.0));
346 strings.push_back (_("eighth"));
347 note_types.insert (make_pair (_("sixteenth"), 16.0));
348 strings.push_back (_("sixteenth"));
349 note_types.insert (make_pair (_("thirty-second"), 32.0));
350 strings.push_back (_("thirty-second"));
351 note_types.insert (make_pair (_("sixty-fourth"), 64.0));
352 strings.push_back (_("sixty-fourth"));
353 note_types.insert (make_pair (_("one-hundred-twenty-eighth"), 128.0));
354 strings.push_back (_("one-hundred-twenty-eighth"));
356 set_popdown_strings (note_type, strings);
358 for (x = note_types.begin(); x != note_types.end(); ++x) {
359 if (x->second == divisor) {
360 note_type.set_active_text (x->first);
365 if (x == note_types.end()) {
366 note_type.set_active_text (strings[3]); // "quarter"
369 Label* note_label = manage (new Label (_("Note value:"), ALIGN_LEFT, ALIGN_CENTER));
370 Label* bpb_label = manage (new Label (_("Beats per bar:"), ALIGN_LEFT, ALIGN_CENTER));
371 Table* table = manage (new Table (3, 2));
372 table->set_spacings (6);
374 table->attach (*bpb_label, 0, 1, 0, 1, FILL|EXPAND, FILL|EXPAND);
375 table->attach (bpb_entry, 1, 2, 0, 1, FILL|EXPAND, FILL|EXPAND);
376 table->attach (*note_label, 0, 1, 1, 2, FILL|EXPAND, FILL|EXPAND);
377 table->attach (note_type, 1, 2, 1, 2, FILL|EXPAND, SHRINK);
379 snprintf (buf, sizeof (buf), "%" PRIu32, when.bars);
380 when_bar_entry.set_text (buf);
383 Label* when_label = manage (new Label(_("Meter begins at bar:"), ALIGN_LEFT, ALIGN_CENTER));
385 table->attach (*when_label, 0, 1, 2, 3, FILL | EXPAND, FILL | EXPAND);
386 table->attach (when_bar_entry, 1, 2, 2, 3, FILL | EXPAND, FILL | EXPAND);
389 get_vbox()->set_border_width (12);
390 get_vbox()->pack_start (*table, false, false);
392 add_button (Stock::CANCEL, RESPONSE_CANCEL);
393 add_button (Stock::APPLY, RESPONSE_ACCEPT);
394 set_response_sensitive (RESPONSE_ACCEPT, true);
395 set_default_response (RESPONSE_ACCEPT);
397 get_vbox()->show_all ();
399 set_name ("MetricDialog");
400 bpb_entry.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &MeterDialog::response), RESPONSE_ACCEPT));
401 bpb_entry.signal_key_press_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_press), false);
402 bpb_entry.signal_key_release_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_release));
403 when_bar_entry.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &MeterDialog::response), RESPONSE_ACCEPT));
404 when_bar_entry.signal_key_press_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_press), false);
405 when_bar_entry.signal_key_release_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_release));
406 note_type.signal_changed().connect (sigc::mem_fun (*this, &MeterDialog::note_type_change));
410 MeterDialog::is_user_input_valid() const
412 return (when_bar_entry.get_text() != "")
413 && (when_bar_entry.get_text() != "0")
414 && (bpb_entry.get_text() != "");
418 MeterDialog::entry_key_press (GdkEventKey* ev)
421 switch (ev->keyval) {
466 MeterDialog::entry_key_release (GdkEventKey*)
468 set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
473 MeterDialog::note_type_change ()
475 set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
479 MeterDialog::get_bpb ()
483 if (sscanf (bpb_entry.get_text().c_str(), "%lf", &bpb) != 1) {
491 MeterDialog::get_note_type ()
493 NoteTypes::iterator x = note_types.find (note_type.get_active_text());
495 if (x == note_types.end()) {
496 error << string_compose(_("incomprehensible meter note type (%1)"), note_type.get_active_text()) << endmsg;
504 MeterDialog::get_bbt_time (Timecode::BBT_Time& requested)
506 if (sscanf (when_bar_entry.get_text().c_str(), "%" PRIu32, &requested.bars) != 1) {