| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include <math.h> |
| 20 |
#include <SDL.h> |
| 21 |
|
| 22 |
#include "quakedef.h" |
| 23 |
|
| 24 |
#include "snd_main.h" |
| 25 |
|
| 26 |
|
| 27 |
static unsigned int sdlaudiotime = 0; |
| •28 |
static int audio_device = 0; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
static void Buffer_Callback (void *userdata, Uint8 *stream, int len) |
| 33 |
{ |
| 34 |
unsigned int factor, RequestedFrames, MaxFrames, FrameCount; |
| 35 |
unsigned int StartOffset, EndOffset; |
| 36 |
|
| 37 |
factor = snd_renderbuffer->format.channels * snd_renderbuffer->format.width; |
| 38 |
if ((unsigned int)len % factor != 0) |
| 39 |
Sys_Error("SDL sound: invalid buffer length passed to Buffer_Callback (%d bytes)\n", len); |
| 40 |
|
| 41 |
RequestedFrames = (unsigned int)len / factor; |
| 42 |
|
| 43 |
if (SndSys_LockRenderBuffer()) |
| 44 |
{ |
| 45 |
if (snd_usethreadedmixing) |
| 46 |
{ |
| 47 |
S_MixToBuffer(stream, RequestedFrames); |
| 48 |
if (snd_blocked) |
| 49 |
memset(stream, snd_renderbuffer->format.width == 1 ? 0x80 : 0, len); |
| 50 |
SndSys_UnlockRenderBuffer(); |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
MaxFrames = snd_renderbuffer->endframe - snd_renderbuffer->startframe; |
| 56 |
if (MaxFrames > RequestedFrames) |
| 57 |
FrameCount = RequestedFrames; |
| 58 |
else |
| 59 |
FrameCount = MaxFrames; |
| 60 |
StartOffset = snd_renderbuffer->startframe % snd_renderbuffer->maxframes; |
| 61 |
EndOffset = (snd_renderbuffer->startframe + FrameCount) % snd_renderbuffer->maxframes; |
| 62 |
if (StartOffset > EndOffset) |
| 63 |
{ |
| 64 |
unsigned int PartialLength1, PartialLength2; |
| 65 |
|
| 66 |
PartialLength1 = (snd_renderbuffer->maxframes - StartOffset) * factor; |
| 67 |
memcpy(stream, &snd_renderbuffer->ring[StartOffset * factor], PartialLength1); |
| 68 |
|
| 69 |
PartialLength2 = FrameCount * factor - PartialLength1; |
| 70 |
memcpy(&stream[PartialLength1], &snd_renderbuffer->ring[0], PartialLength2); |
| 71 |
} |
| 72 |
else |
| 73 |
memcpy(stream, &snd_renderbuffer->ring[StartOffset * factor], FrameCount * factor); |
| 74 |
|
| 75 |
snd_renderbuffer->startframe += FrameCount; |
| 76 |
|
| 77 |
if (FrameCount < RequestedFrames && developer_insane.integer && vid_activewindow) |
| 78 |
Con_DPrintf("SDL sound: %u sample frames missing\n", RequestedFrames - FrameCount); |
| 79 |
|
| 80 |
sdlaudiotime += RequestedFrames; |
| 81 |
|
| 82 |
SndSys_UnlockRenderBuffer(); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
qboolean SndSys_Init (const snd_format_t* requested, snd_format_t* suggested) |
| 96 |
{ |
| 97 |
unsigned int buffersize; |
| 98 |
SDL_AudioSpec wantspec; |
| 99 |
SDL_AudioSpec obtainspec; |
| 100 |
|
| 101 |
snd_threaded = false; |
| 102 |
|
| 103 |
Con_DPrint ("SndSys_Init: using the SDL module\n"); |
| 104 |
|
| 105 |
|
| 106 |
if( SDL_InitSubSystem( SDL_INIT_AUDIO ) ) { |
| 107 |
Con_Print( "Initializing the SDL Audio subsystem failed!\n" ); |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
buffersize = (unsigned int)ceil((double)requested->speed / 25.0); |
| 112 |
|
| 113 |
|
| 114 |
wantspec.callback = Buffer_Callback; |
| 115 |
wantspec.userdata = NULL; |
| 116 |
wantspec.freq = requested->speed; |
| 117 |
wantspec.format = ((requested->width == 1) ? AUDIO_U8 : AUDIO_S16SYS); |
| 118 |
wantspec.channels = requested->channels; |
| 119 |
wantspec.samples = CeilPowerOf2(buffersize); |
| 120 |
|
| 121 |
Con_Printf("Wanted audio Specification:\n" |
| 122 |
"\tChannels : %i\n" |
| 123 |
"\tFormat : 0x%X\n" |
| 124 |
"\tFrequency : %i\n" |
| 125 |
"\tSamples : %i\n", |
| 126 |
wantspec.channels, wantspec.format, wantspec.freq, wantspec.samples); |
| 127 |
|
| •128 |
if ((audio_device = SDL_OpenAudioDevice(NULL, 0, &wantspec, &obtainspec, 0)) == 0) |
| 129 |
{ |
| 130 |
Con_Printf( "Failed to open the audio device! (%s)\n", SDL_GetError() ); |
| 131 |
return false; |
| 132 |
} |
| 133 |
|
| 134 |
Con_Printf("Obtained audio specification:\n" |
| 135 |
"\tChannels : %i\n" |
| 136 |
"\tFormat : 0x%X\n" |
| 137 |
"\tFrequency : %i\n" |
| 138 |
"\tSamples : %i\n", |
| 139 |
obtainspec.channels, obtainspec.format, obtainspec.freq, obtainspec.samples); |
| 140 |
|
| 141 |
|
| 142 |
if (wantspec.freq != obtainspec.freq || |
| 143 |
wantspec.format != obtainspec.format || |
| 144 |
wantspec.channels != obtainspec.channels) |
| 145 |
{ |
| •146 |
SDL_CloseAudioDevice(audio_device); |
| 147 |
|
| 148 |
|
| 149 |
if (suggested != NULL) |
| 150 |
{ |
| 151 |
suggested->speed = obtainspec.freq; |
| 152 |
|
| 153 |
suggested->width = ((obtainspec.format == AUDIO_U8) ? 1 : 2); |
| 154 |
suggested->channels = obtainspec.channels; |
| 155 |
} |
| 156 |
|
| 157 |
return false; |
| 158 |
} |
| 159 |
|
| 160 |
snd_threaded = true; |
| 161 |
|
| 162 |
snd_renderbuffer = Snd_CreateRingBuffer(requested, 0, NULL); |
| 163 |
if (snd_channellayout.integer == SND_CHANNELLAYOUT_AUTO) |
| 164 |
Cvar_SetValueQuick (&snd_channellayout, SND_CHANNELLAYOUT_STANDARD); |
| 165 |
|
| 166 |
sdlaudiotime = 0; |
| •167 |
SDL_PauseAudioDevice(audio_device, 0); |
| 168 |
|
| 169 |
return true; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
void SndSys_Shutdown(void) |
| 181 |
{ |
| •182 |
if (audio_device > 0) { |
| 183 |
SDL_CloseAudioDevice(audio_device); |
| 184 |
audio_device = 0; |
| 185 |
} |
| 186 |
if (snd_renderbuffer != NULL) |
| 187 |
{ |
| 188 |
Mem_Free(snd_renderbuffer->ring); |
| 189 |
Mem_Free(snd_renderbuffer); |
| 190 |
snd_renderbuffer = NULL; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
void SndSys_Submit (void) |
| 203 |
{ |
| 204 |
|
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
unsigned int SndSys_GetSoundTime (void) |
| 216 |
{ |
| 217 |
return sdlaudiotime; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
qboolean SndSys_LockRenderBuffer (void) |
| 229 |
{ |
| •230 |
SDL_LockAudioDevice(audio_device); |
| 231 |
return true; |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
void SndSys_UnlockRenderBuffer (void) |
| 243 |
{ |
| •244 |
SDL_UnlockAudioDevice(audio_device); |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
void SndSys_SendKeyEvents(void) |
| 255 |
{ |
| 256 |
|
| 257 |
} |