ardour
tempo_dialog.cc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2000-2007 Paul Davis
3 
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.
8 
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.
13 
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.
17 
18 */
19 
20 #include <cstdio> // for snprintf, grrr
21 
22 #include <gtkmm/stock.h>
23 
24 #include "gtkmm2ext/utils.h"
25 
26 #include "ardour_ui.h"
27 #include "tempo_dialog.h"
28 
29 #include "i18n.h"
30 
31 using namespace std;
32 using namespace Gtk;
33 using namespace Gtkmm2ext;
34 using namespace ARDOUR;
35 using namespace PBD;
36 
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"))
45 {
46  Timecode::BBT_Time when;
47  Tempo tempo (map.tempo_at (frame));
48  map.bbt_time (frame, when);
49 
50  init (when, tempo.beats_per_minute(), tempo.note_type(), true);
51 }
52 
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"))
61 {
62  init (section.start(), section.beats_per_minute(), section.note_type(), section.movable());
63 }
64 
65 void
66 TempoDialog::init (const Timecode::BBT_Time& when, double bpm, double note_type, bool movable)
67 {
68  vector<string> strings;
69  NoteTypes::iterator x;
70 
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);
76 
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"));
95 
97 
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);
101  break;
102  }
103  }
104 
105  if (x == note_types.end()) {
106  pulse_selector.set_active_text (strings[3]); // "quarter"
107  }
108 
109  Table* table;
110 
111  if (ARDOUR_UI::config()->get_allow_non_quarter_pulse()) {
112  table = manage (new Table (5, 5));
113  } else {
114  table = manage (new Table (5, 4));
115  }
116 
117  table->set_spacings (6);
118  table->set_homogeneous (false);
119 
120  int row;
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);
124 
125  if (ARDOUR_UI::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);
128  row = 2;
129  } else {
130  row = 1;
131  }
132 
133  char buf[64];
134 
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);
139 
140  if (movable) {
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);
145 
146  when_bar_label.set_name ("MetricLabel");
147  when_beat_label.set_name ("MetricLabel");
148 
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));
151 
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));
154 
155  Label* when_label = manage (new Label(_("Tempo begins at"), ALIGN_LEFT, ALIGN_CENTER));
156  table->attach (*when_label, 0, 1, row, row+1);
157  }
158 
159  get_vbox()->set_border_width (12);
160  get_vbox()->pack_end (*table);
161  table->show_all ();
162 
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);
167 
168  bpm_spinner.show ();
169  tap_tempo_button.show ();
170  get_vbox()->pack_end (tap_tempo_button);
171  bpm_spinner.grab_focus ();
172 
173  set_name ("MetricDialog");
174 
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));
186 
187  tapped = false;
188 }
189 
190 bool
192 {
193  return (when_beat_entry.get_text() != "")
194  && (when_bar_entry.get_text() != "")
195  && (when_bar_entry.get_text() != "0");
196 }
197 
198 void
200 {
201  set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
202 }
203 
204 bool
206 {
207  return false;
208 }
209 
210 bool
212 {
213  /* the value has been modified, accept should work now */
214 
215  set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
216  return false;
217 }
218 
219 bool
221 {
222  set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
223  return false;
224 }
225 
226 double
228 {
229  return bpm_spinner.get_value ();
230 }
231 
232 bool
233 TempoDialog::get_bbt_time (Timecode::BBT_Time& requested)
234 {
235  if (sscanf (when_bar_entry.get_text().c_str(), "%" PRIu32, &requested.bars) != 1) {
236  return false;
237  }
238 
239  if (sscanf (when_beat_entry.get_text().c_str(), "%" PRIu32, &requested.beats) != 1) {
240  return false;
241  }
242 
243  requested.ticks = 0;
244 
245  return true;
246 }
247 
248 double
250 {
251  NoteTypes::iterator x = note_types.find (pulse_selector.get_active_text());
252 
253  if (x == note_types.end()) {
254  error << string_compose(_("incomprehensible pulse note type (%1)"), pulse_selector.get_active_text()) << endmsg;
255  return 0;
256  }
257 
258  return x->second;
259 }
260 
261 void
263 {
264  set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
265 }
266 
267 bool
269 {
270  gint64 now;
271  now = g_get_monotonic_time (); // microseconds
272 
273  if (tapped) {
274  double interval, bpm;
275  static const double decay = 0.5;
276 
277  interval = (now - last_tap) * 1.0e-6;
278  if (interval <= 6.0) {
279  // <= 6 seconds (say): >= 10 bpm
280  if (average_interval > 0) {
281  if (average_interval > interval / 1.2 && average_interval < interval * 1.2) {
282  average_interval = interval * decay
283  + average_interval * (1.0-decay);
284  } else {
285  average_interval = 0;
286  }
287  } else {
288  average_interval = interval;
289  }
290 
291  if (average_interval > 0) {
292  bpm = 60.0 / average_interval;
293  bpm_spinner.set_value (bpm);
294  }
295  } else {
296  average_interval = 0;
297  }
298  } else {
299  average_interval = 0;
300  tapped = true;
301  }
302  last_tap = now;
303 
304  return true;
305 }
306 
307 bool
309 {
310  tapped = false;
311  return false;
312 }
313 
314 MeterDialog::MeterDialog (TempoMap& map, framepos_t frame, const string&)
315  : ArdourDialog (_("New Meter"))
316 {
317  Timecode::BBT_Time when;
318  frame = map.round_to_bar(frame, RoundNearest);
319  Meter meter (map.meter_at(frame));
320 
321  map.bbt_time (frame, when);
322  init (when, meter.divisions_per_bar(), meter.note_divisor(), true);
323 }
324 
325 MeterDialog::MeterDialog (MeterSection& section, const string&)
326  : ArdourDialog (_("Edit Meter"))
327 {
328  init (section.start(), section.divisions_per_bar(), section.note_divisor(), section.movable());
329 }
330 
331 void
332 MeterDialog::init (const Timecode::BBT_Time& when, double bpb, double divisor, bool movable)
333 {
334  char buf[64];
335  vector<string> strings;
336  NoteTypes::iterator x;
337 
338  snprintf (buf, sizeof (buf), "%.2f", bpb);
339  bpb_entry.set_text (buf);
340  bpb_entry.select_region (0, -1);
341  bpb_entry.set_alignment (1.0);
342 
343  note_types.insert (make_pair (_("whole"), 1.0));
344  strings.push_back (_("whole"));
345  note_types.insert (make_pair (_("second"), 2.0));
346  strings.push_back (_("second"));
347  note_types.insert (make_pair (_("third"), 3.0));
348  strings.push_back (_("third"));
349  note_types.insert (make_pair (_("quarter"), 4.0));
350  strings.push_back (_("quarter"));
351  note_types.insert (make_pair (_("eighth"), 8.0));
352  strings.push_back (_("eighth"));
353  note_types.insert (make_pair (_("sixteenth"), 16.0));
354  strings.push_back (_("sixteenth"));
355  note_types.insert (make_pair (_("thirty-second"), 32.0));
356  strings.push_back (_("thirty-second"));
357  note_types.insert (make_pair (_("sixty-fourth"), 64.0));
358  strings.push_back (_("sixty-fourth"));
359  note_types.insert (make_pair (_("one-hundred-twenty-eighth"), 128.0));
360  strings.push_back (_("one-hundred-twenty-eighth"));
361 
362  set_popdown_strings (note_type, strings);
363 
364  for (x = note_types.begin(); x != note_types.end(); ++x) {
365  if (x->second == divisor) {
366  note_type.set_active_text (x->first);
367  break;
368  }
369  }
370 
371  if (x == note_types.end()) {
372  note_type.set_active_text (strings[3]); // "quarter"
373  }
374 
375  Label* note_label = manage (new Label (_("Note value:"), ALIGN_LEFT, ALIGN_CENTER));
376  Label* bpb_label = manage (new Label (_("Beats per bar:"), ALIGN_LEFT, ALIGN_CENTER));
377  Table* table = manage (new Table (3, 2));
378  table->set_spacings (6);
379 
380  table->attach (*bpb_label, 0, 1, 0, 1, FILL|EXPAND, FILL|EXPAND);
381  table->attach (bpb_entry, 1, 2, 0, 1, FILL|EXPAND, FILL|EXPAND);
382  table->attach (*note_label, 0, 1, 1, 2, FILL|EXPAND, FILL|EXPAND);
383  table->attach (note_type, 1, 2, 1, 2, FILL|EXPAND, SHRINK);
384 
385  snprintf (buf, sizeof (buf), "%" PRIu32, when.bars);
386  when_bar_entry.set_text (buf);
387  when_bar_entry.set_alignment (1.0);
388 
389  if (movable) {
390  Label* when_label = manage (new Label(_("Meter begins at bar:"), ALIGN_LEFT, ALIGN_CENTER));
391 
392  table->attach (*when_label, 0, 1, 2, 3, FILL | EXPAND, FILL | EXPAND);
393  table->attach (when_bar_entry, 1, 2, 2, 3, FILL | EXPAND, FILL | EXPAND);
394  }
395 
396  get_vbox()->set_border_width (12);
397  get_vbox()->pack_start (*table, false, false);
398 
399  add_button (Stock::CANCEL, RESPONSE_CANCEL);
400  add_button (Stock::APPLY, RESPONSE_ACCEPT);
401  set_response_sensitive (RESPONSE_ACCEPT, true);
402  set_default_response (RESPONSE_ACCEPT);
403 
404  get_vbox()->show_all ();
405 
406  set_name ("MetricDialog");
407  bpb_entry.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &MeterDialog::response), RESPONSE_ACCEPT));
408  bpb_entry.signal_key_press_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_press), false);
409  bpb_entry.signal_key_release_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_release));
410  when_bar_entry.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &MeterDialog::response), RESPONSE_ACCEPT));
411  when_bar_entry.signal_key_press_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_press), false);
412  when_bar_entry.signal_key_release_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_release));
413  note_type.signal_changed().connect (sigc::mem_fun (*this, &MeterDialog::note_type_change));
414 }
415 
416 bool
418 {
419  return (when_bar_entry.get_text() != "")
420  && (when_bar_entry.get_text() != "0")
421  && (bpb_entry.get_text() != "");
422 }
423 
424 bool
426 {
427 
428  switch (ev->keyval) {
429 
430  case GDK_0:
431  case GDK_1:
432  case GDK_2:
433  case GDK_3:
434  case GDK_4:
435  case GDK_5:
436  case GDK_6:
437  case GDK_7:
438  case GDK_8:
439  case GDK_9:
440  case GDK_KP_0:
441  case GDK_KP_1:
442  case GDK_KP_2:
443  case GDK_KP_3:
444  case GDK_KP_4:
445  case GDK_KP_5:
446  case GDK_KP_6:
447  case GDK_KP_7:
448  case GDK_KP_8:
449  case GDK_KP_9:
450  case GDK_period:
451  case GDK_comma:
452  case GDK_KP_Delete:
453  case GDK_KP_Enter:
454  case GDK_Delete:
455  case GDK_BackSpace:
456  case GDK_Escape:
457  case GDK_Return:
458  case GDK_Home:
459  case GDK_End:
460  case GDK_Left:
461  case GDK_Right:
462  case GDK_Num_Lock:
463  case GDK_Tab:
464  return FALSE;
465  default:
466  break;
467  }
468 
469  return TRUE;
470 }
471 
472 bool
474 {
475  set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
476  return false;
477 }
478 
479 void
481 {
482  set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
483 }
484 
485 double
487 {
488  double bpb = 0;
489 
490  if (sscanf (bpb_entry.get_text().c_str(), "%lf", &bpb) != 1) {
491  return 0;
492  }
493 
494  return bpb;
495 }
496 
497 double
499 {
500  NoteTypes::iterator x = note_types.find (note_type.get_active_text());
501 
502  if (x == note_types.end()) {
503  error << string_compose(_("incomprehensible meter note type (%1)"), note_type.get_active_text()) << endmsg;
504  return 0;
505  }
506 
507  return x->second;
508 }
509 
510 bool
511 MeterDialog::get_bbt_time (Timecode::BBT_Time& requested)
512 {
513  if (sscanf (when_bar_entry.get_text().c_str(), "%" PRIu32, &requested.bars) != 1) {
514  return false;
515  }
516 
517  requested.beats = 1;
518  requested.ticks = 0;
519 
520  return true;
521 }
void init(bool wait_for_waves)
Gtk::Entry when_bar_entry
Definition: tempo_dialog.h:69
double get_note_type()
bool bpm_button_press(GdkEventButton *)
void bbt_time(framepos_t when, Timecode::BBT_Time &)
Definition: tempo.cc:1168
TempoDialog(ARDOUR::TempoMap &, framepos_t, const std::string &action)
bool tap_tempo_focus_out(GdkEventFocus *)
void note_type_change()
Definition: ardour_ui.h:130
bool entry_key_release(GdkEventKey *)
Gtk::Entry bpb_entry
Definition: tempo_dialog.h:98
Definition: Beats.hpp:239
LIBPBD_API Transmitter error
bool movable() const
Definition: tempo.h:103
std::ostream & endmsg(std::ostream &ostr)
Definition: transmitter.h:71
LIBGTKMM2EXT_API void set_popdown_strings(Gtk::ComboBoxText &, const std::vector< std::string > &)
Round to nearest.
Definition: types.h:224
framepos_t round_to_bar(framepos_t frame, RoundMode dir)
Definition: tempo.cc:1295
bool bpm_button_release(GdkEventButton *)
const Meter & meter_at(framepos_t) const
Definition: tempo.cc:1652
bool is_user_input_valid() const
const Timecode::BBT_Time & start() const
Definition: tempo.h:99
#define _(Text)
Definition: i18n.h:11
bool get_bbt_time(Timecode::BBT_Time &)
bool entry_key_press(GdkEventKey *)
Gtk::Label when_beat_label
Definition: tempo_dialog.h:72
NoteTypes note_types
Definition: tempo_dialog.h:96
Gtk::ComboBoxText note_type
Definition: tempo_dialog.h:99
Gtk::Entry when_bar_entry
Definition: tempo_dialog.h:103
NoteTypes note_types
Definition: tempo_dialog.h:60
bool entry_key_release(GdkEventKey *)
Definition: amp.h:29
Gtk::SpinButton bpm_spinner
Definition: tempo_dialog.h:68
Gtk::Button tap_tempo_button
Definition: tempo_dialog.h:74
double get_note_type()
int64_t framepos_t
Definition: types.h:66
double note_divisor() const
Definition: tempo.h:71
void bpm_changed()
Gtk::Label pulse_selector_label
Definition: tempo_dialog.h:73
double beats_per_minute() const
Definition: tempo.h:53
double get_bpm()
Gtk::Label when_bar_label
Definition: tempo_dialog.h:71
void pulse_change()
Gtk::ComboBoxText pulse_selector
Definition: tempo_dialog.h:66
double get_bpb()
static UIConfiguration * config()
Definition: ardour_ui.h:188
bool get_bbt_time(Timecode::BBT_Time &)
Definition: debug.h:30
double divisions_per_bar() const
Definition: tempo.h:70
double average_interval
Definition: tempo_dialog.h:64
gint64 last_tap
Definition: tempo_dialog.h:63
bool tap_tempo_button_press(GdkEventButton *)
MeterDialog(ARDOUR::TempoMap &, framepos_t, const std::string &action)
std::vector< std::string > strings
Definition: tempo_dialog.h:100
Gtk::Entry when_beat_entry
Definition: tempo_dialog.h:70
double note_type() const
Definition: tempo.h:54
const Tempo & tempo_at(framepos_t) const
Definition: tempo.cc:1617
std::string string_compose(const std::string &fmt, const T1 &o1)
Definition: compose.h:208
bool is_user_input_valid() const