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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
#include <stdlib.h>
#include <string.h>
#include "dsl.h"
#include "util.h"
#include "SDL.h"
#include "SDL_mixer.h"
extern volatile int MV_MixPage;
static int DSL_ErrorCode = DSL_Ok;
static int mixer_initialized;
static void ( *_CallBackFunc )( void );
static volatile char *_BufferStart;
static int _BufferSize;
static int _NumDivisions;
static int _SampleRate;
static int _remainder;
static Mix_Chunk *blank;
static unsigned char *blank_buf;
/*
possible todo ideas: cache sdl/sdl mixer error messages.
*/
char *DSL_ErrorString( int ErrorNumber )
{
char *ErrorString;
switch (ErrorNumber) {
case DSL_Warning:
case DSL_Error:
ErrorString = DSL_ErrorString(DSL_ErrorCode);
break;
case DSL_Ok:
ErrorString = "SDL Driver ok.";
break;
case DSL_SDLInitFailure:
ErrorString = "SDL Audio initialization failed.";
break;
case DSL_MixerActive:
ErrorString = "SDL Mixer already initialized.";
break;
case DSL_MixerInitFailure:
ErrorString = "SDL Mixer initialization failed.";
break;
default:
ErrorString = "Unknown SDL Driver error.";
break;
}
return ErrorString;
}
static void DSL_SetErrorCode(int ErrorCode)
{
DSL_ErrorCode = ErrorCode;
}
int DSL_Init( void )
{
DSL_SetErrorCode(DSL_Ok);
if (SDL_InitSubSystem(SDL_INIT_AUDIO|SDL_INIT_NOPARACHUTE) < 0) {
DSL_SetErrorCode(DSL_SDLInitFailure);
return DSL_Error;
}
return DSL_Ok;
}
void DSL_Shutdown( void )
{
DSL_StopPlayback();
}
static void mixer_callback(int chan, void *stream, int len, void *udata)
{
Uint8 *stptr;
Uint8 *fxptr;
int copysize;
/* len should equal _BufferSize, else this is screwed up */
stptr = (Uint8 *)stream;
if (_remainder > 0) {
copysize = min(len, _remainder);
fxptr = (Uint8 *)(&_BufferStart[MV_MixPage *
_BufferSize]);
memcpy(stptr, fxptr+(_BufferSize-_remainder), copysize);
len -= copysize;
_remainder -= copysize;
stptr += copysize;
}
while (len > 0) {
/* new buffer */
_CallBackFunc();
fxptr = (Uint8 *)(&_BufferStart[MV_MixPage *
_BufferSize]);
copysize = min(len, _BufferSize);
memcpy(stptr, fxptr, copysize);
len -= copysize;
stptr += copysize;
}
_remainder = len;
}
int DSL_BeginBufferedPlayback( char *BufferStart,
int BufferSize, int NumDivisions, unsigned SampleRate,
int MixMode, void ( *CallBackFunc )( void ) )
{
Uint16 format;
int channels;
int chunksize;
int blah;
if (mixer_initialized) {
DSL_SetErrorCode(DSL_MixerActive);
return DSL_Error;
}
_CallBackFunc = CallBackFunc;
_BufferStart = BufferStart;
_BufferSize = (BufferSize / NumDivisions);
_NumDivisions = NumDivisions;
_SampleRate = SampleRate;
_remainder = 0;
format = (MixMode & SIXTEEN_BIT) ? AUDIO_S16LSB : AUDIO_U8;
channels = (MixMode & STEREO) ? 2 : 1;
/*
I find 50ms to be ideal, at least with my hardware. This clamping mechanism
was added because it seems the above remainder handling isn't so nice --kode54
*/
chunksize = (5 * SampleRate) / 100;
blah = _BufferSize;
if (MixMode & SIXTEEN_BIT) blah >>= 1;
if (MixMode & STEREO) blah >>= 1;
if (chunksize % blah) chunksize += blah - (chunksize % blah);
if (Mix_OpenAudio(SampleRate, format, channels, chunksize) < 0) {
DSL_SetErrorCode(DSL_MixerInitFailure);
return DSL_Error;
}
/*
Mix_SetPostMix(mixer_callback, NULL);
*/
/* have to use a channel because postmix will overwrite the music... */
Mix_RegisterEffect(0, mixer_callback, NULL, NULL);
/* create a dummy sample just to allocate that channel */
blank_buf = (Uint8 *)malloc(4096);
memset(blank_buf, 0, 4096);
blank = Mix_QuickLoad_RAW(blank_buf, 4096);
Mix_PlayChannel(0, blank, -1);
mixer_initialized = 1;
return DSL_Ok;
}
void DSL_StopPlayback( void )
{
if (mixer_initialized) {
Mix_HaltChannel(0);
}
if (blank != NULL) {
Mix_FreeChunk(blank);
}
blank = NULL;
if (blank_buf != NULL) {
free(blank_buf);
}
blank_buf = NULL;
if (mixer_initialized) {
Mix_CloseAudio();
}
mixer_initialized = 0;
}
unsigned DSL_GetPlaybackRate( void )
{
return _SampleRate;
}
uint32_t DisableInterrupts( void )
{
return 0;
}
void RestoreInterrupts( uint32_t flags )
{
}
|