[go: up one dir, main page]

File: ao.c

package info (click to toggle)
cmus 2.10.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,040 kB
  • sloc: ansic: 38,844; sh: 1,578; makefile: 257; python: 157
file content (308 lines) | stat: -rw-r--r-- 6,842 bytes parent folder | download | duplicates (4)
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
/*
 * Copyright 2008-2013 Various Authors
 * Copyright 2006 Johannes Weißl
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include "../op.h"
#include "../xmalloc.h"
#include "../utils.h"
#include "../misc.h"
#include "../debug.h"

/*
 * <ao/ao.h> uses FILE but doesn't include stdio.h.
 * Also we use snprintf().
 */
#include <stdio.h>
#include <strings.h>
#include <ao/ao.h>

#ifdef AO_EBADFORMAT
#define AO_API_1
#endif

static ao_device *libao_device;
static char *wav_dir = NULL;
static int wav_counter = 1;
static int is_wav = 0;

/* configuration */
static char *libao_driver = NULL;
static int libao_buffer_space = 16384;
static int libao_cur_buffer_space = 0;
static char *libao_device_interface = NULL;


static int op_ao_init(void)
{
	/* ignore config value */
	wav_counter = 1;

	ao_initialize();
	return 0;
}

static int op_ao_exit(void)
{
	free(libao_driver);
	ao_shutdown();
	return 0;
}

/* http://www.xiph.org/ao/doc/ao_sample_format.html */
static const struct {
	channel_position_t pos;
	const char *str;
} ao_channel_mapping[] = {
	{ CHANNEL_POSITION_LEFT,			"L" },
	{ CHANNEL_POSITION_RIGHT,			"R" },
	{ CHANNEL_POSITION_CENTER,			"C" },
	{ CHANNEL_POSITION_MONO,			"M" },
	{ CHANNEL_POSITION_FRONT_LEFT_OF_CENTER,	"CL" },
	{ CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER,	"CR" },
	{ CHANNEL_POSITION_REAR_LEFT,			"BL" },
	{ CHANNEL_POSITION_REAR_RIGHT,			"BR" },
	{ CHANNEL_POSITION_REAR_CENTER,			"BC" },
	{ CHANNEL_POSITION_SIDE_LEFT,			"SL" },
	{ CHANNEL_POSITION_SIDE_RIGHT,			"SR" },
	{ CHANNEL_POSITION_LFE,				"LFE" },
	{ CHANNEL_POSITION_INVALID,			"X" },
};

#ifdef AO_API_1
static char *ao_channel_matrix(int channels, const channel_position_t *map)
{
	int i, j;
	char buf[256] = "";

	if (!map || !channel_map_valid(map))
		return NULL;

	for (i = 0; i < channels; i++) {
		const channel_position_t pos = map[i];
		int found = 0;
		for (j = 0; j < N_ELEMENTS(ao_channel_mapping); j++) {
			if (pos == ao_channel_mapping[j].pos) {
				strcat(buf, ao_channel_mapping[j].str);
				strcat(buf, ",");
				found = 1;
				break;
			}
		}
		if (!found)
			strcat(buf, "M,");
	}
	buf[strlen(buf)-1] = '\0';

	return xstrdup(buf);
}
#endif

