File: ctl_equal.c

package info (click to toggle)
alsaequal 0.6-7
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 324 kB
  • sloc: ansic: 2,374; makefile: 126
file content (303 lines) | stat: -rw-r--r-- 8,378 bytes parent folder | download | duplicates (3)
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
 * Copyright (c) 2008 Cooper Street Innovations
 * 		<charles@cooper-street.com>
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

#include <stdio.h>
#include <alsa/asoundlib.h>
#include <alsa/control_external.h>

#include <ladspa.h>
#include "ladspa_utils.h"

typedef struct snd_ctl_equal_control {
	long min;
	long max;
	char *name;
} snd_ctl_equal_control_t;

typedef struct snd_ctl_equal {
	snd_ctl_ext_t ext;
	void *library;
	const LADSPA_Descriptor *klass;
	int num_input_controls;
	LADSPA_Control *control_data;
	snd_ctl_equal_control_t *control_info;
} snd_ctl_equal_t;

static void equal_close(snd_ctl_ext_t *ext)
{
	snd_ctl_equal_t *equal = ext->private_data;
	int i;
	for (i = 0; i < equal->num_input_controls; i++) {
		free(equal->control_info[i].name);
	}
	free(equal->control_info);
	LADSPAcontrolUnMMAP(equal->control_data);
	LADSPAunload(equal->library);
	free(equal);
}

static int equal_elem_count(snd_ctl_ext_t *ext)
{
	snd_ctl_equal_t *equal = ext->private_data;
	return equal->num_input_controls;
}

static int equal_elem_list(snd_ctl_ext_t *ext, unsigned int offset,
		snd_ctl_elem_id_t *id)
{
	snd_ctl_equal_t *equal = ext->private_data;
	snd_ctl_elem_id_set_interface(id, SND_CTL_ELEM_IFACE_MIXER);
	snd_ctl_elem_id_set_name(id, equal->control_info[offset].name);
	snd_ctl_elem_id_set_device(id, offset);
	return 0;
}

static snd_ctl_ext_key_t equal_find_elem(snd_ctl_ext_t *ext,
		const snd_ctl_elem_id_t *id)
{
	snd_ctl_equal_t *equal = ext->private_data;
	const char *name;
	unsigned int i, key;

	name = snd_ctl_elem_id_get_name(id);

	for (i = 0; i < equal->num_input_controls; i++) {
		key = i;
		if (!strcmp(name, equal->control_info[key].name)) {
			return key;
		}
	}

	return SND_CTL_EXT_KEY_NOT_FOUND;
}

static int equal_get_attribute(snd_ctl_ext_t *ext, snd_ctl_ext_key_t key,
		int *type, unsigned int *acc, unsigned int *count)
{
	snd_ctl_equal_t *equal = ext->private_data;
	*type = SND_CTL_ELEM_TYPE_INTEGER;
	*acc = SND_CTL_EXT_ACCESS_READWRITE;
	*count = equal->control_data->channels;
	return 0;
}

static int equal_get_integer_info(snd_ctl_ext_t *ext,
	snd_ctl_ext_key_t key, long *imin, long *imax, long *istep)
{
	*istep = 1;
	*imin = 0;
	*imax = 100;
	return 0;
}

static int equal_read_integer(snd_ctl_ext_t *ext, snd_ctl_ext_key_t key,
		long *value)
{
	snd_ctl_equal_t *equal = ext->private_data;
	int i;

	for(i = 0; i < equal->control_data->channels; i++) {
		value[i] = ((equal->control_data->control[key].data[i] -
			equal->control_info[key].min)/
			(equal->control_info[key].max-
			equal->control_info[key].min))*100;
	}

	return equal->control_data->channels*sizeof(long);
}

static int equal_write_integer(snd_ctl_ext_t *ext, snd_ctl_ext_key_t key,
		long *value)
{
	snd_ctl_equal_t *equal = ext->private_data;
	int i;
	float setting;

	for(i = 0; i < equal->control_data->channels; i++) {
		setting = value[i];
		equal->control_data->control[key].data[i] = (setting/100)*
			(equal->control_info[key].max-
			equal->control_info[key].min)+
			equal->control_info[key].min;
	}

	return 1;
}

static int equal_read_event(snd_ctl_ext_t *ext ATTRIBUTE_UNUSED,
		snd_ctl_elem_id_t *id ATTRIBUTE_UNUSED,
		unsigned int *event_mask ATTRIBUTE_UNUSED)
{
	return -EAGAIN;
}

static snd_ctl_ext_callback_t equal_ext_callback = {
	.close = equal_close,
	.elem_count = equal_elem_count,
	.elem_list = equal_elem_list,
	.find_elem = equal_find_elem,
	.get_attribute = equal_get_attribute,
	.get_integer_info = equal_get_integer_info,
	.read_integer = equal_read_integer,
	.write_integer = equal_write_integer,
	.read_event = equal_read_event,
};

