| 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 |
#include "snd_local.h" |
| 33 |
#include "snd_codec.h" |
| 34 |
#include "client.h" |
| 35 |
|
| 36 |
void S_Update_( void ); |
| 37 |
void S_Base_StopAllSounds(void); |
| 38 |
void S_Base_StopBackgroundTrack( void ); |
| 39 |
|
| 40 |
snd_stream_t *s_backgroundStream = NULL; |
| 41 |
static char s_backgroundLoop[MAX_QPATH]; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
#define SOUND_FULLVOLUME 80 |
| 51 |
|
| 52 |
#define SOUND_ATTENUATE 0.0008f |
| 53 |
|
| 54 |
channel_t s_channels[MAX_CHANNELS]; |
| 55 |
channel_t loop_channels[MAX_CHANNELS]; |
| 56 |
int numLoopChannels; |
| 57 |
|
| 58 |
static int s_soundStarted; |
| 59 |
static qboolean s_soundMuted; |
| 60 |
|
| 61 |
dma_t dma; |
| 62 |
|
| 63 |
static int listener_number; |
| 64 |
static vec3_t listener_origin; |
| 65 |
static vec3_t listener_axis[3]; |
| 66 |
|
| 67 |
int s_soundtime; |
| 68 |
int s_paintedtime; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
#define MAX_SFX 4096 |
| 73 |
sfx_t s_knownSfx[MAX_SFX]; |
| 74 |
int s_numSfx = 0; |
| 75 |
|
| 76 |
#define LOOP_HASH 128 |
| 77 |
static sfx_t *sfxHash[LOOP_HASH]; |
| 78 |
|
| 79 |
cvar_t *s_testsound; |
| 80 |
cvar_t *s_show; |
| 81 |
cvar_t *s_mixahead; |
| 82 |
cvar_t *s_mixPreStep; |
| 83 |
|
| 84 |
static loopSound_t loopSounds[MAX_GENTITIES]; |
| 85 |
static channel_t *freelist = NULL; |
| 86 |
|
| 87 |
int s_rawend[MAX_RAW_STREAMS]; |
| 88 |
portable_samplepair_t s_rawsamples[MAX_RAW_STREAMS][MAX_RAW_SAMPLES]; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
void S_Base_SoundInfo(void) { |
| 97 |
Com_Printf("----- Sound Info -----\n" ); |
| 98 |
if (!s_soundStarted) { |
| 99 |
Com_Printf ("sound system not started\n"); |
| 100 |
} else { |
| 101 |
Com_Printf("%5d channels\n", dma.channels); |
| 102 |
Com_Printf("%5d samples\n", dma.samples); |
| 103 |
Com_Printf("%5d samplebits (%s)\n", dma.samplebits, dma.isfloat ? "float" : "int"); |
| 104 |
Com_Printf("%5d submission_chunk\n", dma.submission_chunk); |
| 105 |
Com_Printf("%5d speed\n", dma.speed); |
| 106 |
Com_Printf("%p dma buffer\n", dma.buffer); |
| 107 |
if ( s_backgroundStream ) { |
| 108 |
Com_Printf("Background file: %s\n", s_backgroundLoop ); |
| 109 |
} else { |
| 110 |
Com_Printf("No background file.\n" ); |
| 111 |
} |
| 112 |
|
| 113 |
} |
| 114 |
Com_Printf("----------------------\n" ); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
#ifdef USE_VOIP |
| 119 |
static |
| 120 |
void S_Base_StartCapture( void ) |
| 121 |
{ |
| 122 |
SNDDMA_StartCapture(); |
| 123 |
} |
| 124 |
|
| 125 |
static |
| 126 |
int S_Base_AvailableCaptureSamples( void ) |
| 127 |
{ |
| 128 |
return SNDDMA_AvailableCaptureSamples(); |
| 129 |
} |
| 130 |
|
| 131 |
static |
| 132 |
void S_Base_Capture( int samples, byte *data ) |
| 133 |
{ |
| 134 |
SNDDMA_Capture(samples, data); |
| 135 |
} |
| 136 |
|
| 137 |
static |
| 138 |
void S_Base_StopCapture( void ) |
| 139 |
{ |
| 140 |
SNDDMA_StopCapture(); |
| 141 |
} |
| 142 |
|
| 143 |
static |
| 144 |
void S_Base_MasterGain( float val ) |
| 145 |
{ |
| 146 |
SNDDMA_MasterGain(val); |
| 147 |
} |
| 148 |
#endif |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
void S_Base_SoundList( void ) { |
| 158 |
int i; |
| 159 |
sfx_t *sfx; |
| 160 |
int size, total; |
| 161 |
char type[4][16]; |
| 162 |
char mem[2][16]; |
| 163 |
|
| 164 |
strcpy(type[0], "16bit"); |
| 165 |
strcpy(type[1], "adpcm"); |
| 166 |
strcpy(type[2], "daub4"); |
| 167 |
strcpy(type[3], "mulaw"); |
| 168 |
strcpy(mem[0], "paged out"); |
| 169 |
strcpy(mem[1], "resident "); |
| 170 |
total = 0; |
| 171 |
for (sfx=s_knownSfx, i=0 ; i<s_numSfx ; i++, sfx++) { |
| 172 |
size = sfx->soundLength; |
| 173 |
total += size; |
| 174 |
Com_Printf("%6i[%s] : %s[%s]\n", size, type[sfx->soundCompressionMethod], |
| 175 |
sfx->soundName, mem[sfx->inMemory] ); |
| 176 |
} |
| 177 |
Com_Printf ("Total resident: %i\n", total); |
| 178 |
S_DisplayFreeMemory(); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
void S_ChannelFree(channel_t *v) { |
| 184 |
v->thesfx = NULL; |
| 185 |
*(channel_t **)v = freelist; |
| 186 |
freelist = (channel_t*)v; |
| 187 |
} |
| 188 |
|
| 189 |
channel_t* S_ChannelMalloc( void ) { |
| 190 |
channel_t *v; |
| 191 |
if (freelist == NULL) { |
| 192 |
return NULL; |
| 193 |
} |
| 194 |
v = freelist; |
| 195 |
freelist = *(channel_t **)freelist; |
| 196 |
v->allocTime = Com_Milliseconds(); |
| 197 |
return v; |
| 198 |
} |
| 199 |
|
| 200 |
void S_ChannelSetup( void ) { |
| 201 |
channel_t *p, *q; |
| 202 |
|
| 203 |
|
| 204 |
Com_Memset( s_channels, 0, sizeof( s_channels ) ); |
| 205 |
|
| 206 |
p = s_channels;; |
| 207 |
q = p + MAX_CHANNELS; |
| 208 |
while (--q > p) { |
| 209 |
*(channel_t **)q = q-1; |
| 210 |
} |
| 211 |
|
| 212 |
*(channel_t **)q = NULL; |
| 213 |
freelist = p + MAX_CHANNELS - 1; |
| 214 |
Com_DPrintf("Channel memory manager started\n"); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
static long S_HashSFXName(const char *name) { |
| 229 |
int i; |
| 230 |
long hash; |
| 231 |
char letter; |
| 232 |
|
| 233 |
hash = 0; |
| 234 |
i = 0; |
| 235 |
while (name[i] != '\0') { |
| 236 |
letter = tolower(name[i]); |
| 237 |
if (letter =='.') break; |
| 238 |
if (letter =='\\') letter = '/'; |
| 239 |
hash+=(long)(letter)*(i+119); |
| 240 |
i++; |
| 241 |
} |
| 242 |
hash &= (LOOP_HASH-1); |
| 243 |
return hash; |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
static sfx_t *S_FindName( const char *name ) { |
| 254 |
int i; |
| 255 |
int hash; |
| 256 |
|
| 257 |
sfx_t *sfx; |
| 258 |
|
| 259 |
if (!name) { |
| 260 |
Com_Error(ERR_FATAL, "Sound name is NULL"); |
| 261 |
} |
| 262 |
|
| 263 |
if (!name[0]) { |
| 264 |
Com_Printf( S_COLOR_YELLOW "WARNING: Sound name is empty\n" ); |
| 265 |
return NULL; |
| 266 |
} |
| 267 |
|
| 268 |
if (strlen(name) >= MAX_QPATH) { |
| 269 |
Com_Printf( S_COLOR_YELLOW "WARNING: Sound name is too long: %s\n", name ); |
| 270 |
return NULL; |
| 271 |
} |
| 272 |
|
| 273 |
if (name[0] == '*') { |
| 274 |
Com_Printf( S_COLOR_YELLOW "WARNING: Tried to load player sound directly: %s\n", name ); |
| 275 |
return NULL; |
| 276 |
} |
| 277 |
|
| 278 |
hash = S_HashSFXName(name); |
| 279 |
|
| 280 |
sfx = sfxHash[hash]; |
| 281 |
|
| 282 |
while (sfx) { |
| 283 |
if (!Q_stricmp(sfx->soundName, name) ) { |
| 284 |
return sfx; |
| 285 |
} |
| 286 |
sfx = sfx->next; |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
for (i=0 ; i < s_numSfx ; i++) { |
| 291 |
if (!s_knownSfx[i].soundName[0]) { |
| 292 |
break; |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
if (i == s_numSfx) { |
| 297 |
if (s_numSfx == MAX_SFX) { |
| 298 |
Com_Error (ERR_FATAL, "S_FindName: out of sfx_t"); |
| 299 |
} |
| 300 |
s_numSfx++; |
| 301 |
} |
| 302 |
|
| 303 |
sfx = &s_knownSfx[i]; |
| 304 |
Com_Memset (sfx, 0, sizeof(*sfx)); |
| 305 |
strcpy (sfx->soundName, name); |
| 306 |
|
| 307 |
sfx->next = sfxHash[hash]; |
| 308 |
sfxHash[hash] = sfx; |
| 309 |
|
| 310 |
return sfx; |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
void S_DefaultSound( sfx_t *sfx ) { |
| 319 |
|
| 320 |
int i; |
| 321 |
|
| 322 |
sfx->soundLength = 512; |
| 323 |
sfx->soundData = SND_malloc(); |
| 324 |
sfx->soundData->next = NULL; |
| 325 |
|
| 326 |
|
| 327 |
for ( i = 0 ; i < sfx->soundLength ; i++ ) { |
| 328 |
sfx->soundData->sndChunk[i] = i; |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
void S_Base_DisableSounds( void ) { |
| 342 |
S_Base_StopAllSounds(); |
| 343 |
s_soundMuted = qtrue; |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
sfxHandle_t S_Base_RegisterSound( const char *name, qboolean compressed ) { |
| 354 |
sfx_t *sfx; |
| 355 |
|
| 356 |
compressed = qfalse; |
| 357 |
if (!s_soundStarted) { |
| 358 |
return 0; |
| 359 |
} |
| 360 |
|
| 361 |
sfx = S_FindName( name ); |
| 362 |
if ( !sfx ) { |
| 363 |
return 0; |
| 364 |
} |
| 365 |
|
| 366 |
if ( sfx->soundData ) { |
| 367 |
if ( sfx->defaultSound ) { |
| 368 |
Com_Printf( S_COLOR_YELLOW "WARNING: could not find %s - using default\n", sfx->soundName ); |
| 369 |
return 0; |
| 370 |
} |
| 371 |
return sfx - s_knownSfx; |
| 372 |
} |
| 373 |
|
| 374 |
sfx->inMemory = qfalse; |
| 375 |
sfx->soundCompressed = compressed; |
| 376 |
|
| 377 |
S_memoryLoad(sfx); |
| 378 |
|
| 379 |
if ( sfx->defaultSound ) { |
| 380 |
Com_Printf( S_COLOR_YELLOW "WARNING: could not find %s - using default\n", sfx->soundName ); |
| 381 |
return 0; |
| 382 |
} |
| 383 |
|
| 384 |
return sfx - s_knownSfx; |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
void S_Base_BeginRegistration( void ) { |
| 394 |
s_soundMuted = qfalse; |
| 395 |
|
| 396 |
if (s_numSfx == 0) { |
| 397 |
SND_setup(); |
| 398 |
|
| 399 |
Com_Memset(s_knownSfx, '\0', sizeof(s_knownSfx)); |
| 400 |
Com_Memset(sfxHash, '\0', sizeof(sfx_t *) * LOOP_HASH); |
| 401 |
|
| 402 |
S_Base_RegisterSound("sound/feedback/hit.wav", qfalse); |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
void S_memoryLoad(sfx_t *sfx) { |
| 407 |
|
| 408 |
if ( !S_LoadSound ( sfx ) ) { |
| 409 |
|
| 410 |
sfx->defaultSound = qtrue; |
| 411 |
} |
| 412 |
sfx->inMemory = qtrue; |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
void S_SpatializeOrigin (vec3_t origin, int master_vol, int *left_vol, int *right_vol) |
| 425 |
{ |
| 426 |
vec_t dot; |
| 427 |
vec_t dist; |
| 428 |
vec_t lscale, rscale, scale; |
| 429 |
vec3_t source_vec; |
| 430 |
vec3_t vec; |
| 431 |
|
| 432 |
const float dist_mult = SOUND_ATTENUATE; |
| 433 |
|
| 434 |
|
| 435 |
VectorSubtract(origin, listener_origin, source_vec); |
| 436 |
|
| 437 |
dist = VectorNormalize(source_vec); |
| 438 |
dist -= SOUND_FULLVOLUME; |
| 439 |
if (dist < 0) |
| 440 |
dist = 0; |
| 441 |
dist *= dist_mult; |
| 442 |
|
| 443 |
VectorRotate( source_vec, listener_axis, vec ); |
| 444 |
|
| 445 |
dot = -vec[1]; |
| 446 |
|
| 447 |
if (dma.channels == 1) |
| 448 |
{ |
| 449 |
rscale = 1.0; |
| 450 |
lscale = 1.0; |
| 451 |
} |
| 452 |
else |
| 453 |
{ |
| 454 |
rscale = 0.5 * (1.0 + dot); |
| 455 |
lscale = 0.5 * (1.0 - dot); |
| 456 |
if ( rscale < 0 ) { |
| 457 |
rscale = 0; |
| 458 |
} |
| 459 |
if ( lscale < 0 ) { |
| 460 |
lscale = 0; |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
scale = (1.0 - dist) * rscale; |
| 466 |
*right_vol = (master_vol * scale); |
| 467 |
if (*right_vol < 0) |
| 468 |
*right_vol = 0; |
| 469 |
|
| 470 |
scale = (1.0 - dist) * lscale; |
| 471 |
*left_vol = (master_vol * scale); |
| 472 |
if (*left_vol < 0) |
| 473 |
*left_vol = 0; |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
static qboolean S_Base_HearingThroughEntity( int entityNum, vec3_t origin ) |
| 488 |
{ |
| 489 |
float distanceSq; |
| 490 |
vec3_t sorigin; |
| 491 |
|
| 492 |
if (origin) |
| 493 |
VectorCopy(origin, sorigin); |
| 494 |
else |
| 495 |
VectorCopy(loopSounds[entityNum].origin, sorigin); |
| 496 |
|
| 497 |
if( listener_number == entityNum ) |
| 498 |
{ |
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
distanceSq = DistanceSquared( |
| 506 |
sorigin, |
| 507 |
listener_origin ); |
| 508 |
|
| 509 |
if( distanceSq > THIRD_PERSON_THRESHOLD_SQ ) |
| 510 |
return qfalse; |
| 511 |
else |
| 512 |
return qtrue; |
| 513 |
} |
| 514 |
else |
| 515 |
return qfalse; |
| 516 |
} |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
static void S_Base_StartSoundEx( vec3_t origin, int entityNum, int entchannel, sfxHandle_t sfxHandle, qboolean localSound ) { |
| 528 |
channel_t *ch; |
| 529 |
sfx_t *sfx; |
| 530 |
int i, oldest, chosen, time; |
| 531 |
int inplay, allowed; |
| 532 |
qboolean fullVolume; |
| 533 |
|
| 534 |
if ( !s_soundStarted || s_soundMuted ) { |
| 535 |
return; |
| 536 |
} |
| 537 |
|
| 538 |
if ( !origin && ( entityNum < 0 || entityNum >= MAX_GENTITIES ) ) { |
| 539 |
Com_Error( ERR_DROP, "S_StartSound: bad entitynum %i", entityNum ); |
| 540 |
} |
| 541 |
|
| 542 |
if ( sfxHandle < 0 || sfxHandle >= s_numSfx ) { |
| 543 |
Com_Printf( S_COLOR_YELLOW "S_StartSound: handle %i out of range\n", sfxHandle ); |
| 544 |
return; |
| 545 |
} |
| 546 |
|
| 547 |
sfx = &s_knownSfx[ sfxHandle ]; |
| 548 |
|
| 549 |
if (sfx->inMemory == qfalse) { |
| 550 |
S_memoryLoad(sfx); |
| 551 |
} |
| 552 |
|
| 553 |
if ( s_show->integer == 1 ) { |
| 554 |
Com_Printf( "%i : %s\n", s_paintedtime, sfx->soundName ); |
| 555 |
} |
| 556 |
|
| 557 |
time = Com_Milliseconds(); |
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
allowed = 4; |
| 563 |
if (entityNum == listener_number) { |
| 564 |
allowed = 8; |
| 565 |
} |
| 566 |
|
| 567 |
fullVolume = qfalse; |
| 568 |
if (localSound || S_Base_HearingThroughEntity(entityNum, origin)) { |
| 569 |
fullVolume = qtrue; |
| 570 |
} |
| 571 |
|
| 572 |
ch = s_channels; |
| 573 |
inplay = 0; |
| 574 |
for ( i = 0; i < MAX_CHANNELS ; i++, ch++ ) { |
| 575 |
if (ch->entnum == entityNum && ch->thesfx == sfx) { |
| 576 |
if (time - ch->allocTime < 50) { |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
return; |
| 581 |
} |
| 582 |
inplay++; |
| 583 |
} |
| 584 |
} |
| 585 |
|
| 586 |
if (inplay>allowed) { |
| 587 |
return; |
| 588 |
} |
| 589 |
|
| 590 |
sfx->lastTimeUsed = time; |
| 591 |
|
| 592 |
ch = S_ChannelMalloc(); |
| 593 |
if (!ch) { |
| 594 |
ch = s_channels; |
| 595 |
|
| 596 |
oldest = sfx->lastTimeUsed; |
| 597 |
chosen = -1; |
| 598 |
for ( i = 0 ; i < MAX_CHANNELS ; i++, ch++ ) { |
| 599 |
if (ch->entnum != listener_number && ch->entnum == entityNum && ch->allocTime<oldest && ch->entchannel != CHAN_ANNOUNCER) { |
| 600 |
oldest = ch->allocTime; |
| 601 |
chosen = i; |
| 602 |
} |
| 603 |
} |
| 604 |
if (chosen == -1) { |
| 605 |
ch = s_channels; |
| 606 |
for ( i = 0 ; i < MAX_CHANNELS ; i++, ch++ ) { |
| 607 |
if (ch->entnum != listener_number && ch->allocTime<oldest && ch->entchannel != CHAN_ANNOUNCER) { |
| 608 |
oldest = ch->allocTime; |
| 609 |
chosen = i; |
| 610 |
} |
| 611 |
} |
| 612 |
if (chosen == -1) { |
| 613 |
ch = s_channels; |
| 614 |
if (ch->entnum == listener_number) { |
| 615 |
for ( i = 0 ; i < MAX_CHANNELS ; i++, ch++ ) { |
| 616 |
if (ch->allocTime<oldest) { |
| 617 |
oldest = ch->allocTime; |
| 618 |
chosen = i; |
| 619 |
} |
| 620 |
} |
| 621 |
} |
| 622 |
if (chosen == -1) { |
| 623 |
Com_DPrintf("dropping sound\n"); |
| 624 |
return; |
| 625 |
} |
| 626 |
} |
| 627 |
} |
| 628 |
ch = &s_channels[chosen]; |
| 629 |
ch->allocTime = sfx->lastTimeUsed; |
| 630 |
} |
| 631 |
|
| 632 |
if (origin) { |
| 633 |
VectorCopy (origin, ch->origin); |
| 634 |
ch->fixed_origin = qtrue; |
| 635 |
} else { |
| 636 |
ch->fixed_origin = qfalse; |
| 637 |
} |
| 638 |
|
| 639 |
ch->master_vol = 127; |
| 640 |
ch->entnum = entityNum; |
| 641 |
ch->thesfx = sfx; |
| 642 |
ch->startSample = START_SAMPLE_IMMEDIATE; |
| 643 |
ch->entchannel = entchannel; |
| 644 |
ch->leftvol = ch->master_vol; |
| 645 |
ch->rightvol = ch->master_vol; |
| 646 |
ch->doppler = qfalse; |
| 647 |
ch->fullVolume = fullVolume; |
| 648 |
} |
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
void S_Base_StartSound( vec3_t origin, int entityNum, int entchannel, sfxHandle_t sfxHandle ) { |
| 658 |
S_Base_StartSoundEx( origin, entityNum, entchannel, sfxHandle, qfalse ); |
| 659 |
} |
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
void S_Base_StartLocalSound( sfxHandle_t sfxHandle, int channelNum ) { |
| 667 |
if ( !s_soundStarted || s_soundMuted ) { |
| 668 |
return; |
| 669 |
} |
| 670 |
|
| 671 |
if ( sfxHandle < 0 || sfxHandle >= s_numSfx ) { |
| 672 |
Com_Printf( S_COLOR_YELLOW "S_StartLocalSound: handle %i out of range\n", sfxHandle ); |
| 673 |
return; |
| 674 |
} |
| 675 |
|
| 676 |
S_Base_StartSoundEx( NULL, listener_number, channelNum, sfxHandle, qtrue ); |
| 677 |
} |
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
void S_Base_ClearSoundBuffer( void ) { |
| 689 |
int clear; |
| 690 |
|
| 691 |
if (!s_soundStarted) |
| 692 |
return; |
| 693 |
|
| 694 |
|
| 695 |
Com_Memset(loopSounds, 0, MAX_GENTITIES*sizeof(loopSound_t)); |
| 696 |
Com_Memset(loop_channels, 0, MAX_CHANNELS*sizeof(channel_t)); |
| 697 |
numLoopChannels = 0; |
| 698 |
|
| 699 |
S_ChannelSetup(); |
| 700 |
|
| 701 |
Com_Memset(s_rawend, '\0', sizeof (s_rawend)); |
| 702 |
|
| 703 |
if (dma.samplebits == 8) |
| 704 |
clear = 0x80; |
| 705 |
else |
| 706 |
clear = 0; |
| 707 |
|
| 708 |
SNDDMA_BeginPainting (); |
| 709 |
if (dma.buffer) |
| 710 |
Com_Memset(dma.buffer, clear, dma.samples * dma.samplebits/8); |
| 711 |
SNDDMA_Submit (); |
| 712 |
} |
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
void S_Base_StopAllSounds(void) { |
| 720 |
if ( !s_soundStarted ) { |
| 721 |
return; |
| 722 |
} |
| 723 |
|
| 724 |
|
| 725 |
S_Base_StopBackgroundTrack(); |
| 726 |
|
| 727 |
S_Base_ClearSoundBuffer (); |
| 728 |
} |
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
void S_Base_StopLoopingSound(int entityNum) { |
| 739 |
loopSounds[entityNum].active = qfalse; |
| 740 |
|
| 741 |
loopSounds[entityNum].kill = qfalse; |
| 742 |
} |
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
void S_Base_ClearLoopingSounds( qboolean killall ) { |
| 751 |
int i; |
| 752 |
for ( i = 0 ; i < MAX_GENTITIES ; i++) { |
| 753 |
if (killall || loopSounds[i].kill == qtrue || (loopSounds[i].sfx && loopSounds[i].sfx->soundLength == 0)) { |
| 754 |
S_Base_StopLoopingSound(i); |
| 755 |
} |
| 756 |
} |
| 757 |
numLoopChannels = 0; |
| 758 |
} |
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
void S_Base_AddLoopingSound( int entityNum, const vec3_t origin, const vec3_t velocity, sfxHandle_t sfxHandle ) { |
| 769 |
sfx_t *sfx; |
| 770 |
|
| 771 |
if ( !s_soundStarted || s_soundMuted ) { |
| 772 |
return; |
| 773 |
} |
| 774 |
|
| 775 |
if ( sfxHandle < 0 || sfxHandle >= s_numSfx ) { |
| 776 |
Com_Printf( S_COLOR_YELLOW "S_AddLoopingSound: handle %i out of range\n", sfxHandle ); |
| 777 |
return; |
| 778 |
} |
| 779 |
|
| 780 |
sfx = &s_knownSfx[ sfxHandle ]; |
| 781 |
|
| 782 |
if (sfx->inMemory == qfalse) { |
| 783 |
S_memoryLoad(sfx); |
| 784 |
} |
| 785 |
|
| 786 |
if ( !sfx->soundLength ) { |
| 787 |
Com_Error( ERR_DROP, "%s has length 0", sfx->soundName ); |
| 788 |
} |
| 789 |
|
| 790 |
VectorCopy( origin, loopSounds[entityNum].origin ); |
| 791 |
VectorCopy( velocity, loopSounds[entityNum].velocity ); |
| 792 |
loopSounds[entityNum].active = qtrue; |
| 793 |
loopSounds[entityNum].kill = qtrue; |
| 794 |
loopSounds[entityNum].doppler = qfalse; |
| 795 |
loopSounds[entityNum].oldDopplerScale = 1.0; |
| 796 |
loopSounds[entityNum].dopplerScale = 1.0; |
| 797 |
loopSounds[entityNum].sfx = sfx; |
| 798 |
|
| 799 |
if (s_doppler->integer && VectorLengthSquared(velocity)>0.0) { |
| 800 |
vec3_t out; |
| 801 |
float lena, lenb; |
| 802 |
|
| 803 |
loopSounds[entityNum].doppler = qtrue; |
| 804 |
lena = DistanceSquared(loopSounds[listener_number].origin, loopSounds[entityNum].origin); |
| 805 |
VectorAdd(loopSounds[entityNum].origin, loopSounds[entityNum].velocity, out); |
| 806 |
lenb = DistanceSquared(loopSounds[listener_number].origin, out); |
| 807 |
if ((loopSounds[entityNum].framenum+1) != cls.framecount) { |
| 808 |
loopSounds[entityNum].oldDopplerScale = 1.0; |
| 809 |
} else { |
| 810 |
loopSounds[entityNum].oldDopplerScale = loopSounds[entityNum].dopplerScale; |
| 811 |
} |
| 812 |
loopSounds[entityNum].dopplerScale = lenb/(lena*100); |
| 813 |
if (loopSounds[entityNum].dopplerScale<=1.0) { |
| 814 |
loopSounds[entityNum].doppler = qfalse; |
| 815 |
} else if (loopSounds[entityNum].dopplerScale>MAX_DOPPLER_SCALE) { |
| 816 |
loopSounds[entityNum].dopplerScale = MAX_DOPPLER_SCALE; |
| 817 |
} |
| 818 |
} |
| 819 |
|
| 820 |
loopSounds[entityNum].framenum = cls.framecount; |
| 821 |
} |
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
void S_Base_AddRealLoopingSound( int entityNum, const vec3_t origin, const vec3_t velocity, sfxHandle_t sfxHandle ) { |
| 832 |
sfx_t *sfx; |
| 833 |
|
| 834 |
if ( !s_soundStarted || s_soundMuted ) { |
| 835 |
return; |
| 836 |
} |
| 837 |
|
| 838 |
if ( sfxHandle < 0 || sfxHandle >= s_numSfx ) { |
| 839 |
Com_Printf( S_COLOR_YELLOW "S_AddRealLoopingSound: handle %i out of range\n", sfxHandle ); |
| 840 |
return; |
| 841 |
} |
| 842 |
|
| 843 |
sfx = &s_knownSfx[ sfxHandle ]; |
| 844 |
|
| 845 |
if (sfx->inMemory == qfalse) { |
| 846 |
S_memoryLoad(sfx); |
| 847 |
} |
| 848 |
|
| 849 |
if ( !sfx->soundLength ) { |
| 850 |
Com_Error( ERR_DROP, "%s has length 0", sfx->soundName ); |
| 851 |
} |
| 852 |
VectorCopy( origin, loopSounds[entityNum].origin ); |
| 853 |
VectorCopy( velocity, loopSounds[entityNum].velocity ); |
| 854 |
loopSounds[entityNum].sfx = sfx; |
| 855 |
loopSounds[entityNum].active = qtrue; |
| 856 |
loopSounds[entityNum].kill = qfalse; |
| 857 |
loopSounds[entityNum].doppler = qfalse; |
| 858 |
} |
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
void S_AddLoopSounds (void) { |
| 872 |
int i, j, time; |
| 873 |
int left_total, right_total, left, right; |
| 874 |
channel_t *ch; |
| 875 |
loopSound_t *loop, *loop2; |
| 876 |
static int loopFrame; |
| 877 |
|
| 878 |
|
| 879 |
numLoopChannels = 0; |
| 880 |
|
| 881 |
time = Com_Milliseconds(); |
| 882 |
|
| 883 |
loopFrame++; |
| 884 |
for ( i = 0 ; i < MAX_GENTITIES ; i++) { |
| 885 |
loop = &loopSounds[i]; |
| 886 |
if ( !loop->active || loop->mergeFrame == loopFrame ) { |
| 887 |
continue; |
| 888 |
} |
| 889 |
|
| 890 |
if (loop->kill) { |
| 891 |
S_SpatializeOrigin( loop->origin, 127, &left_total, &right_total); |
| 892 |
} else { |
| 893 |
S_SpatializeOrigin( loop->origin, 90, &left_total, &right_total); |
| 894 |
} |
| 895 |
|
| 896 |
loop->sfx->lastTimeUsed = time; |
| 897 |
|
| 898 |
for (j=(i+1); j< MAX_GENTITIES ; j++) { |
| 899 |
loop2 = &loopSounds[j]; |
| 900 |
if ( !loop2->active || loop2->doppler || loop2->sfx != loop->sfx) { |
| 901 |
continue; |
| 902 |
} |
| 903 |
loop2->mergeFrame = loopFrame; |
| 904 |
|
| 905 |
if (loop2->kill) { |
| 906 |
S_SpatializeOrigin( loop2->origin, 127, &left, &right); |
| 907 |
} else { |
| 908 |
S_SpatializeOrigin( loop2->origin, 90, &left, &right); |
| 909 |
} |
| 910 |
|
| 911 |
loop2->sfx->lastTimeUsed = time; |
| 912 |
left_total += left; |
| 913 |
right_total += right; |
| 914 |
} |
| 915 |
if (left_total == 0 && right_total == 0) { |
| 916 |
continue; |
| 917 |
} |
| 918 |
|
| 919 |
|
| 920 |
ch = &loop_channels[numLoopChannels]; |
| 921 |
|
| 922 |
if (left_total > 255) { |
| 923 |
left_total = 255; |
| 924 |
} |
| 925 |
if (right_total > 255) { |
| 926 |
right_total = 255; |
| 927 |
} |
| 928 |
|
| 929 |
ch->master_vol = 127; |
| 930 |
ch->leftvol = left_total; |
| 931 |
ch->rightvol = right_total; |
| 932 |
ch->thesfx = loop->sfx; |
| 933 |
ch->doppler = loop->doppler; |
| 934 |
ch->dopplerScale = loop->dopplerScale; |
| 935 |
ch->oldDopplerScale = loop->oldDopplerScale; |
| 936 |
ch->fullVolume = qfalse; |
| 937 |
numLoopChannels++; |
| 938 |
if (numLoopChannels == MAX_CHANNELS) { |
| 939 |
return; |
| 940 |
} |
| 941 |
} |
| 942 |
} |
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
void S_ByteSwapRawSamples( int samples, int width, int numChannels, const byte *data ) { |
| 955 |
int i; |
| 956 |
|
| 957 |
if ( width != 2 ) { |
| 958 |
return; |
| 959 |
} |
| 960 |
if ( LittleShort( 256 ) == 256 ) { |
| 961 |
return; |
| 962 |
} |
| 963 |
|
| 964 |
if ( numChannels == 2 ) { |
| 965 |
samples <<= 1; |
| 966 |
} |
| 967 |
for ( i = 0 ; i < samples ; i++ ) { |
| 968 |
((short *)data)[i] = LittleShort( ((short *)data)[i] ); |
| 969 |
} |
| 970 |
} |
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
void S_Base_RawSamples( int stream, int samples, int rate, int width, int numChannels, const byte *data, float volume, int entityNum) |
| 980 |
{ |
| 981 |
int i; |
| 982 |
int src, dst; |
| 983 |
float scale; |
| 984 |
int intVolumeLeft, intVolumeRight; |
| 985 |
portable_samplepair_t *rawsamples; |
| 986 |
|
| 987 |
if ( !s_soundStarted || s_soundMuted ) { |
| 988 |
return; |
| 989 |
} |
| 990 |
|
| 991 |
if ( (stream < 0) || (stream >= MAX_RAW_STREAMS) ) { |
| 992 |
return; |
| 993 |
} |
| 994 |
|
| 995 |
rawsamples = s_rawsamples[stream]; |
| 996 |
|
| 997 |
if ( s_muted->integer ) { |
| 998 |
intVolumeLeft = intVolumeRight = 0; |
| 999 |
} else { |
| 1000 |
int leftvol, rightvol; |
| 1001 |
|
| 1002 |
if ( entityNum >= 0 && entityNum < MAX_GENTITIES ) { |
| 1003 |
|
| 1004 |
S_SpatializeOrigin( loopSounds[ entityNum ].origin, 256, &leftvol, &rightvol ); |
| 1005 |
} else { |
| 1006 |
leftvol = rightvol = 256; |
| 1007 |
} |
| 1008 |
|
| 1009 |
intVolumeLeft = leftvol * volume * s_volume->value; |
| 1010 |
intVolumeRight = rightvol * volume * s_volume->value; |
| 1011 |
} |
| 1012 |
|
| 1013 |
if ( s_rawend[stream] < s_soundtime ) { |
| 1014 |
Com_DPrintf( "S_Base_RawSamples: resetting minimum: %i < %i\n", s_rawend[stream], s_soundtime ); |
| 1015 |
s_rawend[stream] = s_soundtime; |
| 1016 |
} |
| 1017 |
|
| 1018 |
scale = (float)rate / dma.speed; |
| 1019 |
|
| 1020 |
|
| 1021 |
if (numChannels == 2 && width == 2) |
| 1022 |
{ |
| 1023 |
if (scale == 1.0) |
| 1024 |
{ |
| 1025 |
for (i=0 ; i<samples ; i++) |
| 1026 |
{ |
| 1027 |
dst = s_rawend[stream]&(MAX_RAW_SAMPLES-1); |
| 1028 |
s_rawend[stream]++; |
| 1029 |
rawsamples[dst].left = ((short *)data)[i*2] * intVolumeLeft; |
| 1030 |
rawsamples[dst].right = ((short *)data)[i*2+1] * intVolumeRight; |
| 1031 |
} |
| 1032 |
} |
| 1033 |
else |
| 1034 |
{ |
| 1035 |
for (i=0 ; ; i++) |
| 1036 |
{ |
| 1037 |
src = i*scale; |
| 1038 |
if (src >= samples) |
| 1039 |
break; |
| 1040 |
dst = s_rawend[stream]&(MAX_RAW_SAMPLES-1); |
| 1041 |
s_rawend[stream]++; |
| 1042 |
rawsamples[dst].left = ((short *)data)[src*2] * intVolumeLeft; |
| 1043 |
rawsamples[dst].right = ((short *)data)[src*2+1] * intVolumeRight; |
| 1044 |
} |
| 1045 |
} |
| 1046 |
} |
| 1047 |
else if (numChannels == 1 && width == 2) |
| 1048 |
{ |
| 1049 |
for (i=0 ; ; i++) |
| 1050 |
{ |
| 1051 |
src = i*scale; |
| 1052 |
if (src >= samples) |
| 1053 |
break; |
| 1054 |
dst = s_rawend[stream]&(MAX_RAW_SAMPLES-1); |
| 1055 |
s_rawend[stream]++; |
| 1056 |
rawsamples[dst].left = ((short *)data)[src] * intVolumeLeft; |
| 1057 |
rawsamples[dst].right = ((short *)data)[src] * intVolumeRight; |
| 1058 |
} |
| 1059 |
} |
| 1060 |
else if (numChannels == 2 && width == 1) |
| 1061 |
{ |
| 1062 |
intVolumeLeft *= 256; |
| 1063 |
intVolumeRight *= 256; |
| 1064 |
|
| 1065 |
for (i=0 ; ; i++) |
| 1066 |
{ |
| 1067 |
src = i*scale; |
| 1068 |
if (src >= samples) |
| 1069 |
break; |
| 1070 |
dst = s_rawend[stream]&(MAX_RAW_SAMPLES-1); |
| 1071 |
s_rawend[stream]++; |
| 1072 |
rawsamples[dst].left = ((char *)data)[src*2] * intVolumeLeft; |
| 1073 |
rawsamples[dst].right = ((char *)data)[src*2+1] * intVolumeRight; |
| 1074 |
} |
| 1075 |
} |
| 1076 |
else if (numChannels == 1 && width == 1) |
| 1077 |
{ |
| 1078 |
intVolumeLeft *= 256; |
| 1079 |
intVolumeRight *= 256; |
| 1080 |
|
| 1081 |
for (i=0 ; ; i++) |
| 1082 |
{ |
| 1083 |
src = i*scale; |
| 1084 |
if (src >= samples) |
| 1085 |
break; |
| 1086 |
dst = s_rawend[stream]&(MAX_RAW_SAMPLES-1); |
| 1087 |
s_rawend[stream]++; |
| 1088 |
rawsamples[dst].left = (((byte *)data)[src]-128) * intVolumeLeft; |
| 1089 |
rawsamples[dst].right = (((byte *)data)[src]-128) * intVolumeRight; |
| 1090 |
} |
| 1091 |
} |
| 1092 |
|
| 1093 |
if ( s_rawend[stream] > s_soundtime + MAX_RAW_SAMPLES ) { |
| 1094 |
Com_DPrintf( "S_Base_RawSamples: overflowed %i > %i\n", s_rawend[stream], s_soundtime ); |
| 1095 |
} |
| 1096 |
} |
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
void S_Base_UpdateEntityPosition( int entityNum, const vec3_t origin ) { |
| 1108 |
if ( entityNum < 0 || entityNum >= MAX_GENTITIES ) { |
| 1109 |
Com_Error( ERR_DROP, "S_UpdateEntityPosition: bad entitynum %i", entityNum ); |
| 1110 |
} |
| 1111 |
VectorCopy( origin, loopSounds[entityNum].origin ); |
| 1112 |
} |
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
void S_Base_Respatialize( int entityNum, const vec3_t head, vec3_t axis[3], int inwater ) { |
| 1123 |
int i; |
| 1124 |
channel_t *ch; |
| 1125 |
vec3_t origin; |
| 1126 |
|
| 1127 |
if ( !s_soundStarted || s_soundMuted ) { |
| 1128 |
return; |
| 1129 |
} |
| 1130 |
|
| 1131 |
listener_number = entityNum; |
| 1132 |
VectorCopy(head, listener_origin); |
| 1133 |
VectorCopy(axis[0], listener_axis[0]); |
| 1134 |
VectorCopy(axis[1], listener_axis[1]); |
| 1135 |
VectorCopy(axis[2], listener_axis[2]); |
| 1136 |
|
| 1137 |
|
| 1138 |
ch = s_channels; |
| 1139 |
for ( i = 0 ; i < MAX_CHANNELS ; i++, ch++ ) { |
| 1140 |
if ( !ch->thesfx ) { |
| 1141 |
continue; |
| 1142 |
} |
| 1143 |
|
| 1144 |
if (ch->fullVolume) { |
| 1145 |
ch->leftvol = ch->master_vol; |
| 1146 |
ch->rightvol = ch->master_vol; |
| 1147 |
} else { |
| 1148 |
if (ch->fixed_origin) { |
| 1149 |
VectorCopy( ch->origin, origin ); |
| 1150 |
} else { |
| 1151 |
VectorCopy( loopSounds[ ch->entnum ].origin, origin ); |
| 1152 |
} |
| 1153 |
|
| 1154 |
S_SpatializeOrigin (origin, ch->master_vol, &ch->leftvol, &ch->rightvol); |
| 1155 |
} |
| 1156 |
} |
| 1157 |
|
| 1158 |
|
| 1159 |
S_AddLoopSounds (); |
| 1160 |
} |
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
qboolean S_ScanChannelStarts( void ) { |
| 1171 |
channel_t *ch; |
| 1172 |
int i; |
| 1173 |
qboolean newSamples; |
| 1174 |
|
| 1175 |
newSamples = qfalse; |
| 1176 |
ch = s_channels; |
| 1177 |
|
| 1178 |
for (i=0; i<MAX_CHANNELS ; i++, ch++) { |
| 1179 |
if ( !ch->thesfx ) { |
| 1180 |
continue; |
| 1181 |
} |
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
if ( ch->startSample == START_SAMPLE_IMMEDIATE ) { |
| 1186 |
ch->startSample = s_paintedtime; |
| 1187 |
newSamples = qtrue; |
| 1188 |
continue; |
| 1189 |
} |
| 1190 |
|
| 1191 |
|
| 1192 |
if ( ch->startSample + (ch->thesfx->soundLength) <= s_paintedtime ) { |
| 1193 |
S_ChannelFree(ch); |
| 1194 |
} |
| 1195 |
} |
| 1196 |
|
| 1197 |
return newSamples; |
| 1198 |
} |
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
void S_Base_Update( void ) { |
| 1208 |
int i; |
| 1209 |
int total; |
| 1210 |
channel_t *ch; |
| 1211 |
|
| 1212 |
if ( !s_soundStarted || s_soundMuted ) { |
| 1213 |
|
| 1214 |
return; |
| 1215 |
} |
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
if ( s_show->integer == 2 ) { |
| 1221 |
total = 0; |
| 1222 |
ch = s_channels; |
| 1223 |
for (i=0 ; i<MAX_CHANNELS; i++, ch++) { |
| 1224 |
if (ch->thesfx && (ch->leftvol || ch->rightvol) ) { |
| 1225 |
Com_Printf ("%d %d %s\n", ch->leftvol, ch->rightvol, ch->thesfx->soundName); |
| 1226 |
total++; |
| 1227 |
} |
| 1228 |
} |
| 1229 |
|
| 1230 |
Com_Printf ("----(%i)---- painted: %i\n", total, s_paintedtime); |
| 1231 |
} |
| 1232 |
|
| 1233 |
|
| 1234 |
S_UpdateBackgroundTrack(); |
| 1235 |
|
| 1236 |
|
| 1237 |
S_Update_(); |
| 1238 |
} |
| 1239 |
|
| 1240 |
void S_GetSoundtime(void) |
| 1241 |
{ |
| 1242 |
int samplepos; |
| 1243 |
static int buffers; |
| 1244 |
static int oldsamplepos; |
| 1245 |
|
| 1246 |
if( CL_VideoRecording( ) ) |
| 1247 |
{ |
| 1248 |
float fps = MIN(cl_aviFrameRate->value, 1000.0f); |
| 1249 |
float frameDuration = MAX(dma.speed / fps, 1.0f) + clc.aviSoundFrameRemainder; |
| 1250 |
|
| 1251 |
int msec = (int)frameDuration; |
| 1252 |
s_soundtime += msec; |
| 1253 |
clc.aviSoundFrameRemainder = frameDuration - msec; |
| 1254 |
|
| 1255 |
return; |
| 1256 |
} |
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
samplepos = SNDDMA_GetDMAPos(); |
| 1261 |
if (samplepos < oldsamplepos) |
| 1262 |
{ |
| 1263 |
buffers++; |
| 1264 |
|
| 1265 |
if (s_paintedtime > 0x40000000) |
| 1266 |
{ |
| 1267 |
buffers = 0; |
| 1268 |
s_paintedtime = dma.fullsamples; |
| 1269 |
S_Base_StopAllSounds (); |
| 1270 |
} |
| 1271 |
} |
| 1272 |
oldsamplepos = samplepos; |
| 1273 |
|
| 1274 |
s_soundtime = buffers*dma.fullsamples + samplepos/dma.channels; |
| 1275 |
|
| 1276 |
#if 0 |
| 1277 |
|
| 1278 |
if (s_paintedtime < s_soundtime) |
| 1279 |
{ |
| 1280 |
Com_DPrintf ("S_Update_ : overflow\n"); |
| 1281 |
s_paintedtime = s_soundtime; |
| 1282 |
} |
| 1283 |
#endif |
| 1284 |
|
| 1285 |
if ( dma.submission_chunk < 256 ) { |
| 1286 |
s_paintedtime = s_soundtime + s_mixPreStep->value * dma.speed; |
| 1287 |
} else { |
| 1288 |
s_paintedtime = s_soundtime + dma.submission_chunk; |
| 1289 |
} |
| 1290 |
} |
| 1291 |
|
| 1292 |
|
| 1293 |
void S_Update_(void) { |
| 1294 |
unsigned endtime; |
| 1295 |
static float lastTime = 0.0f; |
| 1296 |
float ma, op; |
| 1297 |
float thisTime, sane; |
| 1298 |
static int ot = -1; |
| 1299 |
|
| 1300 |
if ( !s_soundStarted || s_soundMuted ) { |
| 1301 |
return; |
| 1302 |
} |
| 1303 |
|
| 1304 |
thisTime = Com_Milliseconds(); |
| 1305 |
|
| 1306 |
|
| 1307 |
S_GetSoundtime(); |
| 1308 |
|
| 1309 |
if (s_soundtime == ot) { |
| 1310 |
return; |
| 1311 |
} |
| 1312 |
ot = s_soundtime; |
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
S_ScanChannelStarts(); |
| 1317 |
|
| 1318 |
sane = thisTime - lastTime; |
| 1319 |
if (sane<11) { |
| 1320 |
sane = 11; |
| 1321 |
} |
| 1322 |
|
| 1323 |
ma = s_mixahead->value * dma.speed; |
| 1324 |
op = s_mixPreStep->value + sane*dma.speed*0.01; |
| 1325 |
|
| 1326 |
if (op < ma) { |
| 1327 |
ma = op; |
| 1328 |
} |
| 1329 |
|
| 1330 |
|
| 1331 |
endtime = s_soundtime + ma; |
| 1332 |
|
| 1333 |
|
| 1334 |
endtime = (endtime + dma.submission_chunk-1) |
| 1335 |
& ~(dma.submission_chunk-1); |
| 1336 |
|
| 1337 |
|
| 1338 |
if (endtime - s_soundtime > dma.fullsamples) |
| 1339 |
endtime = s_soundtime + dma.fullsamples; |
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
SNDDMA_BeginPainting (); |
| 1344 |
|
| 1345 |
S_PaintChannels (endtime); |
| 1346 |
|
| 1347 |
SNDDMA_Submit (); |
| 1348 |
|
| 1349 |
lastTime = thisTime; |
| 1350 |
} |
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
|
| 1356 |
|
| 1357 |
|
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
|
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
void S_Base_StopBackgroundTrack( void ) { |
| 1368 |
if(!s_backgroundStream) |
| 1369 |
return; |
| 1370 |
S_CodecCloseStream(s_backgroundStream); |
| 1371 |
s_backgroundStream = NULL; |
| 1372 |
s_rawend[0] = 0; |
| 1373 |
} |
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
|
| 1378 |
|
| 1379 |
|
| 1380 |
static void S_OpenBackgroundStream( const char *filename ) { |
| 1381 |
|
| 1382 |
|
| 1383 |
if(s_backgroundStream) |
| 1384 |
{ |
| 1385 |
S_CodecCloseStream(s_backgroundStream); |
| 1386 |
s_backgroundStream = NULL; |
| 1387 |
} |
| 1388 |
|
| 1389 |
|
| 1390 |
s_backgroundStream = S_CodecOpenStream(filename); |
| 1391 |
if(!s_backgroundStream) { |
| 1392 |
Com_Printf( S_COLOR_YELLOW "WARNING: couldn't open music file %s\n", filename ); |
| 1393 |
return; |
| 1394 |
} |
| 1395 |
|
| 1396 |
if(s_backgroundStream->info.channels != 2 || s_backgroundStream->info.rate != 22050) { |
| 1397 |
Com_Printf(S_COLOR_YELLOW "WARNING: music file %s is not 22k stereo\n", filename ); |
| 1398 |
} |
| 1399 |
} |
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
void S_Base_StartBackgroundTrack( const char *intro, const char *loop ){ |
| 1407 |
if ( !intro ) { |
| 1408 |
intro = ""; |
| 1409 |
} |
| 1410 |
if ( !loop || !loop[0] ) { |
| 1411 |
loop = intro; |
| 1412 |
} |
| 1413 |
Com_DPrintf( "S_StartBackgroundTrack( %s, %s )\n", intro, loop ); |
| 1414 |
|
| 1415 |
if(!*intro) |
| 1416 |
{ |
| 1417 |
S_Base_StopBackgroundTrack(); |
| 1418 |
return; |
| 1419 |
} |
| 1420 |
|
| 1421 |
Q_strncpyz( s_backgroundLoop, loop, sizeof( s_backgroundLoop ) ); |
| 1422 |
|
| 1423 |
S_OpenBackgroundStream( intro ); |
| 1424 |
} |
| 1425 |
|
| 1426 |
|
| 1427 |
|
| 1428 |
|
| 1429 |
|
| 1430 |
|
| 1431 |
void S_UpdateBackgroundTrack( void ) { |
| 1432 |
int bufferSamples; |
| 1433 |
int fileSamples; |
| 1434 |
byte raw[30000]; |
| 1435 |
int fileBytes; |
| 1436 |
int r; |
| 1437 |
|
| 1438 |
if(!s_backgroundStream) { |
| 1439 |
return; |
| 1440 |
} |
| 1441 |
|
| 1442 |
|
| 1443 |
if ( s_musicVolume->value <= 0 ) { |
| 1444 |
return; |
| 1445 |
} |
| 1446 |
|
| 1447 |
|
| 1448 |
if ( s_rawend[0] < s_soundtime ) { |
| 1449 |
s_rawend[0] = s_soundtime; |
| 1450 |
} |
| 1451 |
|
| 1452 |
while ( s_rawend[0] < s_soundtime + MAX_RAW_SAMPLES ) { |
| 1453 |
bufferSamples = MAX_RAW_SAMPLES - (s_rawend[0] - s_soundtime); |
| 1454 |
|
| 1455 |
|
| 1456 |
fileSamples = bufferSamples * s_backgroundStream->info.rate / dma.speed; |
| 1457 |
|
| 1458 |
if (!fileSamples) |
| 1459 |
return; |
| 1460 |
|
| 1461 |
|
| 1462 |
fileBytes = fileSamples * (s_backgroundStream->info.width * s_backgroundStream->info.channels); |
| 1463 |
if ( fileBytes > sizeof(raw) ) { |
| 1464 |
fileBytes = sizeof(raw); |
| 1465 |
fileSamples = fileBytes / (s_backgroundStream->info.width * s_backgroundStream->info.channels); |
| 1466 |
} |
| 1467 |
|
| 1468 |
|
| 1469 |
r = S_CodecReadStream(s_backgroundStream, fileBytes, raw); |
| 1470 |
if(r < fileBytes) |
| 1471 |
{ |
| 1472 |
fileSamples = r / (s_backgroundStream->info.width * s_backgroundStream->info.channels); |
| 1473 |
} |
| 1474 |
|
| 1475 |
if(r > 0) |
| 1476 |
{ |
| 1477 |
|
| 1478 |
S_Base_RawSamples(0, fileSamples, s_backgroundStream->info.rate, |
| 1479 |
s_backgroundStream->info.width, s_backgroundStream->info.channels, raw, s_musicVolume->value, -1); |
| 1480 |
} |
| 1481 |
else |
| 1482 |
{ |
| 1483 |
|
| 1484 |
if(s_backgroundLoop[0]) |
| 1485 |
{ |
| 1486 |
S_OpenBackgroundStream( s_backgroundLoop ); |
| 1487 |
if(!s_backgroundStream) |
| 1488 |
return; |
| 1489 |
} |
| 1490 |
else |
| 1491 |
{ |
| 1492 |
S_Base_StopBackgroundTrack(); |
| 1493 |
return; |
| 1494 |
} |
| 1495 |
} |
| 1496 |
|
| 1497 |
} |
| 1498 |
} |
| 1499 |
|
| 1500 |
|
| 1501 |
|
| 1502 |
|
| 1503 |
|
| 1504 |
|
| 1505 |
|
| 1506 |
|
| 1507 |
void S_FreeOldestSound( void ) { |
| 1508 |
int i, oldest, used; |
| 1509 |
sfx_t *sfx; |
| 1510 |
sndBuffer *buffer, *nbuffer; |
| 1511 |
|
| 1512 |
oldest = Com_Milliseconds(); |
| 1513 |
used = 0; |
| 1514 |
|
| 1515 |
for (i=1 ; i < s_numSfx ; i++) { |
| 1516 |
sfx = &s_knownSfx[i]; |
| 1517 |
if (sfx->inMemory && sfx->lastTimeUsed<oldest) { |
| 1518 |
used = i; |
| 1519 |
oldest = sfx->lastTimeUsed; |
| 1520 |
} |
| 1521 |
} |
| 1522 |
|
| 1523 |
sfx = &s_knownSfx[used]; |
| 1524 |
|
| 1525 |
Com_DPrintf("S_FreeOldestSound: freeing sound %s\n", sfx->soundName); |
| 1526 |
|
| 1527 |
buffer = sfx->soundData; |
| 1528 |
while(buffer != NULL) { |
| 1529 |
nbuffer = buffer->next; |
| 1530 |
SND_free(buffer); |
| 1531 |
buffer = nbuffer; |
| 1532 |
} |
| 1533 |
sfx->inMemory = qfalse; |
| 1534 |
sfx->soundData = NULL; |
| 1535 |
} |
| 1536 |
|
| 1537 |
|
| 1538 |
|
| 1539 |
|
| 1540 |
|
| 1541 |
void S_Base_Shutdown( void ) { |
| 1542 |
if ( !s_soundStarted ) { |
| 1543 |
return; |
| 1544 |
} |
| 1545 |
|
| 1546 |
SNDDMA_Shutdown(); |
| 1547 |
SND_shutdown(); |
| 1548 |
|
| 1549 |
s_soundStarted = 0; |
| 1550 |
s_numSfx = 0; |
| 1551 |
|
| 1552 |
Cmd_RemoveCommand("s_info"); |
| 1553 |
} |
| 1554 |
|
| 1555 |
|
| 1556 |
|
| 1557 |
|
| 1558 |
|
| 1559 |
|
| 1560 |
qboolean S_Base_Init( soundInterface_t *si ) { |
| 1561 |
qboolean r; |
| 1562 |
|
| 1563 |
if( !si ) { |
| 1564 |
return qfalse; |
| 1565 |
} |
| 1566 |
|
| 1567 |
s_mixahead = Cvar_Get ("s_mixahead", "0.2", CVAR_ARCHIVE); |
| 1568 |
s_mixPreStep = Cvar_Get ("s_mixPreStep", "0.05", CVAR_ARCHIVE); |
| 1569 |
s_show = Cvar_Get ("s_show", "0", CVAR_CHEAT); |
| 1570 |
s_testsound = Cvar_Get ("s_testsound", "0", CVAR_CHEAT); |
| 1571 |
|
| 1572 |
r = SNDDMA_Init(); |
| 1573 |
|
| 1574 |
if ( r ) { |
| 1575 |
s_soundStarted = 1; |
| 1576 |
s_soundMuted = 1; |
| 1577 |
|
| 1578 |
|
| 1579 |
Com_Memset(sfxHash, 0, sizeof(sfx_t *)*LOOP_HASH); |
| 1580 |
|
| 1581 |
s_soundtime = 0; |
| 1582 |
s_paintedtime = 0; |
| 1583 |
|
| 1584 |
S_Base_StopAllSounds( ); |
| 1585 |
} else { |
| 1586 |
return qfalse; |
| 1587 |
} |
| 1588 |
|
| 1589 |
si->Shutdown = S_Base_Shutdown; |
| 1590 |
si->StartSound = S_Base_StartSound; |
| 1591 |
si->StartLocalSound = S_Base_StartLocalSound; |
| 1592 |
si->StartBackgroundTrack = S_Base_StartBackgroundTrack; |
| 1593 |
si->StopBackgroundTrack = S_Base_StopBackgroundTrack; |
| 1594 |
si->RawSamples = S_Base_RawSamples; |
| 1595 |
si->StopAllSounds = S_Base_StopAllSounds; |
| 1596 |
si->ClearLoopingSounds = S_Base_ClearLoopingSounds; |
| 1597 |
si->AddLoopingSound = S_Base_AddLoopingSound; |
| 1598 |
si->AddRealLoopingSound = S_Base_AddRealLoopingSound; |
| 1599 |
si->StopLoopingSound = S_Base_StopLoopingSound; |
| 1600 |
si->Respatialize = S_Base_Respatialize; |
| 1601 |
si->UpdateEntityPosition = S_Base_UpdateEntityPosition; |
| 1602 |
si->Update = S_Base_Update; |
| 1603 |
si->DisableSounds = S_Base_DisableSounds; |
| 1604 |
si->BeginRegistration = S_Base_BeginRegistration; |
| 1605 |
si->RegisterSound = S_Base_RegisterSound; |
| 1606 |
si->ClearSoundBuffer = S_Base_ClearSoundBuffer; |
| 1607 |
si->SoundInfo = S_Base_SoundInfo; |
| 1608 |
si->SoundList = S_Base_SoundList; |
| 1609 |
|
| 1610 |
#ifdef USE_VOIP |
| 1611 |
si->StartCapture = S_Base_StartCapture; |
| 1612 |
si->AvailableCaptureSamples = S_Base_AvailableCaptureSamples; |
| 1613 |
si->Capture = S_Base_Capture; |
| 1614 |
si->StopCapture = S_Base_StopCapture; |
| 1615 |
si->MasterGain = S_Base_MasterGain; |
| 1616 |
#endif |
| 1617 |
|
| 1618 |
return qtrue; |
| 1619 |
} |