summaryrefslogtreecommitdiff
path: root/gtk2_ardour/transform_dialog.cc
blob: 2f6dcff405ac30f81a5d6de4bcb061863eb2743c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
 * Copyright (C) 2014-2019 David Robillard <d@drobilla.net>
 * Copyright (C) 2015-2017 Robin Gareus <robin@gareus.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <gtkmm/box.h>
#include <gtkmm/label.h>
#include <gtkmm/stock.h>

#include "transform_dialog.h"

#include "pbd/i18n.h"

using namespace ARDOUR;

TransformDialog::Model::Model()
	: source_list(Gtk::ListStore::create(source_cols))
	, property_list(Gtk::ListStore::create(property_cols))
	, operator_list(Gtk::ListStore::create(operator_cols))
{
	static const char* source_labels[] = {
		/* no NOTHING */
		_("this note's"),
		_("the previous note's"),
		_("this note's index"),
		_("the number of notes"),
		_("exactly"),
		_("a random number from"),
		NULL
	};
	for (int s = 0; source_labels[s]; ++s) {
		Gtk::TreeModel::Row row = *(source_list->append());
		row[source_cols.source] = (Source)(s + 1);  // Skip NOTHING
		row[source_cols.label]  = source_labels[s];
	}
	// Special row for ramp, which doesn't correspond to a source
	Gtk::TreeModel::Row row = *(source_list->append());
	row[source_cols.source] = Value::NOWHERE;
	row[source_cols.label]  = _("equal steps from");

	static const char* property_labels[] = {
		_("note number"),
		_("velocity"),
		_("start time"),
		_("length"),
		_("channel"),
		NULL
	};
	for (int p = 0; property_labels[p]; ++p) {
		Gtk::TreeModel::Row row = *(property_list->append());
		row[property_cols.property] = (Property)p;
		row[property_cols.label]    = property_labels[p];
	}

	static const char* operator_labels[] = {
		/* no PUSH */ "+", "-", "*", "/", "mod", NULL
	};
	for (int o = 0; operator_labels[o]; ++o) {
		Gtk::TreeModel::Row row = *(operator_list->append());
		row[operator_cols.op]    = (Operator)(o + 1);  // Skip PUSH
		row[operator_cols.label] = operator_labels[o];
	}
}

TransformDialog::TransformDialog()
	: ArdourDialog(_("Transform"), false, false)
{
	_property_combo.set_model(_model.property_list);
	_property_combo.pack_start(_model.property_cols.label);
	_property_combo.set_active(1);
	_property_combo.signal_changed().connect(
		sigc::mem_fun(this, &TransformDialog::property_changed));

	Gtk::HBox* property_hbox = Gtk::manage(new Gtk::HBox);
	property_hbox->pack_start(*Gtk::manage(new Gtk::Label(_("Set "))), false, false);
	property_hbox->pack_start(_property_combo, false, false);
	property_hbox->pack_start(*Gtk::manage(new Gtk::Label(_(" to "))), false, false);

	_seed_chooser = Gtk::manage(new ValueChooser(_model));
	_seed_chooser->set_target_property(MidiModel::NoteDiffCommand::Velocity);
	_seed_chooser->source_combo.set_active(0);
	property_hbox->pack_start(*_seed_chooser, false, false);

	Gtk::HBox* add_hbox = Gtk::manage(new Gtk::HBox);
	_add_button.add(
		*Gtk::manage(new Gtk::Image(Gtk::Stock::ADD, Gtk::ICON_SIZE_BUTTON)));
	add_hbox->pack_start(_add_button, false, false);
	_add_button.signal_clicked().connect(
		sigc::mem_fun(*this, &TransformDialog::add_clicked));

	get_vbox()->set_spacing(6);
	get_vbox()->pack_start(*property_hbox, false, false);
	get_vbox()->pack_start(_operations_box, false, false);
	get_vbox()->pack_start(*add_hbox, false, false);

	add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
	add_button(_("Transform"), Gtk::RESPONSE_OK);

	show_all();
	_seed_chooser->value_spinner.hide();
	_seed_chooser->source_changed();
}