SND_CTL_PLUGIN_DEFINE_FUNC(equal)
{
	/* TODO: Plug all of the memory leaks if these some initialization
		failure */
	snd_config_iterator_t it, next;
	snd_ctl_equal_t *equal;
	const char *controls = ".alsaequal.bin";
	const char *library = "/usr/lib/ladspa/caps.so";
	const char *module = "Eq10";
	long channels = 2;
	const char *sufix = " Playback Volume";
	int err, i, index;

	/* Parse configuration options from asoundrc */
	snd_config_for_each(it, next, conf) {
		snd_config_t *n = snd_config_iterator_entry(it);
		const char *id;
		if (snd_config_get_id(n, &id) < 0)
			continue;
		if (strcmp(id, "comment") == 0 || strcmp(id, "type") == 0)
			continue;
		if (strcmp(id, "controls") == 0) {
			snd_config_get_string(n, &controls);
			continue;
		}
		if (strcmp(id, "library") == 0) {
			snd_config_get_string(n, &library);
			continue;
		}
		if (strcmp(id, "module") == 0) {
			snd_config_get_string(n, &module);
			continue;
		}
		if (strcmp(id, "channels") == 0) {
			snd_config_get_integer(n, &channels);
			if(channels < 1) {
				SNDERR("channels < 1");
				return -EINVAL;
			}
			continue;
		}
		SNDERR("Unknown field %s", id);
		return -EINVAL;
	}

	/* Intialize the local object data */
	equal = calloc(1, sizeof(*equal));
	if (equal == NULL)
		return -ENOMEM;

	equal->ext.version = SND_CTL_EXT_VERSION;
	equal->ext.card_idx = 0;
	equal->ext.poll_fd = -1;
	equal->ext.callback = &equal_ext_callback;
	equal->ext.private_data = equal;

	/* Open the LADSPA Plugin */
	equal->library = LADSPAload(library);
	if(equal->library == NULL) {
		return -1;
	}

	equal->klass = LADSPAfind(equal->library, library, module);
	if(equal->klass == NULL) {
		return -1;
	}

	/* Import data from the LADSPA Plugin */
	strncpy(equal->ext.id, equal->klass->Label, sizeof(equal->ext.id));
	strncpy(equal->ext.driver, "LADSPA Plugin", sizeof(equal->ext.driver));
	strncpy(equal->ext.name, equal->klass->Label, sizeof(equal->ext.name));
	strncpy(equal->ext.longname, equal->klass->Name,
			sizeof(equal->ext.longname));
	strncpy(equal->ext.mixername, "alsaequal", sizeof(equal->ext.mixername));

	/* Create the ALSA External Plugin */
	err = snd_ctl_ext_create(&equal->ext, name, SND_CTL_NONBLOCK);
	if (err < 0) {
		return -1;
	}

	/* MMAP to the controls file */
	equal->control_data = LADSPAcontrolMMAP(equal->klass, controls, channels);
	if(equal->control_data == NULL) {
		return -1;
	}
	
	equal->num_input_controls = 0;
	for(i = 0; i < equal->control_data->num_controls; i++) {
		if(equal->control_data->control[i].type == LADSPA_CNTRL_INPUT) {
			equal->num_input_controls++;
		}
	}
	
	/* Pull in data from controls file */
	equal->control_info = malloc(
			sizeof(snd_ctl_equal_control_t)*equal->num_input_controls);
	if(equal->control_info == NULL) {
		return -1;
	}

	for(i = 0; i < equal->num_input_controls; i++) {
		if(equal->control_data->control[i].type == LADSPA_CNTRL_INPUT) {
			index = equal->control_data->control[i].index;
			if((equal->klass->PortDescriptors[index] &
					(LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL)) == 0) {
				SNDERR("Problem with control file %s, %d.", controls, index);
				return -1;
			}
			equal->control_info[i].min =
					equal->klass->PortRangeHints[index].LowerBound;
			equal->control_info[i].max =
					equal->klass->PortRangeHints[index].UpperBound;
			equal->control_info[i].name = malloc(
					strlen(equal->klass->PortNames[index]) +
					strlen(sufix) + 6);
			if(equal->control_info[i].name == NULL) {
				return -1;
			}
			sprintf(equal->control_info[i].name, "%02d. %s%s",
					index, equal->klass->PortNames[index], sufix);
		}
	}

	/* Make sure that the control file makes sense */
	if(equal->klass->PortDescriptors[equal->control_data->input_index] !=
			(LADSPA_PORT_INPUT | LADSPA_PORT_AUDIO)) {
		SNDERR("Problem with control file %s.", controls);
		return -1;
	}
	if(equal->klass->PortDescriptors[equal->control_data->output_index] !=
			(LADSPA_PORT_OUTPUT | LADSPA_PORT_AUDIO)) {
		SNDERR("Problem with control file %s.", controls);
		return -1;
	}

	*handlep = equal->ext.handle;
	return 0;

}

SND_CTL_PLUGIN_SYMBOL(equal);