2 Copyright (C) 2009-2014 Paul Davis
3 Author: David Robillard
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <gtkmm/box.h>
21 #include <gtkmm/label.h>
22 #include <gtkmm/stock.h>
24 #include "transform_dialog.h"
30 using namespace ARDOUR;
32 TransformDialog::Model::Model()
33 : source_list(Gtk::ListStore::create(source_cols))
34 , property_list(Gtk::ListStore::create(property_cols))
35 , operator_list(Gtk::ListStore::create(operator_cols))
37 static const char* source_labels[] = {
40 _("the previous note's"),
41 _("this note's index"),
42 _("the number of notes"),
44 _("a random number from"),
47 for (int s = 0; source_labels[s]; ++s) {
48 Gtk::TreeModel::Row row = *(source_list->append());
49 row[source_cols.source] = (Source)(s + 1); // Skip NOTHING
50 row[source_cols.label] = source_labels[s];
52 // Special row for ramp, which doesn't correspond to a source
53 Gtk::TreeModel::Row row = *(source_list->append());
54 row[source_cols.source] = Value::NOWHERE;
55 row[source_cols.label] = _("equal steps from");
57 static const char* property_labels[] = {
65 for (int p = 0; property_labels[p]; ++p) {
66 Gtk::TreeModel::Row row = *(property_list->append());
67 row[property_cols.property] = (Property)p;
68 row[property_cols.label] = property_labels[p];
71 static const char* operator_labels[] = {
72 /* no PUSH */ "+", "-", "*", "/", NULL
74 for (int o = 0; operator_labels[o]; ++o) {
75 Gtk::TreeModel::Row row = *(operator_list->append());
76 row[operator_cols.op] = (Operator)(o + 1); // Skip PUSH
77 row[operator_cols.label] = operator_labels[o];
81 TransformDialog::TransformDialog()
82 : ArdourDialog(_("Transform"), false, false)
84 _property_combo.set_model(_model.property_list);
85 _property_combo.pack_start(_model.property_cols.label);
86 _property_combo.set_active(1);
87 _property_combo.signal_changed().connect(
88 sigc::mem_fun(this, &TransformDialog::property_changed));
90 Gtk::HBox* property_hbox = Gtk::manage(new Gtk::HBox);
91 property_hbox->pack_start(*Gtk::manage(new Gtk::Label(_("Set "))), false, false);
92 property_hbox->pack_start(_property_combo, false, false);
93 property_hbox->pack_start(*Gtk::manage(new Gtk::Label(_(" to "))), false, false);
95 _seed_chooser = Gtk::manage(new ValueChooser(_model));
96 _seed_chooser->set_target_property(MidiModel::NoteDiffCommand::Velocity);
97 _seed_chooser->source_combo.set_active(0);
98 property_hbox->pack_start(*_seed_chooser, false, false);
100 Gtk::HBox* add_hbox = Gtk::manage(new Gtk::HBox);
102 *manage(new Gtk::Image(Gtk::Stock::ADD, Gtk::ICON_SIZE_BUTTON)));
103 add_hbox->pack_start(_add_button, false, false);
104 _add_button.signal_clicked().connect(
105 sigc::mem_fun(*this, &TransformDialog::add_clicked));
107 get_vbox()->set_spacing(6);
108 get_vbox()->pack_start(*property_hbox, false, false);
109 get_vbox()->pack_start(_operations_box, false, false);
110 get_vbox()->pack_start(*add_hbox, false, false);
112 add_button(Stock::CANCEL, Gtk::RESPONSE_CANCEL);
113 add_button(_("Transform"), Gtk::RESPONSE_OK);
116 _seed_chooser->value_spinner.hide();
119 TransformDialog::ValueChooser::ValueChooser(const Model& model)
121 , target_property((Property)1)
124 source_combo.set_model(model.source_list);
125 source_combo.pack_start(model.source_cols.label);
126 source_combo.signal_changed().connect(
127 sigc::mem_fun(this, &TransformDialog::ValueChooser::source_changed));
129 property_combo.set_model(model.property_list);
130 property_combo.pack_start(model.property_cols.label);
133 pack_start(source_combo, false, false);
134 pack_start(property_combo, false, false);
135 pack_start(value_spinner, false, false);
136 pack_start(to_label, false, false);
137 pack_start(max_spinner, false, false);
140 source_combo.set_active(4);
141 property_combo.set_active(1);
142 set_target_property(MidiModel::NoteDiffCommand::Velocity);
143 max_spinner.set_value(127);
148 set_spinner_for(Gtk::SpinButton& spinner,
149 MidiModel::NoteDiffCommand::Property prop)
152 case MidiModel::NoteDiffCommand::NoteNumber:
153 case MidiModel::NoteDiffCommand::Velocity:
154 spinner.get_adjustment()->set_lower(1); // no 0, note off
155 spinner.get_adjustment()->set_upper(127);
156 spinner.get_adjustment()->set_step_increment(1);
157 spinner.get_adjustment()->set_page_increment(10);
158 spinner.set_digits(0);
160 case MidiModel::NoteDiffCommand::StartTime:
161 spinner.get_adjustment()->set_lower(0);
162 spinner.get_adjustment()->set_upper(1024);
163 spinner.get_adjustment()->set_step_increment(0.125);
164 spinner.get_adjustment()->set_page_increment(1.0);
165 spinner.set_digits(2);
167 case MidiModel::NoteDiffCommand::Length:
168 spinner.get_adjustment()->set_lower(1.0 / 64.0);
169 spinner.get_adjustment()->set_upper(32);
170 spinner.get_adjustment()->set_step_increment(1.0 / 64.0);
171 spinner.get_adjustment()->set_page_increment(1.0);
172 spinner.set_digits(2);
174 case MidiModel::NoteDiffCommand::Channel:
175 spinner.get_adjustment()->set_lower(0);
176 spinner.get_adjustment()->set_upper(15);
177 spinner.get_adjustment()->set_step_increment(1);
178 spinner.get_adjustment()->set_page_increment(10);
179 spinner.set_digits(0);
185 TransformDialog::ValueChooser::set_target_property(Property prop)
187 target_property = prop;
188 set_spinner_for(value_spinner, prop);
189 set_spinner_for(max_spinner, prop);
193 TransformDialog::ValueChooser::source_changed()
195 Gtk::TreeModel::const_iterator s = source_combo.get_active();
196 const Source source = (*s)[model.source_cols.source];
198 value_spinner.hide();
201 if (source == Value::LITERAL) {
202 value_spinner.show();
203 property_combo.hide();
204 } else if (source == Value::RANDOM) {
205 value_spinner.show();
208 property_combo.hide();
209 } else if (source == Value::NOWHERE) {
210 /* Bit of a kludge, hijack this for ramps since it's the only thing
211 that doesn't correspond to a source. When we add more fancy
212 code-generating value chooser options, the column model will need to
213 be changed a bit to reflect this. */
214 value_spinner.show();
217 property_combo.hide();
218 } else if (source == Value::INDEX || source == Value::N_NOTES) {
219 value_spinner.hide();
220 property_combo.hide();
222 value_spinner.hide();
223 property_combo.show();
228 TransformDialog::ValueChooser::get(std::list<Operation>& ops)
230 Gtk::TreeModel::const_iterator s = source_combo.get_active();
231 const Source source = (*s)[model.source_cols.source];
233 if (source == Transform::Value::RANDOM) {
234 /* Special case: a RANDOM value is always 0..1, so here we produce some
235 code to produce a random number in a range: "rand value *". */
236 const double a = value_spinner.get_value();
237 const double b = max_spinner.get_value();
238 const double min = std::min(a, b);
239 const double max = std::max(a, b);
240 const double range = max - min;
242 // "rand range * min +" (i.e. (rand * range) + min)
243 ops.push_back(Operation(Operation::PUSH, Value(Value::RANDOM)));
244 ops.push_back(Operation(Operation::PUSH, Value(range)));
245 ops.push_back(Operation(Operation::MULT));
246 ops.push_back(Operation(Operation::PUSH, Value(min)));
247 ops.push_back(Operation(Operation::ADD));
249 } else if (source == Transform::Value::NOWHERE) {
250 /* Special case: hijack NOWHERE for ramps (see above). The language
251 knows nothing of ramps, we generate code to calculate the
252 appropriate value here. */
253 const double first = value_spinner.get_value();
254 const double last = max_spinner.get_value();
255 const double rise = last - first;
257 // "index rise * n_notes / first +" (i.e. index * rise / n_notes + first)
258 ops.push_back(Operation(Operation::PUSH, Value(Value::INDEX)));
259 ops.push_back(Operation(Operation::PUSH, Value(rise)));
260 ops.push_back(Operation(Operation::MULT));
261 ops.push_back(Operation(Operation::PUSH, Value(Value::N_NOTES)));
262 ops.push_back(Operation(Operation::DIV));
263 ops.push_back(Operation(Operation::PUSH, Value(first)));
264 ops.push_back(Operation(Operation::ADD));
268 // Produce a simple Value
269 Value val((*s)[model.source_cols.source]);
270 if (val.source == Transform::Value::THIS_NOTE ||
271 val.source == Transform::Value::PREV_NOTE) {
272 Gtk::TreeModel::const_iterator p = property_combo.get_active();
273 val.prop = (*p)[model.property_cols.property];
274 } else if (val.source == Transform::Value::LITERAL) {
276 MidiModel::NoteDiffCommand::value_type(target_property),
277 value_spinner.get_value());
279 ops.push_back(Operation(Operation::PUSH, val));
282 TransformDialog::OperationChooser::OperationChooser(const Model& model)
284 , value_chooser(model)
286 operator_combo.set_model(model.operator_list);
287 operator_combo.pack_start(model.operator_cols.label);
288 operator_combo.set_active(0);
290 pack_start(operator_combo, false, false);
291 pack_start(value_chooser, false, false);
292 pack_start(*Gtk::manage(new Gtk::Label(" ")), true, true);
293 pack_start(remove_button, false, false);
296 *manage(new Gtk::Image(Gtk::Stock::REMOVE, Gtk::ICON_SIZE_BUTTON)));
298 remove_button.signal_clicked().connect(
299 sigc::mem_fun(*this, &TransformDialog::OperationChooser::remove_clicked));
301 value_chooser.source_combo.set_active(0);
304 value_chooser.property_combo.hide();
305 value_chooser.value_spinner.set_value(1);
309 TransformDialog::OperationChooser::get(std::list<Operation>& ops)
311 Gtk::TreeModel::const_iterator o = operator_combo.get_active();
313 value_chooser.get(ops);
314 ops.push_back(Operation((*o)[model.operator_cols.op]));
318 TransformDialog::OperationChooser::remove_clicked()
324 TransformDialog::get()
326 Transform::Program prog;
328 // Set target property
329 prog.prop = (*_property_combo.get_active())[_model.property_cols.property];
331 // Append code to push seed to stack
332 _seed_chooser->get(prog.ops);
334 // Append all operations' code to program
335 const std::vector<Gtk::Widget*>& choosers = _operations_box.get_children();
336 for (std::vector<Gtk::Widget*>::const_iterator o = choosers.begin();
337 o != choosers.end(); ++o) {
338 OperationChooser* chooser = dynamic_cast<OperationChooser*>(*o);
340 chooser->get(prog.ops);
348 TransformDialog::property_changed()
350 Gtk::TreeModel::const_iterator i = _property_combo.get_active();
351 _seed_chooser->set_target_property((*i)[_model.property_cols.property]);
355 TransformDialog::add_clicked()
357 _operations_box.pack_start(
358 *Gtk::manage(new OperationChooser(_model)), false, false);