aboutsummaryrefslogtreecommitdiffstats
path: root/src/audiomixertest.c
blob: 641fe70bfaa426327995d7bf02b3d20ba8f6a1e9 (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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/* WirePlumber
 *
 * Copyright © 2023 Collabora Ltd.
 *    @author Ashok Sidipotu <ashok.sidipotu@collabora.com>
 *
 * SPDX-License-Identifier: MIT
 */

#include <wp/wp.h>
#include <stdio.h>
#include <getopt.h>
#include <math.h>
#include "audiomixer.h"

typedef struct _AudioMixerTest AudioMixerTest;

struct _AudioMixerTest
{
	struct audiomixer *am;
	WpCore *core;
	const struct mixer_control **ctrls;
	unsigned int nctrls;
	GMainLoop *loop;
};

static void
audio_mixer_clear (AudioMixerTest *self)
{
	audiomixer_free (self->am);
	g_clear_pointer (&self->loop, g_main_loop_unref);
}

static gint
set_mute (AudioMixerTest *self, gint id)
{
	gint ret = -1;
	gboolean mute = FALSE;
	int i;

	for (i = 0; i < self->nctrls; i++) {
		const struct mixer_control *ctrl = self->ctrls[i];
		if (id == i) {

			if (g_str_equal ("bass", ctrl->name) || g_str_equal ("treble", ctrl->name))
				g_warning ("mute cannot be applied for %s control", ctrl->name);
			else {
				/* toggle mute value */
				mute = (ctrl->mute) ? FALSE : TRUE;
				audiomixer_change_mute (self->am, ctrl, mute);
				ret = TRUE;
			}

			break;
		}
	}

	return ret;
}

static gint
set_gain (AudioMixerTest *self, gint id, gfloat gain)
{
	gint ret = -1;
	int i;

	for (i = 0; i < self->nctrls; i++) {
		const struct mixer_control *ctrl = self->ctrls[i];

		if(id == i) {

			if (g_str_equal ("bass", ctrl->name) || g_str_equal ("treble", ctrl->name)) {
				if (fabs (ctrl->gain - gain) < 0.00001)
					g_warning ("gain already at requested level %f", ctrl->gain);
				else {
					audiomixer_change_gain (self->am, ctrl, gain);
					ret = TRUE;
				}
			}
			else
				g_warning ("gain cannot be applied for %s control", ctrl->name);

			break;
		}
	}

	return ret;
}

static gint
set_volume (AudioMixerTest *self, gint id, double vol)
{
	gint ret = -1;
	int i;

	for (i = 0; i < self->nctrls; i++) {
		const struct mixer_control *ctrl = self->ctrls[i];

		if (id == i) {
			if (g_str_equal ("bass", ctrl->name) || g_str_equal ("treble", ctrl->name))
				g_warning ("volume cannot be applied for %s control", ctrl->name);
			else {

				if (fabs (ctrl->volume - vol) < 0.00001)
					g_warning ("volume is already at requested level %f", ctrl->volume);
				else {
					audiomixer_change_volume (self->am, ctrl, vol);
					ret = TRUE;
				}

			}
			break;
		}
	}

	return ret;
}


static gint
set_channels_volume (AudioMixerTest *self, gint id, double lvol, double rvol)
{
	gint ret = -1;
	int i;
	gboolean set_left_channel_volume = TRUE;
	gboolean set_right_channel_volume = TRUE;

	for (i = 0; i < self->nctrls; i++) {
		const struct mixer_control *ctrl = self->ctrls[i];

		if (id == i) {
			if (g_str_equal ("bass", ctrl->name) || g_str_equal ("treble", ctrl->name))
				g_warning ("volume cannot be applied for %s control", ctrl->name);
			else {

				if (fabs (ctrl->lvolume - lvol) < 0.00001) {
					set_left_channel_volume = FALSE;
					lvol = ctrl->lvolume;
					g_warning ("left volume is already at requested level %f",
						ctrl->lvolume);
				}

				if (fabs (ctrl->rvolume - rvol) < 0.00001) {
					set_right_channel_volume = FALSE;
					rvol = ctrl->rvolume;
					g_warning ("right volume is already at requested level %f",
						ctrl->rvolume);
				}

				if (set_left_channel_volume || set_right_channel_volume) {
					audiomixer_change_channel_volume (self->am, ctrl, lvol, rvol);
					ret = TRUE;
				}

			}
			break;
		}
	}

	return ret;
}

static void
print_ctrls (AudioMixerTest *self)
{
	const struct mixer_control **ctrls = self->ctrls;
	unsigned int nctrls = self->nctrls;
	int i;

	fprintf (stdout, "\nControls:");
	for (i = 0; i < nctrls; i++) {
		const struct mixer_control *ctrl = ctrls[i];
		if (g_str_equal ("bass", ctrl->name) || g_str_equal ("treble", ctrl->name))
			fprintf(stdout, "\n%2d. %-25s   [gain: %.2f]", i, ctrl->name,
				ctrl->gain);
		else
			fprintf (stdout, "\n%2d. %-25s   [vol: %.2f, lvol: %.2f, rvol: %.2f,"
				" mute:%d]", i, ctrl->name, ctrl->volume, ctrl->lvolume, ctrl->rvolume,
				ctrl->mute);
	}
	fprintf (stdout, "\n");
}

static void
refresh_ctrls (AudioMixerTest *self)
{
	self->ctrls = audiomixer_get_active_controls (self->am, &self->nctrls);
	print_ctrls (self);
}

static void show_help (void)
{
	fprintf (stdout,
		"\n"
		"  -h, --help           Show this help\n"
		"  -p, --print-controls prints controls\n"
		"  -i, --id             control id(serial#) of the control, take a look at the controls to get the id of control\n"
		"                       Examples:\n"
		"                       audio-mixer-test -> prints the controls and help text\n"
		"                       audio-mixer-test -p -> prints only the controls\n"
		"  -v, --set-volume     set volume level for a volume control(all controls except bass and treble) this option sets\n"
		"                       volume for all the channels, for setting channel specific volumes check -l or -r options\n"
		"                       Examples:\n"
		"                       audio-mixer-test -v 0.2                   -> sets volume(for all channels) of the 1st control(default id)\n"
		"                                                                    with 0.2\n"
		"                       audio-mixer-test -i 9 -v 0.2              -> sets volume of the 9th control with 0.2\n"
		"  -l, --left-chan-vol  set left channel volume for a volume control(all controls except bass and treble)\n"
		"  -r, --right-chan-vol set right channel volume for a volume control(all controls except bass and treble)\n"
		"                       only stereo channels are supported for now, and vol update on both the channels is required\n"
		"                       Examples:\n"
		"                       audio-mixer-test -l 0.2                   -> gives an error as only one channel is updated\n"
		"                       audio-mixer-test -l 0.1 -r 0.2            -> sets left channel volume of 0.1 and right channel volume of\n"
		"                                                                    0.2 on the 1st control (default id) with 0.2\n"
		"                       audio-mixer-test -i 9 -l 0.1 -r 0.2       -> sets left channel volume of 0.1 and right channel volume\n"
		"                                                                    of 0.2 on the 9th control with 0.2\n"
		"  -g, --set-gain       set gain level for gain controls like bass and treble\n"
		"                       Examples:\n"
		"                       audio-mixer-test -i 12 -g 0.8             -> sets gain of the 11th control with 0.8\n"
		"  -m, --set-mute       mute/unmute volume controls(all controls except bass and treble) takes no arguments\n"
		"                       Examples:\n"
		"                       audio-mixer-test -m                       -> mutes the 1st control (default id)\n"
		"                       audio-mixer-test -m                       -> unmutes the 1st control (default id), if it is issued after\n"
		"                                                                    the above command\n"
		"                       audio-mixer-test -i 9 -m                  -> mutes 9th control (Multimedia) with 0.8\n");
}

static void
mixer_value_change_cb (void *data,
	unsigned int change_mask,
	const struct mixer_control *ctrl)
{
	AudioMixerTest *self = (AudioMixerTest *)data;
	refresh_ctrls (self);
	g_main_loop_quit (self->loop);
}

static void
mixer_controls_changed (void *data)
{
	AudioMixerTest *self = (AudioMixerTest *)data;
	g_main_loop_quit (self->loop);
}

gint
main (gint argc, gchar **argv)
{
	AudioMixerTest self = { 0 };
	g_autoptr (GError) error = NULL;
	gint c, ret = 0;
	struct audiomixer_events audiomixer_events = { 0 };

	gint id = -1;
	double vol = 0.0, lvol = 0.0, rvol = 0.0;
	gfloat gain = 0.0;
	gboolean set_right_channel_volume = FALSE;
	gboolean set_left_channel_volume = FALSE;

	self.loop = g_main_loop_new (NULL, FALSE);

	self.am = audiomixer_new ();

	if (!self.am) {
		g_warning ("unable to open audiomixer");
		goto exit;
	}

	audiomixer_lock (self.am);
	ret = audiomixer_ensure_controls (self.am, 3);
	audiomixer_unlock (self.am);
	if (ret < 0) {
		g_warning ("ensure controls failed");
		goto exit;
	}

	audiomixer_events.controls_changed = mixer_controls_changed;
	audiomixer_add_event_listener (self.am, &audiomixer_events, (void *)&self);

	g_debug ("waiting for controls to be available");

	do {
		self.ctrls = audiomixer_get_active_controls (self.am, &self.nctrls);

		/*
		 * not a clean check but it appears like this is the best we can do at the
		 * moment.
		 */
		if (self.nctrls <= 4)
			/* end points are not registered, wait for them to show up */
			g_main_loop_run (self.loop);
		else
			break;

	} while (1);

	if (argc == 1) {
		print_ctrls (&self);
		show_help ();
		return 0;
	}

	audiomixer_events.value_changed = mixer_value_change_cb;
	audiomixer_add_event_listener (self.am, &audiomixer_events, (void *)&self);

	static const struct option long_options[] = {
		{ "help",						no_argument,				NULL, 'h' },
		{ "print-controls",	no_argument,				NULL, 'p' },
		{ "id",							required_argument,	NULL, 'i' },
		{ "left-chan-vol",	required_argument,	NULL, 'l' },
		{ "right-chan-vol",	required_argument,	NULL, 'r' },
		{ "set-volume",			required_argument,	NULL, 'v' },
		{ "set-mute",				no_argument,				NULL, 'm' },
		{ "set-gain",				required_argument,	NULL, 'g' },
		{ NULL,	0, NULL, 0}
	};

	while ((c = getopt_long (argc, argv, "hpi:v:mg:l:r:", long_options, NULL)) != -1) {
		switch(c) {
		case 'h':
			show_help ();
			break;

		case 'p':
			print_ctrls (&self);
			break;

		case 'i':
			id = atoi (optarg);
			if (!(id >= 0 && id < self.nctrls)) {
				ret = -1;
				g_warning ("id(%d) is invalid", id);
			}
			break;

		case 'v':
			vol = (double)atof (optarg);
			if (id == -1) {
				g_warning ("control id not given defaulting it to 0(Master Playback)");
				id = 0;
			}

			ret = set_volume (&self, id, vol);
			if (ret != TRUE)
				g_warning ("set-volume failed");
			else
				/* wait for volume to be acked */
				g_main_loop_run (self.loop);

			break;

		case 'l':
			if (id == -1) {
				g_warning ("control id not given defaulting it to 0(Master Playback)");
				id = 0;
			}

			lvol = (double)atof (optarg);
			set_left_channel_volume = TRUE;
			break;

		case 'r':
			if (id == -1) {
				g_warning ("control id not given defaulting it to 0(Master Playback)");
				id = 0;
			}

			rvol = (double)atof (optarg);
			set_right_channel_volume = TRUE;
			break;

		case 'm':
			if (id == -1) {
				g_warning ("control id not given defaulting it to 0(Master Playback)");
				id = 0;
			}

			ret = set_mute (&self, id);
			if (ret != TRUE)
				g_warning ("set-mute failed");
			else
				/* wait for mute to be acked */
				g_main_loop_run (self.loop);

			break;

		case 'g':
			gain = atof (optarg);
			if (id == -1) {
				g_warning ("control id not given defaulting it to 11(bass)");
				id = 11; /* bass ctrl */
			}

			ret = set_gain (&self, id, gain);
			if (ret != TRUE)
				g_warning ("set-gain failed");
			else
				/* wait for gain to be acked */
				g_main_loop_run (self.loop);

			break;

		default:
			show_help ();
			break;
		}
	}

	if (set_left_channel_volume && set_right_channel_volume) {
		ret = set_channels_volume (&self, id, lvol, rvol);
		if (ret != TRUE)
			g_warning ("set_channels_volume failed");
		else
			/* wait for volume to be acked */
			g_main_loop_run (self.loop);
	}
	else if (set_left_channel_volume || set_right_channel_volume) {
		g_warning ("set volume of both left and right channels");
	}

exit:
	/* clean up at program exit */
	audio_mixer_clear (&self);
	return ret;
}