summaryrefslogtreecommitdiff
path: root/libs/ardour/transform.cc
blob: 9f029829deb4e4cd3044a33fadaeaeeb0cb605dd (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
/*
    Copyright (C) 2014 Paul Davis
    Author: David Robillard

    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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <glib.h>

#include "ardour/transform.h"
#include "ardour/midi_model.h"

namespace ARDOUR {

Transform::Transform(const Program& prog)
	: _prog(prog)
{}

Variant
Transform::Context::pop()
{
	if (stack.empty()) {
		return Variant();
	}

	const Variant top = stack.top();
	stack.pop();
	return top;
}

Variant
Transform::Value::eval(const Context& ctx) const
{
	switch (source) {
	case NOWHERE:
		return Variant();
	case THIS_NOTE:
		return MidiModel::NoteDiffCommand::get_value(ctx.this_note, prop);
	case PREV_NOTE:
		if (!ctx.prev_note) {
			return Variant();
		}
		return MidiModel::NoteDiffCommand::get_value(ctx.prev_note, prop);
	case INDEX:
		return Variant(Variant::INT, ctx.index);
	case N_NOTES:
		return Variant(Variant::INT, ctx.n_notes);
	case LITERAL:
		return value;
	case RANDOM:
		return Variant(g_random_double());
	}

	return Variant();
}

void
Transform::Operation::eval(Context& ctx) const
{
	if (op == PUSH) {
		const Variant a = arg.eval(ctx);
		if (!!a) {
			/* Argument evaluated to a value, push it to the stack.  Otherwise,
			   there was a reference to the previous note, but this is the
			   first, so skip this operation and do nothing. */
			ctx.stack.push(a);
		}
		return;
	}

	// Pop operands off the stack
	const Variant rhs = ctx.pop();
	const Variant lhs = ctx.pop();
	if (!lhs || !rhs) {
		// Stack underflow (probably previous note reference), do nothing
		return;
	}

	// We can get away with just using double math and converting twice
	double value = lhs.to_double();
	switch (op) {
	case ADD:
		value += rhs.to_double();
		break;
	case SUB:
		value -= rhs.to_double();
		break;
	case MULT:
		value *= rhs.to_double();
		break;
	case DIV:
		if (rhs.to_double() == 0.0) {
			return;  // Program will fail safely
		}
		value /= rhs.to_double();
		break;
	case MOD:
		if (rhs.to_double() == 0.0) {
			return;  // Program will fail safely
		}
		value = fmod(value, rhs.to_double());
		break;
	default: break;
	}

	// Push result on to the stack
	ctx.stack.push(Variant(lhs.type(), value));
}

Command*
Transform::operator()(boost::shared_ptr<MidiModel> model,
                      Evoral::Beats                position,
                      std::vector<Notes>&          seqs)
{
	typedef MidiModel::NoteDiffCommand Command;

	Command* cmd = new Command(model, name());

	for (std::vector<Notes>::iterator s = seqs.begin(); s != seqs.end(); ++s) {
		Context ctx;
		ctx.n_notes = (*s).size();
		for (Notes::const_iterator i = (*s).begin(); i != (*s).end(); ++i) {
			const NotePtr note = *i;

			// Clear stack and run program
			ctx.stack     = std::stack<Variant>();
			ctx.this_note = note;
			for (std::list<Operation>::const_iterator o = _prog.ops.begin();
			     o != _prog.ops.end();
			     ++o) {
				(*o).eval(ctx);
			}

			// Result is on top of the stack
			if (!ctx.stack.empty() && !!ctx.stack.top()) {
				// Get the result from the top of the stack
				Variant result = ctx.stack.top();
				if (result.type() != Command::value_type(_prog.prop)) {
					// Coerce to appropriate type
					result = Variant(Command::value_type(_prog.prop),
					                 result.to_double());
				}

				// Apply change
				cmd->change(note, _prog.prop, result);
			}
			// else error or reference to note before the first, skip

			// Move forward
			ctx.prev_note = note;
			++ctx.index;
		}
	}

	return cmd;
}

}  // namespace ARDOUR