Tempo ramps - fix more namespace confusion
[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 "tempo_dialog.h"
27 #include "ui_config.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(), TempoSection::Constant, true);
51 }
52
53 TempoDialog::TempoDialog (TempoMap& map, 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         Timecode::BBT_Time when;
63         map.bbt_time (map.frame_at_beat (section.beat()), when);
64         init (when, section.beats_per_minute(), section.note_type(), section.type(), section.movable());
65 }
66
67 void
68 TempoDialog::init (const Timecode::BBT_Time& when, double bpm, double note_type, TempoSection::Type type, bool movable)
69 {
70         vector<string> strings;
71         NoteTypes::iterator x;
72
73         bpm_spinner.set_numeric (true);
74         bpm_spinner.set_digits (2);
75         bpm_spinner.set_wrap (true);
76         bpm_spinner.set_value (bpm);
77         bpm_spinner.set_alignment (1.0);
78
79         note_types.insert (make_pair (_("whole"), 1.0));
80         strings.push_back (_("whole"));
81         note_types.insert (make_pair (_("second"), 2.0));
82         strings.push_back (_("second"));
83         note_types.insert (make_pair (_("third"), 3.0));
84         strings.push_back (_("third"));
85         note_types.insert (make_pair (_("quarter"), 4.0));
86         strings.push_back (_("quarter"));
87         note_types.insert (make_pair (_("eighth"), 8.0));
88         strings.push_back (_("eighth"));
89         note_types.insert (make_pair (_("sixteenth"), 16.0));
90         strings.push_back (_("sixteenth"));
91         note_types.insert (make_pair (_("thirty-second"), 32.0));
92         strings.push_back (_("thirty-second"));
93         note_types.insert (make_pair (_("sixty-fourth"), 64.0));
94         strings.push_back (_("sixty-fourth"));
95         note_types.insert (make_pair (_("one-hundred-twenty-eighth"), 128.0));
96         strings.push_back (_("one-hundred-twenty-eighth"));
97
98         set_popdown_strings (pulse_selector, strings);
99
100         for (x = note_types.begin(); x != note_types.end(); ++x) {
101                 if (x->second == note_type) {
102                         pulse_selector.set_active_text (x->first);
103                         break;
104                 }
105         }
106
107         if (x == note_types.end()) {
108                 pulse_selector.set_active_text (strings[3]); // "quarter"
109         }
110
111         strings.clear();
112
113         tempo_types.insert (make_pair (_("ramped"), TempoSection::Ramp));
114         strings.push_back (_("ramped"));
115         tempo_types.insert (make_pair (_("constant"), TempoSection::Constant));
116         strings.push_back (_("constant"));
117         set_popdown_strings (tempo_type, strings);
118         TempoTypes::iterator tt;
119         for (tt = tempo_types.begin(); tt != tempo_types.end(); ++tt) {
120                 if (tt->second == type) {
121                         tempo_type.set_active_text (tt->first);
122                         break;
123                 }
124         }
125         if (tt == tempo_types.end()) {
126                 tempo_type.set_active_text (strings[0]); // "ramped"
127         }
128
129         Table* table;
130
131         if (UIConfiguration::instance().get_allow_non_quarter_pulse()) {
132                 table = manage (new Table (5, 6));
133         } else {
134                 table = manage (new Table (5, 5));
135         }
136
137         table->set_spacings (6);
138         table->set_homogeneous (false);
139
140         int row;
141         Label* bpm_label = manage (new Label(_("Beats per minute:"), ALIGN_LEFT, ALIGN_CENTER));
142         table->attach (*bpm_label, 0, 1, 0, 1);
143         table->attach (bpm_spinner, 1, 5, 0, 1);
144
145         if (UIConfiguration::instance().get_allow_non_quarter_pulse()) {
146                 table->attach (pulse_selector_label, 0, 1, 1, 2);
147                 table->attach (pulse_selector, 1, 5, 1, 2);
148                 row = 2;
149         } else {
150                 row = 1;
151         }
152
153         char buf[64];
154
155         snprintf (buf, sizeof (buf), "%" PRIu32, when.bars);
156         when_bar_entry.set_text (buf);
157         snprintf (buf, sizeof (buf), "%" PRIu32, when.beats);
158         when_beat_entry.set_text (buf);
159
160         if (movable) {
161                 when_bar_entry.set_width_chars(4);
162                 when_beat_entry.set_width_chars (4);
163                 when_bar_entry.set_alignment (1.0);
164                 when_beat_entry.set_alignment (1.0);
165
166                 when_bar_label.set_name ("MetricLabel");
167                 when_beat_label.set_name ("MetricLabel");
168
169                 table->attach (when_bar_label, 1, 2, row, row+1, Gtk::AttachOptions(0), Gtk::AttachOptions(0));
170                 table->attach (when_bar_entry, 2, 3, row, row+1, Gtk::AttachOptions(0), Gtk::AttachOptions(0));
171
172                 table->attach (when_beat_label, 3, 4, row, row+1, Gtk::AttachOptions(0), Gtk::AttachOptions(0));
173                 table->attach (when_beat_entry, 4, 5, row, row+1, Gtk::AttachOptions(0), Gtk::AttachOptions(0));
174
175                 Label* when_label = manage (new Label(_("Tempo begins at"), ALIGN_LEFT, ALIGN_CENTER));
176                 table->attach (*when_label, 0, 1, row, row+1);
177         }
178
179         Label* tempo_type_label = manage (new Label(_("Tempo Type:"), ALIGN_LEFT, ALIGN_CENTER));
180         table->attach (*tempo_type_label, 0, 1, row+1, row+2);
181         table->attach (tempo_type, 1, 2, row+1, row + 2);
182         get_vbox()->set_border_width (12);
183         get_vbox()->pack_end (*table);
184
185         table->show_all ();
186
187         add_button (Stock::CANCEL, RESPONSE_CANCEL);
188         add_button (Stock::APPLY, RESPONSE_ACCEPT);
189         set_response_sensitive (RESPONSE_ACCEPT, true);
190         set_default_response (RESPONSE_ACCEPT);
191
192         bpm_spinner.show ();
193         tap_tempo_button.show ();
194         get_vbox()->pack_end (tap_tempo_button);
195         bpm_spinner.grab_focus ();
196
197         set_name ("MetricDialog");
198
199         bpm_spinner.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &TempoDialog::response), RESPONSE_ACCEPT));
200         bpm_spinner.signal_button_press_event().connect (sigc::mem_fun (*this, &TempoDialog::bpm_button_press), false);
201         bpm_spinner.signal_button_release_event().connect (sigc::mem_fun (*this, &TempoDialog::bpm_button_release), false);
202         bpm_spinner.signal_changed().connect (sigc::mem_fun (*this, &TempoDialog::bpm_changed));
203         when_bar_entry.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &TempoDialog::response), RESPONSE_ACCEPT));
204         when_bar_entry.signal_key_release_event().connect (sigc::mem_fun (*this, &TempoDialog::entry_key_release), false);
205         when_beat_entry.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &TempoDialog::response), RESPONSE_ACCEPT));
206         when_beat_entry.signal_key_release_event().connect (sigc::mem_fun (*this, &TempoDialog::entry_key_release), false);
207         pulse_selector.signal_changed().connect (sigc::mem_fun (*this, &TempoDialog::pulse_change));
208         tempo_type.signal_changed().connect (sigc::mem_fun (*this, &TempoDialog::tempo_type_change));
209         tap_tempo_button.signal_button_press_event().connect (sigc::mem_fun (*this, &TempoDialog::tap_tempo_button_press), false);
210         tap_tempo_button.signal_focus_out_event().connect (sigc::mem_fun (*this, &TempoDialog::tap_tempo_focus_out));
211
212         tapped = false;
213 }
214
215 bool
216 TempoDialog::is_user_input_valid() const
217 {
218         return (when_beat_entry.get_text() != "")
219                 && (when_bar_entry.get_text() != "")
220                 && (when_bar_entry.get_text() != "0");
221 }
222
223 void
224 TempoDialog::bpm_changed ()
225 {
226         set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
227 }
228
229 bool
230 TempoDialog::bpm_button_press (GdkEventButton*)
231 {
232         return false;
233 }
234
235 bool
236 TempoDialog::bpm_button_release (GdkEventButton*)
237 {
238         /* the value has been modified, accept should work now */
239
240         set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
241         return false;
242 }
243
244 bool
245 TempoDialog::entry_key_release (GdkEventKey*)
246 {
247         set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
248         return false;
249 }
250
251 double
252 TempoDialog::get_bpm ()
253 {
254         return bpm_spinner.get_value ();
255 }
256
257 bool
258 TempoDialog::get_bbt_time (Timecode::BBT_Time& requested)
259 {
260         if (sscanf (when_bar_entry.get_text().c_str(), "%" PRIu32, &requested.bars) != 1) {
261                 return false;
262         }
263
264         if (sscanf (when_beat_entry.get_text().c_str(), "%" PRIu32, &requested.beats) != 1) {
265                 return false;
266         }
267
268         requested.ticks = 0;
269
270         return true;
271 }
272
273 double
274 TempoDialog::get_note_type ()
275 {
276         NoteTypes::iterator x = note_types.find (pulse_selector.get_active_text());
277
278         if (x == note_types.end()) {
279                 error << string_compose(_("incomprehensible pulse note type (%1)"), pulse_selector.get_active_text()) << endmsg;
280                 return 0;
281         }
282
283         return x->second;
284 }
285
286 TempoSection::Type
287 TempoDialog::get_tempo_type ()
288 {
289         TempoTypes::iterator x = tempo_types.find (tempo_type.get_active_text());
290
291         if (x == tempo_types.end()) {
292                 error << string_compose(_("incomprehensible tempo type (%1)"), tempo_type.get_active_text()) << endmsg;
293                 return TempoSection::Constant;
294         }
295
296         return x->second;
297 }
298
299 void
300 TempoDialog::pulse_change ()
301 {
302         set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
303 }
304
305 void
306 TempoDialog::tempo_type_change ()
307 {
308         set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
309 }
310
311 bool
312 TempoDialog::tap_tempo_button_press (GdkEventButton *ev)
313 {
314         double t;
315
316         // Linear least-squares regression
317         if (tapped) {
318                 t = 1e-6 * (g_get_monotonic_time () - first_t); // Subtract first_t to avoid precision problems
319
320                 double n = tap_count;
321                 sum_y += t;
322                 sum_x += n;
323                 sum_xy += n * t;
324                 sum_xx += n * n;
325                 double T = (sum_xy/n - sum_x/n * sum_y/n) / (sum_xx/n - sum_x/n * sum_x/n);
326
327                 if (t - last_t < T / 1.2 || t - last_t > T * 1.2) {
328                         tapped = false;
329                 } else {
330                         bpm_spinner.set_value (60.0 / T);
331                 }
332         }
333         if (!tapped) {
334                 first_t = g_get_monotonic_time ();
335                 t = 0.0;
336                 sum_y = 0.0;
337                 sum_x = 1.0;
338                 sum_xy = 0.0;
339                 sum_xx = 1.0;
340                 tap_count = 1.0;
341
342                 tapped = true;
343         }
344         tap_count++;
345         last_t = t;
346
347         return true;
348 }
349
350 bool
351 TempoDialog::tap_tempo_focus_out (GdkEventFocus* )
352 {
353         tapped = false;
354         return false;
355 }
356
357 MeterDialog::MeterDialog (TempoMap& map, framepos_t frame, const string&)
358         : ArdourDialog (_("New Meter"))
359 {
360         Timecode::BBT_Time when;
361         frame = map.round_to_bar(frame, RoundNearest);
362         Meter meter (map.meter_at(frame));
363
364         map.bbt_time (frame, when);
365         init (when, meter.divisions_per_bar(), meter.note_divisor(), true);
366 }
367
368 MeterDialog::MeterDialog (TempoMap& map, MeterSection& section, const string&)
369         : ArdourDialog (_("Edit Meter"))
370 {
371         Timecode::BBT_Time when;
372         map.bbt_time (map.frame_at_beat (section.beat()), when);
373         init (when, section.divisions_per_bar(), section.note_divisor(), section.movable());
374 }
375
376 void
377 MeterDialog::init (const Timecode::BBT_Time& when, double bpb, double divisor, bool movable)
378 {
379         char buf[64];
380         vector<string> strings;
381         NoteTypes::iterator x;
382
383         snprintf (buf, sizeof (buf), "%.2f", bpb);
384         bpb_entry.set_text (buf);
385         bpb_entry.select_region (0, -1);
386         bpb_entry.set_alignment (1.0);
387
388         note_types.insert (make_pair (_("whole"), 1.0));
389         strings.push_back (_("whole"));
390         note_types.insert (make_pair (_("second"), 2.0));
391         strings.push_back (_("second"));
392         note_types.insert (make_pair (_("third"), 3.0));
393         strings.push_back (_("third"));
394         note_types.insert (make_pair (_("quarter"), 4.0));
395         strings.push_back (_("quarter"));
396         note_types.insert (make_pair (_("eighth"), 8.0));
397         strings.push_back (_("eighth"));
398         note_types.insert (make_pair (_("sixteenth"), 16.0));
399         strings.push_back (_("sixteenth"));
400         note_types.insert (make_pair (_("thirty-second"), 32.0));
401         strings.push_back (_("thirty-second"));
402         note_types.insert (make_pair (_("sixty-fourth"), 64.0));
403         strings.push_back (_("sixty-fourth"));
404         note_types.insert (make_pair (_("one-hundred-twenty-eighth"), 128.0));
405         strings.push_back (_("one-hundred-twenty-eighth"));
406
407         set_popdown_strings (note_type, strings);
408
409         for (x = note_types.begin(); x != note_types.end(); ++x) {
410                 if (x->second == divisor) {
411                         note_type.set_active_text (x->first);
412                         break;
413                 }
414         }
415
416         if (x == note_types.end()) {
417                 note_type.set_active_text (strings[3]); // "quarter"
418         }
419
420         Label* note_label = manage (new Label (_("Note value:"), ALIGN_LEFT, ALIGN_CENTER));
421         Label* bpb_label = manage (new Label (_("Beats per bar:"), ALIGN_LEFT, ALIGN_CENTER));
422         Table* table = manage (new Table (3, 2));
423         table->set_spacings (6);
424
425         table->attach (*bpb_label, 0, 1, 0, 1, FILL|EXPAND, FILL|EXPAND);
426         table->attach (bpb_entry, 1, 2, 0, 1, FILL|EXPAND, FILL|EXPAND);
427         table->attach (*note_label, 0, 1, 1, 2, FILL|EXPAND, FILL|EXPAND);
428         table->attach (note_type, 1, 2, 1, 2, FILL|EXPAND, SHRINK);
429
430         snprintf (buf, sizeof (buf), "%" PRIu32, when.bars);
431         when_bar_entry.set_text (buf);
432         when_bar_entry.set_alignment (1.0);
433
434         if (movable) {
435                 Label* when_label = manage (new Label(_("Meter begins at bar:"), ALIGN_LEFT, ALIGN_CENTER));
436
437                 table->attach (*when_label, 0, 1, 2, 3, FILL | EXPAND, FILL | EXPAND);
438                 table->attach (when_bar_entry, 1, 2, 2, 3, FILL | EXPAND, FILL | EXPAND);
439         }
440
441         get_vbox()->set_border_width (12);
442         get_vbox()->pack_start (*table, false, false);
443
444         add_button (Stock::CANCEL, RESPONSE_CANCEL);
445         add_button (Stock::APPLY, RESPONSE_ACCEPT);
446         set_response_sensitive (RESPONSE_ACCEPT, true);
447         set_default_response (RESPONSE_ACCEPT);
448
449         get_vbox()->show_all ();
450
451         set_name ("MetricDialog");
452         bpb_entry.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &MeterDialog::response), RESPONSE_ACCEPT));
453         bpb_entry.signal_key_press_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_press), false);
454         bpb_entry.signal_key_release_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_release));
455         when_bar_entry.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &MeterDialog::response), RESPONSE_ACCEPT));
456         when_bar_entry.signal_key_press_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_press), false);
457         when_bar_entry.signal_key_release_event().connect (sigc::mem_fun (*this, &MeterDialog::entry_key_release));
458         note_type.signal_changed().connect (sigc::mem_fun (*this, &MeterDialog::note_type_change));
459 }
460
461 bool
462 MeterDialog::is_user_input_valid() const
463 {
464         return (when_bar_entry.get_text() != "")
465                 && (when_bar_entry.get_text() != "0")
466                 && (bpb_entry.get_text() != "");
467 }
468
469 bool
470 MeterDialog::entry_key_press (GdkEventKey* ev)
471 {
472
473         switch (ev->keyval) {
474
475         case GDK_0:
476         case GDK_1:
477         case GDK_2:
478         case GDK_3:
479         case GDK_4:
480         case GDK_5:
481         case GDK_6:
482         case GDK_7:
483         case GDK_8:
484         case GDK_9:
485         case GDK_KP_0:
486         case GDK_KP_1:
487         case GDK_KP_2:
488         case GDK_KP_3:
489         case GDK_KP_4:
490         case GDK_KP_5:
491         case GDK_KP_6:
492         case GDK_KP_7:
493         case GDK_KP_8:
494         case GDK_KP_9:
495         case GDK_period:
496         case GDK_comma:
497         case  GDK_KP_Delete:
498         case  GDK_KP_Enter:
499         case  GDK_Delete:
500         case  GDK_BackSpace:
501         case  GDK_Escape:
502         case  GDK_Return:
503         case  GDK_Home:
504         case  GDK_End:
505         case  GDK_Left:
506         case  GDK_Right:
507         case  GDK_Num_Lock:
508         case  GDK_Tab:
509                 return FALSE;
510         default:
511                 break;
512         }
513
514         return TRUE;
515 }
516
517 bool
518 MeterDialog::entry_key_release (GdkEventKey*)
519 {
520         set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
521         return false;
522 }
523
524 void
525 MeterDialog::note_type_change ()
526 {
527         set_response_sensitive (RESPONSE_ACCEPT, is_user_input_valid());
528 }
529
530 double
531 MeterDialog::get_bpb ()
532 {
533         double bpb = 0;
534
535         if (sscanf (bpb_entry.get_text().c_str(), "%lf", &bpb) != 1) {
536                 return 0;
537         }
538
539         return bpb;
540 }
541
542 double
543 MeterDialog::get_note_type ()
544 {
545         NoteTypes::iterator x = note_types.find (note_type.get_active_text());
546
547         if (x == note_types.end()) {
548                 error << string_compose(_("incomprehensible meter note type (%1)"), note_type.get_active_text()) << endmsg;
549                 return 0;
550         }
551
552         return x->second;
553 }
554
555 bool
556 MeterDialog::get_bbt_time (Timecode::BBT_Time& requested)
557 {
558         if (sscanf (when_bar_entry.get_text().c_str(), "%" PRIu32, &requested.bars) != 1) {
559                 return false;
560         }
561
562         requested.beats = 1;
563         requested.ticks = 0;
564
565         return true;
566 }