src/audio/disk/SDL_diskaudio.c
author Ryan C. Gordon <icculus@icculus.org>
Sat, 06 Aug 2016 03:45:45 -0400
changeset 10247 886ab9de5f43
parent 10246 db0b712b79dd
child 10263 2a002e96888f
permissions -rw-r--r--
disk audio: Make default i/o delay match what device is meant to be running at.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
68
ac6645260d31 Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     1
/*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     2
  Simple DirectMedia Layer
9998
f67cf37e9cd4 Updated copyright to 2016
Sam Lantinga <slouken@libsdl.org>
parents: 9786
diff changeset
     3
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
68
ac6645260d31 Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     5
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     7
  arising from the use of this software.
68
ac6645260d31 Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    11
  freely, subject to the following restrictions:
68
ac6645260d31 Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    14
     claim that you wrote the original software. If you use this software
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    18
     misrepresented as being the original software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
68
ac6645260d31 Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    20
*/
8093
b43765095a6f Make internal SDL sources include SDL_internal.h instead of SDL_config.h
Ryan C. Gordon <icculus@icculus.org>
parents: 7719
diff changeset
    21
#include "../../SDL_internal.h"
68
ac6645260d31 Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    22
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5594
diff changeset
    23
#if SDL_AUDIO_DRIVER_DISK
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5594
diff changeset
    24
68
ac6645260d31 Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    25
/* Output raw audio data to a file. */
ac6645260d31 Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    26
1465
8dfa9a6d69a5 Updated WinCE support by Dmitry (with some tweaks)
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    27
#if HAVE_STDIO_H
68
ac6645260d31 Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    28
#include <stdio.h>
1465
8dfa9a6d69a5 Updated WinCE support by Dmitry (with some tweaks)
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    29
#endif
68
ac6645260d31 Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    30
1465
8dfa9a6d69a5 Updated WinCE support by Dmitry (with some tweaks)
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    31
#include "SDL_rwops.h"
1358
c71e05b4dc2e More header massaging... works great on Windows. ;-)
Sam Lantinga <slouken@libsdl.org>
parents: 1341
diff changeset
    32
#include "SDL_timer.h"
68
ac6645260d31 Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    33
#include "SDL_audio.h"
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
    34
#include "../SDL_audio_c.h"
68
ac6645260d31 Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    35
#include "SDL_diskaudio.h"
ac6645260d31 Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    36
10246
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
    37
/* !!! FIXME: these should be SDL hints, not environment variables. */
68
ac6645260d31 Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    38
/* environment variables and defaults. */
ac6645260d31 Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    39
#define DISKENVR_OUTFILE         "SDL_DISKAUDIOFILE"
ac6645260d31 Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    40
#define DISKDEFAULT_OUTFILE      "sdlaudio.raw"
10246
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
    41
#define DISKENVR_INFILE         "SDL_DISKAUDIOFILEIN"
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
    42
#define DISKDEFAULT_INFILE      "sdlaudio-in.raw"
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
    43
#define DISKENVR_IODELAY      "SDL_DISKAUDIODELAY"
68
ac6645260d31 Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    44
ac6645260d31 Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    45
/* This function waits until it is possible to write a full sound buffer */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1594
diff changeset
    46
static void
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
    47
DISKAUD_WaitDevice(_THIS)
68
ac6645260d31 Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    48
{
10246
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
    49
    SDL_Delay(this->hidden->io_delay);
68
ac6645260d31 Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    50
}
ac6645260d31 Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    51
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1594
diff changeset
    52
static void
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
    53
DISKAUD_PlayDevice(_THIS)
68
ac6645260d31 Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    54
{
10246
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
    55
    const size_t written = SDL_RWwrite(this->hidden->io,
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
    56
                                       this->hidden->mixbuf,
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
    57
                                       1, this->spec.size);
68
ac6645260d31 Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    58
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1594
diff changeset
    59
    /* If we couldn't write, assume fatal error for now */
10246
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
    60
    if (written != this->spec.size) {
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
    61
        SDL_OpenedAudioDeviceDisconnected(this);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1594
diff changeset
    62
    }
68
ac6645260d31 Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    63
#ifdef DEBUG_AUDIO
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1594
diff changeset
    64
    fprintf(stderr, "Wrote %d bytes of audio data\n", written);
68
ac6645260d31 Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    65
#endif
ac6645260d31 Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    66
}
ac6645260d31 Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    67
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1594
diff changeset
    68
static Uint8 *
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
    69
