src/audio/dummy/SDL_dummyaudio.c
author Ryan C. Gordon
Tue, 03 Oct 2006 22:17:59 +0000
branchSDL-ryan-multiple-audio-device
changeset 3792 866c310e2cb5
parent 3784 37c9c4590689
child 3796 b19680c84cdf
permissions -rw-r--r--
Changed some 1.3 audio symbol names.
icculus@1532
     1
/*
icculus@1532
     2
    SDL - Simple DirectMedia Layer
icculus@1532
     3
    Copyright (C) 1997-2006 Sam Lantinga
icculus@1532
     4
icculus@1532
     5
    This library is free software; you can redistribute it and/or
icculus@1532
     6
    modify it under the terms of the GNU Lesser General Public
icculus@1532
     7
    License as published by the Free Software Foundation; either
icculus@1532
     8
    version 2.1 of the License, or (at your option) any later version.
icculus@1532
     9
icculus@1532
    10
    This library is distributed in the hope that it will be useful,
icculus@1532
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
icculus@1532
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
icculus@1532
    13
    Lesser General Public License for more details.
icculus@1532
    14
icculus@1532
    15
    You should have received a copy of the GNU Lesser General Public
icculus@1532
    16
    License along with this library; if not, write to the Free Software
icculus@1532
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
icculus@1532
    18
icculus@1532
    19
    Sam Lantinga
icculus@1532
    20
    slouken@libsdl.org
icculus@1532
    21
icculus@1537
    22
    This file written by Ryan C. Gordon (icculus@icculus.org)
icculus@1532
    23
*/
icculus@1532
    24
#include "SDL_config.h"
icculus@1532
    25
slouken@1533
    26
/* Output audio to nowhere... */
icculus@1532
    27
icculus@1532
    28
#include "SDL_rwops.h"
icculus@1532
    29
#include "SDL_timer.h"
icculus@1532
    30
#include "SDL_audio.h"
icculus@1532
    31
#include "../SDL_audiomem.h"
icculus@1532
    32
#include "../SDL_audio_c.h"
icculus@1532
    33
#include "../SDL_audiodev_c.h"
icculus@1532
    34
#include "SDL_dummyaudio.h"
icculus@1532
    35
icculus@1532
    36
/* The tag name used by DUMMY audio */
icculus@1532
    37
#define DUMMYAUD_DRIVER_NAME         "dummy"
icculus@1532
    38
icculus@1532
    39
/* Audio driver functions */
icculus@3792
    40
static int DUMMYAUD_OpenDevice(_THIS, const char *devname, int iscapture);
icculus@3792
    41
static void DUMMYAUD_WaitDevice(_THIS);
icculus@3792
    42
static void DUMMYAUD_PlayDevice(_THIS);
icculus@3792
    43
static Uint8 *DUMMYAUD_GetDeviceBuf(_THIS);
icculus@3792
    44
static void DUMMYAUD_CloseDevice(_THIS);
icculus@1532
    45
icculus@1532
    46
/* Audio driver bootstrap functions */
slouken@1895
    47
static int
slouken@1895
    48
DUMMYAUD_Available(void)
icculus@1532
    49
{
icculus@3784
    50
    /* !!! FIXME: check this at a higher level... */
icculus@3784
    51
    /* only ever use this driver if explicitly requested. */
slouken@1895
    52
    const char *envr = SDL_getenv("SDL_AUDIODRIVER");
slouken@1895
    53
    if (envr && (SDL_strcmp(envr, DUMMYAUD_DRIVER_NAME) == 0)) {
slouken@1895
    54
        return (1);
slouken@1895
    55
    }
slouken@1895
    56
    return (0);
icculus@1532
    57
}
icculus@1532
    58
icculus@3784
    59
static int
icculus@3784
    60
DUMMYAUD_Init(SDL_AudioDriverImpl *impl)
icculus@1532
    61
{
icculus@3784
    62
    /* Set the function pointers */
icculus@3792
    63
    impl->OpenDevice = DUMMYAUD_OpenDevice;
icculus@3792
    64
    impl->WaitDevice = DUMMYAUD_WaitDevice;
icculus@3792
    65
    impl->PlayDevice = DUMMYAUD_PlayDevice;
icculus@3792
    66
    impl->GetDeviceBuf = DUMMYAUD_GetDeviceBuf;
icculus@3792
    67
    impl->CloseDevice = DUMMYAUD_CloseDevice;
icculus@1532
    68
icculus@3784
    69
    return 1;
icculus@1532
    70
}
icculus@1532
    71