TransformDialog::ValueChooser::ValueChooser(const Model& model)
	: model(model)
	, target_property((Property)1)
	, to_label(" to ")
{
	source_combo.set_model(model.source_list);
	source_combo.pack_start(model.source_cols.label);
	source_combo.signal_changed().connect(
		sigc::mem_fun(this, &TransformDialog::ValueChooser::source_changed));

	property_combo.set_model(model.property_list);
	property_combo.pack_start(model.property_cols.label);

	set_spacing(4);
	pack_start(source_combo, false, false);
	pack_start(property_combo, false, false);
	pack_start(value_spinner, false, false);
	pack_start(to_label, false, false);
	pack_start(max_spinner, false, false);
	show_all();

	source_combo.set_active(4);
	property_combo.set_active(1);
	set_target_property(MidiModel::NoteDiffCommand::Velocity);
	max_spinner.set_value(127);
	source_changed();
}

static void
set_spinner_for(Gtk::SpinButton&                     spinner,
                MidiModel::NoteDiffCommand::Property prop)
{
	switch (prop) {
	case MidiModel::NoteDiffCommand::NoteNumber:
	case MidiModel::NoteDiffCommand::Velocity:
		spinner.get_adjustment()->set_lower(1); // no 0, note off
		spinner.get_adjustment()->set_upper(127);
		spinner.get_adjustment()->set_step_increment(1);
		spinner.get_adjustment()->set_page_increment(10);
		spinner.set_digits(0);
		break;
	case MidiModel::NoteDiffCommand::StartTime:
		spinner.get_adjustment()->set_lower(0);
		spinner.get_adjustment()->set_upper(1024);
		spinner.get_adjustment()->set_step_increment(0.125);
		spinner.get_adjustment()->set_page_increment(1.0);
		spinner.set_digits(2);
		break;
	case MidiModel::NoteDiffCommand::Length:
		spinner.get_adjustment()->set_lower(1.0 / 64.0);
		spinner.get_adjustment()->set_upper(32);
		spinner.get_adjustment()->set_step_increment(1.0 / 64.0);
		spinner.get_adjustment()->set_page_increment(1.0);
		spinner.set_digits(2);
		break;
	case MidiModel::NoteDiffCommand::Channel:
		spinner.get_adjustment()->set_lower(1);
		spinner.get_adjustment()->set_upper(16);
		spinner.get_adjustment()->set_step_increment(1);
		spinner.get_adjustment()->set_page_increment(10);
		spinner.set_digits(0);
		break;
	}
	spinner.set_value(
		std::min(spinner.get_adjustment()->get_upper(),
		         std::max(spinner.get_adjustment()->get_lower(), spinner.get_value())));
}

void
TransformDialog::ValueChooser::set_target_property(Property prop)
{
	target_property = prop;
	set_spinner_for(value_spinner, prop);
	set_spinner_for(max_spinner, prop);
}

void
TransformDialog::ValueChooser::source_changed()
{
	Gtk::TreeModel::const_iterator s      = source_combo.get_active();
	const Source                   source = (*s)[model.source_cols.source];

	value_spinner.hide();
	to_label.hide();
	max_spinner.hide();
	if (source == Value::LITERAL) {
		value_spinner.show();
		property_combo.hide();
	} else if (source == Value::RANDOM) {
		value_spinner.show();
		to_label.show();
		max_spinner.show();
		property_combo.hide();
	} else if (source == Value::NOWHERE) {
		/* Bit of a kludge, hijack this for ramps since it's the only thing
		   that doesn't correspond to a source.  When we add more fancy
		   code-generating value chooser options, the column model will need to
		   be changed a bit to reflect this. */
		value_spinner.show();
		to_label.show();
		max_spinner.show();
		property_combo.hide();
	} else if (source == Value::INDEX || source == Value::N_NOTES) {
		value_spinner.hide();
		property_combo.hide();
	} else {
		value_spinner.hide();
		property_combo.show();
	}
}

double
TransformDialog::ValueChooser::get_value() const
{
	const bool is_channel = target_property == MidiModel::NoteDiffCommand::Channel;
	return value_spinner.get_value() + (is_channel ? -1.0 : 0.0);
}

double
TransformDialog::ValueChooser::get_max() const
{
	const bool is_channel = target_property == MidiModel::NoteDiffCommand::Channel;
	return max_spinner.get_value() + (is_channel ? -1.0 : 0.0);
}