DISKAUD_GetDeviceBuf(_THIS)
68
ac6645260d31 Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    70
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1594
diff changeset
    71
    return (this->hidden->mixbuf);
68
ac6645260d31 Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    72
}
ac6645260d31 Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    73
10246
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
    74
static int
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
    75
DISKAUD_CaptureFromDevice(_THIS, void *buffer, int buflen)
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
    76
{
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
    77
    struct SDL_PrivateAudioData *h = this->hidden;
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
    78
    const int origbuflen = buflen;
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
    79
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
    80
    SDL_Delay(h->io_delay);
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
    81
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
    82
    if (h->io) {
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
    83
        const size_t br = SDL_RWread(h->io, buffer, 1, buflen);
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
    84
        buflen -= (int) br;
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
    85
        buffer = ((Uint8 *) buffer) + br;
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
    86
        if (buflen > 0) {  /* EOF (or error, but whatever). */
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
    87
            SDL_RWclose(h->io);
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
    88
            h->io = NULL;
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
    89
        }
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
    90
    }
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
    91
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
    92
    /* if we ran out of file, just write silence. */
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
    93
    SDL_memset(buffer, this->spec.silence, buflen);
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
    94
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
    95
    return origbuflen;
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
    96
}
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
    97
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
    98
static void
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
    99
DISKAUD_FlushCapture(_THIS)
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   100
{
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   101
    /* no op...we don't advance the file pointer or anything. */
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   102
}
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   103
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   104
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1594
diff changeset
   105
static void
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   106
DISKAUD_CloseDevice(_THIS)
68
ac6645260d31 Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   107
{
10246
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   108
    if (this->hidden->io != NULL) {
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   109
        SDL_RWclose(this->hidden->io);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1594
diff changeset
   110
    }
10238
620329de23d4 audio: Removed internal SDL_audiomem.h and macros.
Ryan C. Gordon <icculus@icculus.org>
parents: 10237
diff changeset
   111
    SDL_free(this->hidden->mixbuf);
10237
9530fc07da6c audio: Clean up some CloseDevice() interface details.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   112
    SDL_free(this->hidden);
68
ac6645260d31 Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   113
}
ac6645260d31 Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   114
10246
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   115
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   116
static const char *
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   117
get_filename(const int iscapture, const char *devname)
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   118
{
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   119
    if (devname == NULL) {
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   120
        devname = SDL_getenv(iscapture ? DISKENVR_INFILE : DISKENVR_OUTFILE);
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   121
        if (devname == NULL) {
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   122
            devname = iscapture ? DISKDEFAULT_INFILE : DISKDEFAULT_OUTFILE;
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   123
        }
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   124
    }
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   125
    return devname;
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   126
}
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   127
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1594
diff changeset
   128
