-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathaudio.c
162 lines (143 loc) · 4.43 KB
/
audio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* SDLCL - SDL Compatibility Library
* Copyright (C) 2017 Alan Williams <mralert@gmail.com>
*
* Portions taken from SDL 1.2.15
* Copyright (C) 1997-2012 Sam Latinga <slouken@libsdl.org>
*
* 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
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "SDL2.h"
#include "audio.h"
#include "rwops.h"
DECLSPEC int SDLCALL SDL_AudioInit (const char *driver_name) {
return rSDL_AudioInit(driver_name);
}
DECLSPEC void SDLCALL SDL_AudioQuit (void) {
rSDL_AudioQuit();
}
typedef struct SDL1_AudioSpec {
int freq;
Uint16 format;
Uint8 channels;
Uint8 silence;
Uint16 samples;
Uint16 padding;
Uint32 size;
void (SDLCALL *callback)(void *userdata, Uint8 *stream, int len);
void *userdata;
} SDL1_AudioSpec;
typedef struct callback_data {
void (SDLCALL *callback)(void *userdata, Uint8 *stream, int len);
void *userdata;
} callback_data;
static callback_data cbdata;
static SDL_AudioSpec audio_spec;
static void SDLCALL callback (void *userdata, Uint8 *stream, int len) {
(void)userdata;
memset(stream, audio_spec.silence, len);
cbdata.callback(cbdata.userdata, stream, len);
}
DECLSPEC int SDLCALL SDL_OpenAudio (SDL1_AudioSpec *desired, SDL1_AudioSpec *obtained) {
SDL_AudioSpec desired2;
memset(&desired2, 0, sizeof(SDL_AudioSpec));
desired2.freq = desired->freq;
desired2.format = desired->format;
desired2.channels = desired->channels;
desired2.samples = desired->samples;
desired2.callback = callback;
desired2.userdata = NULL;
if (obtained) {
if (!rSDL_OpenAudio(&desired2, &audio_spec)) {
cbdata.callback = desired->callback;
cbdata.userdata = desired->userdata;
obtained->freq = audio_spec.freq;
obtained->format = audio_spec.format;
obtained->channels = audio_spec.channels;
obtained->samples = audio_spec.samples;
obtained->size = audio_spec.size;
obtained->silence = audio_spec.silence;
return 0;
}
} else {
if (!rSDL_OpenAudio(&desired2, NULL)) {
cbdata.callback = desired->callback;
cbdata.userdata = desired->userdata;
return 0;
}
}
return -1;
}
DECLSPEC void SDLCALL SDL_PauseAudio (int pause_on) {
rSDL_PauseAudio(pause_on);
}
typedef enum {
SDL1_AUDIO_STOPPED = 0,
SDL1_AUDIO_PLAYING,
SDL1_AUDIO_PAUSED
} SDL1_audiostatus;
DECLSPEC SDL1_audiostatus SDLCALL SDL_GetAudioStatus (void) {
switch (rSDL_GetAudioStatus()) {
case SDL_AUDIO_STOPPED: return SDL1_AUDIO_STOPPED;
case SDL_AUDIO_PLAYING: return SDL1_AUDIO_PLAYING;
case SDL_AUDIO_PAUSED: return SDL1_AUDIO_PAUSED;
default: return SDL1_AUDIO_STOPPED;
}
}
DECLSPEC void SDLCALL SDL_MixAudio (Uint8 *dst, Uint8 *src, Uint32 len, int volume) {
rSDL_MixAudio(dst, src, len, volume);
}
DECLSPEC void SDLCALL SDL_LockAudio (void) {
rSDL_LockAudio();
}
DECLSPEC void SDLCALL SDL_UnlockAudio (void) {
rSDL_UnlockAudio();
}
DECLSPEC void SDLCALL SDL_CloseAudio (void) {
rSDL_CloseAudio();
}
DECLSPEC char *SDLCALL SDL_AudioDriverName (char *namebuf, int maxlen) {
const char *name = rSDL_GetCurrentAudioDriver();
if (!name) return NULL;
strncpy(namebuf, name, maxlen);
namebuf[maxlen - 1] = 0;
return namebuf;
}
DECLSPEC SDL1_AudioSpec *SDLCALL SDL_LoadWAV_RW (SDL1_RWops *src, int freesrc, SDL1_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len) {
SDL_AudioSpec spec2, *ret;
SDL_RWops *src2 = SDLCL_RWFromSDL1(src);
if (!src2) {
if (freesrc && src) SDL1_RWclose(src);
return NULL;
}
ret = rSDL_LoadWAV_RW(src2, freesrc, &spec2, audio_buf, audio_len);
if (!freesrc) rSDL_FreeRW(src2);
if (ret) {
spec->freq = spec2.freq;
spec->format = spec2.format;
spec->channels = spec2.channels;
spec->samples = spec2.samples;
spec->size = spec2.size;
spec->silence = spec2.silence;
return spec;
}
return NULL;
}
DECLSPEC void SDLCALL SDL_FreeWAV(Uint8 *audio_buf) {
rSDL_FreeWAV(audio_buf);
}