This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_dummyaudio.c
152 lines (128 loc) · 4.42 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
static int DUMMYAUD_OpenAudio(_THIS, const char *devname, int iscapture);
41
42
43
44
45
46
static void DUMMYAUD_WaitAudio(_THIS);
static void DUMMYAUD_PlayAudio(_THIS);
static Uint8 *DUMMYAUD_GetAudioBuf(_THIS);
static void DUMMYAUD_CloseAudio(_THIS);
/* 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->OpenAudio = DUMMYAUD_OpenAudio;
impl->WaitAudio = DUMMYAUD_WaitAudio;
impl->PlayAudio = DUMMYAUD_PlayAudio;
impl->GetAudioBuf = DUMMYAUD_GetAudioBuf;
impl->CloseAudio = DUMMYAUD_CloseAudio;
68
69
return 1;
70
71
72
}
AudioBootStrap DUMMYAUD_bootstrap = {
73
DUMMYAUD_DRIVER_NAME, "SDL dummy audio driver",
74
DUMMYAUD_Available, DUMMYAUD_Init
75
76
77
};
/* This function waits until it is possible to write a full sound buffer */
78
79
static void
DUMMYAUD_WaitAudio(_THIS)
80
{
81
82
83
84
85
/* 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);
86
87
}
88
89
static void
DUMMYAUD_PlayAudio(_THIS)
90
{
91
/* no-op...this is a null driver. */
92
93
}
94
95
static Uint8 *
DUMMYAUD_GetAudioBuf(_THIS)
96
{
97
return (this->hidden->mixbuf);
98
99
}
100
101
static void
DUMMYAUD_CloseAudio(_THIS)
102
{
103
104
105
106
if (this->hidden->mixbuf != NULL) {
SDL_FreeAudioMem(this->hidden->mixbuf);
this->hidden->mixbuf = NULL;
}
107
108
SDL_free(this->hidden);
this->hidden = NULL;
109
110
}
111
static int
112
DUMMYAUD_OpenAudio(_THIS, const char *devname, int iscapture)
113
{
114
115
float bytes_per_sec = 0.0f;
116
117
118
119
120
121
122
123
124
/* 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));
125
/* Allocate mixing buffer */
126
this->hidden->mixlen = this->spec.size;
127
128
this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
if (this->hidden->mixbuf == NULL) {
129
130
DUMMYAUD_CloseAudio(this);
return 0;
131
}
132
SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);
133
134
135
bytes_per_sec = (float) (SDL_AUDIO_BITSIZE(this->spec.format) / 8) *
this->spec.channels * this->spec.freq;
136
137
138
139
140
141
142
143
144
145
/*
* 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 =
146
(Uint32) ((((float) this->spec.size) / bytes_per_sec) * 1000.0f);
147
148
/* We're ready to rock and roll. :-) */
149
return 1;
150
151
}
152
/* vi: set ts=4 sw=4 expandtab: */