-
Notifications
You must be signed in to change notification settings - Fork 0
/
sndio.c
268 lines (215 loc) · 5 KB
/
sndio.c
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
/*
* Copyright (C) 2011 Donovan "Tsomi" Watteau <tsoomi@gmail.com>
*
* Based on Thomas Pfaff's work for XMMS, and some suggestions from
* Alexandre Ratchov.
*
* 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 <sys/types.h>
#include <sys/ioctl.h>
#include <sys/audioio.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sndio.h>
#include "op.h"
#include "mixer.h"
#include "sf.h"
#include "xmalloc.h"
static sample_format_t sndio_sf;
static struct sio_par par;
static struct sio_hdl *hdl;
static int sndio_volume = SIO_MAXVOL;
static int sndio_paused;
static int sndio_mixer_set_volume(int l, int r)
{
sndio_volume = l > r ? l : r;
if (hdl == NULL)
return -OP_ERROR_NOT_INITIALIZED;
if (!sio_setvol(hdl, sndio_volume))
return -OP_ERROR_INTERNAL;
return OP_ERROR_SUCCESS;
}
static int sndio_mixer_get_volume(int *l, int *r)
{
*l = *r = sndio_volume;
return OP_ERROR_SUCCESS;
}
static int sndio_set_sf(sample_format_t sf)
{
struct sio_par apar;
sndio_sf = sf;
sio_initpar(&par);
par.pchan = sf_get_channels(sndio_sf);
par.rate = sf_get_rate(sndio_sf);
sndio_paused = 0;
if (sf_get_signed(sndio_sf))
par.sig = 1;
else
par.sig = 0;
if (sf_get_bigendian(sndio_sf))
par.le = 0;
else
par.le = 1;
switch (sf_get_bits(sndio_sf)) {
case 16:
par.bits = 16;
break;
case 8:
par.bits = 8;
break;
default:
return -OP_ERROR_SAMPLE_FORMAT;
}
par.appbufsz = par.rate * 300 / 1000;
apar = par;
if (!sio_setpar(hdl, &par))
return -OP_ERROR_INTERNAL;
if (!sio_getpar(hdl, &par))
return -OP_ERROR_INTERNAL;
if (apar.rate != par.rate || apar.pchan != par.pchan ||
apar.bits != par.bits || (par.bits > 8 && apar.le != par.le) ||
apar.sig != par.sig)
return -OP_ERROR_INTERNAL;
sndio_mixer_set_volume(sndio_volume, sndio_volume);
if (!sio_start(hdl))
return -OP_ERROR_INTERNAL;
return OP_ERROR_SUCCESS;
}
static int sndio_init(void)
{
return OP_ERROR_SUCCESS;
}
static int sndio_exit(void)
{
return OP_ERROR_SUCCESS;
}
static int sndio_close(void)
{
if (hdl != NULL) {
sio_close(hdl);
hdl = NULL;
}
return OP_ERROR_SUCCESS;
}
static int sndio_open(sample_format_t sf, const channel_position_t *channel_map)
{
int ret = 0;
hdl = sio_open(NULL, SIO_PLAY, 0);
if (hdl == NULL)
return -OP_ERROR_INTERNAL;
if ((ret = sndio_set_sf(sf)) < 0) {
sndio_close();
return ret;
}
return OP_ERROR_SUCCESS;
}
static int sndio_write(const char *buf, int cnt)
{
size_t rc;
rc = sio_write(hdl, buf, cnt);
if (rc == 0)
return -OP_ERROR_INTERNAL;
return rc;
}
static int op_sndio_set_option(int key, const char *val)
{
return -OP_ERROR_NOT_OPTION;
}
static int op_sndio_get_option(int key, char **val)
{
return -OP_ERROR_NOT_OPTION;
}
static int sndio_pause(void)
{
if (!sndio_paused) {
if (!sio_stop(hdl))
return -OP_ERROR_INTERNAL;
sndio_paused = 1;
}
return OP_ERROR_SUCCESS;
}
static int sndio_unpause(void)
{
if (sndio_paused) {
if (!sio_start(hdl))
return -OP_ERROR_INTERNAL;
sndio_paused = 0;
}
return OP_ERROR_SUCCESS;
}
static int sndio_buffer_space(void)
{
/*
* Do as if there's always some space and let sio_write() block.
*/
return par.bufsz * par.bps * par.pchan;
}
static int sndio_mixer_init(void)
{
return OP_ERROR_SUCCESS;
}
static int sndio_mixer_exit(void)
{
return OP_ERROR_SUCCESS;
}
static int sndio_mixer_open(int *volume_max)
{
*volume_max = SIO_MAXVOL;
return OP_ERROR_SUCCESS;
}
static int sndio_mixer_close(void)
{
return OP_ERROR_SUCCESS;
}
static int sndio_mixer_set_option(int key, const char *val)
{
return -OP_ERROR_NOT_OPTION;
}
static int sndio_mixer_get_option(int key, char **val)
{
return -OP_ERROR_NOT_OPTION;
}
const struct output_plugin_ops op_pcm_ops = {
.init = sndio_init,
.exit = sndio_exit,
.open = sndio_open,
.close = sndio_close,
.write = sndio_write,
.pause = sndio_pause,
.unpause = sndio_unpause,
.buffer_space = sndio_buffer_space,
.set_option = op_sndio_set_option,
.get_option = op_sndio_get_option
};
const struct mixer_plugin_ops op_mixer_ops = {
.init = sndio_mixer_init,
.exit = sndio_mixer_exit,
.open = sndio_mixer_open,
.close = sndio_mixer_close,
.set_volume = sndio_mixer_set_volume,
.get_volume = sndio_mixer_get_volume,
.set_option = sndio_mixer_set_option,
.get_option = sndio_mixer_get_option
};
const char * const op_pcm_options[] = {
NULL
};
const char * const op_mixer_options[] = {
NULL
};
const int op_priority = 2;