slouken@0
|
1 |
/*
|
slouken@5535
|
2 |
Simple DirectMedia Layer
|
slouken@5535
|
3 |
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
|
slouken@0
|
4 |
|
slouken@5535
|
5 |
This software is provided 'as-is', without any express or implied
|
slouken@5535
|
6 |
warranty. In no event will the authors be held liable for any damages
|
slouken@5535
|
7 |
arising from the use of this software.
|
slouken@0
|
8 |
|
slouken@5535
|
9 |
Permission is granted to anyone to use this software for any purpose,
|
slouken@5535
|
10 |
including commercial applications, and to alter it and redistribute it
|
slouken@5535
|
11 |
freely, subject to the following restrictions:
|
slouken@0
|
12 |
|
slouken@5535
|
13 |
1. The origin of this software must not be misrepresented; you must not
|
slouken@5535
|
14 |
claim that you wrote the original software. If you use this software
|
slouken@5535
|
15 |
in a product, an acknowledgment in the product documentation would be
|
slouken@5535
|
16 |
appreciated but is not required.
|
slouken@5535
|
17 |
2. Altered source versions must be plainly marked as such, and must not be
|
slouken@5535
|
18 |
misrepresented as being the original software.
|
slouken@5535
|
19 |
3. This notice may not be removed or altered from any source distribution.
|
slouken@0
|
20 |
*/
|
slouken@1402
|
21 |
#include "SDL_config.h"
|
slouken@0
|
22 |
|
slouken@0
|
23 |
/* Allow access to a raw mixing buffer */
|
slouken@0
|
24 |
|
slouken@1895
|
25 |
#include <stdio.h> /* For perror() */
|
slouken@1895
|
26 |
#include <string.h> /* For strerror() */
|
slouken@0
|
27 |
#include <errno.h>
|
slouken@0
|
28 |
#include <unistd.h>
|
slouken@0
|
29 |
#include <fcntl.h>
|
slouken@0
|
30 |
#include <signal.h>
|
slouken@0
|
31 |
#include <sys/time.h>
|
slouken@0
|
32 |
#include <sys/ioctl.h>
|
slouken@0
|
33 |
#include <sys/stat.h>
|
slouken@1361
|
34 |
|
slouken@1361
|
35 |
#if SDL_AUDIO_DRIVER_OSS_SOUNDCARD_H
|
slouken@94
|
36 |
/* This is installed on some systems */
|
slouken@94
|
37 |
#include <soundcard.h>
|
slouken@94
|
38 |
#else
|
slouken@94
|
39 |
/* This is recommended by OSS */
|
slouken@0
|
40 |
#include <sys/soundcard.h>
|
slouken@94
|
41 |
#endif
|
slouken@0
|
42 |
|
slouken@1358
|
43 |
#include "SDL_timer.h"
|
slouken@0
|
44 |
#include "SDL_audio.h"
|
slouken@1361
|
45 |
#include "../SDL_audiomem.h"
|
slouken@1361
|
46 |
#include "../SDL_audio_c.h"
|
slouken@1361
|
47 |
#include "../SDL_audiodev_c.h"
|
slouken@0
|
48 |
#include "SDL_dspaudio.h"
|
slouken@0
|
49 |
|
slouken@0
|
50 |
/* The tag name used by DSP audio */
|
slouken@0
|
51 |
#define DSP_DRIVER_NAME "dsp"
|
slouken@0
|
52 |
|
icculus@5593
|
53 |
static void
|
icculus@5593
|
54 |
DSP_Deinitialize(void)
|
icculus@2049
|
55 |
{
|
icculus@2049
|
56 |
}
|
icculus@2049
|
57 |
|
icculus@2049
|
58 |
|
icculus@2049
|
59 |
static void
|
icculus@2049
|
60 |
DSP_DetectDevices(int iscapture)
|
slouken@0
|
61 |
{
|
icculus@5593
|
62 |
SDL_EnumUnixAudioDevices(iscapture, 0, NULL);
|
slouken@0
|
63 |
}
|
slouken@0
|
64 |
|
icculus@2049
|
65 |
|
slouken@1895
|
66 |
static void
|
icculus@2049
|
67 |
DSP_CloseDevice(_THIS)
|
slouken@0
|
68 |
{
|
icculus@2049
|
69 |
if (this->hidden != NULL) {
|
icculus@2049
|
70 |
if (this->hidden->mixbuf != NULL) {
|
icculus@2049
|
71 |
SDL_FreeAudioMem(this->hidden->mixbuf);
|
icculus@2049
|
72 |
this->hidden->mixbuf = NULL;
|
icculus@2049
|
73 |
}
|
icculus@2049
|
74 |
if (this->hidden->audio_fd >= 0) {
|
icculus@2049
|
75 |
close(this->hidden->audio_fd);
|
icculus@2049
|
76 |
this->hidden->audio_fd = -1;
|
icculus@2049
|
77 |
}
|
icculus@2049
|
78 |
SDL_free(this->hidden);
|
icculus@2049
|
79 |
this->hidden = NULL;
|
slouken@1895
|
80 |
}
|
slouken@0
|
81 |
}
|
slouken@0
|
82 |
|
icculus@2049
|
83 |
|
slouken@1895
|
84 |
static int
|
icculus@2049
|
85 |
DSP_OpenDevice(_THIS, const char *devname, int iscapture)
|
slouken@0
|
86 |
{
|
icculus@2049
|
87 |
const int flags = ((iscapture) ? OPEN_FLAGS_INPUT : OPEN_FLAGS_OUTPUT);
|
slouken@1895
|
88 |
int format;
|
slouken@1895
|
89 |
int value;
|
slouken@1895
|
90 |
int frag_spec;
|
icculus@1982
|
91 |
SDL_AudioFormat test_format;
|
slouken@0
|
92 |
|
icculus@2049
|
93 |
/* We don't care what the devname is...we'll try to open anything. */
|
icculus@2049
|
94 |
/* ...but default to first name in the list... */
|
icculus@2049
|
95 |
if (devname == NULL) {
|
icculus@5593
|
96 |
devname = SDL_GetAudioDeviceName(0, iscapture);
|
icculus@5593
|
97 |
if (devname == NULL) {
|
icculus@2049
|
98 |
SDL_SetError("No such audio device");
|
icculus@2049
|
99 |
return 0;
|
icculus@2049
|
100 |
}
|
icculus@2049
|
101 |
}
|
icculus@2049
|
102 |
|
icculus@2071
|
103 |
/* Make sure fragment size stays a power of 2, or OSS fails. */
|
icculus@2071
|
104 |
/* I don't know which of these are actually legal values, though... */
|
icculus@2071
|
105 |
if (this->spec.channels > 8)
|
icculus@2071
|
106 |
this->spec.channels = 8;
|
icculus@2071
|
107 |
else if (this->spec.channels > 4)
|
icculus@2071
|
108 |
this->spec.channels = 4;
|
icculus@2071
|
109 |
else if (this->spec.channels > 2)
|
icculus@2071
|
110 |
this->spec.channels = 2;
|
icculus@2071
|
111 |
|
icculus@2049
|
112 |
/* Initialize all variables that we clean on shutdown */
|
icculus@2049
|
113 |
this->hidden = (struct SDL_PrivateAudioData *)
|
slouken@2060
|
114 |
SDL_malloc((sizeof *this->hidden));
|
icculus@2049
|
115 |
if (this->hidden == NULL) {
|
icculus@2049
|
116 |
SDL_OutOfMemory();
|
icculus@2049
|
117 |
return 0;
|
icculus@2049
|
118 |
}
|
icculus@2049
|
119 |
SDL_memset(this->hidden, 0, (sizeof *this->hidden));
|
icculus@2049
|
120 |
|
slouken@1895
|
121 |
/* Open the audio device */
|
icculus@2049
|
122 |
this->hidden->audio_fd = open(devname, flags, 0);
|
icculus@2049
|
123 |
if (this->hidden->audio_fd < 0) {
|
icculus@2049
|
124 |
DSP_CloseDevice(this);
|
icculus@2049
|
125 |
SDL_SetError("Couldn't open %s: %s", devname, strerror(errno));
|
icculus@2049
|
126 |
return 0;
|
slouken@1895
|
127 |
}
|
icculus@2049
|
128 |
this->hidden->mixbuf = NULL;
|
slouken@968
|
129 |
|
slouken@1895
|
130 |
/* Make the file descriptor use blocking writes with fcntl() */
|
slouken@1895
|
131 |
{
|
icculus@2049
|
132 |
long ctlflags;
|
icculus@2049
|
133 |
ctlflags = fcntl(this->hidden->audio_fd, F_GETFL);
|
icculus@2049
|
134 |
ctlflags &= ~O_NONBLOCK;
|
icculus@2049
|
135 |
if (fcntl(this->hidden->audio_fd, F_SETFL, ctlflags) < 0) {
|
icculus@2049
|
136 |
DSP_CloseDevice(this);
|
slouken@1895
|
137 |
SDL_SetError("Couldn't set audio blocking mode");
|
icculus@2049
|
138 |
return 0;
|
slouken@1895
|
139 |
}
|
slouken@1895
|
140 |
}
|
slouken@968
|
141 |
|
slouken@1895
|
142 |
/* Get a list of supported hardware formats */
|
icculus@2049
|
143 |
if (ioctl(this->hidden->audio_fd, SNDCTL_DSP_GETFMTS, &value) < 0) {
|
slouken@1895
|
144 |
perror("SNDCTL_DSP_GETFMTS");
|
icculus@2049
|
145 |
DSP_CloseDevice(this);
|
slouken@1895
|
146 |
SDL_SetError("Couldn't get audio format list");
|
icculus@2049
|
147 |
return 0;
|
slouken@1895
|
148 |
}
|
slouken@968
|
149 |
|
slouken@1895
|
150 |
/* Try for a closest match on audio format */
|
slouken@1895
|
151 |
format = 0;
|
icculus@2049
|
152 |
for (test_format = SDL_FirstAudioFormat(this->spec.format);
|
slouken@1895
|
153 |
!format && test_format;) {
|
slouken@968
|
154 |
#ifdef DEBUG_AUDIO
|
slouken@1895
|
155 |
fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
|
slouken@968
|
156 |
#endif
|
slouken@1895
|
157 |
switch (test_format) {
|
slouken@1895
|
158 |
case AUDIO_U8:
|
slouken@1895
|
159 |
if (value & AFMT_U8) {
|
slouken@1895
|
160 |
format = AFMT_U8;
|
slouken@1895
|
161 |
}
|
slouken@1895
|
162 |
break;
|
slouken@1895
|
163 |
case AUDIO_S16LSB:
|
slouken@1895
|
164 |
if (value & AFMT_S16_LE) {
|
slouken@1895
|
165 |
format = AFMT_S16_LE;
|
slouken@1895
|
166 |
}
|
slouken@1895
|
167 |
break;
|
slouken@1895
|
168 |
case AUDIO_S16MSB:
|
slouken@1895
|
169 |
if (value & AFMT_S16_BE) {
|
slouken@1895
|
170 |
format = AFMT_S16_BE;
|
slouken@1895
|
171 |
}
|
slouken@1895
|
172 |
break;
|
slouken@968
|
173 |
#if 0
|
slouken@968
|
174 |
/*
|
slouken@968
|
175 |
* These formats are not used by any real life systems so they are not
|
slouken@968
|
176 |
* needed here.
|
slouken@968
|
177 |
*/
|
slouken@1895
|
178 |
case AUDIO_S8:
|
slouken@1895
|
179 |
if (value & AFMT_S8) {
|
slouken@1895
|
180 |
format = AFMT_S8;
|
slouken@1895
|
181 |
}
|
slouken@1895
|
182 |
break;
|
slouken@1895
|
183 |
case AUDIO_U16LSB:
|
slouken@1895
|
184 |
if (value & AFMT_U16_LE) {
|
slouken@1895
|
185 |
format = AFMT_U16_LE;
|
slouken@1895
|
186 |
}
|
slouken@1895
|
187 |
break;
|
slouken@1895
|
188 |
case AUDIO_U16MSB:
|
slouken@1895
|
189 |
if (value & AFMT_U16_BE) {
|
slouken@1895
|
190 |
format = AFMT_U16_BE;
|
slouken@1895
|
191 |
}
|
slouken@1895
|
192 |
break;
|
slouken@968
|
193 |
#endif
|
slouken@1895
|
194 |
default:
|
slouken@1895
|
195 |
format = 0;
|
slouken@1895
|
196 |
break;
|
slouken@1895
|
197 |
}
|
slouken@1895
|
198 |
if (!format) {
|
slouken@1895
|
199 |
test_format = SDL_NextAudioFormat();
|
slouken@1895
|
200 |
}
|
slouken@1895
|
201 |
}
|
slouken@1895
|
202 |
if (format == 0) {
|
icculus@2049
|
203 |
DSP_CloseDevice(this);
|
slouken@1895
|
204 |
SDL_SetError("Couldn't find any hardware audio formats");
|
icculus@2049
|
205 |
return 0;
|
slouken@1895
|
206 |
}
|
icculus@2049
|
207 |
this->spec.format = test_format;
|
slouken@968
|
208 |
|
slouken@1895
|
209 |
/* Set the audio format */
|
slouken@1895
|
210 |
value = format;
|
slouken@2060
|
211 |
if ((ioctl(this->hidden->audio_fd, SNDCTL_DSP_SETFMT, &value) < 0) ||
|
slouken@2060
|
212 |
(value != format)) {
|
slouken@1895
|
213 |
perror("SNDCTL_DSP_SETFMT");
|
icculus@2049
|
214 |
DSP_CloseDevice(this);
|
slouken@1895
|
215 |
SDL_SetError("Couldn't set audio format");
|
icculus@2049
|
216 |
return 0;
|
slouken@1895
|
217 |
}
|
slouken@968
|
218 |
|
slouken@1895
|
219 |
/* Set the number of channels of output */
|
icculus@2049
|
220 |
value = this->spec.channels;
|
icculus@2049
|
221 |
if (ioctl(this->hidden->audio_fd, SNDCTL_DSP_CHANNELS, &value) < 0) {
|
slouken@1895
|
222 |
perror("SNDCTL_DSP_CHANNELS");
|
icculus@2049
|
223 |
DSP_CloseDevice(this);
|
slouken@1895
|
224 |
SDL_SetError("Cannot set the number of channels");
|
icculus@2049
|
225 |
return 0;
|
slouken@1895
|
226 |
}
|
icculus@2049
|
227 |
this->spec.channels = value;
|
slouken@968
|
228 |
|
slouken@1895
|
229 |
/* Set the DSP frequency */
|
icculus@2049
|
230 |
value = this->spec.freq;
|
icculus@2049
|
231 |
if (ioctl(this->hidden->audio_fd, SNDCTL_DSP_SPEED, &value) < 0) {
|
slouken@1895
|
232 |
perror("SNDCTL_DSP_SPEED");
|
icculus@2049
|
233 |
DSP_CloseDevice(this);
|
slouken@1895
|
234 |
SDL_SetError("Couldn't set audio frequency");
|
icculus@2049
|
235 |
return 0;
|
slouken@1895
|
236 |
}
|
icculus@2049
|
237 |
this->spec.freq = value;
|
slouken@0
|
238 |
|
slouken@1895
|
239 |
/* Calculate the final parameters for this audio specification */
|
icculus@2049
|
240 |
SDL_CalculateAudioSpec(&this->spec);
|
slouken@0
|
241 |
|
slouken@1895
|
242 |
/* Determine the power of two of the fragment size */
|
icculus@2049
|
243 |
for (frag_spec = 0; (0x01U << frag_spec) < this->spec.size; ++frag_spec);
|
icculus@2049
|
244 |
if ((0x01U << frag_spec) != this->spec.size) {
|
icculus@2049
|
245 |
DSP_CloseDevice(this);
|
slouken@1895
|
246 |
SDL_SetError("Fragment size must be a power of two");
|
icculus@2049
|
247 |
return 0;
|
slouken@1895
|
248 |
}
|
slouken@1895
|
249 |
frag_spec |= 0x00020000; /* two fragments, for low latency */
|
slouken@0
|
250 |
|
slouken@1895
|
251 |
/* Set the audio buffering parameters */
|
slouken@0
|
252 |
#ifdef DEBUG_AUDIO
|
slouken@1895
|
253 |
fprintf(stderr, "Requesting %d fragments of size %d\n",
|
slouken@1895
|
254 |
(frag_spec >> 16), 1 << (frag_spec & 0xFFFF));
|
slouken@0
|
255 |
#endif
|
icculus@2049
|
256 |
if (ioctl(this->hidden->audio_fd, SNDCTL_DSP_SETFRAGMENT, &frag_spec) < 0) {
|
slouken@1895
|
257 |
perror("SNDCTL_DSP_SETFRAGMENT");
|
slouken@1895
|
258 |
}
|
slouken@0
|
259 |
#ifdef DEBUG_AUDIO
|
slouken@1895
|
260 |
{
|
slouken@1895
|
261 |
audio_buf_info info;
|
icculus@2049
|
262 |
ioctl(this->hidden->audio_fd, SNDCTL_DSP_GETOSPACE, &info);
|
slouken@1895
|
263 |
fprintf(stderr, "fragments = %d\n", info.fragments);
|
slouken@1895
|
264 |
fprintf(stderr, "fragstotal = %d\n", info.fragstotal);
|
slouken@1895
|
265 |
fprintf(stderr, "fragsize = %d\n", info.fragsize);
|
slouken@1895
|
266 |
fprintf(stderr, "bytes = %d\n", info.bytes);
|
slouken@1895
|
267 |
}
|
slouken@0
|
268 |
#endif
|
slouken@0
|
269 |
|
slouken@1895
|
270 |
/* Allocate mixing buffer */
|
icculus@2049
|
271 |
this->hidden->mixlen = this->spec.size;
|
icculus@2049
|
272 |
this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
|
icculus@2049
|
273 |
if (this->hidden->mixbuf == NULL) {
|
icculus@2049
|
274 |
DSP_CloseDevice(this);
|
icculus@2049
|
275 |
SDL_OutOfMemory();
|
icculus@2049
|
276 |
return 0;
|
slouken@1895
|
277 |
}
|
icculus@2049
|
278 |
SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);
|
slouken@0
|
279 |
|
slouken@1895
|
280 |
/* We're ready to rock and roll. :-) */
|
icculus@2049
|
281 |
return 1;
|
icculus@2049
|
282 |
}
|
icculus@2049
|
283 |
|
icculus@2049
|
284 |
|
icculus@2049
|
285 |
static void
|
icculus@2049
|
286 |
DSP_PlayDevice(_THIS)
|
icculus@2049
|
287 |
{
|
icculus@2049
|
288 |
const Uint8 *mixbuf = this->hidden->mixbuf;
|
icculus@2049
|
289 |
const int mixlen = this->hidden->mixlen;
|
icculus@2049
|
290 |
if (write(this->hidden->audio_fd, mixbuf, mixlen) == -1) {
|
icculus@2049
|
291 |
perror("Audio write");
|
icculus@2049
|
292 |
this->enabled = 0;
|
icculus@2049
|
293 |
}
|
icculus@2049
|
294 |
#ifdef DEBUG_AUDIO
|
icculus@2049
|
295 |
fprintf(stderr, "Wrote %d bytes of audio data\n", mixlen);
|
icculus@2049
|
296 |
#endif
|
slouken@0
|
297 |
}
|
slouken@1895
|
298 |
|
icculus@2049
|
299 |
static Uint8 *
|
icculus@2049
|
300 |
DSP_GetDeviceBuf(_THIS)
|
icculus@2049
|
301 |
{
|
icculus@2049
|
302 |
return (this->hidden->mixbuf);
|
icculus@2049
|
303 |
}
|
icculus@2049
|
304 |
|
icculus@2049
|
305 |
static int
|
slouken@2060
|
306 |
DSP_Init(SDL_AudioDriverImpl * impl)
|
icculus@2049
|
307 |
{
|
icculus@2049
|
308 |
/* Set the function pointers */
|
icculus@2049
|
309 |
impl->DetectDevices = DSP_DetectDevices;
|
icculus@2049
|
310 |
impl->OpenDevice = DSP_OpenDevice;
|
icculus@2049
|
311 |
impl->PlayDevice = DSP_PlayDevice;
|
icculus@2049
|
312 |
impl->GetDeviceBuf = DSP_GetDeviceBuf;
|
icculus@2049
|
313 |
impl->CloseDevice = DSP_CloseDevice;
|
icculus@2049
|
314 |
impl->Deinitialize = DSP_Deinitialize;
|
icculus@2049
|
315 |
|
icculus@3699
|
316 |
return 1; /* this audio target is available. */
|
icculus@2049
|
317 |
}
|
icculus@2049
|
318 |
|
icculus@2049
|
319 |
|
icculus@2049
|
320 |
AudioBootStrap DSP_bootstrap = {
|
icculus@2049
|
321 |
DSP_DRIVER_NAME, "OSS /dev/dsp standard audio", DSP_Init, 0
|
icculus@2049
|
322 |
};
|
icculus@2049
|
323 |
|
slouken@1895
|
324 |
/* vi: set ts=4 sw=4 expandtab: */
|