void
TransformDialog::ValueChooser::get(std::list<Operation>& ops)
{
	Gtk::TreeModel::const_iterator s      = source_combo.get_active();
	const Source                   source = (*s)[model.source_cols.source];

	if (source == Transform::Value::RANDOM) {
		/* Special case: a RANDOM value is always 0..1, so here we produce some
		   code to produce a random number in a range: "rand value *". */
		const double a     = get_value();
		const double b     = get_max();
		const double min   = std::min(a, b);
		const double max   = std::max(a, b);
		const double range = max - min;

		// "rand range * min +" ((rand * range) + min)
		ops.push_back(Operation(Operation::PUSH, Value(Value::RANDOM)));
		ops.push_back(Operation(Operation::PUSH, Value(range)));
		ops.push_back(Operation(Operation::MULT));
		ops.push_back(Operation(Operation::PUSH, Value(min)));
		ops.push_back(Operation(Operation::ADD));
		return;
	} else if (source == Transform::Value::NOWHERE) {
		/* Special case: hijack NOWHERE for ramps (see above).  The language
		   knows nothing of ramps, we generate code to calculate the
		   appropriate value here. */
		const double first = get_value();
		const double last  = get_max();
		const double rise  = last - first;

		// "index rise * n_notes 1 - / first +" (index * rise / (n_notes - 1) + first)
		ops.push_back(Operation(Operation::PUSH, Value(Value::INDEX)));
		ops.push_back(Operation(Operation::PUSH, Value(rise)));
		ops.push_back(Operation(Operation::MULT));
		ops.push_back(Operation(Operation::PUSH, Value(Value::N_NOTES)));
		ops.push_back(Operation(Operation::PUSH, Value(1)));
		ops.push_back(Operation(Operation::SUB));
		ops.push_back(Operation(Operation::DIV));
		ops.push_back(Operation(Operation::PUSH, Value(first)));
		ops.push_back(Operation(Operation::ADD));
		return;
	}

	// Produce a simple Value
	Value val((*s)[model.source_cols.source]);
	if (val.source == Transform::Value::THIS_NOTE ||
	    val.source == Transform::Value::PREV_NOTE) {
		Gtk::TreeModel::const_iterator p = property_combo.get_active();
		val.prop = (*p)[model.property_cols.property];
	} else if (val.source == Transform::Value::LITERAL) {
		val.value = Variant(
			MidiModel::NoteDiffCommand::value_type(target_property),
			get_value());
	}
	ops.push_back(Operation(Operation::PUSH, val));
}

TransformDialog::OperationChooser::OperationChooser(const Model& model)
	: model(model)
	, value_chooser(model)
{
	operator_combo.set_model(model.operator_list);
	operator_combo.pack_start(model.operator_cols.label);
	operator_combo.set_active(0);

	pack_start(operator_combo, false, false);
	pack_start(value_chooser, false, false);
	pack_start(*Gtk::manage(new Gtk::Label(" ")), true, true);
	pack_start(remove_button, false, false);

	remove_button.add(
		*Gtk::manage(new Gtk::Image(Gtk::Stock::REMOVE, Gtk::ICON_SIZE_BUTTON)));

	remove_button.signal_clicked().connect(
		sigc::mem_fun(*this, &TransformDialog::OperationChooser::remove_clicked));

	value_chooser.source_combo.set_active(0);

	show_all();
	value_chooser.property_combo.hide();
	value_chooser.value_spinner.set_value(1);
	value_chooser.source_changed();
}

void
TransformDialog::OperationChooser::get(std::list<Operation>& ops)
{
	Gtk::TreeModel::const_iterator o = operator_combo.get_active();

	value_chooser.get(ops);
	ops.push_back(Operation((*o)[model.operator_cols.op]));
}

void
TransformDialog::OperationChooser::remove_clicked()
{
	delete this;
}

Transform::Program
TransformDialog::get()
{
	Transform::Program prog;

	// Set target property
	prog.prop = (*_property_combo.get_active())[_model.property_cols.property];

	// Append code to push seed to stack
	_seed_chooser->get(prog.ops);

	// Append all operations' code to program
	const std::vector<Gtk::Widget*>& choosers = _operations_box.get_children();
	for (std::vector<Gtk::Widget*>::const_iterator o = choosers.begin();
	     o != choosers.end(); ++o) {
		OperationChooser* chooser = dynamic_cast<OperationChooser*>(*o);
		if (chooser) {
			chooser->get(prog.ops);
		}
	}

	return prog;
}

void
TransformDialog::property_changed()
{
	Gtk::TreeModel::const_iterator i = _property_combo.get_active();
	_seed_chooser->set_target_property((*i)[_model.property_cols.property]);
}

void
TransformDialog::add_clicked()
{
	_operations_box.pack_start(
		*Gtk::manage(new OperationChooser(_model)), false, false);
}