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/rc_configuration.h"
28 #include "tempo_dialog.h"
34 using namespace Gtkmm2ext;
35 using namespace ARDOUR;
38 TempoDialog::TempoDialog (TempoMap& map, framepos_t frame, const string&)
39 : ArdourDialog (_("New Tempo"))
40 , bpm_adjustment (60.0, 1.0, 999.9, 0.1, 1.0)
41 , bpm_spinner (bpm_adjustment)
42 , when_bar_label (_("bar:"), ALIGN_LEFT, ALIGN_CENTER)
43 , when_beat_label (_("beat:"), ALIGN_LEFT, ALIGN_CENTER)
44 , pulse_selector_label (_("Pulse note"), ALIGN_LEFT, ALIGN_CENTER)
45 , tap_tempo_button (_("Tap tempo"))
47 Timecode::BBT_Time when;
48 Tempo tempo (map.tempo_at (frame));
49 map.bbt_time (frame, when);
51 init (when, tempo.beats_per_minute(), tempo.note_type(), true);
54 TempoDialog::TempoDialog (TempoSection& section, const string&)
55 : ArdourDialog (_("Edit Tempo"))
56 , bpm_adjustment (60.0, 1.0, 999.9, 0.1, 1.0)
57 , bpm_spinner (bpm_adjustment)
58 , when_bar_label (_("bar:"), ALIGN_LEFT, ALIGN_CENTER)
59 , when_beat_label (_("beat:"), ALIGN_LEFT, ALIGN_CENTER)
60 , pulse_selector_label (_("Pulse note"), ALIGN_LEFT, ALIGN_CENTER)
61 , tap_tempo_button (_("Tap tempo"))
63 init (section.start(), section.beats_per_minute(), section.note_type(), section.movable());
67 TempoDialog::init (const Timecode::BBT_Time& when, double bpm, double note_type, bool movable)
69 vector<string> strings;
70 NoteTypes::iterator x;
72 bpm_spinner.set_numeric (true);
73 bpm_spinner.set_digits (2);
74 bpm_spinner.set_wrap (true);
75 bpm_spinner.set_value (bpm);
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 (Config->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 (Config->get_allow_non_quarter_pulse()) {
126 table->attach (pulse_selector_label, 0, 1, 1, 2);
127 table->attach (pulse_selector, 1, 5, 1, 2);
136 snprintf (buf, sizeof (buf), "%" PRIu32, when.bars);
137 when_bar_entry.set_text (buf);
138 snprintf (buf, sizeof (buf), "%" PRIu32, when.beats);
139 when_beat_entry.set_text (buf);
141 when_bar_entry.set_width_chars(4);
142 when_beat_entry.set_width_chars (4);
144 when_bar_label.set_name ("MetricLabel");
145 when_beat_label.set_name ("MetricLabel");
147 table->attach (when_bar_label, 1, 2, row, row+1, Gtk::AttachOptions(0), Gtk::AttachOptions(0));
148 table->attach (when_bar_entry, 2, 3, row, row+1, Gtk::AttachOptions(0), Gtk::AttachOptions(0));
150 table->attach (when_beat_label, 3, 4, row, row+1, Gtk::AttachOptions(0), Gtk::AttachOptions(0));
151 table->attach (when_beat_entry, 4, 5, row, row+1, Gtk::AttachOptions(0), Gtk::AttachOptions(0));
153 Label* when_label = manage (new Label(_("Tempo begins at"), ALIGN_LEFT, ALIGN_CENTER));
154 table->attach (*when_label, 0, 1, row, row+1);
157 get_vbox()->set_border_width (12);
158 get_vbox()->pack_end (*table);
161 add_button (Stock::CANCEL, RESPONSE_CANCEL);
162 add_button (Stock::APPLY, RESPONSE_ACCEPT);
163 set_response_sensitive (RESPONSE_ACCEPT, false);
164 set_default_response (RESPONSE_ACCEPT);
167 tap_tempo_button.show ();
168 get_vbox()->pack_end (tap_tempo_button);
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_clicked().connect (sigc::mem_fun (*this, &TempoDialog::tap_tempo));
185 TempoDialog::bpm_changed ()
187 set_response_sensitive (RESPONSE_ACCEPT, true);
191 TempoDialog::bpm_button_press (GdkEventButton*)
197 TempoDialog::bpm_button_release (GdkEventButton*)
199 /* the value has been modified, accept should work now */
201 set_response_sensitive (RESPONSE_ACCEPT, true);
206 TempoDialog::entry_key_release (GdkEventKey*)
208 if (when_beat_entry.get_text() != "" && when_bar_entry.get_text() != "") {
209 set_response_sensitive (RESPONSE_ACCEPT, true);
211 set_response_sensitive (RESPONSE_ACCEPT, false);
217 TempoDialog::get_bpm ()
219 return bpm_spinner.get_value ();
223 TempoDialog::get_bbt_time (Timecode::BBT_Time& requested)
225 if (sscanf (when_bar_entry.get_text().c_str(), "%" PRIu32, &requested.bars) != 1) {
229 if (sscanf (when_beat_entry.get_text().c_str(), "%" PRIu32, &requested.beats) != 1) {
239 TempoDialog::get_note_type ()
241 NoteTypes::iterator x = note_types.find (pulse_selector.get_active_text());
243 if (x == note_types.end()) {
244 error << string_compose(_("incomprehensible pulse note type (%1)"), pulse_selector.get_active_text()) << endmsg;
252 TempoDialog::pulse_change ()
254 set_response_sensitive (RESPONSE_ACCEPT, true);
258 TempoDialog::tap_tempo ()
261 gettimeofday (&now, NULL);
263 if (last_tap.tv_sec >= 0 || last_tap.tv_usec > 0) {
265 double interval, bpm;
266 timersub (&now, &last_tap, &diff);
267 interval = diff.tv_sec + diff.tv_usec * 1.0e-6;
269 bpm = 60.0 / interval;
271 bpm_spinner.set_value (bpm);
279 MeterDialog::MeterDialog (TempoMap& map, framepos_t frame, const string&)
280 : ArdourDialog (_("New Meter"))
282 Timecode::BBT_Time when;
283 frame = map.round_to_bar(frame,0);
284 Meter meter (map.meter_at(frame));
286 map.bbt_time (frame, when);
287 init (when, meter.divisions_per_bar(), meter.note_divisor(), true);
290 MeterDialog::MeterDialog (MeterSection& section, const string&)
291 : ArdourDialog (_("Edit Meter"))
293 init (section.start(), section.divisions_per_bar(), section.note_divisor(), section.movable());
297 MeterDialog::init (const Timecode::BBT_Time& when, double bpb, double divisor, bool movable)
300 vector<string> strings;
301 NoteTypes::iterator x;
303 snprintf (buf, sizeof (buf), "%.2f", bpb);
304 bpb_entry.set_text (buf);
305 bpb_entry.select_region (0, -1);
307 note_types.insert (make_pair (_("whole"), 1.0));
308 strings.push_back (_("whole"));
309 note_types.insert (make_pair (_("second"), 2.0));
310 strings.push_back (_("second"));
311 note_types.insert (make_pair (_("third"), 3.0));
312 strings.push_back (_("third"));
313 note_types.insert (make_pair (_("quarter"), 4.0));
314 strings.push_back (_("quarter"));
315 note_types.insert (make_pair (_("eighth"), 8.0));
316 strings.push_back (_("eighth"));
317 note_types.insert (make_pair (_("sixteenth"), 16.0));
318 strings.push_back (_("sixteenth"));
319 note_types.insert (make_pair (_("thirty-second"), 32.0));
320 strings.push_back (_("thirty-second"));
321 note_types.insert (make_pair (_("sixty-fourth"), 64.0));
322 strings.push_back (_("sixty-fourth"));
323 note_types.insert (make_pair (_("one-hundred-twenty-eighth"), 128.0));
324 strings.push_back (_("one-hundred-twenty-eighth"));
326 set_popdown_strings (note_type, strings);
328 for (x = note_types.begin(); x != note_types.end(); ++x) {
329 if (x->second == divisor) {
330 note_type.set_active_text (x->first);
335 if (x == note_types.end()) {
336 note_type.set_active_text (strings[3]); // "quarter"
339 Label* note_label = manage (new Label (_("Note value:"), ALIGN_LEFT, ALIGN_CENTER));
340 Label* bpb_label = manage (new Label (_("Beats per bar:"), ALIGN_LEFT, ALIGN_CENTER));
341 Table* table = manage (new Table (3, 2));
342 table->set_spacings (6);
344 table->attach (*bpb_label, 0, 1, 0, 1, FILL|EXPAND, FILL|EXPAND);
345 table->attach (bpb_entry, 1, 2, 0, 1, FILL|EXPAND, FILL|EXPAND);
346 table->attach (*note_label, 0, 1, 1, 2, FILL|EXPAND, FILL|EXPAND);
347 table->attach (note_type, 1, 2, 1, 2, FILL|EXPAND, SHRINK);
352 snprintf (buf, sizeof (buf), "%" PRIu32, when.bars);
353 when_bar_entry.set_text (buf);
355 Label* when_label = manage (new Label(_("Meter begins at bar:"), ALIGN_LEFT, ALIGN_CENTER));
357 table->attach (*when_label, 0, 1, 2, 3, FILL | EXPAND, FILL | EXPAND);
358 table->attach (when_bar_entry, 1, 2, 2, 3, FILL | EXPAND, FILL | EXPAND);
360 when_bar_entry.set_text ("0");
363 get_vbox()->set_border_width (12);
364 get_vbox()->pack_start (*table, false, false);
366 add_button (Stock::CANCEL, RESPONSE_CANCEL);
367 add_button (Stock::APPLY, RESPONSE_ACCEPT);
368 set_response_sensitive (RESPONSE_ACCEPT, false);
369 set_default_response (RESPONSE_ACCEPT);
371 get_vbox()->show_all ();
373 set_name ("MetricDialog");
374 bpb_entry.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &MeterDialog::response), RESPONSE_ACCEPT));
375 bpb_entry.signal_key_press_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_press), false);
376 bpb_entry.signal_key_release_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_release));
377 when_bar_entry.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &MeterDialog::response), RESPONSE_ACCEPT));
378 when_bar_entry.signal_key_press_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_press), false);
379 when_bar_entry.signal_key_release_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_release));
380 note_type.signal_changed().connect (sigc::mem_fun (*this, &MeterDialog::note_type_change));
384 MeterDialog::entry_key_press (GdkEventKey* ev)
387 switch (ev->keyval) {
432 MeterDialog::entry_key_release (GdkEventKey*)
434 if (when_bar_entry.get_text() != "" && bpb_entry.get_text() != "") {
435 set_response_sensitive (RESPONSE_ACCEPT, true);
437 set_response_sensitive (RESPONSE_ACCEPT, false);
443 MeterDialog::note_type_change ()
445 set_response_sensitive (RESPONSE_ACCEPT, true);
449 MeterDialog::get_bpb ()
453 if (sscanf (bpb_entry.get_text().c_str(), "%lf", &bpb) != 1) {
461 MeterDialog::get_note_type ()
463 NoteTypes::iterator x = note_types.find (note_type.get_active_text());
465 if (x == note_types.end()) {
466 error << string_compose(_("incomprehensible meter note type (%1)"), note_type.get_active_text()) << endmsg;
474 MeterDialog::get_bbt_time (Timecode::BBT_Time& requested)
476 if (sscanf (when_bar_entry.get_text().c_str(), "%" PRIu32, &requested.bars) != 1) {