summaryrefslogtreecommitdiff
path: root/libs/surfaces/websockets/strips.cc
blob: e80d8af5d7346cd5ca62c2d6e07d7db399d690a6 (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
/*
 * Copyright (C) 2020 Luciano Iam <lucianito@gmail.com>
 *
 * 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 "ardour/dB.h"
#include "ardour/plugin_insert.h"
#include "ardour/session.h"
#include "pbd/controllable.h"

#include "strips.h"

using namespace ARDOUR;

int
ArdourStrips::start ()
{
	/* take an indexed snapshot of current strips */
	StripableList strips;
	session ().get_stripables (strips, PresentationInfo::AllStripables);

	for (StripableList::iterator strip = strips.begin (); strip != strips.end (); ++strip) {
		_strips.push_back (*strip);
	}

	return 0;
}

int
ArdourStrips::stop ()
{
	_strips.clear ();
	return 0;
}

double
ArdourStrips::to_db (double k)
{
	if (k == 0) {
		return -std::numeric_limits<double>::infinity ();
	}

	float db = accurate_coefficient_to_dB (static_cast<float> (k));

	return static_cast<double> (db);
}

double
ArdourStrips::from_db (double db)
{
	if (db < -192) {
		return 0;
	}

	float k = dB_to_coefficient (static_cast<float> (db));

	return static_cast<double> (k);
}

double
ArdourStrips::strip_gain (uint32_t strip_n) const
{
	return to_db (nth_strip (strip_n)->gain_control ()->get_value ());
}

void
ArdourStrips::set_strip_gain (uint32_t strip_n, double db)
{
	nth_strip (strip_n)->gain_control ()->set_value (from_db (db), PBD::Controllable::NoGroup);
}

double
ArdourStrips::strip_pan (uint32_t strip_n) const
{
	boost::shared_ptr<AutomationControl> ac = nth_strip (strip_n)->pan_azimuth_control ();
	if (!ac) {
		/* TODO: inform GUI that strip has no panner */
		return 0;
	}
	/* scale from [0.0 ; 1.0] to [-1.0 ; 1.0] */
	return 2.0 * ac->get_value () - 1.0; //TODO: prefer ac->internal_to_interface (c->get_value ());
}

void
ArdourStrips::set_strip_pan (uint32_t strip_n, double value)
{
	boost::shared_ptr<AutomationControl> ac = nth_strip (strip_n)->pan_azimuth_control ();
	if (!ac) {
		return;
	}
	/* TODO: prefer ac->set_value (ac->interface_to_internal (value), NoGroup); */
	value = (value + 1.0) / 2.0;
	ac->set_value (value, PBD::Controllable::NoGroup);
}

bool
ArdourStrips::strip_mute (uint32_t strip_n) const
{
	return nth_strip (strip_n)->mute_control ()->muted ();
}

void
ArdourStrips::set_strip_mute (uint32_t strip_n, bool mute)
{
	nth_strip (strip_n)->mute_control ()->set_value (mute ? 1.0 : 0.0, PBD::Controllable::NoGroup);
}

bool
ArdourStrips::strip_plugin_enabled (uint32_t strip_n, uint32_t plugin_n) const
{
	return strip_plugin_insert (strip_n, plugin_n)->enabled ();
}

void
ArdourStrips::set_strip_plugin_enabled (uint32_t strip_n, uint32_t plugin_n, bool enabled)
{
	strip_plugin_insert (strip_n, plugin_n)->enable (enabled);
}

TypedValue
ArdourStrips::strip_plugin_param_value (uint32_t strip_n, uint32_t plugin_n,
                                        uint32_t param_n) const
{
	return plugin_param_value (strip_plugin_param_control (strip_n, plugin_n, param_n));
}

void
ArdourStrips::set_strip_plugin_param_value (uint32_t strip_n, uint32_t plugin_n,
                                            uint32_t param_n, TypedValue value)
{
	boost::shared_ptr<AutomationControl> control = strip_plugin_param_control (
	    strip_n, plugin_n, param_n);

	if (control) {
		ParameterDescriptor pd = control->desc ();
		double              dbl_val;

		if (pd.toggled) {
			dbl_val = static_cast<double> (static_cast<bool> (value));
		} else if (pd.enumeration || pd.integer_step) {
			dbl_val = static_cast<double> (static_cast<int> (value));
		} else {
			dbl_val = static_cast<double> (value);
		}

		control->set_value (dbl_val, PBD::Controllable::NoGroup);
	}
}

uint32_t
ArdourStrips::strip_count () const
{
	return _strips.size ();
}

boost::shared_ptr<Stripable>
ArdourStrips::nth_strip (uint32_t strip_n) const
{
	if (strip_n < _strips.size ()) {
		return _strips[strip_n];
	}

	return boost::shared_ptr<Stripable> ();
}

TypedValue
ArdourStrips::plugin_param_value (boost::shared_ptr<ARDOUR::AutomationControl> control)
{
	TypedValue value = TypedValue ();

	if (control) {
		ParameterDescriptor pd = control->desc ();

		if (pd.toggled) {
			value = TypedValue (static_cast<bool> (control->get_value ()));
		} else if (pd.enumeration || pd.integer_step) {
			value = TypedValue (static_cast<int> (control->get_value ()));
		} else {
			value = TypedValue (control->get_value ());
		}
	}

	return value;
}

boost::shared_ptr<PluginInsert>
ArdourStrips::strip_plugin_insert (uint32_t strip_n, uint32_t plugin_n) const
{
	boost::shared_ptr<Stripable> strip     = nth_strip (strip_n);
	boost::shared_ptr<Route>     route     = boost::dynamic_pointer_cast<Route> (strip);
	boost::shared_ptr<Processor> processor = route->nth_plugin (plugin_n);

	if (processor) {
		boost::shared_ptr<PluginInsert> insert =
		    boost::static_pointer_cast<PluginInsert> (processor);

		if (insert) {
			return insert;
		}
	}

	return boost::shared_ptr<PluginInsert> ();
}

boost::shared_ptr<AutomationControl>
ArdourStrips::strip_plugin_param_control (uint32_t strip_n, uint32_t plugin_n,
                                          uint32_t param_n) const
{
	boost::shared_ptr<PluginInsert> insert = strip_plugin_insert (strip_n, plugin_n);

	if (insert) {
		bool                      ok         = false;
		boost::shared_ptr<Plugin> plugin     = insert->plugin ();
		uint32_t                  control_id = plugin->nth_parameter (param_n, ok);

		if (ok && plugin->parameter_is_input (control_id)) {
			boost::shared_ptr<AutomationControl> control =
			    insert->automation_control (Evoral::Parameter (PluginAutomation, 0, control_id));
			return control;
		}
	}

	return boost::shared_ptr<AutomationControl> ();
}