Only show user-presets in favorite sidebar
[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 "pbd/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_("1/4 Note"),
38         N_("1/8 Note"),
39         N_("1/16 Note"),
40         N_("1/32 Note"),
41         N_("1/64 Note"),
42         N_("1/128 Note"),
43         
44         N_("1/3 (8th triplet)"),
45         N_("1/6 (16th triplet)"),
46         N_("1/12 (32nd triplet)"),
47
48         N_("1/5 (8th quintuplet)"),
49         N_("1/10 (16th quintuplet)"),
50         N_("1/20 (32nd quintuplet)"),
51
52         N_("1/7 (8th septuplet)"),
53         N_("1/14 (16th septuplet)"),
54         N_("1/28 (32nd septuplet)"),
55
56         0
57 };
58
59 static const int _grid_beats[] = {
60         0,
61         1, 2, 4, 8, 16, 32,
62         3, 6, 12,
63         5, 10, 20,
64         7, 14, 28,
65         0
66 };
67
68 std::vector<std::string> QuantizeDialog::grid_strings;
69
70 QuantizeDialog::QuantizeDialog (PublicEditor& e)
71         : ArdourDialog (_("Quantize"), false, false)
72         , editor (e)
73         , strength_adjustment (100.0, 0.0, 100.0, 1.0, 10.0)
74         , strength_spinner (strength_adjustment)
75         , strength_label (_("Strength"))
76         , swing_adjustment (100.0, -130.0, 130.0, 1.0, 10.0)
77         , swing_spinner (swing_adjustment)
78         , swing_button (_("Swing"))
79         , threshold_adjustment (0.0, -Timecode::BBT_Time::ticks_per_beat, Timecode::BBT_Time::ticks_per_beat, 1.0, 10.0)
80         , threshold_spinner (threshold_adjustment)
81         , threshold_label (_("Threshold (ticks)"))
82         , snap_start_button (_("Snap note start"))
83         , snap_end_button (_("Snap note end"))
84 {
85         if (grid_strings.empty()) {
86                 grid_strings =  I18N (_grid_strings);
87         }
88
89         set_popdown_strings (start_grid_combo, grid_strings);
90         start_grid_combo.set_active_text (grid_strings.front());
91         set_popdown_strings (end_grid_combo, grid_strings);
92         end_grid_combo.set_active_text (grid_strings.front());
93
94         Table* table = manage (new Table (6, 2));
95         table->set_spacings (12);
96         table->set_border_width (12);
97
98         int r = 0;
99
100         table->attach (snap_start_button, 0, 1, r, r + 1);
101         table->attach (start_grid_combo, 1, 2, r, r + 1);
102         ++r;
103
104         table->attach (snap_end_button, 0, 1, r, r + 1);
105         table->attach (end_grid_combo, 1, 2, r, r + 1);
106         ++r;
107
108         threshold_label.set_alignment (0, 0.5);
109         table->attach (threshold_label, 0, 1, r, r + 1);
110         table->attach (threshold_spinner, 1, 2, r, r + 1);
111         ++r;
112
113         strength_label.set_alignment (0, 0.5);
114         table->attach (strength_label, 0, 1, r, r + 1);
115         table->attach (strength_spinner, 1, 2, r, r + 1);
116         ++r;
117
118         table->attach (swing_button, 0, 1, r, r + 1);
119         table->attach (swing_spinner, 1, 2, r, r + 1);
120
121         snap_start_button.set_active (true);
122         snap_end_button.set_active (false);
123
124         get_vbox()->pack_start (*table);
125         get_vbox()->show_all ();
126
127         add_button (Stock::CANCEL, RESPONSE_CANCEL);
128         add_button (_("Quantize"), RESPONSE_OK);
129 }
130
131 QuantizeDialog::~QuantizeDialog()
132 {
133 }
134
135 double
136 QuantizeDialog::start_grid_size () const
137 {
138         return grid_size_to_musical_time (start_grid_combo.get_active_text ());
139 }
140
141 double
142 QuantizeDialog::end_grid_size () const
143 {
144         return grid_size_to_musical_time (end_grid_combo.get_active_text ());
145 }
146
147 double
148 QuantizeDialog::grid_size_to_musical_time (const string& txt) const
149 {
150         if ( txt == _grid_strings[0] ) {  //"Main Grid"
151                 bool success;
152
153                 Temporal::Beats b = editor.get_grid_type_as_beats (success, 0);
154                 if (!success) {
155                         return 1.0;
156                 }
157                 return b.to_double();
158         }
159
160
161         double divisor = 1.0;
162         for (size_t i = 1; i < grid_strings.size(); ++i) {
163                 if (txt == grid_strings[i]) {
164                         assert (_grid_beats[i] != 0);
165                         divisor = 1.0 / _grid_beats[i];
166                         break;
167                 }
168         }
169
170         return divisor;
171 }
172
173 float
174 QuantizeDialog::swing () const
175 {
176         if (!swing_button.get_active()) {
177                 return 0.0f;
178         }
179
180         return swing_adjustment.get_value ();
181 }
182
183 float
184 QuantizeDialog::strength () const
185 {
186         return strength_adjustment.get_value ();
187 }
188
189 float
190 QuantizeDialog::threshold () const
191 {
192         return threshold_adjustment.get_value ();
193 }