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