Fix quantization and other time-related ops.
[ardour.git] / gtk2_ardour / quantize_dialog.cc
1 /*
2    Copyright (C) 2009 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 <gtkmm/stock.h>
21 #include <gtkmm/table.h>
22 #include "gtkmm2ext/utils.h"
23
24 #include "pbd/convert.h"
25 #include "quantize_dialog.h"
26 #include "public_editor.h"
27
28 #include "i18n.h"
29
30 using namespace std;
31 using namespace Gtk;
32 using namespace Gtkmm2ext;
33 using namespace ARDOUR;
34
35 static const gchar *_grid_strings[] = {
36         N_("main grid"),
37         N_("Beats/128"),
38         N_("Beats/64"),
39         N_("Beats/32"),
40         N_("Beats/16"),
41         N_("Beats/8"),
42         N_("Beats/4"),
43         N_("Beats/3"),
44         N_("Beats/2"),
45         N_("Beats"),
46         0
47 };
48
49 std::vector<std::string> QuantizeDialog::grid_strings;
50
51 QuantizeDialog::QuantizeDialog (PublicEditor& e)
52         : ArdourDialog (_("Quantize"), false, false)
53         , editor (e)
54         , strength_adjustment (100.0, 0.0, 100.0, 1.0, 10.0)
55         , strength_spinner (strength_adjustment)
56         , strength_label (_("Strength"))
57         , swing_adjustment (100.0, -130.0, 130.0, 1.0, 10.0)
58         , swing_spinner (swing_adjustment)
59         , swing_button (_("Swing"))
60         , threshold_adjustment (0.0, -Timecode::BBT_Time::ticks_per_beat, Timecode::BBT_Time::ticks_per_beat, 1.0, 10.0)
61         , threshold_spinner (threshold_adjustment)
62         , threshold_label (_("Threshold (ticks)"))
63         , snap_start_button (_("Snap note start"))
64         , snap_end_button (_("Snap note end"))
65 {
66         if (grid_strings.empty()) {
67                 grid_strings =  I18N (_grid_strings);
68         }
69
70         set_popdown_strings (start_grid_combo, grid_strings);
71         start_grid_combo.set_active_text (grid_strings.front());
72         set_popdown_strings (end_grid_combo, grid_strings);
73         end_grid_combo.set_active_text (grid_strings.front());
74
75         Table* table = manage (new Table (6, 2));
76         table->set_spacings (12);
77         table->set_border_width (12);
78
79         int r = 0;
80
81         table->attach (snap_start_button, 0, 1, r, r + 1);
82         table->attach (start_grid_combo, 1, 2, r, r + 1);
83         ++r;
84
85         table->attach (snap_end_button, 0, 1, r, r + 1);
86         table->attach (end_grid_combo, 1, 2, r, r + 1);
87         ++r;
88
89         threshold_label.set_alignment (0, 0.5);
90         table->attach (threshold_label, 0, 1, r, r + 1);
91         table->attach (threshold_spinner, 1, 2, r, r + 1);
92         ++r;
93
94         strength_label.set_alignment (0, 0.5);
95         table->attach (strength_label, 0, 1, r, r + 1);
96         table->attach (strength_spinner, 1, 2, r, r + 1);
97         ++r;
98
99         table->attach (swing_button, 0, 1, r, r + 1);
100         table->attach (swing_spinner, 1, 2, r, r + 1);
101
102         snap_start_button.set_active (true);
103         snap_end_button.set_active (false);
104
105         get_vbox()->pack_start (*table);
106         show_all ();
107
108         add_button (Stock::CANCEL, RESPONSE_CANCEL);
109         add_button (_("Quantize"), RESPONSE_OK);
110 }
111
112 QuantizeDialog::~QuantizeDialog()
113 {
114 }
115
116 double
117 QuantizeDialog::start_grid_size () const
118 {
119         return grid_size_to_musical_time (start_grid_combo.get_active_text ());
120 }
121
122 double
123 QuantizeDialog::end_grid_size () const
124 {
125         return grid_size_to_musical_time (start_grid_combo.get_active_text ());
126 }
127
128 double
129 QuantizeDialog::grid_size_to_musical_time (const string& txt) const
130 {
131         if (txt == "main grid") {
132                 bool success;
133
134                 Evoral::MusicalTime b = editor.get_grid_type_as_beats (success, 0);
135                 if (!success) {
136                         return 1.0;
137                 }
138                 return b.to_double();
139         }
140
141         if (txt == _("Beats/128")) {
142                 return 1.0/128.0;
143         } else if (txt == _("Beats/64")) {
144                 return 1.0/64.0;
145         } else if (txt == _("Beats/32")) {
146                 return 1.0/32.0;
147         } else if (txt == _("Beats/16")) {
148                 return 1.0/16.0;
149         } if (txt == _("Beats/8")) {
150                 return 1.0/8.0;
151         } else if (txt == _("Beats/4")) {
152                 return 1.0/4.0;
153         } else if (txt == _("Beats/3")) {
154                 return 1.0/3.0;
155         } else if (txt == _("Beats/2")) {
156                 return 1.0/2.0;
157         } else if (txt == _("Beats")) {
158                 return 1.0;
159         }
160
161         return 1.0;
162 }
163
164 float
165 QuantizeDialog::swing () const
166 {
167         if (!swing_button.get_active()) {
168                 return 0.0f;
169         }
170
171         return swing_adjustment.get_value ();
172 }
173
174 float
175 QuantizeDialog::strength () const
176 {
177         return strength_adjustment.get_value ();
178 }
179
180 float
181 QuantizeDialog::threshold () const
182 {
183         return threshold_adjustment.get_value ();
184 }