Makefile: [fix] clean: do not fail
[ng-jackspa.git] / control.c
blob067dd5b26310645947ce386f81a73519face24cf
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
13 * more details.
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/>.
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <math.h>
22 #include "control.h"
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);
28 return val;
31 void control_exchange(control_t *control)
33 LADSPA_Data buf;
34 buf = *control->val;
35 *control->val = control->sel;
36 control->sel = buf;
39 int control_init(control_t *control, state_t *state, unsigned long port)
41 control->port = 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;
65 control->max = 1.0;
67 else if (LADSPA_IS_HINT_BOUNDED_ABOVE(descriptor)) {
68 control->min = 0.0;
69 control->max = upper_bound;
71 else {
72 control->min = -1.0;
73 control->max = 1.0;
76 /* control->def */
77 if (LADSPA_IS_HINT_HAS_DEFAULT(descriptor)) {
78 control->def = (LADSPA_Data *)malloc(sizeof(LADSPA_Data));
79 if (!control->def) {
80 fprintf(stderr, "memory allocation error\n");
81 return 1;
83 switch (descriptor & LADSPA_HINT_DEFAULT_MASK) {
84 case LADSPA_HINT_DEFAULT_MINIMUM:
85 *control->def = lower_bound;
86 break;
87 case LADSPA_HINT_DEFAULT_LOW:
88 *control->def = lower_bound * 0.75 + upper_bound * 0.25;
89 break;
90 case LADSPA_HINT_DEFAULT_MIDDLE:
91 *control->def = lower_bound * 0.5 + upper_bound * 0.5;
92 break;
93 case LADSPA_HINT_DEFAULT_HIGH:
94 *control->def = lower_bound * 0.25 + upper_bound * 0.75;
95 break;
96 case LADSPA_HINT_DEFAULT_MAXIMUM:
97 *control->def = upper_bound;
98 break;
99 case LADSPA_HINT_DEFAULT_0:
100 *control->def = 0.0;
101 break;
102 case LADSPA_HINT_DEFAULT_1:
103 *control->def = 1.0;
104 break;
105 case LADSPA_HINT_DEFAULT_100:
106 *control->def = 100.0;
107 break;
108 case LADSPA_HINT_DEFAULT_440:
109 *control->def = 440.0;
110 break;
111 default:
112 fprintf(stderr, "default not found\n");
113 free(control->def), control->def = NULL;
116 else
117 control->def = NULL;
119 /* Check the default */
120 if (control->def) {
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)) {
133 control->min = 0.0;
134 control->max = 1.0;
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);
148 else {
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;
159 return 0;