Fix crash if new tempo is set to start at bar #0
[ardour.git] / gtk2_ardour / tempo_dialog.cc
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/rc_configuration.h"
27
28 #include "tempo_dialog.h"
29
30 #include "i18n.h"
31
32 using namespace std;
33 using namespace Gtk;
34 using namespace Gtkmm2ext;
35 using namespace ARDOUR;
36 using namespace PBD;
37
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"))
46 {
47         Timecode::BBT_Time when;
48         Tempo tempo (map.tempo_at (frame));
49         map.bbt_time (frame, when);
50
51         init (when, tempo.beats_per_minute(), tempo.note_type(), true);
52 }
53
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"))
62 {
63         init (section.start(), section.beats_per_minute(), section.note_type(), section.movable());
64 }
65
66 void
67 TempoDialog::init (const Timecode::BBT_Time& when, double bpm, double note_type, bool movable)
68 {
69         vector<string> strings;
70         NoteTypes::iterator x;
71
72         bpm_spinner.set_numeric (true);
73         bpm_spinner.set_digits (2);
74         bpm_spinner.set_wrap (true);
75         bpm_spinner.set_value (bpm);
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
96         set_popdown_strings (pulse_selector, strings);
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 (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 (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         if (movable) {
134                 char buf[64];
135
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);
140
141                 when_bar_entry.set_width_chars(4);
142                 when_beat_entry.set_width_chars (4);
143
144                 when_bar_label.set_name ("MetricLabel");
145                 when_beat_label.set_name ("MetricLabel");
146
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));
149
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));
152
153                 Label* when_label = manage (new Label(_("Tempo begins at"), ALIGN_LEFT, ALIGN_CENTER));
154                 table->attach (*when_label, 0, 1, row, row+1);
155         }
156
157         get_vbox()->set_border_width (12);
158         get_vbox()->pack_end (*table);
159         table->show_all ();
160
161         add_button (Stock::CANCEL, RESPONSE_CANCEL);
162         add_button (Stock::APPLY, RESPONSE_ACCEPT);
163         set_response_sensitive (RESPONSE_ACCEPT, true);
164         set_default_response (RESPONSE_ACCEPT);
165
166         bpm_spinner.show ();
167         tap_tempo_button.show ();
168         get_vbox()->pack_end (tap_tempo_button);
169
170         set_name ("MetricDialog");
171
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));
182 }
183
184 bool
185 TempoDialog::is_user_input_valid() const
186 {
187         return (when_beat_entry.get_text() != "")
188                 && (when_bar_entry.get_text() != "")
189                 && (when_bar_entry.get_text() != "0");
190 }
191
192 void
193 TempoDialog::bpm_changed ()
194 {
195         set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
196 }
197
198 bool
199 TempoDialog::bpm_button_press (GdkEventButton*)
200 {
201         return false;
202 }
203
204 bool
205 TempoDialog::bpm_button_release (GdkEventButton*)
206 {
207         /* the value has been modified, accept should work now */
208
209         set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
210         return false;
211 }
212
213 bool
214 TempoDialog::entry_key_release (GdkEventKey*)
215 {
216         set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
217         return false;
218 }
219
220 double
221 TempoDialog::get_bpm ()
222 {
223         return bpm_spinner.get_value ();
224 }
225
226 bool
227 TempoDialog::get_bbt_time (Timecode::BBT_Time& requested)
228 {
229         if (sscanf (when_bar_entry.get_text().c_str(), "%" PRIu32, &requested.bars) != 1) {
230                 return false;
231         }
232
233         if (sscanf (when_beat_entry.get_text().c_str(), "%" PRIu32, &requested.beats) != 1) {
234                 return false;
235         }
236
237         requested.ticks = 0;
238
239         return true;
240 }
241
242 double
243 TempoDialog::get_note_type ()
244 {
245         NoteTypes::iterator x = note_types.find (pulse_selector.get_active_text());
246         
247         if (x == note_types.end()) {
248                 error << string_compose(_("incomprehensible pulse note type (%1)"), pulse_selector.get_active_text()) << endmsg;
249                 return 0;
250         }
251
252         return x->second;
253 }
254
255 void
256 TempoDialog::pulse_change ()
257 {
258         set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
259 }
260
261 void
262 TempoDialog::tap_tempo ()
263 {
264         gint64 now;
265         now = g_get_monotonic_time (); // microseconds
266
267         if (last_tap > 0) {
268                 double interval, bpm;
269                 static const double decay = 0.5;
270
271                 interval = (now - last_tap) * 1.0e-6;
272                 if (interval <= 6.0) {
273                         // >= 10 bpm, say
274                         if (average_interval > 0) {
275                                 average_interval = interval * decay
276                                         + average_interval * (1.0-decay);
277                         } else {
278                                 average_interval = interval;
279                         }
280
281                         bpm = 60.0 / average_interval;
282                         bpm_spinner.set_value (bpm);
283                 } else {
284                         average_interval = 0;
285                 }
286         } else {
287                 average_interval = 0;
288         }
289         last_tap = now;
290 }
291
292 MeterDialog::MeterDialog (TempoMap& map, framepos_t frame, const string&)
293         : ArdourDialog (_("New Meter"))
294 {
295         Timecode::BBT_Time when;
296         frame = map.round_to_bar(frame, RoundNearest);
297         Meter meter (map.meter_at(frame));
298
299         map.bbt_time (frame, when);
300         init (when, meter.divisions_per_bar(), meter.note_divisor(), true);
301 }
302
303 MeterDialog::MeterDialog (MeterSection& section, const string&)
304         : ArdourDialog (_("Edit Meter"))
305 {
306         init (section.start(), section.divisions_per_bar(), section.note_divisor(), section.movable());
307 }
308
309 void
310 MeterDialog::init (const Timecode::BBT_Time& when, double bpb, double divisor, bool movable)
311 {
312         char buf[64];
313         vector<string> strings;
314         NoteTypes::iterator x;
315
316         snprintf (buf, sizeof (buf), "%.2f", bpb);
317         bpb_entry.set_text (buf);
318         bpb_entry.select_region (0, -1);
319
320         note_types.insert (make_pair (_("whole"), 1.0));
321         strings.push_back (_("whole"));
322         note_types.insert (make_pair (_("second"), 2.0));
323         strings.push_back (_("second"));
324         note_types.insert (make_pair (_("third"), 3.0));
325         strings.push_back (_("third"));
326         note_types.insert (make_pair (_("quarter"), 4.0));
327         strings.push_back (_("quarter"));
328         note_types.insert (make_pair (_("eighth"), 8.0));
329         strings.push_back (_("eighth"));
330         note_types.insert (make_pair (_("sixteenth"), 16.0));
331         strings.push_back (_("sixteenth"));
332         note_types.insert (make_pair (_("thirty-second"), 32.0));
333         strings.push_back (_("thirty-second"));
334         note_types.insert (make_pair (_("sixty-fourth"), 64.0));
335         strings.push_back (_("sixty-fourth"));
336         note_types.insert (make_pair (_("one-hundred-twenty-eighth"), 128.0));
337         strings.push_back (_("one-hundred-twenty-eighth"));
338
339         set_popdown_strings (note_type, strings);
340
341         for (x = note_types.begin(); x != note_types.end(); ++x) {
342                 if (x->second == divisor) {
343                         note_type.set_active_text (x->first);
344                         break;
345                 }
346         }
347         
348         if (x == note_types.end()) {
349                 note_type.set_active_text (strings[3]); // "quarter"
350         }
351
352         Label* note_label = manage (new Label (_("Note value:"), ALIGN_LEFT, ALIGN_CENTER));
353         Label* bpb_label = manage (new Label (_("Beats per bar:"), ALIGN_LEFT, ALIGN_CENTER));
354         Table* table = manage (new Table (3, 2));
355         table->set_spacings (6);
356
357         table->attach (*bpb_label, 0, 1, 0, 1, FILL|EXPAND, FILL|EXPAND);
358         table->attach (bpb_entry, 1, 2, 0, 1, FILL|EXPAND, FILL|EXPAND);
359         table->attach (*note_label, 0, 1, 1, 2, FILL|EXPAND, FILL|EXPAND);
360         table->attach (note_type, 1, 2, 1, 2, FILL|EXPAND, SHRINK);
361
362         if (movable) {
363                 char buf[64];
364
365                 snprintf (buf, sizeof (buf), "%" PRIu32, when.bars);
366                 when_bar_entry.set_text (buf);
367
368                 Label* when_label = manage (new Label(_("Meter begins at bar:"), ALIGN_LEFT, ALIGN_CENTER));
369
370                 table->attach (*when_label, 0, 1, 2, 3, FILL | EXPAND, FILL | EXPAND);
371                 table->attach (when_bar_entry, 1, 2, 2, 3, FILL | EXPAND, FILL | EXPAND);
372         } else {
373                 when_bar_entry.set_text ("0");
374         }
375
376         get_vbox()->set_border_width (12);
377         get_vbox()->pack_start (*table, false, false);
378
379         add_button (Stock::CANCEL, RESPONSE_CANCEL);
380         add_button (Stock::APPLY, RESPONSE_ACCEPT);
381         set_response_sensitive (RESPONSE_ACCEPT, true);
382         set_default_response (RESPONSE_ACCEPT);
383
384         get_vbox()->show_all ();
385
386         set_name ("MetricDialog");
387         bpb_entry.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &MeterDialog::response), RESPONSE_ACCEPT));
388         bpb_entry.signal_key_press_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_press), false);
389         bpb_entry.signal_key_release_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_release));
390         when_bar_entry.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &MeterDialog::response), RESPONSE_ACCEPT));
391         when_bar_entry.signal_key_press_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_press), false);
392         when_bar_entry.signal_key_release_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_release));
393         note_type.signal_changed().connect (sigc::mem_fun (*this, &MeterDialog::note_type_change));
394 }
395
396 bool
397 MeterDialog::is_user_input_valid() const
398 {
399         return (when_bar_entry.get_text() != "")
400                 && (when_bar_entry.get_text() != "0")
401                 && (bpb_entry.get_text() != "");
402 }
403
404 bool
405 MeterDialog::entry_key_press (GdkEventKey* ev)
406 {
407
408         switch (ev->keyval) {
409
410         case GDK_0:
411         case GDK_1:
412         case GDK_2:
413         case GDK_3:
414         case GDK_4:
415         case GDK_5:
416         case GDK_6:
417         case GDK_7:
418         case GDK_8:
419         case GDK_9:
420         case GDK_KP_0:
421         case GDK_KP_1:
422         case GDK_KP_2:
423         case GDK_KP_3:
424         case GDK_KP_4:
425         case GDK_KP_5:
426         case GDK_KP_6:
427         case GDK_KP_7:
428         case GDK_KP_8:
429         case GDK_KP_9:
430         case GDK_period:
431         case GDK_comma:
432         case  GDK_KP_Delete:
433         case  GDK_KP_Enter:
434         case  GDK_Delete:
435         case  GDK_BackSpace:
436         case  GDK_Escape:
437         case  GDK_Return:
438         case  GDK_Home:
439         case  GDK_End:
440         case  GDK_Left:
441         case  GDK_Right:
442         case  GDK_Num_Lock:
443         case  GDK_Tab:
444                 return FALSE;
445         default:
446                 break;
447         }
448
449         return TRUE;
450 }
451
452 bool
453 MeterDialog::entry_key_release (GdkEventKey*)
454 {
455         set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
456         return false;
457 }
458
459 void
460 MeterDialog::note_type_change ()
461 {
462         set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
463 }
464
465 double
466 MeterDialog::get_bpb ()
467 {
468         double bpb = 0;
469
470         if (sscanf (bpb_entry.get_text().c_str(), "%lf", &bpb) != 1) {
471                 return 0;
472         }
473
474         return bpb;
475 }
476
477 double
478 MeterDialog::get_note_type ()
479 {
480         NoteTypes::iterator x = note_types.find (note_type.get_active_text());
481         
482         if (x == note_types.end()) {
483                 error << string_compose(_("incomprehensible meter note type (%1)"), note_type.get_active_text()) << endmsg;
484                 return 0;
485         }
486
487         return x->second;
488 }
489
490 bool
491 MeterDialog::get_bbt_time (Timecode::BBT_Time& requested)
492 {
493         if (sscanf (when_bar_entry.get_text().c_str(), "%" PRIu32, &requested.bars) != 1) {
494                 return false;
495         }
496
497         requested.beats = 1;
498         requested.ticks = 0;
499
500         return true;
501 }