add support for beats/2 quantization in dialog
[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 "gtkmm2ext/utils.h"
22
23 #include "pbd/convert.h"
24 #include "quantize_dialog.h"
25 #include "public_editor.h"
26
27 #include "i18n.h"
28
29 using namespace std;
30 using namespace Gtk;
31 using namespace Gtkmm2ext;
32 using namespace ARDOUR;
33
34 static const gchar *_grid_strings[] = {
35         N_("main grid"),
36         N_("Beats/128"),
37         N_("Beats/64"),
38         N_("Beats/32"),
39         N_("Beats/16"),
40         N_("Beats/8"),
41         N_("Beats/4"),
42         N_("Beats/3"),
43         N_("Beats/2"),
44         N_("Beats"),
45         0
46 };
47
48 static const gchar *_type_strings[] = {
49         N_("Grid"),
50         N_("Legato"),
51         N_("Groove"),
52         0
53 };
54
55 std::vector<std::string> QuantizeDialog::grid_strings;
56 std::vector<std::string> QuantizeDialog::type_strings;
57
58 QuantizeDialog::QuantizeDialog (PublicEditor& e)
59         : ArdourDialog (_("Quantize"), false, false)
60         , editor (e)
61         , type_label (_("Quantize Type"))
62         , strength_adjustment (100.0, 0.0, 100.0, 1.0, 10.0)
63         , strength_spinner (strength_adjustment)
64         , strength_label (_("Strength"))
65         , swing_adjustment (100.0, -130.0, 130.0, 1.0, 10.0)
66         , swing_spinner (swing_adjustment)
67         , swing_button (_("Swing"))
68         , threshold_adjustment (0.0, -1920.0, 1920.0, 1.0, 10.0) // XXX MAGIC TICK NUMBER FIX ME
69         , threshold_spinner (threshold_adjustment)
70         , threshold_label (_("Threshold (ticks)"))
71         , snap_start_button (_("Snap note start"))
72         , snap_end_button (_("Snap note end"))
73 {
74         if (grid_strings.empty()) {
75                 grid_strings =  I18N (_grid_strings);
76                 type_strings =  I18N (_type_strings);
77         }
78
79         set_popdown_strings (start_grid_combo, grid_strings);
80         start_grid_combo.set_active_text (grid_strings.front());
81         set_popdown_strings (end_grid_combo, grid_strings);
82         end_grid_combo.set_active_text (grid_strings.front());
83
84         set_popdown_strings (type_combo, type_strings);
85         type_combo.set_active_text (type_strings.front());
86
87         get_vbox()->set_border_width (12);
88
89         HBox* hbox;
90
91         hbox = manage (new HBox);
92         hbox->set_spacing (12);
93         hbox->set_border_width (6);
94         hbox->pack_start (type_label);
95         hbox->pack_start (type_combo);
96         hbox->show ();
97         type_label.show ();
98         type_combo.show ();
99         get_vbox()->pack_start (*hbox);
100
101         hbox = manage (new HBox);
102         hbox->set_spacing (12);
103         hbox->set_border_width (6);
104         hbox->pack_start (snap_start_button);
105         hbox->pack_start (start_grid_combo);
106         hbox->show ();
107         snap_start_button.show ();
108         start_grid_combo.show ();
109         get_vbox()->pack_start (*hbox);
110
111         hbox = manage (new HBox);
112         hbox->set_spacing (12);
113         hbox->set_border_width (6);
114         hbox->pack_start (snap_end_button);
115         hbox->pack_start (end_grid_combo);
116         hbox->show ();
117         snap_end_button.show ();
118         end_grid_combo.show ();
119         get_vbox()->pack_start (*hbox);
120
121         hbox = manage (new HBox);
122         hbox->set_spacing (12);
123         hbox->set_border_width (6);
124         hbox->pack_start (threshold_label);
125         hbox->pack_start (threshold_spinner);
126         hbox->show ();
127         threshold_label.show ();
128         threshold_spinner.show ();
129         get_vbox()->pack_start (*hbox);
130
131         hbox = manage (new HBox);
132         hbox->set_spacing (12);
133         hbox->set_border_width (6);
134         hbox->pack_start (strength_label);
135         hbox->pack_start (strength_spinner);
136         hbox->show ();
137         strength_label.show ();
138         strength_spinner.show ();
139         get_vbox()->pack_start (*hbox);
140
141         hbox = manage (new HBox);
142         hbox->set_spacing (12);
143         hbox->set_border_width (6);
144         hbox->pack_start (swing_button);
145         hbox->pack_start (swing_spinner);
146         hbox->show ();
147         swing_button.show ();
148         swing_spinner.show ();
149         get_vbox()->pack_start (*hbox);
150
151         snap_start_button.set_active (true);
152         snap_end_button.set_active (false);
153
154         add_button (Stock::CANCEL, RESPONSE_CANCEL);
155         add_button (Stock::OK, RESPONSE_OK);
156 }
157
158 QuantizeDialog::~QuantizeDialog()
159 {
160 }
161
162 double
163 QuantizeDialog::start_grid_size () const
164 {
165         return grid_size_to_musical_time (start_grid_combo.get_active_text ());
166 }
167
168 double
169 QuantizeDialog::end_grid_size () const
170 {
171         return grid_size_to_musical_time (start_grid_combo.get_active_text ());
172 }
173
174 double
175 QuantizeDialog::grid_size_to_musical_time (const string& txt) const
176 {
177         if (txt == "main grid") {
178                 bool success;
179
180                 Evoral::MusicalTime b = editor.get_grid_type_as_beats (success, 0);
181                 if (!success) {
182                         return 1.0;
183                 }
184                 return (double) b;
185         }
186
187         if (txt == _("Beats/128")) {
188                 return 1.0/128.0;
189         } else if (txt == _("Beats/64")) {
190                 return 1.0/64.0;
191         } else if (txt == _("Beats/32")) {
192                 return 1.0/32.0;
193         } else if (txt == _("Beats/16")) {
194                 return 1.0/16.0;
195         } if (txt == _("Beats/8")) {
196                 return 1.0/8.0;
197         } else if (txt == _("Beats/4")) {
198                 return 1.0/4.0;
199         } else if (txt == _("Beats/3")) {
200                 return 1.0/3.0;
201         } else if (txt == _("Beats/2")) {
202                 return 1.0/2.0;
203         } else if (txt == _("Beats")) {
204                 return 1.0;
205         }
206         
207         return 1.0;
208 }
209
210 float
211 QuantizeDialog::swing () const
212 {
213         if (!swing_button.get_active()) {
214                 return 0.0f;
215         }
216
217         return swing_adjustment.get_value ();
218 }
219
220 float
221 QuantizeDialog::strength () const
222 {
223         return strength_adjustment.get_value ();
224 }
225
226 float
227 QuantizeDialog::threshold () const
228 {
229         return threshold_adjustment.get_value ();
230 }