1 /*
2  * Copyright (C) 2018-2020 Alexandros Theodotou <alex at zrythm dot org>
3  *
4  * This file is part of Zrythm
5  *
6  * Zrythm is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Zrythm is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with Zrythm.  If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 /**
21  * \file
22  *
23  * Functions for control ports.
24  */
25 
26 #ifndef __AUDIO_CONTROL_PORT_H__
27 #define __AUDIO_CONTROL_PORT_H__
28 
29 #include "audio/port_identifier.h"
30 #include "audio/tempo_track.h"
31 
32 #include <stdbool.h>
33 
34 typedef struct Port Port;
35 
36 /**
37  * Used for queueing changes to be applied during
38  * processing.
39  *
40  * Used only for non-plugin ports such as BPM and
41  * time signature.
42  */
43 typedef struct ControlPortChange
44 {
45   /**
46    * Flag to identify the port the change is for.
47    *
48    * @seealso PORT_FLAG_BPM.
49    */
50   PortFlags     flag1;
51 
52   /**
53    * Flag to identify the port the change is for.
54    *
55    * @seealso PORT_FLAG2_BEATS_PER_BAR and
56    *   PORT_FLAG2_BEAT_UNIT.
57    */
58   PortFlags2    flag2;
59 
60   /** Real (not normalized) value to set. */
61   float         real_val;
62 
63   /** Integer val. */
64   int           ival;
65 
66   BeatUnit      beat_unit;
67 
68 } ControlPortChange;
69 
70 /**
71  * @addtogroup audio
72  *
73  * @{
74  */
75 
76 /**
77  * Converts normalized value (0.0 to 1.0) to
78  * real value (eg. -10.0 to 100.0).
79  *
80  * @note This behaves differently from
81  *   \ref port_set_control_value() and
82  *   \ref port_get_control_value() and should be
83  *   used in widgets.
84  */
85 NONNULL
86 PURE
87 float
88 control_port_normalized_val_to_real (
89   const Port * const self,
90   float              normalized_val);
91 
92 /**
93  * Converts real value (eg. -10.0 to 100.0) to
94  * normalized value (0.0 to 1.0).
95  *
96  * @note This behaves differently from
97  *   \ref port_set_control_value() and
98  *   \ref port_get_control_value() and should be
99  *   used in widgets.
100  */
101 NONNULL
102 PURE
103 float
104 control_port_real_val_to_normalized (
105   const Port * const self,
106   float              real_val);
107 
108 /**
109  * Checks if the given value is toggled.
110  */
111 #define control_port_is_val_toggled(val) \
112   (val > 0.001f)
113 
114 /**
115  * Returns if the control port is toggled.
116  */
117 #define control_port_is_toggled(self) \
118   (control_port_is_val_toggled (self->control))
119 
120 /**
121  * Gets the control value for an integer port.
122  */
123 int
124 control_port_get_int (
125   Port * self);
126 
127 /**
128  * Gets the control value for an integer port.
129  */
130 int
131 control_port_get_int_from_val (
132   float val);
133 
134 /**
135  * Returns the snapped value (eg, if toggle,
136  * returns 0.f or 1.f).
137  */
138 float
139 control_port_get_snapped_val (
140   Port * self);
141 
142 /**
143  * Returns the snapped value (eg, if toggle,
144  * returns 0.f or 1.f).
145  */
146 float
147 control_port_get_snapped_val_from_val (
148   Port * self,
149   float  val);
150 
151 /**
152  * Get the current real value of the control.
153  *
154  * TODO "normalize" parameter.
155  */
156 float
157 control_port_get_val (
158   Port * self);
159 
160 /**
161  * Get the current normalized value of the control.
162  */
163 float
164 control_port_get_normalized_val (
165   Port * self);
166 
167 /**
168  * Get the current real unsnapped value of the
169  * control.
170  */
171 float
172 control_port_get_unsnapped_val (
173   Port * self);
174 
175 /**
176  * Get the default real value of the control.
177  */
178 float
179 control_port_get_default_val (
180   Port * self);
181 
182 /**
183  * Get the default real value of the control.
184  */
185 void
186 control_port_set_real_val (
187   Port * self,
188   float  val);
189 
190 /**
191  * Get the default real value of the control and
192  * sends UI events.
193  */
194 void
195 control_port_set_real_val_w_events (
196   Port * self,
197   float  val);
198 
199 /**
200  * Wrapper over port_set_control_value() for toggles.
201  */
202 void
203 control_port_set_toggled (
204   Port * self,
205   bool   toggled,
206   bool   forward_events);
207 
208 /**
209  * Updates the actual value.
210  *
211  * The given value is always a normalized 0.0-1.0
212  * value and must be translated to the actual value
213  * before setting it.
214  *
215  * @param automating Whether this is from an
216  *   automation event. This will set Lv2Port's
217  *   automating field to true, which will cause the
218  *   plugin to receive a UI event for this change.
219  */
220 HOT
221 NONNULL
222 void
223 control_port_set_val_from_normalized (
224   Port * self,
225   float  val,
226   bool   automating);
227 
228 /**
229  * @}
230  */
231 
232 #endif
233