1 /* control.c - interface to the controls of a jackspa plugin instance
2 * Copyright © 2013 Géraud Meyer <graud@gmx.com>
4 * This file is part of ng-jackspa.
6 * ng-jackspa is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License version 2 as published by the
8 * Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along
16 * with ng-jackspa. If not, see <http://www.gnu.org/licenses/>.
24 LADSPA_Data
control_rounding(const control_t
*control
, LADSPA_Data val
)
26 if (control
->type
== JACKSPA_INT
|| control
->type
== JACKSPA_TOGGLE
)
27 return nearbyintf(val
);
31 void control_exchange(control_t
*control
)
35 *control
->val
= control
->sel
;
39 int control_init(control_t
*control
, state_t
*state
, unsigned long port
)
42 control
->desc
= &state
->descriptor
->PortDescriptors
[port
];
43 control
->hint
= &state
->descriptor
->PortRangeHints
[port
];
44 LADSPA_PortRangeHintDescriptor descriptor
= control
->hint
->HintDescriptor
;
45 LADSPA_Data lower_bound
= control
->hint
->LowerBound
;
46 LADSPA_Data upper_bound
= control
->hint
->UpperBound
;
48 control
->name
= state
->descriptor
->PortNames
[port
];
49 control
->val
= &state
->control_port_values
[port
];
51 /* control->min, control->max */
52 if (LADSPA_IS_HINT_SAMPLE_RATE(descriptor
)) {
53 int sample_rate
= jack_get_sample_rate(state
->jack_client
);
54 lower_bound
*= sample_rate
;
55 upper_bound
*= sample_rate
;
57 if ( LADSPA_IS_HINT_BOUNDED_BELOW(descriptor
) &&
58 LADSPA_IS_HINT_BOUNDED_ABOVE(descriptor
) )
60 control
->min
= lower_bound
;
61 control
->max
= upper_bound
;
63 else if (LADSPA_IS_HINT_BOUNDED_BELOW(descriptor
)) {
64 control
->min
= lower_bound
;
67 else if (LADSPA_IS_HINT_BOUNDED_ABOVE(descriptor
)) {
69 control
->max
= upper_bound
;
77 if (LADSPA_IS_HINT_HAS_DEFAULT(descriptor
)) {
78 control
->def
= (LADSPA_Data
*)malloc(sizeof(LADSPA_Data
));
80 fprintf(stderr
, "memory allocation error\n");
83 switch (descriptor
& LADSPA_HINT_DEFAULT_MASK
) {
84 case LADSPA_HINT_DEFAULT_MINIMUM
:
85 *control
->def
= lower_bound
;
87 case LADSPA_HINT_DEFAULT_LOW
:
88 *control
->def
= lower_bound
* 0.75 + upper_bound
* 0.25;
90 case LADSPA_HINT_DEFAULT_MIDDLE
:
91 *control
->def
= lower_bound
* 0.5 + upper_bound
* 0.5;
93 case LADSPA_HINT_DEFAULT_HIGH
:
94 *control
->def
= lower_bound
* 0.25 + upper_bound
* 0.75;
96 case LADSPA_HINT_DEFAULT_MAXIMUM
:
97 *control
->def
= upper_bound
;
99 case LADSPA_HINT_DEFAULT_0
:
102 case LADSPA_HINT_DEFAULT_1
:
105 case LADSPA_HINT_DEFAULT_100
:
106 *control
->def
= 100.0;
108 case LADSPA_HINT_DEFAULT_440
:
109 *control
->def
= 440.0;
112 fprintf(stderr
, "default not found\n");
113 free(control
->def
), control
->def
= NULL
;
119 /* Check the default */
121 if (*control
->def
< control
->min
) {
122 fprintf(stderr
, "default smaller than the minimum\n");
123 *control
->def
= control
->min
;
125 if (*control
->def
> control
->max
) {
126 fprintf(stderr
, "default greater than the maximum\n");
127 *control
->def
= control
->max
;
131 /* control->inc & Overrides */
132 if (LADSPA_IS_HINT_TOGGLED(descriptor
)) {
135 control
->inc
.fine
= 1.0;
136 control
->inc
.coarse
= 1.0;
137 control
->type
= JACKSPA_TOGGLE
;
138 if (control
->def
) *control
->def
= nearbyintf(*control
->def
);
140 else if (LADSPA_IS_HINT_INTEGER(descriptor
)) {
141 control
->min
= nearbyintf(control
->min
);
142 control
->max
= nearbyintf(control
->max
);
143 control
->inc
.fine
= 1.0;
144 control
->inc
.coarse
= 1.0;
145 control
->type
= JACKSPA_INT
;
146 if (control
->def
) *control
->def
= nearbyintf(*control
->def
);
149 control
->inc
.fine
= (control
->max
- control
->min
) / 500;
150 control
->inc
.coarse
= (control
->max
- control
->min
) / 50;
151 control
->type
= JACKSPA_FLOAT
;
154 /* control->sel, control->val */
155 if (control
->def
) control
->sel
= *control
->def
;
156 else control
->sel
= control
->min
;
157 *control
->val
= control
->sel
;