2015-06-21 17:33:46 +02:00
|
|
|
/*
|
|
|
|
Simple DirectMedia Layer
|
2021-01-02 19:25:38 +01:00
|
|
|
Copyright (C) 1997-2021 Sam Lantinga <slouken@libsdl.org>
|
2015-06-21 17:33:46 +02:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
#include "../../SDL_internal.h"
|
|
|
|
|
2017-05-25 01:56:59 +02:00
|
|
|
#if SDL_AUDIO_DRIVER_NETBSD
|
2015-06-21 17:33:46 +02:00
|
|
|
|
|
|
|
/*
|
2017-05-25 01:56:59 +02:00
|
|
|
* Driver for native NetBSD audio(4).
|
2020-04-05 19:44:51 +02:00
|
|
|
* nia@NetBSD.org
|
2015-06-21 17:33:46 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#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>
|
|
|
|
|
|
|
|
#include "SDL_timer.h"
|
|
|
|
#include "SDL_audio.h"
|
2017-08-15 05:22:19 +02:00
|
|
|
#include "../../core/unix/SDL_poll.h"
|
2015-06-21 17:33:46 +02:00
|
|
|
#include "../SDL_audio_c.h"
|
|
|
|
#include "../SDL_audiodev_c.h"
|
2017-06-29 23:00:09 +02:00
|
|
|
#include "SDL_netbsdaudio.h"
|
2015-06-21 17:33:46 +02:00
|
|
|
|
|
|
|
/* #define DEBUG_AUDIO */
|
|
|
|
|
|
|
|
static void
|
2017-05-25 01:56:59 +02:00
|
|
|
NETBSDAUDIO_DetectDevices(void)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
|
|
|
SDL_EnumUnixAudioDevices(0, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2017-05-25 01:56:59 +02:00
|
|
|
NETBSDAUDIO_Status(_THIS)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
|
|
|
#ifdef DEBUG_AUDIO
|
|
|
|
/* *INDENT-OFF* */
|
|
|
|
audio_info_t info;
|
2019-06-08 22:03:36 +02:00
|
|
|
const struct audio_prinfo *prinfo;
|
2015-06-21 17:33:46 +02:00
|
|
|
|
|
|
|
if (ioctl(this->hidden->audio_fd, AUDIO_GETINFO, &info) < 0) {
|
|
|
|
fprintf(stderr, "AUDIO_GETINFO failed.\n");
|
|
|
|
return;
|
|
|
|
}
|
2016-08-03 07:57:41 +02:00
|
|
|
|
2019-06-08 22:03:36 +02:00
|
|
|
prinfo = this->iscapture ? &info.record : &info.play;
|
2016-08-03 07:57:41 +02:00
|
|
|
|
2015-06-21 17:33:46 +02:00
|
|
|
fprintf(stderr, "\n"
|
2016-08-03 07:57:41 +02:00
|
|
|
"[%s info]\n"
|
2015-06-21 17:33:46 +02:00
|
|
|
"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"
|
|
|
|
"",
|
2016-08-03 07:57:41 +02:00
|
|
|
this->iscapture ? "record" : "play",
|
|
|
|
prinfo->buffer_size,
|
|
|
|
prinfo->sample_rate,
|
|
|
|
prinfo->channels,
|
|
|
|
prinfo->precision,
|
|
|
|
prinfo->encoding,
|
|
|
|
prinfo->seek,
|
|
|
|
prinfo->samples,
|
|
|
|
prinfo->eof,
|
|
|
|
prinfo->pause ? "yes" : "no",
|
|
|
|
prinfo->error ? "yes" : "no",
|
|
|
|
prinfo->waiting ? "yes" : "no",
|
|
|
|
prinfo->active ? "yes" : "no");
|
2015-06-21 17:33:46 +02:00
|
|
|
|
|
|
|
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" : "?"));
|
2019-06-08 22:03:36 +02:00
|
|
|
|
|
|
|
fprintf(stderr, "\n"
|
|
|
|
"[audio spec]\n"
|
|
|
|
"format : 0x%x\n"
|
|
|
|
"size : %u\n"
|
|
|
|
"",
|
|
|
|
this->spec.format,
|
|
|
|
this->spec.size);
|
2015-06-21 17:33:46 +02:00
|
|
|
/* *INDENT-ON* */
|
|
|
|
#endif /* DEBUG_AUDIO */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2017-05-25 01:56:59 +02:00
|
|
|
NETBSDAUDIO_PlayDevice(_THIS)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2019-06-08 22:03:36 +02:00
|
|
|
struct SDL_PrivateAudioData *h = this->hidden;
|
|
|
|
int written;
|
2015-06-21 17:33:46 +02:00
|
|
|
|
2019-06-08 22:03:36 +02:00
|
|
|
/* Write the audio data */
|
|
|
|
written = write(h->audio_fd, h->mixbuf, h->mixlen);
|
|
|
|
if (written == -1) {
|
|
|
|
/* Non recoverable error has occurred. It should be reported!!! */
|
2015-06-21 17:33:46 +02:00
|
|
|
SDL_OpenedAudioDeviceDisconnected(this);
|
2019-06-08 22:03:36 +02:00
|
|
|
perror("audio");
|
|
|
|
return;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
2019-06-08 22:03:36 +02:00
|
|
|
|
|
|
|
#ifdef DEBUG_AUDIO
|
|
|
|
fprintf(stderr, "Wrote %d bytes of audio data\n", written);
|
|
|
|
#endif
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static Uint8 *
|
2017-05-25 01:56:59 +02:00
|
|
|
NETBSDAUDIO_GetDeviceBuf(_THIS)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
|
|
|
return (this->hidden->mixbuf);
|
|
|
|
}
|
|
|
|
|
2016-08-03 07:57:41 +02:00
|
|
|
|
|
|
|
static int
|
2017-05-25 01:56:59 +02:00
|
|
|
NETBSDAUDIO_CaptureFromDevice(_THIS, void *_buffer, int buflen)
|
2016-08-03 07:57:41 +02:00
|
|
|
{
|
|
|
|
Uint8 *buffer = (Uint8 *) _buffer;
|
2019-06-08 22:03:36 +02:00
|
|
|
int br;
|
|
|
|
|
|
|
|
br = read(this->hidden->audio_fd, buffer, buflen);
|
|
|
|
if (br == -1) {
|
|
|
|
/* Non recoverable error has occurred. It should be reported!!! */
|
|
|
|
perror("audio");
|
|
|
|
return -1;
|
|
|
|
}
|
2016-08-03 07:57:41 +02:00
|
|
|
|
|
|
|
#ifdef DEBUG_AUDIO
|
2019-06-08 22:03:36 +02:00
|
|
|
fprintf(stderr, "Captured %d bytes of audio data\n", br);
|
2016-08-03 07:57:41 +02:00
|
|
|
#endif
|
2019-06-08 22:03:36 +02:00
|
|
|
return 0;
|
2016-08-03 07:57:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-05-25 01:56:59 +02:00
|
|
|
NETBSDAUDIO_FlushCapture(_THIS)
|
2016-08-03 07:57:41 +02:00
|
|
|
{
|
|
|
|
audio_info_t info;
|
|
|
|
size_t remain;
|
|
|
|
Uint8 buf[512];
|
|
|
|
|
|
|
|
if (ioctl(this->hidden->audio_fd, AUDIO_GETINFO, &info) < 0) {
|
|
|
|
return; /* oh well. */
|
|
|
|
}
|
|
|
|
|
|
|
|
remain = (size_t) (info.record.samples * (SDL_AUDIO_BITSIZE(this->spec.format) / 8));
|
|
|
|
while (remain > 0) {
|
|
|
|
const size_t len = SDL_min(sizeof (buf), remain);
|
|
|
|
const int br = read(this->hidden->audio_fd, buf, len);
|
|
|
|
if (br <= 0) {
|
|
|
|
return; /* oh well. */
|
|
|
|
}
|
|
|
|
remain -= br;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-21 17:33:46 +02:00
|
|
|
static void
|
2017-05-25 01:56:59 +02:00
|
|
|
NETBSDAUDIO_CloseDevice(_THIS)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2016-08-05 07:44:41 +02:00
|
|
|
if (this->hidden->audio_fd >= 0) {
|
|
|
|
close(this->hidden->audio_fd);
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
2016-08-05 07:44:15 +02:00
|
|
|
SDL_free(this->hidden->mixbuf);
|
2016-08-05 07:44:41 +02:00
|
|
|
SDL_free(this->hidden);
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2017-05-25 01:56:59 +02:00
|
|
|
NETBSDAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
|
|
|
SDL_AudioFormat format = 0;
|
2021-01-08 19:09:37 +01:00
|
|
|
audio_info_t info, hwinfo;
|
2019-06-08 22:03:36 +02:00
|
|
|
struct audio_prinfo *prinfo = iscapture ? &info.record : &info.play;
|
2015-06-21 17:33:46 +02:00
|
|
|
|
|
|
|
/* 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) {
|
|
|
|
devname = SDL_GetAudioDeviceName(0, iscapture);
|
|
|
|
if (devname == NULL) {
|
|
|
|
return SDL_SetError("No such audio device");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize all variables that we clean on shutdown */
|
|
|
|
this->hidden = (struct SDL_PrivateAudioData *)
|
|
|
|
SDL_malloc((sizeof *this->hidden));
|
|
|
|
if (this->hidden == NULL) {
|
|
|
|
return SDL_OutOfMemory();
|
|
|
|
}
|
2016-08-05 07:59:06 +02:00
|
|
|
SDL_zerop(this->hidden);
|
2015-06-21 17:33:46 +02:00
|
|
|
|
|
|
|
/* Open the audio device */
|
2019-06-08 22:03:36 +02:00
|
|
|
this->hidden->audio_fd = open(devname, iscapture ? O_RDONLY : O_WRONLY);
|
2015-06-21 17:33:46 +02:00
|
|
|
if (this->hidden->audio_fd < 0) {
|
|
|
|
return SDL_SetError("Couldn't open %s: %s", devname, strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
AUDIO_INITINFO(&info);
|
|
|
|
|
2021-01-08 19:09:37 +01:00
|
|
|
#ifdef AUDIO_GETFORMAT /* Introduced in NetBSD 9.0 */
|
|
|
|
if (ioctl(this->hidden->audio_fd, AUDIO_GETFORMAT, &hwinfo) != -1) {
|
|
|
|
/*
|
|
|
|
* Use the device's native sample rate so the kernel doesn't have to
|
|
|
|
* resample.
|
|
|
|
*/
|
|
|
|
this->spec.freq = iscapture ?
|
|
|
|
hwinfo.record.sample_rate : hwinfo.play.sample_rate;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-06-08 22:03:36 +02:00
|
|
|
prinfo->encoding = AUDIO_ENCODING_NONE;
|
2021-01-08 19:09:37 +01:00
|
|
|
prinfo->sample_rate = this->spec.freq;
|
|
|
|
prinfo->channels = this->spec.channels;
|
2015-06-21 17:33:46 +02:00
|
|
|
|
2019-06-08 22:03:36 +02:00
|
|
|
for (format = SDL_FirstAudioFormat(this->spec.format); format;) {
|
2015-06-21 17:33:46 +02:00
|
|
|
switch (format) {
|
|
|
|
case AUDIO_U8:
|
2016-08-03 07:57:41 +02:00
|
|
|
prinfo->encoding = AUDIO_ENCODING_ULINEAR;
|
|
|
|
prinfo->precision = 8;
|
2015-06-21 17:33:46 +02:00
|
|
|
break;
|
|
|
|
case AUDIO_S8:
|
2016-08-03 07:57:41 +02:00
|
|
|
prinfo->encoding = AUDIO_ENCODING_SLINEAR;
|
|
|
|
prinfo->precision = 8;
|
2015-06-21 17:33:46 +02:00
|
|
|
break;
|
|
|
|
case AUDIO_S16LSB:
|
2016-08-03 07:57:41 +02:00
|
|
|
prinfo->encoding = AUDIO_ENCODING_SLINEAR_LE;
|
|
|
|
prinfo->precision = 16;
|
2015-06-21 17:33:46 +02:00
|
|
|
break;
|
|
|
|
case AUDIO_S16MSB:
|
2016-08-03 07:57:41 +02:00
|
|
|
prinfo->encoding = AUDIO_ENCODING_SLINEAR_BE;
|
|
|
|
prinfo->precision = 16;
|
2015-06-21 17:33:46 +02:00
|
|
|
break;
|
|
|
|
case AUDIO_U16LSB:
|
2016-08-03 07:57:41 +02:00
|
|
|
prinfo->encoding = AUDIO_ENCODING_ULINEAR_LE;
|
|
|
|
prinfo->precision = 16;
|
2015-06-21 17:33:46 +02:00
|
|
|
break;
|
|
|
|
case AUDIO_U16MSB:
|
2016-08-03 07:57:41 +02:00
|
|
|
prinfo->encoding = AUDIO_ENCODING_ULINEAR_BE;
|
|
|
|
prinfo->precision = 16;
|
2015-06-21 17:33:46 +02:00
|
|
|
break;
|
2020-04-05 19:44:51 +02:00
|
|
|
case AUDIO_S32LSB:
|
|
|
|
prinfo->encoding = AUDIO_ENCODING_SLINEAR_LE;
|
|
|
|
prinfo->precision = 32;
|
|
|
|
break;
|
|
|
|
case AUDIO_S32MSB:
|
|
|
|
prinfo->encoding = AUDIO_ENCODING_SLINEAR_BE;
|
|
|
|
prinfo->precision = 32;
|
|
|
|
break;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
2019-06-08 22:03:36 +02:00
|
|
|
if (prinfo->encoding != AUDIO_ENCODING_NONE) {
|
2015-06-21 17:33:46 +02:00
|
|
|
break;
|
|
|
|
}
|
2019-06-08 22:03:36 +02:00
|
|
|
format = SDL_NextAudioFormat();
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
2019-06-08 22:03:36 +02:00
|
|
|
if (prinfo->encoding == AUDIO_ENCODING_NONE) {
|
2015-06-21 17:33:46 +02:00
|
|
|
return SDL_SetError("No supported encoding for 0x%x", this->spec.format);
|
|
|
|
}
|
|
|
|
|
|
|
|
info.hiwat = 5;
|
|
|
|
info.lowat = 3;
|
2021-03-10 09:36:46 +01:00
|
|
|
if (ioctl(this->hidden->audio_fd, AUDIO_SETINFO, &info) < 0) {
|
|
|
|
return SDL_SetError("AUDIO_SETINFO failed for %s: %s", devname, strerror(errno));
|
|
|
|
}
|
2019-06-08 22:03:36 +02:00
|
|
|
|
2021-03-10 09:36:46 +01:00
|
|
|
if (ioctl(this->hidden->audio_fd, AUDIO_GETINFO, &info) < 0) {
|
|
|
|
return SDL_SetError("AUDIO_GETINFO failed for %s: %s", devname, strerror(errno));
|
|
|
|
}
|
2021-01-08 19:09:37 +01:00
|
|
|
|
|
|
|
/* Final spec used for the device. */
|
|
|
|
this->spec.format = format;
|
2016-08-03 07:57:41 +02:00
|
|
|
this->spec.freq = prinfo->sample_rate;
|
2019-06-08 22:03:36 +02:00
|
|
|
this->spec.channels = prinfo->channels;
|
2016-08-03 07:57:41 +02:00
|
|
|
|
2021-01-08 19:09:37 +01:00
|
|
|
SDL_CalculateAudioSpec(&this->spec);
|
|
|
|
|
2016-08-03 07:57:41 +02:00
|
|
|
if (!iscapture) {
|
|
|
|
/* Allocate mixing buffer */
|
|
|
|
this->hidden->mixlen = this->spec.size;
|
2016-08-05 07:44:15 +02:00
|
|
|
this->hidden->mixbuf = (Uint8 *) SDL_malloc(this->hidden->mixlen);
|
2016-08-03 07:57:41 +02:00
|
|
|
if (this->hidden->mixbuf == NULL) {
|
|
|
|
return SDL_OutOfMemory();
|
|
|
|
}
|
|
|
|
SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
2017-05-25 01:56:59 +02:00
|
|
|
NETBSDAUDIO_Status(this);
|
2015-06-21 17:33:46 +02:00
|
|
|
|
|
|
|
/* We're ready to rock and roll. :-) */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2017-05-25 01:56:59 +02:00
|
|
|
NETBSDAUDIO_Init(SDL_AudioDriverImpl * impl)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
|
|
|
/* Set the function pointers */
|
2017-05-25 01:56:59 +02:00
|
|
|
impl->DetectDevices = NETBSDAUDIO_DetectDevices;
|
|
|
|
impl->OpenDevice = NETBSDAUDIO_OpenDevice;
|
|
|
|
impl->PlayDevice = NETBSDAUDIO_PlayDevice;
|
|
|
|
impl->GetDeviceBuf = NETBSDAUDIO_GetDeviceBuf;
|
|
|
|
impl->CloseDevice = NETBSDAUDIO_CloseDevice;
|
|
|
|
impl->CaptureFromDevice = NETBSDAUDIO_CaptureFromDevice;
|
|
|
|
impl->FlushCapture = NETBSDAUDIO_FlushCapture;
|
2015-06-21 17:33:46 +02:00
|
|
|
|
2016-08-03 07:57:41 +02:00
|
|
|
impl->HasCaptureSupport = SDL_TRUE;
|
2015-06-21 17:33:46 +02:00
|
|
|
impl->AllowsArbitraryDeviceNames = 1;
|
|
|
|
|
|
|
|
return 1; /* this audio target is available. */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-25 01:56:59 +02:00
|
|
|
AudioBootStrap NETBSDAUDIO_bootstrap = {
|
|
|
|
"netbsd", "NetBSD audio", NETBSDAUDIO_Init, 0
|
2015-06-21 17:33:46 +02:00
|
|
|
};
|
|
|
|
|
2017-05-25 01:56:59 +02:00
|
|
|
#endif /* SDL_AUDIO_DRIVER_NETBSD */
|
2015-06-21 17:33:46 +02:00
|
|
|
|
|
|
|
/* vi: set ts=4 sw=4 expandtab: */
|