| | 1 | #include "system.h" |
| | 2 | #include "prototyp.h" |
| | 3 | #include "stratdef.h" |
| | 4 | #include "psndplat.h" |
| | 5 | #include "userprofile.h" |
| | 6 | #include <assert.h> |
| | 7 | |
| | 8 | int MasterVolumeFadeLevel = ONE_FIXED; |
| | 9 | static enum {FADE_STATUS_READY, FADE_STATUS_UP, FADE_STATUS_DOWN, FADE_STATUS_DOWN_FAST} MasterVolumeFadeStatus = FADE_STATUS_READY; |
| | 10 | static unsigned int Sound_MaxActive_HW = SOUND_MAXACTIVE_SW + 32; |
| | 11 | |
| | 12 | void Sound_Stop(int activeSoundNumber) |
| | 13 | { |
| | 14 | if((activeSoundNumber < 0) || (activeSoundNumber >= SOUND_MAXACTIVE)) |
| | 15 | return; |
| | 16 | |
| | 17 | if(SID_NOSOUND != ActiveSounds[activeSoundNumber].soundIndex) |
| | 18 | { |
| | 19 | /* update game sound instances, and external reference */ |
| | 20 | SOUNDINDEX soundNo = ActiveSounds[activeSoundNumber].soundIndex; |
| | 21 | GameSounds[soundNo].activeInstances--; |
| | 22 | |
| | 23 | assert((GameSounds[soundNo].activeInstances >= 0) && (GameSounds[soundNo].activeInstances < SOUND_MAXINSTANCES)); |
| | 24 | |
| | 25 | if(ActiveSounds[activeSoundNumber].externalRef) |
| | 26 | *(ActiveSounds[activeSoundNumber].externalRef) = SOUND_NOACTIVEINDEX; |
| | 27 | |
| | 28 | /* stop the sound: it may have already stopped, of course, but never mind */ |
| | 29 | |
| | 30 | PlatStopSound(activeSoundNumber); |
| | 31 | |
| | 32 | //fprintf(stderr, "PSND: Stop: %d %d %s\n", activeSoundNumber, soundNo, GameSounds[soundNo].wavName); |
| | 33 | ActiveSounds[activeSoundNumber].soundIndex = SID_NOSOUND; |
| | 34 | } |
| | 35 | } |
| | 36 | |
| | 37 | void SoundSys_StopAll() |
| | 38 | { |
| | 39 | int i; |
| | 40 | for(i=0; i < SOUND_MAXACTIVE; i++) |
| | 41 | Sound_Stop(i); |
| | 42 | } |
| | 43 | |
| | 44 | void SoundSys_ChangeVolume(int volume) |
| | 45 | { |
| | 46 | static int GlobalVolume = VOLUME_DEFAULT; |
| | 47 | |
| | 48 | if (GlobalVolume != volume) |
| | 49 | { |
| | 50 | int newVolume = (volume > VOLUME_MAX) ? VOLUME_MAX : (volume < VOLUME_MIN) ? VOLUME_MIN : volume; |
| | 51 | |
| | 52 | if (newVolume != GlobalVolume) |
| | 53 | { |
| | 54 | GlobalVolume = newVolume; |
| | 55 | |
| | 56 | /* call the platform function: if we get back an error, ignore it */ |
| | 57 | PlatChangeGlobalVolume(newVolume); |
| | 58 | } |
| | 59 | } |
| | 60 | } |
| | 61 | |
| | 62 | static void HandleFadingLevel() |
| | 63 | { |
| | 64 | switch(MasterVolumeFadeStatus) |
| | 65 | { |
| | 66 | default: |
| | 67 | case FADE_STATUS_READY: |
| | 68 | break; |
| | 69 | case FADE_STATUS_UP: |
| | 70 | { |
| | 71 | MasterVolumeFadeLevel += NormalFrameTime/2; |
| | 72 | |
| | 73 | if (MasterVolumeFadeLevel > ONE_FIXED) |
| | 74 | { |
| | 75 | MasterVolumeFadeLevel = ONE_FIXED; |
| | 76 | MasterVolumeFadeStatus = FADE_STATUS_READY; |
| | 77 | } |
| | 78 | } |
| | 79 | break; |
| | 80 | case FADE_STATUS_DOWN: |
| | 81 | { |
| | 82 | MasterVolumeFadeLevel -= NormalFrameTime/8; |
| | 83 | |
| | 84 | if (MasterVolumeFadeLevel < 0) |
| | 85 | { |
| | 86 | MasterVolumeFadeLevel = 0; |
| | 87 | MasterVolumeFadeStatus = FADE_STATUS_READY; |
| | 88 | } |
| | 89 | } |
| | 90 | break; |
| | 91 | case FADE_STATUS_DOWN_FAST: |
| | 92 | { |
| | 93 | MasterVolumeFadeLevel -= NormalFrameTime; |
| | 94 | |
| | 95 | if (MasterVolumeFadeLevel < 0) |
| | 96 | { |
| | 97 | MasterVolumeFadeLevel = 0; |
| | 98 | MasterVolumeFadeStatus = FADE_STATUS_READY; |
| | 99 | } |
| | 100 | } |
| | 101 | } |
| | 102 | } |
| | 103 | |
| | 104 | void SoundSys_Management() |
| | 105 | { |
| | 106 | int i; |
| | 107 | |
| | 108 | for(i=0; i < SOUND_MAXACTIVE; i++) |
| | 109 | { |
| | 110 | if(SID_NOSOUND != ActiveSounds[i].soundIndex) |
| | 111 | { |
| | 112 | if(SoundHasStopped(i)) |
| | 113 | { |
| | 114 | Sound_Stop(i); |
| | 115 | } |
| | 116 | else if(ActiveSounds[i].threedee) |
| | 117 | { |
| | 118 | PlatDo3dSound(i); |
| | 119 | } |
| | 120 | } |
| | 121 | } |
| | 122 | |
| | 123 | HandleFadingLevel(); |
| | 124 | PlatUpdatePlayer(); |
| | 125 | |
| | 126 | #if DEBUG_SOUNDS |
| | 127 | { |
| | 128 | int i = Sound_MaxActive_HW; |
| | 129 | int num_active = 0; |
| | 130 | |
| | 131 | while(i-- > 0) |
| | 132 | { |
| | 133 | if(ActiveSounds[i].soundIndex != SID_NOSOUND) |
| | 134 | num_active++; |
| | 135 | } |
| | 136 | |
| | 137 | printf("Number of Active Sounds %u \n", num_active); |
| | 138 | |
| | 139 | i = Sound_MaxActive_HW; |
| | 140 | |
| | 141 | while(i-- > 0) |
| | 142 | { |
| | 143 | if(ActiveSounds[i].soundIndex != SID_NOSOUND) |
| | 144 | printf("%s\n", GameSounds[ActiveSounds[i].soundIndex].wavName); |
| | 145 | } |
| | 146 | } |
| | 147 | #endif |
| | 148 | |
| | 149 | switch(UserProfile.active_bonus) |
| | 150 | { |
| | 151 | case CHEATMODE_WARPSPEED: |
| | 152 | case CHEATMODE_JOHNWOO: |
| | 153 | UpdateSoundFrequencies(); |
| | 154 | default: |
| | 155 | break; |
| | 156 | } |
| | 157 | } |
| | 158 | |
| | 159 | void SoundSys_ResetFadeLevel() |
| | 160 | { |
| | 161 | MasterVolumeFadeLevel = ONE_FIXED; |
| | 162 | MasterVolumeFadeStatus = FADE_STATUS_READY; |
| | 163 | } |
| | 164 | |
| | 165 | void SoundSys_FadeIn() |
| | 166 | { |
| | 167 | /* always fade in from silence ? */ |
| | 168 | MasterVolumeFadeLevel = ONE_FIXED/2; |
| | 169 | MasterVolumeFadeStatus = FADE_STATUS_UP; |
| | 170 | } |
| | 171 | |
| | 172 | void SoundSys_FadeOut() |
| | 173 | { |
| | 174 | MasterVolumeFadeLevel = ONE_FIXED; |
| | 175 | MasterVolumeFadeStatus = FADE_STATUS_DOWN; |
| | 176 | } |
| | 177 | |
| | 178 | void SoundSys_FadeOutFast() |
| | 179 | { |
| | 180 | MasterVolumeFadeLevel = ONE_FIXED; |
| | 181 | MasterVolumeFadeStatus = FADE_STATUS_DOWN_FAST; |
| | 182 | } |
| | 183 | |
| | 184 | static int FindFreeActiveSound(unsigned int min, unsigned int max) |
| | 185 | { |
| | 186 | unsigned int i = min; |
| | 187 | |
| | 188 | for(; i < max; i++) |
| | 189 | { |
| | 190 | if(ActiveSounds[i].soundIndex == SID_NOSOUND) |
| | 191 | return i; |
| | 192 | } |
| | 193 | |
| | 194 | return SOUND_NOACTIVEINDEX; |
| | 195 | } |
| | 196 | |
| | 197 | static int FindLowerPriorityActiveSound(ACTIVESOUNDPRIORITY testPriority, unsigned int min, unsigned int max) |
| | 198 | { |
| | 199 | unsigned int i = min; |
| | 200 | |
| | 201 | for(; i < max; i++) |
| | 202 | { |
| | 203 | if((ActiveSounds[i].soundIndex != SID_NOSOUND) && (ActiveSounds[i].priority < testPriority)) |
| | 204 | return i; |
| | 205 | } |
| | 206 | |
| | 207 | return SOUND_NOACTIVEINDEX; |
| | 208 | } |
| | 209 | |
| | 210 | /* Patrick 5/6/97 ------------------------------------------------------------- |
| | 211 | Functions for playing and controlling individual sounds |
| | 212 | ----------------------------------------------------------------------------*/ |
| | 213 | void Sound_Play(SOUNDINDEX soundNumber, const char *format, ...) |
| | 214 | { |
| | 215 | /* check soundIndex for bounds, whether it has been loaded, and number of instances */ |
| | 216 | if((soundNumber < 0) || (soundNumber >= SID_MAXIMUM) || !GameSounds[soundNumber].loaded || !(GameSounds[soundNumber].activeInstances <
SOUND_MAXINSTANCES)) |
| | 217 | return; |
| | 218 | |
| | 219 | int loop = 0; |
| | 220 | int *externalRef = NULL; |
| | 221 | ACTIVESOUNDPRIORITY priority = ASP_Minimum; |
| | 222 | SOUND3DDATA * p_3ddata = NULL; |
| | 223 | VECTORCH * worldPosn = NULL; |
| | 224 | int reverb_off = 0; |
| | 225 | int soundStartPosition = 0; |
| | 226 | |
| | 227 | //printf("About to play sound %i %s\n", soundNumber, GameSounds[soundNumber].wavName); |
| | 228 | |
| | 229 | int volume = GameSounds[soundNumber].volume; |
| | 230 | int pitch = GameSounds[soundNumber].pitch; |
| | 231 | int marine_ignore = 0; |
| | 232 | |
| | 233 | /* examine the format string: if it is null, ignore it */ |
| | 234 | if(format) |
| | 235 | { |
| | 236 | const char *nextChar = format; |
| | 237 | va_list argPtr; |
| | 238 | |
| | 239 | va_start(argPtr, format); |
| | 240 | |
| | 241 | while(*nextChar != '\0') |
| | 242 | { |
| | 243 | switch(*nextChar) |
| | 244 | { |
| | 245 | case 'd': |
| | 246 | worldPosn = va_arg(argPtr, VECTORCH*); |
| | 247 | break; |
| | 248 | case 'n': |
| | 249 | p_3ddata = va_arg(argPtr, SOUND3DDATA*); |
| | 250 | break; |
| | 251 | case 'e': |
| | 252 | externalRef = va_arg(argPtr, int*); |
| | 253 | break; |
| | 254 | case 'l': |
| | 255 | loop = 1; |
| | 256 | //no break; |
| | 257 | case 'h': |
| | 258 | priority = ASP_Maximum; |
| | 259 | break; |
| | 260 | case 'v': |
| | 261 | { |
| | 262 | volume = va_arg(argPtr, int); |
| | 263 | |
| | 264 | if(volume > VOLUME_MAX) |
| | 265 | volume = VOLUME_MAX; |
| | 266 | else if(volume < VOLUME_MIN) |
| | 267 | volume = VOLUME_MIN; |
| | 268 | } |
| | 269 | break; |
| | 270 | case 'p': |
| | 271 | { |
| | 272 | pitch = va_arg(argPtr, int); |
| | 273 | |
| | 274 | if(pitch > PITCH_MAX) |
| | 275 | pitch = PITCH_MAX; |
| | 276 | else if(pitch < PITCH_MIN) |
| | 277 | pitch = PITCH_MIN; |
| | 278 | } |
| | 279 | break; |
| | 280 | case 'm': |
| | 281 | marine_ignore = 1; |
| | 282 | break; |
| | 283 | case 'r': |
| | 284 | reverb_off = 1; |
| | 285 | break; |
| | 286 | case 'P': |
| | 287 | soundStartPosition = va_arg(argPtr,int); |
| | 288 | default: |
| | 289 | break; |
| | 290 | } |
| | 291 | nextChar++; |
| | 292 | } |
| | 293 | va_end(argPtr); |
| | 294 | } |
| | 295 | |
| | 296 | /* check for invalid parameter combinations */ |
| | 297 | if(loop && (externalRef == NULL)) |
| | 298 | { |
| | 299 | printf("SoundPlay bad params.\n"); |
| | 300 | return; |
| | 301 | } |
| | 302 | |
| | 303 | /* Deal with resource allocation. */ |
| | 304 | /* Range of active buffers to search. */ |
| | 305 | unsigned int min, max; |
| | 306 | GameSounds[soundNumber].flags |= SAMPLE_IN_SW; |
| | 307 | if(GameSounds[soundNumber].flags & SAMPLE_IN_SW) |
| | 308 | { |
| | 309 | min = 0; |
| | 310 | max = SOUND_MAXACTIVE_SW; |
| | 311 | } |
| | 312 | else |
| | 313 | { |
| | 314 | min = SOUND_MAXACTIVE_SW + 1; |
| | 315 | max = Sound_MaxActive_HW; |
| | 316 | } |
| | 317 | |
| | 318 | int newIndex = FindFreeActiveSound(min, max); |
| | 319 | |
| | 320 | if((newIndex == SOUND_NOACTIVEINDEX) && !(GameSounds[soundNumber].flags & SAMPLE_IN_SW)) |
| | 321 | { |
| | 322 | //failed to find a free hardware slot , so try software slot instead. |
| | 323 | //mainly to cope with cards that can load sounds into hardware , but can't play them there |
| | 324 | newIndex = FindFreeActiveSound(0, SOUND_MAXACTIVE_SW); |
| | 325 | } |
| | 326 | |
| | 327 | if(newIndex == SOUND_NOACTIVEINDEX) |
| | 328 | { |
| | 329 | printf("Having to stop another sound.\n"); |
| | 330 | /* try to find an existing sound with a lower priority */ |
| | 331 | newIndex = FindLowerPriorityActiveSound(priority, min, max); |
| | 332 | |
| | 333 | if(newIndex == SOUND_NOACTIVEINDEX && !(GameSounds[soundNumber].flags & SAMPLE_IN_SW)) |
| | 334 | { |
| | 335 | //failed to find a free hardware slot , so try software slot instead. |
| | 336 | //mainly to cope with cards that can load sounds into hardware , but can't play them there |
| | 337 | newIndex = FindLowerPriorityActiveSound(priority, 0, SOUND_MAXACTIVE_SW); |
| | 338 | } |
| | 339 | |
| | 340 | if(newIndex == SOUND_NOACTIVEINDEX) |
| | 341 | { |
| | 342 | printf("Failed to find a lower priority sound.\n"); |
| | 343 | return; /* give up */ |
| | 344 | } |
| | 345 | |
| | 346 | /* remove it, and use it's slot */ |
| | 347 | printf("Stopping a lower priority sound.\n"); |
| | 348 | Sound_Stop(newIndex); |
| | 349 | } |
| | 350 | //else printf("Found a free slot.\n"); |
| | 351 | |
| | 352 | if (GameSounds[soundNumber].loaded) |
| | 353 | { |
| | 354 | ActiveSounds[newIndex].soundIndex = soundNumber; |
| | 355 | ActiveSounds[newIndex].priority = priority; |
| | 356 | ActiveSounds[newIndex].volume = volume; |
| | 357 | ActiveSounds[newIndex].pitch = pitch; |
| | 358 | ActiveSounds[newIndex].externalRef = externalRef; |
| | 359 | ActiveSounds[newIndex].loop = loop; |
| | 360 | ActiveSounds[newIndex].threedee = 0; |
| | 361 | ActiveSounds[newIndex].paused = 0; |
| | 362 | ActiveSounds[newIndex].marine_ignore = marine_ignore; |
| | 363 | ActiveSounds[newIndex].reverb_off = reverb_off; |
| | 364 | |
| | 365 | if(worldPosn) |
| | 366 | { |
| | 367 | ActiveSounds[newIndex].threedeedata.position = *worldPosn; |
| | 368 | ActiveSounds[newIndex].threedeedata.outer_range = 32000; |
| | 369 | ActiveSounds[newIndex].threedee = 1; |
| | 370 | } |
| | 371 | else if(p_3ddata) |
| | 372 | { |
| | 373 | ActiveSounds[newIndex].threedeedata = *p_3ddata; |
| | 374 | ActiveSounds[newIndex].threedee = 1; |
| | 375 | } |
| | 376 | |
| | 377 | if(PlatPlaySound(newIndex)) |
| | 378 | { |
| | 379 | GameSounds[soundNumber].activeInstances++; |
| | 380 | |
| | 381 | if(externalRef) |
| | 382 | *externalRef = newIndex; |
| | 383 | } |
| | 384 | else |
| | 385 | { |
| | 386 | /* the sound failed to play: any platform cleanups should have been done, so just bank the sound slot */ |
| | 387 | ActiveSounds[newIndex].soundIndex = SID_NOSOUND; |
| | 388 | printf("Error: PlatPlaySound failed for %d.\n", soundNumber); |
| | 389 | } |
| | 390 | } |
| | 391 | else |
| | 392 | { |
| | 393 | printf("GameSound %d not loaded \n", soundNumber); |
| | 394 | } |
| | 395 | } |
| | 396 | |
| | 397 | void Sound_ChangeVolume(int activeSoundNumber, int volume) |
| | 398 | { |
| | 399 | if((activeSoundNumber < 0) || (activeSoundNumber >= SOUND_MAXACTIVE)) |
| | 400 | return; |
| | 401 | |
| | 402 | if(SID_NOSOUND != ActiveSounds[activeSoundNumber].soundIndex) |
| | 403 | { |
| | 404 | /* check the new volume is different from the old one */ |
| | 405 | if(volume != ActiveSounds[activeSoundNumber].volume) |
| | 406 | { |
| | 407 | ActiveSounds[activeSoundNumber].volume = (volume > VOLUME_MAX) ? VOLUME_MAX : (volume < VOLUME_MIN) ? VOLUME_MIN : volume; |
| | 408 | |
| | 409 | /* if we're a 2d sound, just change the volume, but if we're 3d then call 3d update instead */ |
| | 410 | |
| | 411 | if(ActiveSounds[activeSoundNumber].threedee) |
| | 412 | PlatDo3dSound(activeSoundNumber); |
| | 413 | else |
| | 414 | PlatChangeSoundVolume(activeSoundNumber,ActiveSounds[activeSoundNumber].volume); |
| | 415 | } |
| | 416 | } |
| | 417 | } |
| | 418 | |
| | 419 | void Sound_ChangePitch(int activeSoundNumber, int pitch) |
| | 420 | { |
| | 421 | if((activeSoundNumber < 0) || (activeSoundNumber >= SOUND_MAXACTIVE)) |
| | 422 | return; |
| | 423 | |
| | 424 | if(SID_NOSOUND != ActiveSounds[activeSoundNumber].soundIndex) |
| | 425 | { |
| | 426 | if(pitch != ActiveSounds[activeSoundNumber].pitch) |
| | 427 | { |
| | 428 | ActiveSounds[activeSoundNumber].pitch = (pitch > PITCH_MAX) ? PITCH_MAX : (pitch < PITCH_MIN) ? PITCH_MIN : pitch; |
| | 429 | PlatChangeSoundPitch(activeSoundNumber,ActiveSounds[activeSoundNumber].pitch); |
| | 430 | } |
| | 431 | } |
| | 432 | } |
| | 433 | |
| | 434 | void Sound_Update3d(int activeSoundNumber, VECTORCH* posn) |
| | 435 | { |
| | 436 | if((activeSoundNumber < 0) || (activeSoundNumber >= SOUND_MAXACTIVE)) |
| | 437 | return; |
| | 438 | |
| | 439 | /* Check there's a sound in this slot */ |
| | 440 | if(ActiveSounds[activeSoundNumber].soundIndex != SID_NOSOUND) |
| | 441 | ActiveSounds[activeSoundNumber].threedeedata.position = *posn; |
| | 442 | } |
| | 443 | |
| | 444 | void Sound_UpdateNew3d(int activeSoundNumber, SOUND3DDATA * s3d) |
| | 445 | { |
| | 446 | if((activeSoundNumber < 0) || (activeSoundNumber >= SOUND_MAXACTIVE)) |
| | 447 | return; |
| | 448 | |
| | 449 | /* Check there's a sound in this slot */ |
| | 450 | if(ActiveSounds[activeSoundNumber].soundIndex != SID_NOSOUND) |
| | 451 | ActiveSounds[activeSoundNumber].threedeedata = *s3d; |
| | 452 | } |
| | 453 | |
| | 454 | static SOUNDINDEX GetSoundIndexFromNameAndIndex(const char* name, SOUNDINDEX index) |
| | 455 | { |
| | 456 | int i = 0; |
| | 457 | |
| | 458 | if(index >= 0 && index < SID_MAXIMUM) |
| | 459 | { |
| | 460 | if(GameSounds[index].loaded) |
| | 461 | { |
| | 462 | if(!strcmp(GameSounds[index].wavName, name)) |
| | 463 | return index; |
| | 464 | } |
| | 465 | } |
| | 466 | |
| | 467 | for(; i < SID_MAXIMUM; i++) |
| | 468 | { |
| | 469 | if(GameSounds[i].loaded) |
| | 470 | { |
| | 471 | if(!strcmp(GameSounds[i].wavName, name)) |
| | 472 | return (SOUNDINDEX) i; |
| | 473 | } |
| | 474 | } |
| | 475 | |
| | 476 | return SID_NOSOUND; |
| | 477 | } |
| | 478 | |
| | 479 | #include "savegame.h" |
| | 480 | |
| | 481 | typedef struct sound_save_block |
| | 482 | { |
| | 483 | SAVE_BLOCK_HEADER header; |
| | 484 | |
| | 485 | int fileNameLength; |
| | 486 | |
| | 487 | SOUNDINDEX soundIndex; |
| | 488 | ACTIVESOUNDPRIORITY priority; |
| | 489 | int volume; |
| | 490 | int pitch; |
| | 491 | unsigned int loop :1; |
| | 492 | unsigned int threedee :1; |
| | 493 | unsigned int marine_ignore :1; |
| | 494 | unsigned int reverb_off :1; |
| | 495 | unsigned int externalRef :1; |
| | 496 | SOUND3DDATA threedeedata; |
| | 497 | int position; |
| | 498 | |
| | 499 | } SOUND_SAVE_BLOCK; |
| | 500 | |
| | 501 | //defines for load/save macros |
| | 502 | #define SAVELOAD_BLOCK block |
| | 503 | #define SAVELOAD_BEHAV sound |
| | 504 | |
| | 505 | void Load_SoundState(int* soundHandle) |
| | 506 | { |
| | 507 | if(!soundHandle) |
| | 508 | return; |
| | 509 | |
| | 510 | SOUND_SAVE_BLOCK* block = (SOUND_SAVE_BLOCK*)GetNextBlockIfOfType(SaveBlock_SoundState); |
| | 511 | |
| | 512 | if(!block) |
| | 513 | return; |
| | 514 | |
| | 515 | const char* name = (const char*)(block+1); |
| | 516 | //stop the current sound |
| | 517 | |
| | 518 | if(*soundHandle == SOUND_NOACTIVEINDEX) |
| | 519 | Sound_Stop(*soundHandle); |
| | 520 | |
| | 521 | //check the size |
| | 522 | if(block->header.size != sizeof(*block) + block->fileNameLength) |
| | 523 | return; |
| | 524 | |
| | 525 | SOUNDINDEX soundIndex = GetSoundIndexFromNameAndIndex(name,block->soundIndex); |
| | 526 | |
| | 527 | if(soundIndex == SID_NOSOUND) |
| | 528 | return; |
| | 529 | |
| | 530 | char playOptions[20] = "evpP"; |
| | 531 | |
| | 532 | if(block->loop) |
| | 533 | strcat(playOptions,"l"); |
| | 534 | |
| | 535 | if(block->marine_ignore) |
| | 536 | strcat(playOptions,"m"); |
| | 537 | |
| | 538 | if(block->reverb_off) |
| | 539 | strcat(playOptions,"r"); |
| | 540 | |
| | 541 | if(block->priority == ASP_Maximum) |
| | 542 | strcat(playOptions,"h"); |
| | 543 | |
| | 544 | if(block->threedee) |
| | 545 | { |
| | 546 | strcat(playOptions,"n"); |
| | 547 | Sound_Play(soundIndex, playOptions, soundHandle, block->volume, block->pitch, block->position, &block->threedeedata); |
| | 548 | } |
| | 549 | else |
| | 550 | { |
| | 551 | Sound_Play(soundIndex, playOptions, soundHandle, block->volume, block->pitch, block->position); |
| | 552 | } |
| | 553 | } |
| | 554 | |
| | 555 | void Save_SoundState(int* soundHandle) |
| | 556 | { |
| | 557 | if(!soundHandle) |
| | 558 | return; |
| | 559 | |
| | 560 | if(*soundHandle == SOUND_NOACTIVEINDEX) |
| | 561 | { |
| | 562 | SAVE_BLOCK_HEADER* header; |
| | 563 | GET_SAVE_BLOCK_POINTER(header); |
| | 564 | |
| | 565 | //fill in the header |
| | 566 | header->size = sizeof(*header); |
| | 567 | header->type = SaveBlock_SoundState; |
| | 568 | } |
| | 569 | else |
| | 570 | { |
| | 571 | ACTIVESOUNDSAMPLE* sound = &ActiveSounds[*soundHandle]; |
| | 572 | |
| | 573 | const char* name = GameSounds[sound->soundIndex].wavName; |
| | 574 | int name_length = strlen(name) + 1; |
| | 575 | |
| | 576 | SOUND_SAVE_BLOCK* block = GetPointerForSaveBlock(sizeof(*block)+name_length); |
| | 577 | |
| | 578 | //fill in the header |
| | 579 | block->header.size = sizeof(*block) + name_length; |
| | 580 | block->header.type = SaveBlock_SoundState; |
| | 581 | |
| | 582 | COPYELEMENT_SAVE(soundIndex) |
| | 583 | COPYELEMENT_SAVE(priority) |
| | 584 | COPYELEMENT_SAVE(volume) |
| | 585 | COPYELEMENT_SAVE(pitch) |
| | 586 | COPYELEMENT_SAVE(loop) |
| | 587 | COPYELEMENT_SAVE(threedee) |
| | 588 | COPYELEMENT_SAVE(marine_ignore) |
| | 589 | COPYELEMENT_SAVE(reverb_off) |
| | 590 | COPYELEMENT_SAVE(threedeedata) |
| | 591 | block->externalRef = 1; |
| | 592 | |
| | 593 | block->position = 0; |
| | 594 | block->fileNameLength = name_length; |
| | 595 | |
| | 596 | //the volume in the active sound list is scaled differently from the volume used |
| | 597 | //by Sound_Play |
| | 598 | block->volume <<= 7; |
| | 599 | block->volume /= VOLUME_PLAT2DSCALE; |
| | 600 | |
| | 601 | block->position = 0; |
| | 602 | |
| | 603 | //fprintf(stderr, "Save_SoundState: GetCurrentPosition!\n"); |
| | 604 | strcpy((char*)(block+1),name); |
| | 605 | } |
| | 606 | } |
| | 607 | |
| | 608 | void Load_SoundState_NoRef(SAVE_BLOCK_HEADER* header) |
| | 609 | { |
| | 610 | SOUND_SAVE_BLOCK* block = (SOUND_SAVE_BLOCK*) header; |
| | 611 | const char* name = (const char*)(block+1); |
| | 612 | |
| | 613 | //check the size |
| | 614 | if(block->header.size != sizeof(*block) + block->fileNameLength) |
| | 615 | return; |
| | 616 | |
| | 617 | //only load if the sound doesn't require an external reference |
| | 618 | if(block->externalRef) |
| | 619 | return; |
| | 620 | |
| | 621 | SOUNDINDEX soundIndex = GetSoundIndexFromNameAndIndex(name,block->soundIndex); |
| | 622 | |
| | 623 | if(soundIndex != SID_NOSOUND) |
| | 624 | { |
| | 625 | char playOptions[20] = "vpP"; |
| | 626 | |
| | 627 | if(block->marine_ignore) |
| | 628 | strcat(playOptions, "m"); |
| | 629 | |
| | 630 | if(block->reverb_off) |
| | 631 | strcat(playOptions, "r"); |
| | 632 | |
| | 633 | if(block->priority == ASP_Maximum) |
| | 634 | strcat(playOptions, "h"); |
| | 635 | |
| | 636 | if(block->threedee) |
| | 637 | { |
| | 638 | strcat(playOptions, "n"); |
| | 639 | Sound_Play(soundIndex, playOptions, block->volume, block->pitch, block->position, &block->threedeedata); |
| | 640 | } |
| | 641 | else |
| | 642 | { |
| | 643 | Sound_Play(soundIndex, playOptions, block->volume, block->pitch, block->position); |
| | 644 | } |
| | 645 | } |
| | 646 | } |
| | 647 | |
| | 648 | void Save_SoundsWithNoReference() |
| | 649 | { |
| | 650 | int i = 0; |
| | 651 | |
| | 652 | for(; i < SOUND_MAXACTIVE; i++) |
| | 653 | { |
| | 654 | if(ActiveSounds[i].soundIndex != SID_NOSOUND) |
| | 655 | { |
| | 656 | if(!ActiveSounds[i].externalRef) |
| | 657 | { |
| | 658 | ACTIVESOUNDSAMPLE* sound = &ActiveSounds[i]; |
| | 659 | SOUND_SAVE_BLOCK* block; |
| | 660 | |
| | 661 | const char* name = GameSounds[sound->soundIndex].wavName; |
| | 662 | int name_length = strlen(name) + 1; |
| | 663 | |
| | 664 | block = GetPointerForSaveBlock(sizeof(*block)+name_length); |
| | 665 | |
| | 666 | //fill in the header |
| | 667 | block->header.size = sizeof(*block) + name_length; |
| | 668 | block->header.type = SaveBlock_SoundState; |
| | 669 | |
| | 670 | COPYELEMENT_SAVE(soundIndex) |
| | 671 | COPYELEMENT_SAVE(priority) |
| | 672 | COPYELEMENT_SAVE(volume) |
| | 673 | COPYELEMENT_SAVE(pitch) |
| | 674 | COPYELEMENT_SAVE(loop) |
| | 675 | COPYELEMENT_SAVE(threedee) |
| | 676 | COPYELEMENT_SAVE(marine_ignore) |
| | 677 | COPYELEMENT_SAVE(reverb_off) |
| | 678 | COPYELEMENT_SAVE(threedeedata) |
| | 679 | block->externalRef = 0; |
| | 680 | block->position = 0; |
| | 681 | block->fileNameLength = name_length; |
| | 682 | |
| | 683 | //the volume in the active sound list is scaled differently from the volume used by Sound_Play |
| | 684 | block->volume <<= 7; |
| | 685 | block->volume /= VOLUME_PLAT2DSCALE; |
| | 686 | |
| | 687 | block->position = 0; |
| | 688 | |
| | 689 | strcpy((char*)(block+1),name); |
| | 690 | } |
| | 691 | } |
| | 692 | } |
| | 693 | } |