icculus@1532
    72
AudioBootStrap DUMMYAUD_bootstrap = {
slouken@1895
    73
    DUMMYAUD_DRIVER_NAME, "SDL dummy audio driver",
icculus@3784
    74
    DUMMYAUD_Available, DUMMYAUD_Init
icculus@1532
    75
};
icculus@1532
    76
icculus@1532
    77
/* This function waits until it is possible to write a full sound buffer */
slouken@1895
    78
static void
icculus@3792
    79
DUMMYAUD_WaitDevice(_THIS)
icculus@1532
    80
{
slouken@1895
    81
    /* Don't block on first calls to simulate initial fragment filling. */
slouken@1895
    82
    if (this->hidden->initial_calls)
slouken@1895
    83
        this->hidden->initial_calls--;
slouken@1895
    84
    else
slouken@1895
    85
        SDL_Delay(this->hidden->write_delay);
icculus@1532
    86
}
icculus@1532
    87
slouken@1895
    88
static void
icculus@3792
    89
DUMMYAUD_PlayDevice(_THIS)
icculus@1532
    90
{
slouken@1895
    91
    /* no-op...this is a null driver. */
icculus@1532
    92
}
icculus@1532
    93
slouken@1895
    94
static Uint8 *
icculus@3792
    95
DUMMYAUD_GetDeviceBuf(_THIS)
icculus@1532
    96
{
slouken@1895
    97
    return (this->hidden->mixbuf);
icculus@1532
    98
}
icculus@1532
    99
slouken@1895
   100
static void
icculus@3792
   101
DUMMYAUD_CloseDevice(_THIS)
icculus@1532
   102
{
slouken@1895
   103
    if (this->hidden->mixbuf != NULL) {
slouken@1895
   104
        SDL_FreeAudioMem(this->hidden->mixbuf);
slouken@1895
   105
        this->hidden->mixbuf = NULL;
slouken@1895
   106
    }
icculus@3784
   107
    SDL_free(this->hidden);
icculus@3784
   108
    this->hidden = NULL;
icculus@1532
   109
}
icculus@1532
   110
slouken@1895
   111
static int
icculus@3792
   112
DUMMYAUD_OpenDevice(_THIS, const char *devname, int iscapture)
icculus@1532
   113
{
slouken@1895
   114
    float bytes_per_sec = 0.0f;
icculus@1537
   115
icculus@3784
   116
    /* Initialize all variables that we clean on shutdown */
icculus@3784
   117
    this->hidden = (struct SDL_PrivateAudioData *)
icculus@3784
   118
                    SDL_malloc((sizeof *this->hidden));
icculus@3784
   119
    if (this->hidden == NULL) {
icculus@3784
   120
        SDL_OutOfMemory();
icculus@3784
   121
        return 0;
icculus@3784
   122
    }
icculus@3784
   123
    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
icculus@3784
   124
slouken@1895
   125
    /* Allocate mixing buffer */
icculus@3784
   126
    this->hidden->mixlen = this->spec.size;
slouken@1895
   127
    this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
slouken@1895
   128
    if (this->hidden->mixbuf == NULL) {
icculus@3792
   129
        DUMMYAUD_CloseDevice(this);
icculus@3784
   130
        return 0;
slouken@1895
   131
    }
icculus@3784
   132
    SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);
icculus@1532
   133
icculus@3784
   134
    bytes_per_sec = (float) (SDL_AUDIO_BITSIZE(this->spec.format) / 8) *
icculus@3784
   135
                             this->spec.channels * this->spec.freq;
icculus@1537
   136
slouken@1895
   137
    /*
slouken@1895
   138
     * We try to make this request more audio at the correct rate for
slouken@1895
   139
     *  a given audio spec, so timing stays fairly faithful.
slouken@1895
   140
     * Also, we have it not block at all for the first two calls, so
slouken@1895
   141
     *  it seems like we're filling two audio fragments right out of the
slouken@1895
   142
     *  gate, like other SDL drivers tend to do.
slouken@1895
   143
     */
slouken@1895
   144
    this->hidden->initial_calls = 2;
slouken@1895
   145
    this->hidden->write_delay =
icculus@3784
   146
        (Uint32) ((((float) this->spec.size) / bytes_per_sec) * 1000.0f);
icculus@1537
   147
slouken@1895
   148
    /* We're ready to rock and roll. :-) */
icculus@3784
   149
    return 1;
icculus@1532
   150
}
icculus@1532
   151
slouken@1895
   152
/* vi: set ts=4 sw=4 expandtab: */