Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
455 lines (395 loc) · 13 KB

SDL_bsdaudio.c

File metadata and controls

455 lines (395 loc) · 13 KB
 
Apr 8, 2011
Apr 8, 2011
2
3
Simple DirectMedia Layer
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
Apr 8, 2011
Apr 8, 2011
5
6
7
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Apr 8, 2011
Apr 8, 2011
9
10
11
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
Apr 8, 2011
Apr 8, 2011
13
14
15
16
17
18
19
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Feb 21, 2006
Feb 21, 2006
21
#include "SDL_config.h"
Mar 21, 2006
Mar 21, 2006
24
* Driver for native OpenBSD/NetBSD audio(4).
25
26
27
28
29
30
31
32
33
34
35
36
* vedge@vedge.com.ar.
*/
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/audioio.h>
Feb 10, 2006
Feb 10, 2006
37
#include "SDL_timer.h"
38
#include "SDL_audio.h"
Feb 16, 2006
Feb 16, 2006
39
40
41
#include "../SDL_audiomem.h"
#include "../SDL_audio_c.h"
#include "../SDL_audiodev_c.h"
Mar 22, 2006
Mar 22, 2006
42
#include "SDL_bsdaudio.h"
Mar 21, 2006
Mar 21, 2006
44
/* The tag name used by NetBSD/OpenBSD audio */
Mar 21, 2006
Mar 21, 2006
45
46
47
#ifdef __NetBSD__
#define BSD_AUDIO_DRIVER_NAME "netbsd"
#define BSD_AUDIO_DRIVER_DESC "Native NetBSD audio"
Mar 21, 2006
Mar 21, 2006
48
#else
Mar 21, 2006
Mar 21, 2006
49
50
#define BSD_AUDIO_DRIVER_NAME "openbsd"
#define BSD_AUDIO_DRIVER_DESC "Native OpenBSD audio"
Mar 21, 2006
Mar 21, 2006
51
#endif
52
53
54
55
56
57
58
59
60
61
62
/* Open the audio device for playback, and don't block if busy */
/* #define USE_BLOCKING_WRITES */
/* Use timer for synchronization */
/* #define USE_TIMER_SYNC */
/* #define DEBUG_AUDIO */
/* #define DEBUG_AUDIO_STREAM */
#ifdef USE_BLOCKING_WRITES
Oct 17, 2006
Oct 17, 2006
63
64
#define OPEN_FLAGS_OUTPUT O_WRONLY
#define OPEN_FLAGS_INPUT O_RDONLY
Oct 17, 2006
Oct 17, 2006
66
67
#define OPEN_FLAGS_OUTPUT (O_WRONLY|O_NONBLOCK)
#define OPEN_FLAGS_INPUT (O_RDONLY|O_NONBLOCK)
68
69
#endif
Oct 17, 2006
Oct 17, 2006
70
71
72
73
74
/* !!! FIXME: so much cut and paste with dsp/dma drivers... */
static char **outputDevices = NULL;
static int outputDeviceCount = 0;
static char **inputDevices = NULL;
static int inputDeviceCount = 0;
Oct 17, 2006
Oct 17, 2006
76
77
78
79
80
static inline void
free_device_list(char ***devs, int *count)
{
SDL_FreeUnixAudioDevices(devs, count);
}
Oct 17, 2006
Oct 17, 2006
82
83
84
85
86
87
88
static inline void
build_device_list(int iscapture, char ***devs, int *count)
{
const int flags = ((iscapture) ? OPEN_FLAGS_INPUT : OPEN_FLAGS_OUTPUT);
free_device_list(devs, count);
SDL_EnumUnixAudioDevices(flags, 0, NULL, devs, count);
}
Oct 17, 2006
Oct 17, 2006
90
91
static inline void
build_device_lists(void)
Oct 17, 2006
Oct 17, 2006
93
94
build_device_list(0, &outputDevices, &outputDeviceCount);
build_device_list(1, &inputDevices, &inputDeviceCount);
Oct 17, 2006
Oct 17, 2006
97
98
99
static inline void
free_device_lists(void)
Oct 17, 2006
Oct 17, 2006
101
102
free_device_list(&outputDevices, &outputDeviceCount);
free_device_list(&inputDevices, &inputDeviceCount);
Oct 17, 2006
Oct 17, 2006
105
106
107
static void
BSDAUDIO_Deinitialize(void)
Oct 17, 2006
Oct 17, 2006
109
110
free_device_lists();
}
Oct 17, 2006
Oct 17, 2006
112
113
114
115
116
117
118
119
120
121
static int
BSDAUDIO_DetectDevices(int iscapture)
{
if (iscapture) {
build_device_list(1, &inputDevices, &inputDeviceCount);
return inputDeviceCount;
} else {
build_device_list(0, &outputDevices, &outputDeviceCount);
return outputDeviceCount;
Oct 17, 2006
Oct 17, 2006
123
Oct 28, 2006
Oct 28, 2006
124
return 0; /* shouldn't ever hit this. */
Oct 17, 2006
Oct 17, 2006
125
126
127
128
129
130
131
132
133
}
static const char *
BSDAUDIO_GetDeviceName(int index, int iscapture)
{
if ((iscapture) && (index < inputDeviceCount)) {
return inputDevices[index];
} else if ((!iscapture) && (index < outputDeviceCount)) {
return outputDevices[index];
134
135
}
Oct 17, 2006
Oct 17, 2006
136
137
138
139
140
141
142
143
144
SDL_SetError("No such device");
return NULL;
}
static void
BSDAUDIO_Status(_THIS)
{
#ifdef DEBUG_AUDIO
Oct 29, 2006
Oct 29, 2006
145
/* *INDENT-OFF* */
Oct 17, 2006
Oct 17, 2006
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
audio_info_t info;
if (ioctl(this->hidden->audio_fd, AUDIO_GETINFO, &info) < 0) {
fprintf(stderr, "AUDIO_GETINFO failed.\n");
return;
}
fprintf(stderr, "\n"
"[play/record info]\n"
"buffer size : %d bytes\n"
"sample rate : %i Hz\n"
"channels : %i\n"
"precision : %i-bit\n"
"encoding : 0x%x\n"
"seek : %i\n"
"sample count : %i\n"
"EOF count : %i\n"
"paused : %s\n"
"error occured : %s\n"
"waiting : %s\n"
"active : %s\n"
"",
info.play.buffer_size,
info.play.sample_rate,
info.play.channels,
info.play.precision,
info.play.encoding,
info.play.seek,
info.play.samples,
info.play.eof,
info.play.pause ? "yes" : "no",
info.play.error ? "yes" : "no",
info.play.waiting ? "yes" : "no",
info.play.active ? "yes" : "no");
Jul 10, 2006
Jul 10, 2006
179
Oct 17, 2006
Oct 17, 2006
180
181
182
183
184
185
186
187
188
189
190
191
192
193
fprintf(stderr, "\n"
"[audio info]\n"
"monitor_gain : %i\n"
"hw block size : %d bytes\n"
"hi watermark : %i\n"
"lo watermark : %i\n"
"audio mode : %s\n"
"",
info.monitor_gain,
info.blocksize,
info.hiwat, info.lowat,
(info.mode == AUMODE_PLAY) ? "PLAY"
: (info.mode = AUMODE_RECORD) ? "RECORD"
: (info.mode == AUMODE_PLAY_ALL ? "PLAY_ALL" : "?"));
Oct 28, 2006
Oct 28, 2006
194
/* *INDENT-ON* */
Oct 17, 2006
Oct 17, 2006
195
#endif /* DEBUG_AUDIO */
196
197
198
199
200
}
/* This function waits until it is possible to write a full sound buffer */
static void
Oct 17, 2006
Oct 17, 2006
201
BSDAUDIO_WaitDevice(_THIS)
Jul 10, 2006
Jul 10, 2006
203
204
#ifndef USE_BLOCKING_WRITES /* Not necessary when using blocking writes */
/* See if we need to use timed audio synchronization */
Oct 17, 2006
Oct 17, 2006
205
if (this->hidden->frame_ticks) {
Jul 10, 2006
Jul 10, 2006
206
207
208
/* Use timer for general audio synchronization */
Sint32 ticks;
Oct 28, 2006
Oct 28, 2006
209
210
211
ticks =
((Sint32) (this->hidden->next_frame - SDL_GetTicks())) -
FUDGE_TICKS;
Jul 10, 2006
Jul 10, 2006
212
213
214
215
216
217
218
219
220
if (ticks > 0) {
SDL_Delay(ticks);
}
} else {
/* Use select() for audio synchronization */
fd_set fdset;
struct timeval timeout;
FD_ZERO(&fdset);
Oct 17, 2006
Oct 17, 2006
221
FD_SET(this->hidden->audio_fd, &fdset);
Jul 10, 2006
Jul 10, 2006
222
223
timeout.tv_sec = 10;
timeout.tv_usec = 0;
Jul 8, 2001
Jul 8, 2001
224
#ifdef DEBUG_AUDIO
Jul 10, 2006
Jul 10, 2006
225
fprintf(stderr, "Waiting for audio to get ready\n");
Jul 8, 2001
Jul 8, 2001
226
#endif
Oct 28, 2006
Oct 28, 2006
227
228
if (select(this->hidden->audio_fd + 1, NULL, &fdset, NULL, &timeout)
<= 0) {
Jul 10, 2006
Jul 10, 2006
229
230
231
232
233
234
235
236
237
const char *message =
"Audio timeout - buggy audio driver? (disabled)";
/* In general we should never print to the screen,
but in this case we have no other way of letting
the user know what happened.
*/
fprintf(stderr, "SDL: %s\n", message);
this->enabled = 0;
/* Don't try to close - may hang */
Oct 17, 2006
Oct 17, 2006
238
this->hidden->audio_fd = -1;
Jul 8, 2001
Jul 8, 2001
239
#ifdef DEBUG_AUDIO
Jul 10, 2006
Jul 10, 2006
240
fprintf(stderr, "Done disabling audio\n");
Jul 8, 2001
Jul 8, 2001
241
#endif
Jul 10, 2006
Jul 10, 2006
242
}
Jul 8, 2001
Jul 8, 2001
243
#ifdef DEBUG_AUDIO
Jul 10, 2006
Jul 10, 2006
244
fprintf(stderr, "Ready!\n");
245
#endif
Jul 10, 2006
Jul 10, 2006
246
}
247
248
249
250
#endif /* !USE_BLOCKING_WRITES */
}
static void
Oct 17, 2006
Oct 17, 2006
251
BSDAUDIO_PlayDevice(_THIS)
Jul 10, 2006
Jul 10, 2006
253
254
255
256
int written, p = 0;
/* Write the audio data, checking for EAGAIN on broken audio drivers */
do {
Oct 17, 2006
Oct 17, 2006
257
written = write(this->hidden->audio_fd,
Oct 28, 2006
Oct 28, 2006
258
&this->hidden->mixbuf[p], this->hidden->mixlen - p);
Oct 17, 2006
Oct 17, 2006
259
Jul 10, 2006
Jul 10, 2006
260
261
262
263
264
265
266
267
268
269
270
271
if (written > 0)
p += written;
if (written == -1 && errno != 0 && errno != EAGAIN && errno != EINTR) {
/* Non recoverable error has occurred. It should be reported!!! */
perror("audio");
break;
}
if (p < written
|| ((written < 0) && ((errno == 0) || (errno == EAGAIN)))) {
SDL_Delay(1); /* Let a little CPU time go by */
}
Aug 27, 2008
Aug 27, 2008
272
} while (p < written);
Jul 10, 2006
Jul 10, 2006
273
274
/* If timer synchronization is enabled, set the next write frame */
Oct 17, 2006
Oct 17, 2006
275
276
if (this->hidden->frame_ticks) {
this->hidden->next_frame += this->hidden->frame_ticks;
Jul 10, 2006
Jul 10, 2006
277
278
279
280
281
282
}
/* If we couldn't write, assume fatal error for now */
if (written < 0) {
this->enabled = 0;
}
Jul 8, 2001
Jul 8, 2001
283
#ifdef DEBUG_AUDIO
Jul 10, 2006
Jul 10, 2006
284
fprintf(stderr, "Wrote %d bytes of audio data\n", written);
285
286
287
#endif
}
Jul 10, 2006
Jul 10, 2006
288
static Uint8 *
Oct 17, 2006
Oct 17, 2006
289
BSDAUDIO_GetDeviceBuf(_THIS)
Oct 17, 2006
Oct 17, 2006
291
return (this->hidden->mixbuf);
292
293
294
}
static void
Oct 17, 2006
Oct 17, 2006
295
BSDAUDIO_CloseDevice(_THIS)
Oct 17, 2006
Oct 17, 2006
297
298
299
300
301
302
303
304
305
306
307
if (this->hidden != NULL) {
if (this->hidden->mixbuf != NULL) {
SDL_FreeAudioMem(this->hidden->mixbuf);
this->hidden->mixbuf = NULL;
}
if (this->hidden->audio_fd >= 0) {
close(this->hidden->audio_fd);
this->hidden->audio_fd = -1;
}
SDL_free(this->hidden);
this->hidden = NULL;
308
309
310
}
}
Oct 17, 2006
Oct 17, 2006
311
312
static int
BSDAUDIO_OpenDevice(_THIS, const char *devname, int iscapture)
Oct 17, 2006
Oct 17, 2006
314
315
const int flags = ((iscapture) ? OPEN_FLAGS_INPUT : OPEN_FLAGS_OUTPUT);
SDL_AudioFormat format = 0;
316
317
audio_info_t info;
Oct 17, 2006
Oct 17, 2006
318
319
320
/* We don't care what the devname is...we'll try to open anything. */
/* ...but default to first name in the list... */
if (devname == NULL) {
Oct 28, 2006
Oct 28, 2006
321
322
if (((iscapture) && (inputDeviceCount == 0)) ||
((!iscapture) && (outputDeviceCount == 0))) {
Oct 17, 2006
Oct 17, 2006
323
324
325
326
SDL_SetError("No such audio device");
return 0;
}
devname = ((iscapture) ? inputDevices[0] : outputDevices[0]);
327
328
}
Oct 17, 2006
Oct 17, 2006
329
330
/* Initialize all variables that we clean on shutdown */
this->hidden = (struct SDL_PrivateAudioData *)
Oct 28, 2006
Oct 28, 2006
331
SDL_malloc((sizeof *this->hidden));
Oct 17, 2006
Oct 17, 2006
332
333
334
335
336
if (this->hidden == NULL) {
SDL_OutOfMemory();
return 0;
}
SDL_memset(this->hidden, 0, (sizeof *this->hidden));
Oct 17, 2006
Oct 17, 2006
338
339
340
341
342
343
/* Open the audio device */
this->hidden->audio_fd = open(devname, flags, 0);
if (this->hidden->audio_fd < 0) {
SDL_SetError("Couldn't open %s: %s", devname, strerror(errno));
return 0;
}
344
345
AUDIO_INITINFO(&info);
Jul 10, 2006
Jul 10, 2006
346
347
/* Calculate the final parameters for this audio specification */
Oct 17, 2006
Oct 17, 2006
348
SDL_CalculateAudioSpec(&this->spec);
Jul 10, 2006
Jul 10, 2006
349
350
351
/* Set to play mode */
info.mode = AUMODE_PLAY;
Oct 17, 2006
Oct 17, 2006
352
353
if (ioctl(this->hidden->audio_fd, AUDIO_SETINFO, &info) < 0) {
BSDAUDIO_CloseDevice(this);
Jul 10, 2006
Jul 10, 2006
354
SDL_SetError("Couldn't put device into play mode");
Oct 17, 2006
Oct 17, 2006
355
return 0;
Jul 10, 2006
Jul 10, 2006
357
Jul 31, 2001
Jul 31, 2001
358
AUDIO_INITINFO(&info);
Oct 17, 2006
Oct 17, 2006
359
for (format = SDL_FirstAudioFormat(this->spec.format);
Jul 10, 2006
Jul 10, 2006
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
format; format = SDL_NextAudioFormat()) {
switch (format) {
case AUDIO_U8:
info.play.encoding = AUDIO_ENCODING_ULINEAR;
info.play.precision = 8;
break;
case AUDIO_S8:
info.play.encoding = AUDIO_ENCODING_SLINEAR;
info.play.precision = 8;
break;
case AUDIO_S16LSB:
info.play.encoding = AUDIO_ENCODING_SLINEAR_LE;
info.play.precision = 16;
break;
case AUDIO_S16MSB:
info.play.encoding = AUDIO_ENCODING_SLINEAR_BE;
info.play.precision = 16;
break;
case AUDIO_U16LSB:
info.play.encoding = AUDIO_ENCODING_ULINEAR_LE;
info.play.precision = 16;
break;
case AUDIO_U16MSB:
info.play.encoding = AUDIO_ENCODING_ULINEAR_BE;
info.play.precision = 16;
break;
default:
continue;
}
Oct 17, 2006
Oct 17, 2006
389
390
if (ioctl(this->hidden->audio_fd, AUDIO_SETINFO, &info) == 0) {
Jul 10, 2006
Jul 10, 2006
391
break;
Oct 17, 2006
Oct 17, 2006
392
}
393
394
}
Jul 10, 2006
Jul 10, 2006
395
if (!format) {
Oct 17, 2006
Oct 17, 2006
396
397
398
BSDAUDIO_CloseDevice(this);
SDL_SetError("No supported encoding for 0x%x", this->spec.format);
return 0;
399
400
}
Oct 17, 2006
Oct 17, 2006
401
this->spec.format = format;
Jul 31, 2001
Jul 31, 2001
403
AUDIO_INITINFO(&info);
Oct 17, 2006
Oct 17, 2006
404
405
406
407
info.play.channels = this->spec.channels;
if (ioctl(this->hidden->audio_fd, AUDIO_SETINFO, &info) == -1) {
this->spec.channels = 1;
}
Jul 31, 2001
Jul 31, 2001
408
AUDIO_INITINFO(&info);
Oct 17, 2006
Oct 17, 2006
409
410
info.play.sample_rate = this->spec.freq;
info.blocksize = this->spec.size;
Mar 21, 2006
Mar 21, 2006
411
412
info.hiwat = 5;
info.lowat = 3;
Oct 17, 2006
Oct 17, 2006
413
414
415
(void) ioctl(this->hidden->audio_fd, AUDIO_SETINFO, &info);
(void) ioctl(this->hidden->audio_fd, AUDIO_GETINFO, &info);
this->spec.freq = info.play.sample_rate;
416
/* Allocate mixing buffer */
Oct 17, 2006
Oct 17, 2006
417
418
419
420
421
422
this->hidden->mixlen = this->spec.size;
this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
if (this->hidden->mixbuf == NULL) {
BSDAUDIO_CloseDevice(this);
SDL_OutOfMemory();
return 0;
Oct 17, 2006
Oct 17, 2006
424
SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);
Jul 10, 2006
Jul 10, 2006
425
Oct 17, 2006
Oct 17, 2006
426
BSDAUDIO_Status(this);
427
428
/* We're ready to rock and roll. :-) */
Jul 10, 2006
Jul 10, 2006
429
return (0);
Jul 10, 2006
Jul 10, 2006
431
Oct 17, 2006
Oct 17, 2006
432
static int
Oct 28, 2006
Oct 28, 2006
433
BSDAUDIO_Init(SDL_AudioDriverImpl * impl)
Oct 17, 2006
Oct 17, 2006
434
435
436
437
438
439
440
441
442
443
444
445
{
/* Set the function pointers */
impl->DetectDevices = BSDAUDIO_DetectDevices;
impl->GetDeviceName = BSDAUDIO_GetDeviceName;
impl->OpenDevice = BSDAUDIO_OpenDevice;
impl->PlayDevice = BSDAUDIO_PlayDevice;
impl->WaitDevice = BSDAUDIO_WaitDevice;
impl->GetDeviceBuf = BSDAUDIO_GetDeviceBuf;
impl->CloseDevice = BSDAUDIO_CloseDevice;
impl->Deinitialize = BSDAUDIO_Deinitialize;
build_device_lists();
Jan 26, 2010
Jan 26, 2010
446
447
return 1; /* this audio target is available. */
Oct 17, 2006
Oct 17, 2006
448
449
450
451
452
453
454
}
AudioBootStrap BSD_AUDIO_bootstrap = {
BSD_AUDIO_DRIVER_NAME, BSD_AUDIO_DRIVER_DESC, BSDAUDIO_Init, 0
};
Jul 10, 2006
Jul 10, 2006
455
/* vi: set ts=4 sw=4 expandtab: */