-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinux_a.cpp
More file actions
339 lines (283 loc) · 8.17 KB
/
linux_a.cpp
File metadata and controls
339 lines (283 loc) · 8.17 KB
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
/*
$Id$
TiMidity -- Experimental MIDI to WAVE converter
Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
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, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
linux_audio.c
Functions to play sound on the VoxWare audio driver (Linux or FreeBSD or BSD/OS)
*/
/* Usually Linux, FreeBSD, or BSDI. Probably NetBSD and OpenBSD too */
#ifdef AU_OSS
#define _GNU_SOURCE
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#if defined(__linux__) || defined(__NetBSD__)
/* new with 1.2.0? Didn't need this under 1.1.64 */
#include <sys/ioctl.h>
#endif
#if !defined(__NetBSD__)
#include <sys/soundcard.h>
#else
#include <soundcard.h>
#endif
#include "config.h"
#include "output.h"
#include "controls.h"
#include <stdio.h>
static int open_output(void); /* 0=success, 1=warning, -1=fatal error */
static void close_output(void);
static void output_data(int32 *buf, uint32 count);
static int driver_output_data(unsigned char *buf, uint32 count);
static void flush_output(void);
static void purge_output(void);
static int output_count(uint32 ct);
/* export the playback mode */
#undef dpm
#ifdef LINUX_SECOND_DEVICE
#define dpm linux_play_mode_two
#else
#define dpm linux_play_mode
#endif
PlayMode dpm = {
DEFAULT_RATE, PE_16BIT|PE_SIGNED,
-1,
{0}, /* default: get all the buffer fragments you can */
#ifdef LINUX_SECOND_DEVICE
"Linux 2nd dsp device", 'D',
"/dev/dsp1",
#elif defined(__NetBSD__)
"NetBSD audio device", 'd',
"/dev/sound",
#else
"Linux dsp device", 'd',
"/dev/dsp",
#endif
open_output,
close_output,
output_data,
driver_output_data,
flush_output,
purge_output,
output_count
};
/*************************************************************************/
/* We currently only honor the PE_MONO bit, the sample rate, and the
number of buffer fragments. We try 16-bit signed data first, and
then 8-bit unsigned if it fails. If you have a sound device that
can't handle either, let me know. */
static int open_output(void)
{
int fd, tmp, i, warnings=0;
/* Open the audio device */
/*
fd=open(dpm.name, O_RDWR | O_NDELAY);
if (fd<0) fd=open(dpm.name, O_RDWR | O_NDELAY);
*/
fd=open(dpm.name, O_WRONLY | O_NDELAY);
if (fd<0) fd=open(dpm.name, O_WRONLY | O_NDELAY);
if (fd<0)
{
ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: %s",
dpm.name, sys_errlist[errno]);
return -1;
}
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
/* They can't mean these */
dpm.encoding &= ~(PE_ULAW|PE_BYTESWAP);
/* Set sample width to whichever the user wants. If it fails, try
the other one. */
i=tmp=(dpm.encoding & PE_16BIT) ? LE_LONG(16) : LE_LONG(8);
if (ioctl(fd, SNDCTL_DSP_SAMPLESIZE, &tmp)<0 || tmp!=i)
{
/* Try the other one */
i=tmp=(dpm.encoding & PE_16BIT) ? LE_LONG(8) : LE_LONG(16);
if (ioctl(fd, SNDCTL_DSP_SAMPLESIZE, &tmp)<0 || tmp!=i)
{
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
"%s doesn't support 16- or 8-bit sample width",
dpm.name);
close(fd);
return -1;
}
ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
"Sample width adjusted to %d bits", tmp);
dpm.encoding ^= PE_16BIT;
warnings=1;
}
if (dpm.encoding & PE_16BIT)
dpm.encoding |= PE_SIGNED;
else
dpm.encoding &= ~PE_SIGNED;
/* Try stereo or mono, whichever the user wants. If it fails, try
the other. */
i=tmp=(dpm.encoding & PE_MONO) ? 0 : 1;
if ((ioctl(fd, SNDCTL_DSP_STEREO, &tmp)<0) || tmp!=i)
{
i=tmp=(dpm.encoding & PE_MONO) ? 1 : 0;
if ((ioctl(fd, SNDCTL_DSP_STEREO, &tmp)<0) || tmp!=i)
{
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
"%s doesn't support mono or stereo samples",
dpm.name);
close(fd);
return -1;
}
if (tmp==0) dpm.encoding |= PE_MONO;
else dpm.encoding &= ~PE_MONO;
ctl->cmsg(CMSG_WARNING, VERB_VERBOSE, "Sound adjusted to %sphonic",
(tmp==0) ? "mono" : "stereo");
warnings=1;
}
/* Set the sample rate */
tmp=dpm.rate;
if (ioctl(fd, SNDCTL_DSP_SPEED, &tmp)<0)
{
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
"%s doesn't support a %d Hz sample rate",
dpm.name, dpm.rate);
close(fd);
return -1;
}
if (tmp != (int)dpm.rate)
{
ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
"Output rate adjusted to %d Hz (requested %d Hz)", tmp, dpm.rate);
dpm.rate=tmp;
warnings=1;
}
/* Older VoxWare drivers don't have buffer fragment capabilities */
#ifdef SNDCTL_DSP_SETFRAGMENT
/* Set buffer fragments (in extra_param[0]) */
tmp=AUDIO_BUFFER_BITS;
if (!(dpm.encoding & PE_MONO)) tmp++;
if (dpm.encoding & PE_16BIT) tmp++;
tmp |= (dpm.extra_param[0]<<16);
i=tmp;
if (ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &tmp)<0)
{
ctl->cmsg(CMSG_WARNING, VERB_NORMAL,
"%s doesn't support %d-byte buffer fragments", dpm.name, (1<<i));
/* It should still work in some fashion. We should use a
secondary buffer anyway -- 64k isn't enough. */
warnings=1;
}
#else
if (dpm.extra_param[0])
{
ctl->cmsg(CMSG_WARNING, VERB_NORMAL,
"%s doesn't support buffer fragments", dpm.name);
warnings=1;
}
#endif
dpm.fd=fd;
return warnings;
}
#ifdef SNDCTL_DSP_GETOPTR
static int output_count(uint32 ct)
{
count_info auinfo;
int samples = (int)ct;
if (ioctl(dpm.fd, SNDCTL_DSP_GETOPTR, &auinfo)<0) return samples;
samples = auinfo.bytes;
if (!(dpm.encoding & PE_MONO)) samples >>= 1;
if (dpm.encoding & PE_16BIT) samples >>= 1;
return samples;
}
#else
static int output_count(uint32 ct)
{
int samples = (int)ct;
#ifdef SNDCTL_DSP_GETODELAY
int samples_queued, samples_sent;
samples = samples_sent = b_out_count();
if (samples_sent) {
if (ioctl(dpm.fd, SNDCTL_DSP_GETODELAY, &samples_queued) == -1)
samples_queued = 0;
samples -= samples_queued;
}
if (!(dpm.encoding & PE_MONO)) samples >>= 1;
if (dpm.encoding & PE_16BIT) samples >>= 1;
#endif
return samples;
}
#endif
static int driver_output_data(unsigned char *buf, uint32 count)
{
return write(dpm.fd,buf,count);
}
static void output_data(int32 *buf, uint32 count)
{
int ocount;
if (!(dpm.encoding & PE_MONO)) count*=2; /* Stereo samples */
ocount = (int)count;
if (ocount) {
if (dpm.encoding & PE_16BIT)
{
/* Convert data to signed 16-bit PCM */
s32tos16(buf, count);
ocount *= 2;
}
else
{
/* Convert to 8-bit unsigned and write out. */
s32tou8(buf, count);
}
}
b_out(dpm.id_character, dpm.fd, (int *)buf, ocount);
}
#if 0
static void output_data(int32 *buf, int32 count)
{
if (!(dpm.encoding & PE_MONO)) count*=2; /* Stereo samples */
if (dpm.encoding & PE_16BIT)
{
/* Convert data to signed 16-bit PCM */
s32tos16(buf, count);
/* Write the data out. Linux likes to give an EINTR if you suspend
a program while waiting on a write, so we may need to retry. */
while ((-1==write(dpm.fd, buf, count * 2)) && errno==EINTR)
;
}
else
{
/* Convert to 8-bit unsigned and write out. */
s32tou8(buf, count);
while ((-1==write(dpm.fd, buf, count)) && errno==EINTR)
;
}
}
#endif
static void close_output(void)
{
close(dpm.fd);
}
static void flush_output(void)
{
output_data(0, 0);
#ifdef __NetBSD__
ioctl(dpm.fd, SNDCTL_DSP_SYNC, NULL);
#else
ioctl(dpm.fd, SNDCTL_DSP_SYNC);
#endif
}
static void purge_output(void)
{
b_out(dpm.id_character, dpm.fd, 0, -1);
#ifdef __NetBSD__
ioctl(dpm.fd, SNDCTL_DSP_RESET, NULL);
#else
ioctl(dpm.fd, SNDCTL_DSP_RESET);
#endif
}
#endif /* defined(__linux__) || defined(__FreeBSD__) || defined(__bsdi__) */