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