-
Notifications
You must be signed in to change notification settings - Fork 49
/
soss.c
281 lines (227 loc) · 7.1 KB
/
soss.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
269
270
271
272
273
274
275
276
277
278
279
280
/*
* This file is part of the Advance project.
*
* Copyright (C) 2001, 2002, 2003, 2004 Andrea Mazzoleni
*
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* In addition, as a special exception, Andrea Mazzoleni
* gives permission to link the code of this program with
* the MAME library (or with modified versions of MAME that use the
* same license as MAME), and distribute linked combinations including
* the two. You must obey the GNU General Public License in all
* respects for all of the code used other than MAME. If you modify
* this file, you may extend this exception to your version of the
* file, but you are not obligated to do so. If you do not wish to
* do so, delete this exception statement from your version.
*/
#include "portable.h"
#include "soss.h"
#include "log.h"
#include "error.h"
#include <sys/soundcard.h>
#if HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
/**
* Base for the volume adjustment.
*/
#define OSS_VOLUME_BASE 32768
struct soundb_oss_context {
unsigned channel;
unsigned rate;
unsigned sample_length;
int handle;
int volume; /**< Volume adjustement. OSS_VOLUME_BASE == full volume. */
};
static struct soundb_oss_context oss_state;
static adv_device DEVICE[] = {
{ "auto", -1, "OSS automatic detection" },
{ 0, 0, 0 }
};
adv_error soundb_oss_init(int sound_id, unsigned* rate, adv_bool stereo_flag, double buffer_time)
{
int i;
audio_buf_info info;
log_std(("sound:oss: soundb_oss_init(id:%d, rate:%d, stereo:%d, buffer_time:%g)\n", sound_id, *rate, stereo_flag, buffer_time));
if (stereo_flag) {
oss_state.sample_length = 4;
oss_state.channel = 2;
} else {
oss_state.sample_length = 2;
oss_state.channel = 1;
}
oss_state.volume = OSS_VOLUME_BASE;
oss_state.handle = open("/dev/dsp", O_WRONLY | O_NONBLOCK, 0);
if (!oss_state.handle) {
log_std(("sound:oss: open(/dev/dsp, O_WRONLY | O_NONBLOCK, 0) failed\n"));
goto err;
}
i = AFMT_S16_LE;
if (ioctl(oss_state.handle, SNDCTL_DSP_SETFMT, &i) < 0) {
log_std(("sound:oss: ioctl(SNDCTL_DSP_SETFMT, AFMT_S16_LE) failed\n"));
goto err_close;
}
if (i != AFMT_S16_LE) {
log_std(("sound:oss: ioctl(SNDCTL_DSP_SETFMT, AFMT_S16_LE) return a different format\n"));
goto err_close;
}
if (stereo_flag)
i = 2;
else
i = 1;
if (ioctl(oss_state.handle, SNDCTL_DSP_CHANNELS, &i) < 0) {
log_std(("sound:oss: ioctl(SNDCTL_DSP_CHANNELS, %d) failed\n", i));
goto err_close;
}
i = *rate;
if (ioctl(oss_state.handle, SNDCTL_DSP_SPEED, &i) < 0) {
log_std(("sound:oss: ioctl(SNDCTL_DSP_SPEED, %d) failed\n", i));
goto err_close;
}
oss_state.rate = i;
i = 8;
if (stereo_flag)
++i;
i |= 0x7fff << 16; /* do not limit the number of framents, ignore the buffer_time arg */
if (ioctl(oss_state.handle, SNDCTL_DSP_SETFRAGMENT, &i) < 0) {
log_std(("sound:oss: ioctl(SNDCTL_DSP_SETFRAGMENT, %d:%d) failed\n", i >> 16, i & 0xFFFF));
goto err_close;
}
if (ioctl(oss_state.handle, SNDCTL_DSP_GETOSPACE, &info) < 0) {
log_std(("sound:oss: ioctl(SNDCTL_DSP_GETOSPACE) failed\n"));
goto err_close;
}
log_std(("sound:oss: sound getospace() = fragsize %d, fragtotal %d, freebytes %d\n", (int)info.fragsize, (int)info.fragstotal, (int)info.bytes));
*rate = oss_state.rate;
return 0;
err_close:
close(oss_state.handle);
err:
error_set("Error initializing the OSS interface.\n");
return -1;
}
void soundb_oss_done(void)
{
log_std(("sound:oss: soundb_oss_done()\n"));
close(oss_state.handle);
}
void soundb_oss_stop(void)
{
log_std(("sound:oss: soundb_oss_stop()\n"));
}
unsigned soundb_oss_buffered(void)
{
unsigned bytes_buffered;
audio_buf_info info;
if (ioctl(oss_state.handle, SNDCTL_DSP_GETOSPACE, &info) < 0) {
log_std(("ERROR:sound:oss: ioctl(SNDCTL_DSP_GETOSPACE) failed\n"));
return 0;
}
log_debug(("sound:oss: getospace() = fragsize %d, fragtotal %d, freebytes %d\n", (int)info.fragsize, (int)info.fragstotal, (int)info.bytes));
bytes_buffered = info.fragstotal * info.fragsize - info.bytes;
return bytes_buffered / oss_state.sample_length;
}
void soundb_oss_volume(double volume)
{
log_std(("sound:oss: soundb_oss_volume(volume:%g)\n", (double)volume));
oss_state.volume = volume * OSS_VOLUME_BASE;
if (oss_state.volume < 0)
oss_state.volume = 0;
if (oss_state.volume > OSS_VOLUME_BASE)
oss_state.volume = OSS_VOLUME_BASE;
}
void soundb_oss_play(const adv_sample* sample_map, unsigned sample_count)
{
int r;
log_debug(("sound:oss: soundb_oss_play(count:%d)\n", sample_count));
/* calling write with a 0 size result in wrong output */
while (sample_count) {
unsigned channel_length = oss_state.sample_length / oss_state.channel;
if (oss_state.volume == OSS_VOLUME_BASE) {
r = write(oss_state.handle, sample_map, sample_count * oss_state.sample_length);
} else {
/* adjust the volume and write */
const unsigned buf_size = 2048;
adv_sample buf_map[buf_size];
unsigned run;
unsigned i;
run = sample_count * oss_state.channel;
if (run > buf_size)
run = buf_size;
for (i = 0; i < run; ++i)
buf_map[i] = (int)sample_map[i] * oss_state.volume / OSS_VOLUME_BASE;
r = write(oss_state.handle, buf_map, run * channel_length);
}
if (r < 0) {
if (errno == EAGAIN) {
/* audio buffer full, it should never happen */
log_std(("WARNING:sound:oss: write() failed: internal buffer full\n"));
/* retry */
continue;
}
break;
} else {
sample_count -= r / oss_state.sample_length;
sample_map += r / channel_length;
}
}
}
adv_error soundb_oss_start(double silence_time)
{
adv_sample buf[256];
unsigned sample;
unsigned i;
log_std(("sound:oss: soundb_oss_start(silence_time:%g)\n", silence_time));
for (i = 0; i < 256; ++i)
buf[i] = 0x0;
sample = silence_time * oss_state.rate * oss_state.channel;
log_std(("sound:oss: writing %d bytes, %d sample of silence\n", sample / oss_state.channel * oss_state.sample_length, sample / oss_state.channel));
while (sample) {
unsigned run = sample;
if (run > 256)
run = 256;
sample -= run;
soundb_oss_play(buf, run / oss_state.channel);
}
return 0;
}
unsigned soundb_oss_flags(void)
{
return SOUND_DRIVER_FLAGS_VOLUME_SAMPLE;
}
adv_error soundb_oss_load(adv_conf* context)
{
return 0;
}
void soundb_oss_reg(adv_conf* context)
{
}
/***************************************************************************/
/* Driver */
soundb_driver soundb_oss_driver = {
"oss",
DEVICE,
soundb_oss_load,
soundb_oss_reg,
soundb_oss_init,
soundb_oss_done,
soundb_oss_flags,
soundb_oss_play,
soundb_oss_buffered,
soundb_oss_start,
soundb_oss_stop,
soundb_oss_volume
};