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