2015-06-21 17:33:46 +02:00
|
|
|
/*
|
|
|
|
Simple DirectMedia Layer
|
2016-01-02 19:10:34 +01:00
|
|
|
Copyright (C) 1997-2016 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"
|
|
|
|
|
|
|
|
#if SDL_AUDIO_DRIVER_DISK
|
|
|
|
|
|
|
|
/* Output raw audio data to a file. */
|
|
|
|
|
|
|
|
#if HAVE_STDIO_H
|
|
|
|
#include <stdio.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "SDL_rwops.h"
|
|
|
|
#include "SDL_timer.h"
|
|
|
|
#include "SDL_audio.h"
|
|
|
|
#include "../SDL_audio_c.h"
|
|
|
|
#include "SDL_diskaudio.h"
|
|
|
|
|
2016-08-06 09:39:15 +02:00
|
|
|
/* !!! FIXME: these should be SDL hints, not environment variables. */
|
2015-06-21 17:33:46 +02:00
|
|
|
/* environment variables and defaults. */
|
|
|
|
#define DISKENVR_OUTFILE "SDL_DISKAUDIOFILE"
|
|
|
|
#define DISKDEFAULT_OUTFILE "sdlaudio.raw"
|
2016-08-06 09:39:15 +02:00
|
|
|
#define DISKENVR_INFILE "SDL_DISKAUDIOFILEIN"
|
|
|
|
#define DISKDEFAULT_INFILE "sdlaudio-in.raw"
|
|
|
|
#define DISKENVR_IODELAY "SDL_DISKAUDIODELAY"
|
2015-06-21 17:33:46 +02:00
|
|
|
|
|
|
|
/* This function waits until it is possible to write a full sound buffer */
|
|
|
|
static void
|
2016-08-12 04:22:09 +02:00
|
|
|
DISKAUDIO_WaitDevice(_THIS)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2016-08-06 09:39:15 +02:00
|
|
|
SDL_Delay(this->hidden->io_delay);
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-08-12 04:22:09 +02:00
|
|
|
DISKAUDIO_PlayDevice(_THIS)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2016-08-06 09:39:15 +02:00
|
|
|
const size_t written = SDL_RWwrite(this->hidden->io,
|
|
|
|
this->hidden->mixbuf,
|
|
|
|
1, this->spec.size);
|
2015-06-21 17:33:46 +02:00
|
|
|
|
|
|
|
/* If we couldn't write, assume fatal error for now */
|
2016-08-06 09:39:15 +02:00
|
|
|
if (written != this->spec.size) {
|
2015-06-21 17:33:46 +02:00
|
|
|
SDL_OpenedAudioDeviceDisconnected(this);
|
|
|
|
}
|
|
|
|
#ifdef DEBUG_AUDIO
|
|
|
|
fprintf(stderr, "Wrote %d bytes of audio data\n", written);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static Uint8 *
|
2016-08-12 04:22:09 +02:00
|
|
|
DISKAUDIO_GetDeviceBuf(_THIS)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
|
|
|
return (this->hidden->mixbuf);
|
|
|
|
}
|
|
|
|
|
2016-08-06 09:39:15 +02:00
|
|
|
static int
|
2016-08-12 04:22:09 +02:00
|
|
|
DISKAUDIO_CaptureFromDevice(_THIS, void *buffer, int buflen)
|
2016-08-06 09:39:15 +02:00
|
|
|
{
|
|
|
|
struct SDL_PrivateAudioData *h = this->hidden;
|
|
|
|
const int origbuflen = buflen;
|
|
|
|
|
|
|
|
SDL_Delay(h->io_delay);
|
|
|
|
|
|
|
|
if (h->io) {
|
|
|
|
const size_t br = SDL_RWread(h->io, buffer, 1, buflen);
|
|
|
|
buflen -= (int) br;
|
|
|
|
buffer = ((Uint8 *) buffer) + br;
|
|
|
|
if (buflen > 0) { /* EOF (or error, but whatever). */
|
|
|
|
SDL_RWclose(h->io);
|
|
|
|
h->io = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if we ran out of file, just write silence. */
|
|
|
|
SDL_memset(buffer, this->spec.silence, buflen);
|
|
|
|
|
|
|
|
return origbuflen;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-08-12 04:22:09 +02:00
|
|
|
DISKAUDIO_FlushCapture(_THIS)
|
2016-08-06 09:39:15 +02:00
|
|
|
{
|
|
|
|
/* no op...we don't advance the file pointer or anything. */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-21 17:33:46 +02:00
|
|
|
static void
|
2016-08-12 04:22:09 +02:00
|
|
|
DISKAUDIO_CloseDevice(_THIS)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2016-08-06 09:39:15 +02:00
|
|
|
if (this->hidden->io != NULL) {
|
|
|
|
SDL_RWclose(this->hidden->io);
|
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
|
|
|
}
|
|
|
|
|
2016-08-06 09:39:15 +02:00
|
|
|
|
|
|
|
static const char *
|
|
|
|
get_filename(const int iscapture, const char *devname)
|
|
|
|
{
|
|
|
|
if (devname == NULL) {
|
|
|
|
devname = SDL_getenv(iscapture ? DISKENVR_INFILE : DISKENVR_OUTFILE);
|
|
|
|
if (devname == NULL) {
|
|
|
|
devname = iscapture ? DISKDEFAULT_INFILE : DISKDEFAULT_OUTFILE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return devname;
|
|
|
|
}
|
|
|
|
|
2015-06-21 17:33:46 +02:00
|
|
|
static int
|
2016-08-12 04:22:09 +02:00
|
|
|
DISKAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2015-06-30 19:46:06 +02:00
|
|
|
/* handle != NULL means "user specified the placeholder name on the fake detected device list" */
|
2016-08-06 09:39:15 +02:00
|
|
|
const char *fname = get_filename(iscapture, handle ? NULL : devname);
|
|
|
|
const char *envr = SDL_getenv(DISKENVR_IODELAY);
|
2015-06-21 17:33:46 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
2016-08-06 09:45:45 +02:00
|
|
|
if (envr != NULL) {
|
|
|
|
this->hidden->io_delay = SDL_atoi(envr);
|
|
|
|
} else {
|
|
|
|
this->hidden->io_delay = ((this->spec.samples * 1000) / this->spec.freq);
|
|
|
|
}
|
2015-06-21 17:33:46 +02:00
|
|
|
|
|
|
|
/* Open the audio device */
|
2016-08-06 09:39:15 +02:00
|
|
|
this->hidden->io = SDL_RWFromFile(fname, iscapture ? "rb" : "wb");
|
|
|
|
if (this->hidden->io == NULL) {
|
2015-06-21 17:33:46 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate mixing buffer */
|
2016-08-06 09:39:15 +02:00
|
|
|
if (!iscapture) {
|
|
|
|
this->hidden->mixbuf = (Uint8 *) SDL_malloc(this->spec.size);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
#if HAVE_STDIO_H
|
|
|
|
fprintf(stderr,
|
2016-08-06 09:39:15 +02:00
|
|
|
"WARNING: You are using the SDL disk i/o audio driver!\n"
|
|
|
|
" %s file [%s].\n", iscapture ? "Reading from" : "Writing to",
|
|
|
|
fname);
|
2015-06-21 17:33:46 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* We're ready to rock and roll. :-) */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-30 19:46:06 +02:00
|
|
|
static void
|
2016-08-12 04:22:09 +02:00
|
|
|
DISKAUDIO_DetectDevices(void)
|
2015-06-30 19:46:06 +02:00
|
|
|
{
|
2016-08-06 09:39:15 +02:00
|
|
|
SDL_AddAudioDevice(SDL_FALSE, DEFAULT_OUTPUT_DEVNAME, (void *) 0x1);
|
|
|
|
SDL_AddAudioDevice(SDL_TRUE, DEFAULT_INPUT_DEVNAME, (void *) 0x2);
|
2015-06-30 19:46:06 +02:00
|
|
|
}
|
|
|
|
|
2015-06-21 17:33:46 +02:00
|
|
|
static int
|
2016-08-12 04:22:09 +02:00
|
|
|
DISKAUDIO_Init(SDL_AudioDriverImpl * impl)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
|
|
|
/* Set the function pointers */
|
2016-08-12 04:22:09 +02:00
|
|
|
impl->OpenDevice = DISKAUDIO_OpenDevice;
|
|
|
|
impl->WaitDevice = DISKAUDIO_WaitDevice;
|
|
|
|
impl->PlayDevice = DISKAUDIO_PlayDevice;
|
|
|
|
impl->GetDeviceBuf = DISKAUDIO_GetDeviceBuf;
|
|
|
|
impl->CaptureFromDevice = DISKAUDIO_CaptureFromDevice;
|
|
|
|
impl->FlushCapture = DISKAUDIO_FlushCapture;
|
2016-08-06 09:39:15 +02:00
|
|
|
|
2016-08-12 04:22:09 +02:00
|
|
|
impl->CloseDevice = DISKAUDIO_CloseDevice;
|
|
|
|
impl->DetectDevices = DISKAUDIO_DetectDevices;
|
2015-06-21 17:33:46 +02:00
|
|
|
|
|
|
|
impl->AllowsArbitraryDeviceNames = 1;
|
2016-08-06 09:39:15 +02:00
|
|
|
impl->HasCaptureSupport = SDL_TRUE;
|
2015-06-21 17:33:46 +02:00
|
|
|
|
|
|
|
return 1; /* this audio target is available. */
|
|
|
|
}
|
|
|
|
|
2016-08-12 04:22:09 +02:00
|
|
|
AudioBootStrap DISKAUDIO_bootstrap = {
|
|
|
|
"disk", "direct-to-disk audio", DISKAUDIO_Init, 1
|
2015-06-21 17:33:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* SDL_AUDIO_DRIVER_DISK */
|
|
|
|
|
|
|
|
/* vi: set ts=4 sw=4 expandtab: */
|