src/audio/dummy/SDL_dummyaudio.c
author Sam Lantinga
Mon, 10 Jul 2006 21:04:37 +0000
changeset 1895 c121d94672cb
parent 1538 b1f8c14f0df5
child 2049 5f6550e5184f
child 3784 37c9c4590689
permissions -rw-r--r--
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
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 */
slouken@1895
    40
static int DUMMYAUD_OpenAudio(_THIS, SDL_AudioSpec * spec);
icculus@1532
    41
static void DUMMYAUD_WaitAudio(_THIS);
icculus@1532
    42
static void DUMMYAUD_PlayAudio(_THIS);
icculus@1532
    43
static Uint8 *DUMMYAUD_GetAudioBuf(_THIS);
icculus@1532
    44
static void DUMMYAUD_CloseAudio(_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
{
slouken@1895
    50
    const char *envr = SDL_getenv("SDL_AUDIODRIVER");
slouken@1895
    51
    if (envr && (SDL_strcmp(envr, DUMMYAUD_DRIVER_NAME) == 0)) {
slouken@1895
    52
        return (1);
slouken@1895
    53
    }
slouken@1895
    54
    return (0);
icculus@1532
    55
}
icculus@1532
    56
slouken@1895
    57
static void
slouken@1895
    58
DUMMYAUD_DeleteDevice(SDL_AudioDevice * device)
icculus@1532
    59
{
slouken@1895
    60
    SDL_free(device->hidden);
slouken@1895
    61
    SDL_free(device);
icculus@1532
    62
}
icculus@1532
    63
slouken@1895
    64
static SDL_AudioDevice *
slouken@1895
    65
DUMMYAUD_CreateDevice(int devindex)
icculus@1532
    66
{
slouken@1895
    67
    SDL_AudioDevice *this;
icculus@1532
    68
slouken@1895
    69
    /* Initialize all variables that we clean on shutdown */
slouken@1895
    70
    this = (SDL_AudioDevice *) SDL_malloc(sizeof(SDL_AudioDevice));
slouken@1895
    71
    if (this) {
slouken@1895
    72
        SDL_memset(this, 0, (sizeof *this));
slouken@1895
    73
        this->hidden = (struct SDL_PrivateAudioData *)
slouken@1895
    74
            SDL_malloc((sizeof *this->hidden));
slouken@1895
    75
    }
slouken@1895
    76
    if ((this == NULL) || (this->hidden == NULL)) {
slouken@1895
    77
        SDL_OutOfMemory();
slouken@1895
    78
        if (this) {
slouken@1895
    79
            SDL_free(this);
slouken@1895
    80
        }
slouken@1895
    81
        return (0);
slouken@1895
    82
    }
slouken@1895
    83
    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
icculus@1532
    84
slouken@1895
    85
    /* Set the function pointers */
slouken@1895
    86
    this->OpenAudio = DUMMYAUD_OpenAudio;
slouken@1895
    87
    this->WaitAudio = DUMMYAUD_WaitAudio;
slouken@1895
    88
    this->PlayAudio = DUMMYAUD_PlayAudio;
slouken@1895
    89
    this->GetAudioBuf = DUMMYAUD_GetAudioBuf;
slouken@1895
    90
    this->CloseAudio = DUMMYAUD_CloseAudio;
icculus@1532
    91
slouken@1895
    92
    this->free = DUMMYAUD_DeleteDevice;
icculus@1532
    93
slouken@1895
    94
    return this;
icculus@1532
    95
}
icculus@1532
    96
icculus@1532
    97
AudioBootStrap DUMMYAUD_bootstrap = {
slouken@1895
    98
    DUMMYAUD_DRIVER_NAME, "SDL dummy audio driver",
slouken@1895
    99
    DUMMYAUD_Available, DUMMYAUD_CreateDevice
icculus@1532
   100
};
icculus@1532
   101
icculus@1532
   102
/* This function waits until it is possible to write a full sound buffer */
slouken@1895
   103
static void
slouken@1895
   104
DUMMYAUD_WaitAudio(_THIS)
icculus@1532
   105
{
slouken@1895
   106
    /* Don't block on first calls to simulate initial fragment filling. */
slouken@1895
   107
    if (this->hidden->initial_calls)
slouken@1895
   108
        this->hidden->initial_calls--;
slouken@1895
   109
    else
slouken@1895
   110
        SDL_Delay(this->hidden->write_delay);
icculus@1532
   111
}
icculus@1532
   112
slouken@1895
   113
static void
slouken@1895
   114
DUMMYAUD_PlayAudio(_THIS)
icculus@1532
   115
{
slouken@1895
   116
    /* no-op...this is a null driver. */
icculus@1532
   117
}
icculus@1532
   118
slouken@1895
   119
static Uint8 *
slouken@1895
   120
DUMMYAUD_GetAudioBuf(_THIS)
icculus@1532
   121
{
slouken@1895
   122
    return (this->hidden->mixbuf);
icculus@1532
   123
}
icculus@1532
   124
slouken@1895
   125
static void
slouken@1895
   126
DUMMYAUD_CloseAudio(_THIS)
icculus@1532
   127
{
slouken@1895
   128
    if (this->hidden->mixbuf != NULL) {
slouken@1895
   129
        SDL_FreeAudioMem(this->hidden->mixbuf);
slouken@1895
   130
        this->hidden->mixbuf = NULL;
slouken@1895
   131
    }
icculus@1532
   132
}
icculus@1532
   133
slouken@1895
   134
static int
slouken@1895
   135
DUMMYAUD_OpenAudio(_THIS, SDL_AudioSpec * spec)
icculus@1532
   136
{
slouken@1895
   137
    float bytes_per_sec = 0.0f;
icculus@1537
   138
slouken@1895
   139
    /* Allocate mixing buffer */
slouken@1895
   140
    this->hidden->mixlen = spec->size;
slouken@1895
   141
    this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
slouken@1895
   142
    if (this->hidden->mixbuf == NULL) {
slouken@1895
   143
        return (-1);
slouken@1895
   144
    }
slouken@1895
   145
    SDL_memset(this->hidden->mixbuf, spec->silence, spec->size);
icculus@1532
   146
slouken@1895
   147
    bytes_per_sec = (float) (((spec->format & 0xFF) / 8) *
slouken@1895
   148
                             spec->channels * spec->freq);
icculus@1537
   149
slouken@1895
   150
    /*
slouken@1895
   151
     * We try to make this request more audio at the correct rate for
slouken@1895
   152
     *  a given audio spec, so timing stays fairly faithful.
slouken@1895
   153
     * Also, we have it not block at all for the first two calls, so
slouken@1895
   154
     *  it seems like we're filling two audio fragments right out of the
slouken@1895
   155
     *  gate, like other SDL drivers tend to do.
slouken@1895
   156
     */
slouken@1895
   157
    this->hidden->initial_calls = 2;
slouken@1895
   158
    this->hidden->write_delay =
slouken@1895
   159
        (Uint32) ((((float) spec->size) / bytes_per_sec) * 1000.0f);
icculus@1537
   160
slouken@1895
   161
    /* We're ready to rock and roll. :-) */
slouken@1895
   162
    return (0);
icculus@1532
   163
}
icculus@1532
   164
slouken@1895
   165
/* vi: set ts=4 sw=4 expandtab: */