| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#include <stdlib.h> |
| 24 |
#include <stdio.h> |
| 25 |
|
| 26 |
#ifdef USE_INTERNAL_SDL_HEADERS |
| 27 |
# include "SDL.h" |
| 28 |
#else |
| 29 |
# include <SDL.h> |
| 30 |
#endif |
| 31 |
|
| 32 |
#include "../qcommon/q_shared.h" |
| 33 |
#include "../client/snd_local.h" |
| 34 |
#include "../client/client.h" |
| 35 |
|
| 36 |
qboolean snd_inited = qfalse; |
| 37 |
|
| 38 |
cvar_t *s_sdlBits; |
| 39 |
cvar_t *s_sdlSpeed; |
| 40 |
cvar_t *s_sdlChannels; |
| 41 |
cvar_t *s_sdlDevSamps; |
| 42 |
cvar_t *s_sdlMixSamps; |
| 43 |
|
| 44 |
|
| 45 |
static int dmapos = 0; |
| 46 |
static int dmasize = 0; |
| 47 |
|
| 48 |
static SDL_AudioDeviceID sdlPlaybackDevice; |
| 49 |
|
| 50 |
#if defined USE_VOIP && SDL_VERSION_ATLEAST( 2, 0, 5 ) |
| 51 |
#define USE_SDL_AUDIO_CAPTURE |
| 52 |
|
| 53 |
static SDL_AudioDeviceID sdlCaptureDevice; |
| 54 |
static cvar_t *s_sdlCapture; |
| 55 |
static float sdlMasterGain = 1.0f; |
| 56 |
#endif |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
static void SNDDMA_AudioCallback(void *userdata, Uint8 *stream, int len) |
| 65 |
{ |
| 66 |
int pos = (dmapos * (dma.samplebits/8)); |
| 67 |
if (pos >= dmasize) |
| 68 |
dmapos = pos = 0; |
| 69 |
|
| 70 |
if (!snd_inited) |
| 71 |
{ |
| 72 |
memset(stream, '\0', len); |
| 73 |
return; |
| 74 |
} |
| 75 |
else |
| 76 |
{ |
| 77 |
int tobufend = dmasize - pos; |
| 78 |
int len1 = len; |
| 79 |
int len2 = 0; |
| 80 |
|
| 81 |
if (len1 > tobufend) |
| 82 |
{ |
| 83 |
len1 = tobufend; |
| 84 |
len2 = len - len1; |
| 85 |
} |
| 86 |
memcpy(stream, dma.buffer + pos, len1); |
| 87 |
if (len2 <= 0) |
| 88 |
dmapos += (len1 / (dma.samplebits/8)); |
| 89 |
else |
| 90 |
{ |
| 91 |
memcpy(stream+len1, dma.buffer, len2); |
| 92 |
dmapos = (len2 / (dma.samplebits/8)); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
if (dmapos >= dmasize) |
| 97 |
dmapos = 0; |
| 98 |
|
| 99 |
#ifdef USE_SDL_AUDIO_CAPTURE |
| 100 |
if (sdlMasterGain != 1.0f) |
| 101 |
{ |
| 102 |
int i; |
| 103 |
if (dma.isfloat && (dma.samplebits == 32)) |
| 104 |
{ |
| 105 |
float *ptr = (float *) stream; |
| 106 |
len /= sizeof (*ptr); |
| 107 |
for (i = 0; i < len; i++, ptr++) |
| 108 |
{ |
| 109 |
*ptr *= sdlMasterGain; |
| 110 |
} |
| 111 |
} |
| 112 |
else if (dma.samplebits == 16) |
| 113 |
{ |
| 114 |
Sint16 *ptr = (Sint16 *) stream; |
| 115 |
len /= sizeof (*ptr); |
| 116 |
for (i = 0; i < len; i++, ptr++) |
| 117 |
{ |
| 118 |
*ptr = (Sint16) (((float) *ptr) * sdlMasterGain); |
| 119 |
} |
| 120 |
} |
| 121 |
else if (dma.samplebits == 8) |
| 122 |
{ |
| 123 |
Uint8 *ptr = (Uint8 *) stream; |
| 124 |
len /= sizeof (*ptr); |
| 125 |
for (i = 0; i < len; i++, ptr++) |
| 126 |
{ |
| 127 |
*ptr = (Uint8) (((float) *ptr) * sdlMasterGain); |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
#endif |
| 132 |
} |
| 133 |
|
| 134 |
static struct |
| 135 |
{ |
| 136 |
Uint16 enumFormat; |
| 137 |
char *stringFormat; |
| 138 |
} formatToStringTable[ ] = |
| 139 |
{ |
| 140 |
{ AUDIO_U8, "AUDIO_U8" }, |
| 141 |
{ AUDIO_S8, "AUDIO_S8" }, |
| 142 |
{ AUDIO_U16LSB, "AUDIO_U16LSB" }, |
| 143 |
{ AUDIO_S16LSB, "AUDIO_S16LSB" }, |
| 144 |
{ AUDIO_U16MSB, "AUDIO_U16MSB" }, |
| 145 |
{ AUDIO_S16MSB, "AUDIO_S16MSB" }, |
| 146 |
{ AUDIO_F32LSB, "AUDIO_F32LSB" }, |
| 147 |
{ AUDIO_F32MSB, "AUDIO_F32MSB" } |
| 148 |
}; |
| 149 |
|
| 150 |
static int formatToStringTableSize = ARRAY_LEN( formatToStringTable ); |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
static void SNDDMA_PrintAudiospec(const char *str, const SDL_AudioSpec *spec) |
| 158 |
{ |
| 159 |
int i; |
| 160 |
char *fmt = NULL; |
| 161 |
|
| 162 |
Com_Printf("%s:\n", str); |
| 163 |
|
| 164 |
for( i = 0; i < formatToStringTableSize; i++ ) { |
| 165 |
if( spec->format == formatToStringTable[ i ].enumFormat ) { |
| 166 |
fmt = formatToStringTable[ i ].stringFormat; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
if( fmt ) { |
| 171 |
Com_Printf( " Format: %s\n", fmt ); |
| 172 |
} else { |
| 173 |
Com_Printf( " Format: " S_COLOR_RED "UNKNOWN\n"); |
| 174 |
} |
| 175 |
|
| 176 |
Com_Printf( " Freq: %d\n", (int) spec->freq ); |
| 177 |
Com_Printf( " Samples: %d\n", (int) spec->samples ); |
| 178 |
Com_Printf( " Channels: %d\n", (int) spec->channels ); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
qboolean SNDDMA_Init(void) |
| 187 |
{ |
| 188 |
SDL_AudioSpec desired; |
| 189 |
SDL_AudioSpec obtained; |
| 190 |
int tmp; |
| 191 |
|
| 192 |
if (snd_inited) |
| 193 |
return qtrue; |
| 194 |
|
| 195 |
if (!s_sdlBits) { |
| 196 |
s_sdlBits = Cvar_Get("s_sdlBits", "16", CVAR_ARCHIVE); |
| 197 |
s_sdlSpeed = Cvar_Get("s_sdlSpeed", "0", CVAR_ARCHIVE); |
| 198 |
s_sdlChannels = Cvar_Get("s_sdlChannels", "2", CVAR_ARCHIVE); |
| 199 |
s_sdlDevSamps = Cvar_Get("s_sdlDevSamps", "0", CVAR_ARCHIVE); |
| 200 |
s_sdlMixSamps = Cvar_Get("s_sdlMixSamps", "0", CVAR_ARCHIVE); |
| 201 |
} |
| 202 |
|
| 203 |
Com_Printf( "SDL_Init( SDL_INIT_AUDIO )... " ); |
| 204 |
|
| 205 |
if (SDL_Init(SDL_INIT_AUDIO) != 0) |
| 206 |
{ |
| 207 |
Com_Printf( "FAILED (%s)\n", SDL_GetError( ) ); |
| 208 |
return qfalse; |
| 209 |
} |
| 210 |
|
| 211 |
Com_Printf( "OK\n" ); |
| 212 |
|
| 213 |
Com_Printf( "SDL audio driver is \"%s\".\n", SDL_GetCurrentAudioDriver( ) ); |
| 214 |
|
| 215 |
memset(&desired, '\0', sizeof (desired)); |
| 216 |
memset(&obtained, '\0', sizeof (obtained)); |
| 217 |
|
| 218 |
tmp = ((int) s_sdlBits->value); |
| 219 |
if ((tmp != 16) && (tmp != 8)) |
| 220 |
tmp = 16; |
| 221 |
|
| 222 |
desired.freq = (int) s_sdlSpeed->value; |
| 223 |
if(!desired.freq) desired.freq = 22050; |
| 224 |
desired.format = ((tmp == 16) ? AUDIO_S16SYS : AUDIO_U8); |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
if (s_sdlDevSamps->value) |
| 229 |
desired.samples = s_sdlDevSamps->value; |
| 230 |
else |
| 231 |
{ |
| 232 |
|
| 233 |
if (desired.freq <= 11025) |
| 234 |
desired.samples = 256; |
| 235 |
else if (desired.freq <= 22050) |
| 236 |
desired.samples = 512; |
| 237 |
else if (desired.freq <= 44100) |
| 238 |
desired.samples = 1024; |
| 239 |
else |
| 240 |
desired.samples = 2048; |
| 241 |
} |
| 242 |
|
| 243 |
desired.channels = (int) s_sdlChannels->value; |
| 244 |
desired.callback = SNDDMA_AudioCallback; |
| 245 |
|
| 246 |
sdlPlaybackDevice = SDL_OpenAudioDevice(NULL, SDL_FALSE, &desired, &obtained, SDL_AUDIO_ALLOW_ANY_CHANGE); |
| 247 |
if (sdlPlaybackDevice == 0) |
| 248 |
{ |
| 249 |
Com_Printf("SDL_OpenAudioDevice() failed: %s\n", SDL_GetError()); |
| 250 |
SDL_QuitSubSystem(SDL_INIT_AUDIO); |
| 251 |
return qfalse; |
| 252 |
} |
| 253 |
|
| 254 |
SNDDMA_PrintAudiospec("SDL_AudioSpec", &obtained); |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
tmp = s_sdlMixSamps->value; |
| 264 |
if (!tmp) |
| 265 |
tmp = (obtained.samples * obtained.channels) * 10; |
| 266 |
|
| 267 |
|
| 268 |
tmp -= tmp % obtained.channels; |
| 269 |
|
| 270 |
dmapos = 0; |
| 271 |
dma.samplebits = SDL_AUDIO_BITSIZE(obtained.format); |
| 272 |
dma.isfloat = SDL_AUDIO_ISFLOAT(obtained.format); |
| 273 |
dma.channels = obtained.channels; |
| 274 |
dma.samples = tmp; |
| 275 |
dma.fullsamples = dma.samples / dma.channels; |
| 276 |
dma.submission_chunk = 1; |
| 277 |
dma.speed = obtained.freq; |
| 278 |
dmasize = (dma.samples * (dma.samplebits/8)); |
| 279 |
dma.buffer = calloc(1, dmasize); |
| 280 |
|
| 281 |
#ifdef USE_SDL_AUDIO_CAPTURE |
| 282 |
|
| 283 |
s_sdlCapture = Cvar_Get( "s_sdlCapture", "1", CVAR_ARCHIVE | CVAR_LATCH ); |
| 284 |
if (!s_sdlCapture->integer) |
| 285 |
{ |
| 286 |
Com_Printf("SDL audio capture support disabled by user ('+set s_sdlCapture 1' to enable)\n"); |
| 287 |
} |
| 288 |
#if USE_MUMBLE |
| 289 |
else if (cl_useMumble->integer) |
| 290 |
{ |
| 291 |
Com_Printf("SDL audio capture support disabled for Mumble support\n"); |
| 292 |
} |
| 293 |
#endif |
| 294 |
else |
| 295 |
{ |
| 296 |
|
| 297 |
SDL_AudioSpec spec; |
| 298 |
SDL_zero(spec); |
| 299 |
spec.freq = 48000; |
| 300 |
spec.format = AUDIO_S16SYS; |
| 301 |
spec.channels = 1; |
| 302 |
spec.samples = VOIP_MAX_PACKET_SAMPLES * 4; |
| 303 |
sdlCaptureDevice = SDL_OpenAudioDevice(NULL, SDL_TRUE, &spec, NULL, 0); |
| 304 |
Com_Printf( "SDL capture device %s.\n", |
| 305 |
(sdlCaptureDevice == 0) ? "failed to open" : "opened"); |
| 306 |
} |
| 307 |
|
| 308 |
sdlMasterGain = 1.0f; |
| 309 |
#endif |
| 310 |
|
| 311 |
Com_Printf("Starting SDL audio callback...\n"); |
| 312 |
SDL_PauseAudioDevice(sdlPlaybackDevice, 0); |
| 313 |
|
| 314 |
|
| 315 |
Com_Printf("SDL audio initialized.\n"); |
| 316 |
snd_inited = qtrue; |
| 317 |
return qtrue; |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
int SNDDMA_GetDMAPos(void) |
| 326 |
{ |
| 327 |
return dmapos; |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
void SNDDMA_Shutdown(void) |
| 336 |
{ |
| 337 |
if (sdlPlaybackDevice != 0) |
| 338 |
{ |
| 339 |
Com_Printf("Closing SDL audio playback device...\n"); |
| 340 |
SDL_CloseAudioDevice(sdlPlaybackDevice); |
| 341 |
Com_Printf("SDL audio playback device closed.\n"); |
| 342 |
sdlPlaybackDevice = 0; |
| 343 |
} |
| 344 |
|
| 345 |
#ifdef USE_SDL_AUDIO_CAPTURE |
| 346 |
if (sdlCaptureDevice) |
| 347 |
{ |
| 348 |
Com_Printf("Closing SDL audio capture device...\n"); |
| 349 |
SDL_CloseAudioDevice(sdlCaptureDevice); |
| 350 |
Com_Printf("SDL audio capture device closed.\n"); |
| 351 |
sdlCaptureDevice = 0; |
| 352 |
} |
| 353 |
#endif |
| 354 |
|
| 355 |
SDL_QuitSubSystem(SDL_INIT_AUDIO); |
| 356 |
free(dma.buffer); |
| 357 |
dma.buffer = NULL; |
| 358 |
dmapos = dmasize = 0; |
| 359 |
snd_inited = qfalse; |
| 360 |
Com_Printf("SDL audio shut down.\n"); |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
void SNDDMA_Submit(void) |
| 371 |
{ |
| 372 |
SDL_UnlockAudioDevice(sdlPlaybackDevice); |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
void SNDDMA_BeginPainting (void) |
| 381 |
{ |
| 382 |
SDL_LockAudioDevice(sdlPlaybackDevice); |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
#ifdef USE_VOIP |
| 387 |
void SNDDMA_StartCapture(void) |
| 388 |
{ |
| 389 |
#ifdef USE_SDL_AUDIO_CAPTURE |
| 390 |
if (sdlCaptureDevice) |
| 391 |
{ |
| 392 |
SDL_ClearQueuedAudio(sdlCaptureDevice); |
| 393 |
SDL_PauseAudioDevice(sdlCaptureDevice, 0); |
| 394 |
} |
| 395 |
#endif |
| 396 |
} |
| 397 |
|
| 398 |
int SNDDMA_AvailableCaptureSamples(void) |
| 399 |
{ |
| 400 |
#ifdef USE_SDL_AUDIO_CAPTURE |
| 401 |
|
| 402 |
return sdlCaptureDevice ? (SDL_GetQueuedAudioSize(sdlCaptureDevice) / 2) : 0; |
| 403 |
#else |
| 404 |
return 0; |
| 405 |
#endif |
| 406 |
} |
| 407 |
|
| 408 |
void SNDDMA_Capture(int samples, byte *data) |
| 409 |
{ |
| 410 |
#ifdef USE_SDL_AUDIO_CAPTURE |
| 411 |
|
| 412 |
if (sdlCaptureDevice) |
| 413 |
{ |
| 414 |
SDL_DequeueAudio(sdlCaptureDevice, data, samples * 2); |
| 415 |
} |
| 416 |
else |
| 417 |
#endif |
| 418 |
{ |
| 419 |
SDL_memset(data, '\0', samples * 2); |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
void SNDDMA_StopCapture(void) |
| 424 |
{ |
| 425 |
#ifdef USE_SDL_AUDIO_CAPTURE |
| 426 |
if (sdlCaptureDevice) |
| 427 |
{ |
| 428 |
SDL_PauseAudioDevice(sdlCaptureDevice, 1); |
| 429 |
} |
| 430 |
#endif |
| 431 |
} |
| 432 |
|
| 433 |
void SNDDMA_MasterGain( float val ) |
| 434 |
{ |
| 435 |
#ifdef USE_SDL_AUDIO_CAPTURE |
| 436 |
sdlMasterGain = val; |
| 437 |
#endif |
| 438 |
} |
| 439 |
#endif |
| 440 |
|