static int
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   129
DISKAUD_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
68
ac6645260d31 Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   130
{
9786
d857f324bede Fixed assertion failure in diskaudio target caused by new hotplugging support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   131
    /* handle != NULL means "user specified the placeholder name on the fake detected device list" */
10246
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   132
    const char *fname = get_filename(iscapture, handle ? NULL : devname);
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   133
    const char *envr = SDL_getenv(DISKENVR_IODELAY);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   134
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   135
    this->hidden = (struct SDL_PrivateAudioData *)
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
   136
        SDL_malloc(sizeof(*this->hidden));
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   137
    if (this->hidden == NULL) {
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   138
        return SDL_OutOfMemory();
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   139
    }
10239
f17581d00c26 audio: changed some SDL_memset() calls to SDL_zero(), other minor corrections.
Ryan C. Gordon <icculus@icculus.org>
parents: 10238
diff changeset
   140
    SDL_zerop(this->hidden);
68
ac6645260d31 Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   141
10247
886ab9de5f43 disk audio: Make default i/o delay match what device is meant to be running at.
Ryan C. Gordon <icculus@icculus.org>
parents: 10246
diff changeset
   142
    if (envr != NULL) {
886ab9de5f43 disk audio: Make default i/o delay match what device is meant to be running at.
Ryan C. Gordon <icculus@icculus.org>
parents: 10246
diff changeset
   143
        this->hidden->io_delay = SDL_atoi(envr);
886ab9de5f43 disk audio: Make default i/o delay match what device is meant to be running at.
Ryan C. Gordon <icculus@icculus.org>
parents: 10246
diff changeset
   144
    } else {
886ab9de5f43 disk audio: Make default i/o delay match what device is meant to be running at.
Ryan C. Gordon <icculus@icculus.org>
parents: 10246
diff changeset
   145
        this->hidden->io_delay = ((this->spec.samples * 1000) / this->spec.freq);
886ab9de5f43 disk audio: Make default i/o delay match what device is meant to be running at.
Ryan C. Gordon <icculus@icculus.org>
parents: 10246
diff changeset
   146
    }
7366
38c6f26bb010 Disk audio target was using this->hidden->mixlen before we set it.
Ryan C. Gordon <icculus@icculus.org>
parents: 7038
diff changeset
   147
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1594
diff changeset
   148
    /* Open the audio device */
10246
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   149
    this->hidden->io = SDL_RWFromFile(fname, iscapture ? "rb" : "wb");
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   150
    if (this->hidden->io == NULL) {
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   151
        return -1;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1594
diff changeset
   152
    }
68
ac6645260d31 Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   153
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1594
diff changeset
   154
    /* Allocate mixing buffer */
10246
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   155
    if (!iscapture) {
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   156
        this->hidden->mixbuf = (Uint8 *) SDL_malloc(this->spec.size);
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   157
        if (this->hidden->mixbuf == NULL) {
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   158
            return SDL_OutOfMemory();
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   159
        }
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   160
        SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1594
diff changeset
   161
    }
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   162
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   163
#if HAVE_STDIO_H
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   164
    fprintf(stderr,
10246
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   165
            "WARNING: You are using the SDL disk i/o audio driver!\n"
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   166
            " %s file [%s].\n", iscapture ? "Reading from" : "Writing to",
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   167
            fname);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   168
#endif
68
ac6645260d31 Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   169
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1594
diff changeset
   170
    /* We're ready to rock and roll. :-) */
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   171
    return 0;
68
ac6645260d31 Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   172
}
ac6645260d31 Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   173
9786
d857f324bede Fixed assertion failure in diskaudio target caused by new hotplugging support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   174
static void
d857f324bede Fixed assertion failure in diskaudio target caused by new hotplugging support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   175
DISKAUD_DetectDevices(void)
d857f324bede Fixed assertion failure in diskaudio target caused by new hotplugging support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   176
{
10246
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   177
    SDL_AddAudioDevice(SDL_FALSE, DEFAULT_OUTPUT_DEVNAME, (void *) 0x1);
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   178
    SDL_AddAudioDevice(SDL_TRUE, DEFAULT_INPUT_DEVNAME, (void *) 0x2);
9786
d857f324bede Fixed assertion failure in diskaudio target caused by new hotplugging support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   179
}
d857f324bede Fixed assertion failure in diskaudio target caused by new hotplugging support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   180
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   181
static int
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
   182
DISKAUD_Init(SDL_AudioDriverImpl * impl)
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   183
{
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   184
    /* Set the function pointers */
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   185
    impl->OpenDevice = DISKAUD_OpenDevice;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   186
    impl->WaitDevice = DISKAUD_WaitDevice;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   187
    impl->PlayDevice = DISKAUD_PlayDevice;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   188
    impl->GetDeviceBuf = DISKAUD_GetDeviceBuf;
10246
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   189
    impl->CaptureFromDevice = DISKAUD_CaptureFromDevice;
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   190
    impl->FlushCapture = DISKAUD_FlushCapture;
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   191
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   192
    impl->CloseDevice = DISKAUD_CloseDevice;
9786
d857f324bede Fixed assertion failure in diskaudio target caused by new hotplugging support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   193
    impl->DetectDevices = DISKAUD_DetectDevices;
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   194
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   195
    impl->AllowsArbitraryDeviceNames = 1;
10246
db0b712b79dd disk audio: Implemented "capture" support, cleaned up some things.
Ryan C. Gordon <icculus@icculus.org>
parents: 10239
diff changeset
   196
    impl->HasCaptureSupport = SDL_TRUE;
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   197
3699
4160ba33b597 Removed test for "driver is valid, but doesn't see any audio devices."
Ryan C. Gordon <icculus@icculus.org>
parents: 3697
diff changeset
   198
    return 1;   /* this audio target is available. */
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   199
}
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   200
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   201
AudioBootStrap DISKAUD_bootstrap = {
5594
e741303e08a8 Removed needless macros in various audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   202
    "disk", "direct-to-disk audio", DISKAUD_Init, 1
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   203
};
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   204
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5594
diff changeset
   205
#endif /* SDL_AUDIO_DRIVER_DISK */
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5594
diff changeset
   206
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1594
diff changeset
   207
/* vi: set ts=4 sw=4 expandtab: */