static int op_ao_open(sample_format_t sf, const channel_position_t *channel_map)
{
	ao_sample_format format = {
		.bits        = sf_get_bits(sf),
		.rate        = sf_get_rate(sf),
		.channels    = sf_get_channels(sf),
		.byte_format = sf_get_bigendian(sf) ? AO_FMT_BIG : AO_FMT_LITTLE,
#ifdef AO_API_1
		.matrix      = ao_channel_matrix(sf_get_channels(sf), channel_map)
#endif
	};
	int driver;

	if (libao_driver == NULL) {
		driver = ao_default_driver_id();
	} else {
		driver = ao_driver_id(libao_driver);
		is_wav = strcasecmp(libao_driver, "wav") == 0;
	}
	if (driver == -1) {
		errno = ENODEV;
		return -OP_ERROR_ERRNO;
	}

	if (is_wav) {
		char file[512];

		if (wav_dir == NULL)
			wav_dir = xstrdup(home_dir);
		snprintf(file, sizeof(file), "%s/%02d.wav", wav_dir, wav_counter);
		libao_device = ao_open_file(driver, file, 0, &format, NULL);
	} else {
		ao_option *ao_options = NULL;
		if (libao_device_interface) {
			ao_append_option(&ao_options, "dev", libao_device_interface);
		}
		libao_device = ao_open_live(driver, &format, ao_options);
	}

	if (libao_device == NULL) {
		switch (errno) {
		case AO_ENODRIVER:
		case AO_ENOTFILE:
		case AO_ENOTLIVE:
		case AO_EOPENDEVICE:
			errno = ENODEV;
			return -OP_ERROR_ERRNO;
		case AO_EBADOPTION:
			errno = EINVAL;
			return -OP_ERROR_ERRNO;
		case AO_EOPENFILE:
			errno = EACCES;
			return -OP_ERROR_ERRNO;
		case AO_EFILEEXISTS:
			errno = EEXIST;
			return -OP_ERROR_ERRNO;
		case AO_EFAIL:
		default:
			return -OP_ERROR_INTERNAL;
		}
	}

	/* ensure that the buffer size is a multiple of the frame size */
	libao_cur_buffer_space = is_wav ? 128 * 1024 : libao_buffer_space;
	libao_cur_buffer_space -= libao_cur_buffer_space % sf_get_frame_size(sf);

#ifdef AO_API_1
	d_print("channel matrix: %s\n", format.matrix ? format.matrix : "default");
#endif
	return 0;
}

static int op_ao_close(void)
{
	ao_close(libao_device);
	if (is_wav)
		wav_counter++;
	return 0;
}

static int op_ao_write(const char *buffer, int count)
{
	if (ao_play(libao_device, (void *)buffer, count) == 0)
		return -1;
	return count;
}

static int op_ao_buffer_space(void)
{
	return libao_cur_buffer_space;
}

static int op_ao_set_buffer_size(const char *val)
{
	long int ival;
	if (str_to_int(val, &ival) || ival < 4096) {
		errno = EINVAL;
		return -OP_ERROR_ERRNO;
	}
	libao_buffer_space = ival;
	return 0;
}

static int op_ao_get_buffer_size(char **val)
{
	*val = xnew(char, 22);
	snprintf(*val, 22, "%d", libao_buffer_space);
	return 0;
}

static int op_ao_set_driver(const char *val)
{
	free(libao_driver);
	libao_driver = NULL;
	if (val[0])
		libao_driver = xstrdup(val);
	return 0;
}

static int op_ao_get_driver(char **val)
{
	if (libao_driver)
		*val = xstrdup(libao_driver);
	return 0;
}

static int op_ao_set_wav_counter(const char *val)
{
	long int ival;
	if (str_to_int(val, &ival)) {
		errno = EINVAL;
		return -OP_ERROR_ERRNO;
	}
	wav_counter = ival;
	return 0;
}

static int op_ao_get_wav_counter(char **val)
{
	*val = xnew(char, 22);
	snprintf(*val, 22, "%d", wav_counter);
	return 0;
}

static int op_ao_set_wav_dir(const char *val)
{
	free(wav_dir);
	wav_dir = xstrdup(val);
	return 0;
}

static int op_ao_get_wav_dir(char **val)
{
	if (wav_dir == NULL)
		wav_dir = xstrdup(home_dir);
	*val = expand_filename(wav_dir);
	return 0;
}

static int op_ao_set_device_interface(const char *val)
{
	free(libao_device_interface);
	libao_device_interface = NULL;
	if (val[0])
		libao_device_interface = xstrdup(val);
	return 0;
}

static int op_ao_get_device_interface(char **val)
{
	if (libao_device_interface)
		*val = xstrdup(libao_device_interface);
	return 0;
}

const struct output_plugin_ops op_pcm_ops = {
	.init = op_ao_init,
	.exit = op_ao_exit,
	.open = op_ao_open,
	.close = op_ao_close,
	.write = op_ao_write,
	.buffer_space = op_ao_buffer_space,
};

const struct output_plugin_opt op_pcm_options[] = {
	OPT(op_ao, buffer_size),
	OPT(op_ao, driver),
	OPT(op_ao, wav_counter),
	OPT(op_ao, wav_dir),
	OPT(op_ao, device_interface),
	{ NULL },
};

const int op_priority = 3;
const unsigned op_abi_version = OP_ABI_VERSION;