| 1 |
/* |
| 2 |
* Descent 3 |
| 3 |
* Copyright (C) 2024 Parallax Software |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation, either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <algorithm> |
| 20 |
#include <cstdio> |
| 21 |
#include <cstdlib> |
| 22 |
#include <cstdarg> |
| 23 |
|
| 24 |
#include <SDL.h> |
| 25 |
|
| 26 |
#include "pserror.h" |
| 27 |
#include "mem.h" |
| 28 |
#include "mono.h" |
| 29 |
#include "ssl_lib.h" |
| 30 |
#include "application.h" |
| 31 |
#include "sdlsound.h" |
| 32 |
#include "mixer.h" |
| 33 |
#include "ddio.h" |
| 34 |
#include "args.h" |
| 35 |
|
| 36 |
#define SOUNDLIB_SAMPLE_RATE 22050 |
| 37 |
#define SOUNDLIB_SAMPLE_SIZE 16 |
| 38 |
#define SOUNDLIB_CHANNELS 2 |
| 39 |
#define SOUNDLIB_DEFAULT_SAMPLES 1024 |
| 40 |
|
| 41 |
// =============================== |
| 42 |
#define MAX_SOUNDS_PLAYING_AT_ONCE 256 |
| 43 |
static sound_buffer_info sound_cache[MAX_SOUNDS_PLAYING_AT_ONCE]; |
| 44 |
static int sound_buffer_size = MAX_SOUNDS_PLAYING_AT_ONCE; |
| 45 |
|
| 46 |
lnxsound *ll_sound_ptr; |
| 47 |
|
| 48 |
// A peroidic mixer that uses the primary buffer as a stream buffer |
| 49 |
static void StreamAudio(void *user_ptr, Uint8 *stream, int len); |
| 50 |
|
| 51 |
lnxsound::lnxsound() : llsSystem() { |
| 52 |
ll_sound_ptr = this; |
| 53 |
sound_device = 0; |
| 54 |
in_at_exit = false; |
| 55 |
} |
| 56 |
|
| 57 |
lnxsound::~lnxsound() { |
| 58 |
in_at_exit = true; |
| 59 |
DestroySoundLib(); |
| 60 |
SetSoundCard(nullptr); |
| 61 |
} |
| 62 |
|
| 63 |
// Starts the sound library, maybe have it send back some information -- 3d support? |
| 64 |
int lnxsound::InitSoundLib(char mixer_type, oeApplication *sos, uint8_t max_sounds_played) { |
| 65 |
SDL_AudioSpec spec; |
| 66 |
|
| 67 |
// setup mixer |
| 68 |
tMixerInit mi; |
| 69 |
mi.primary_buffer = nullptr; |
| 70 |
mi.primary_frequency = SOUNDLIB_SAMPLE_RATE; |
| 71 |
mi.max_sounds_available = &sound_buffer_size; |
| 72 |
mi.sound_cache = sound_cache; |
| 73 |
mi.primary_alignment = SOUNDLIB_CHANNELS * (SOUNDLIB_SAMPLE_SIZE >> 3); |
| 74 |
mi.fp_SetError = lnxsound_SetError; |
| 75 |
mi.fp_ErrorText = lnxsound_ErrorText; |
| 76 |
mi.p_error_code = &m_lib_error_code; |
| 77 |
mi.ll_sound_ptr = ll_sound_ptr; |
| 78 |
|
| 79 |
if (!m_mixer.Initialize(&mi)) { |
| 80 |
return false; |
| 81 |
} // if |
| 82 |
|
| 83 |
int sampleCount = SOUNDLIB_DEFAULT_SAMPLES; |
| 84 |
int sampleArgIndex = FindArg("-sdlSndSize"); |
| 85 |
if (sampleArgIndex != 0) { |
| 86 |
const char *sampleCountStr = GetArg(sampleArgIndex + 1); |
| 87 |
if (sampleCountStr) { |
| 88 |
sampleCount = atoi(sampleCountStr); |
| 89 |
if (sampleCount <= 0) { |
| 90 |
sampleCount = SOUNDLIB_DEFAULT_SAMPLES; |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
spec.freq = SOUNDLIB_SAMPLE_RATE; |
| 96 |
spec.format = SOUNDLIB_SAMPLE_SIZE == 8 ? AUDIO_U8 : AUDIO_S16SYS; |
| 97 |
spec.channels = SOUNDLIB_CHANNELS; |
| 98 |
spec.samples = sampleCount; |
| 99 |
spec.callback = StreamAudio; |
| 100 |
spec.userdata = &m_mixer; |
| 101 |
|
| 102 |
sound_device = SDL_OpenAudioDevice(nullptr, 0, &spec, nullptr, 0); |
| 103 |
if (sound_device == 0) { |
| 104 |
strcpy(m_error_text, SDL_GetError()); |
| 105 |
return false; |
| 106 |
} |
| 107 |
|
| 108 |
mprintf(0, "Sound: Hardware configured. Kicking off stream thread..."); |
| 109 |
SDL_PauseAudioDevice(sound_device, 0); |
| 110 |
|
| 111 |
m_total_sounds_played = 0; |
| 112 |
m_cur_sounds_played = 0; |
| 113 |
m_in_sound_frame = false; |
| 114 |
m_pending_actions = false; |
| 115 |
m_cache_stress_timer = 0.0f; |
| 116 |
m_timer_last_frametime = -1; |
| 117 |
m_sound_quality = SQT_HIGH; |
| 118 |
|
| 119 |
return true; |
| 120 |
} |
| 121 |
|
| 122 |
bool lnxsound::GetDeviceSettings(SDL_AudioDeviceID *device, uint32_t *freq, uint32_t *bit_depth, |
| 123 |
uint32_t *channels) const { |
| 124 |
if (sound_device == 0) |
| 125 |
return false; |
| 126 |
|
| 127 |
*device = sound_device; |
| 128 |
*freq = SOUNDLIB_SAMPLE_RATE; |
| 129 |
*bit_depth = SOUNDLIB_SAMPLE_SIZE; |
| 130 |
*channels = SOUNDLIB_CHANNELS; |
| 131 |
|
| 132 |
return true; |
| 133 |
} |
| 134 |
|
| 135 |
// Cleans up after the Sound Library |
| 136 |
void lnxsound::DestroySoundLib() { |
| 137 |
if (sound_device) { |
| 138 |
SDL_CloseAudioDevice(sound_device); |
| 139 |
sound_device = 0; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
// Locks and unlocks sounds (used when changing play_info data) |
| 144 |
bool lnxsound::LockSound(int sound_uid) { return false; } |
| 145 |
|
| 146 |
bool lnxsound::UnlockSound(int sound_uid) { return false; } |
| 147 |
|
| 148 |
bool lnxsound::SetSoundQuality(char quality) { |
| 149 |
int i; |
| 150 |
|
| 151 |
if (quality == m_sound_quality) |
| 152 |
return true; |
| 153 |
|
| 154 |
// pause any sounds that may be playing |
| 155 |
PauseSounds(); |
| 156 |
|
| 157 |
if (quality == SQT_NORMAL) { |
| 158 |
m_sound_quality = SQT_NORMAL; |
| 159 |
} else { |
| 160 |
m_sound_quality = SQT_HIGH; |
| 161 |
} |
| 162 |
|
| 163 |
for (i = 0; i < MAX_SOUNDS; i++) { |
| 164 |
if (Sounds[i].used != 0) { |
| 165 |
int j = Sounds[i].sample_index; |
| 166 |
|
| 167 |
if (SoundFiles[j].sample_8bit && m_sound_quality == SQT_HIGH) { |
| 168 |
mem_free(SoundFiles[j].sample_8bit); |
| 169 |
SoundFiles[j].sample_8bit = nullptr; |
| 170 |
|
| 171 |
CheckAndForceSoundDataAlloc(i); |
| 172 |
} |
| 173 |
if (SoundFiles[j].sample_16bit && m_sound_quality == SQT_NORMAL) { |
| 174 |
int count; |
| 175 |
|
| 176 |
ASSERT(SoundFiles[j].sample_8bit == nullptr); |
| 177 |
SoundFiles[j].sample_8bit = (uint8_t *)mem_malloc(SoundFiles[j].sample_length); |
| 178 |
|
| 179 |
// NOTE: Interesting note on sound conversion: 16 bit sounds are signed (0 biase). 8 bit sounds are unsigned |
| 180 |
// (+128 biase). |
| 181 |
for (count = 0; count < (int)SoundFiles[j].sample_length; count++) { |
| 182 |
SoundFiles[j].sample_8bit[count] = (uint8_t)((((int)SoundFiles[j].sample_16bit[count]) + 32767) >> 8); |
| 183 |
} |
| 184 |
|
| 185 |
mem_free(SoundFiles[j].sample_16bit); |
| 186 |
SoundFiles[j].sample_16bit = nullptr; |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
ResumeSounds(); |
| 192 |
|
| 193 |
return true; |
| 194 |
} |
| 195 |
|
| 196 |
char lnxsound::GetSoundQuality() { return m_sound_quality; } |
| 197 |
|
| 198 |
bool lnxsound::SetSoundMixer(char mixer_type) { return true; } |
| 199 |
|
| 200 |
char lnxsound::GetSoundMixer() { return SOUND_MIXER_SOFTWARE_16; } |
| 201 |
|
| 202 |
// Determines if a sound will play. Takes into account maximum allowable |
| 203 |
// sounds. |
| 204 |
// Also put prioritization code in here |
| 205 |
// ignore reserved slots |
| 206 |
#ifdef _DEBUG |
| 207 |
int16_t lnxsound::FindFreeSoundSlot(int sound_index, float volume, int priority) |
| 208 |
#else |
| 209 |
int16_t lnxsound::FindFreeSoundSlot(float volume, int priority) |
| 210 |
#endif |
| 211 |
{ |
| 212 |
int current_slot; |
| 213 |
sound_buffer_info *sb; |
| 214 |
|
| 215 |
for (current_slot = 0; current_slot < MAX_SOUNDS_PLAYING_AT_ONCE; current_slot++) { |
| 216 |
sb = &sound_cache[current_slot]; |
| 217 |
if (sb->m_status == SSF_UNUSED) { |
| 218 |
return current_slot; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
// no more slots? take priority into account. |
| 223 |
// throw out lowest priority sound slot (must be lower than or equal to new sound priority) |
| 224 |
float weighted_priority = (priority * 2.0f) * volume; |
| 225 |
if (current_slot == MAX_SOUNDS_PLAYING_AT_ONCE) { |
| 226 |
int throw_out_slot = -1, equiv_priority_slot = -1; |
| 227 |
float weighted_priorityA, weighted_priorityB; |
| 228 |
for (current_slot = 0; current_slot < MAX_SOUNDS_PLAYING_AT_ONCE; current_slot++) { |
| 229 |
sb = &sound_cache[current_slot]; |
| 230 |
if (!(sb->m_status & (SSF_PLAY_LOOPING + SSF_PLAY_STREAMING))) { |
| 231 |
weighted_priorityA = sb->play_info->priority * 2.0f * sb->m_volume; |
| 232 |
if (weighted_priorityA < weighted_priority) { |
| 233 |
if (throw_out_slot == -1) { |
| 234 |
throw_out_slot = current_slot; |
| 235 |
} else { |
| 236 |
play_information *play_info2 = sound_cache[throw_out_slot].play_info; |
| 237 |
weighted_priorityB = play_info2->priority * 2.0f * sb->m_volume; |
| 238 |
if (weighted_priorityB > weighted_priorityA) { |
| 239 |
throw_out_slot = current_slot; |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
else if (equiv_priority_slot == -1 && weighted_priorityA == weighted_priority) { |
| 245 |
equiv_priority_slot = current_slot; |
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
// if no slot found to stop, look for a slot with priority == new priority |
| 251 |
if (throw_out_slot == -1) { |
| 252 |
throw_out_slot = equiv_priority_slot; |
| 253 |
} |
| 254 |
if (throw_out_slot > -1) { |
| 255 |
sb = &sound_cache[throw_out_slot]; |
| 256 |
StopSound(sb->m_unique_id, SKT_HOLD_UNTIL_STOP); |
| 257 |
mprintf(0, "DDSNDLIB: Replace sound (p:%d) with sound (p:%d) in slot %d\n", sb->play_info->priority, priority, |
| 258 |
throw_out_slot); |
| 259 |
return throw_out_slot; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
#ifdef _DEBUG |
| 264 |
if (sound_index > -1) { |
| 265 |
mprintf(0, "DDSNDLIB: Sound %s with priority (%d) too low.\n", Sounds[sound_index].name, priority); |
| 266 |
} else { |
| 267 |
mprintf(0, "DDSNDLIB: Sound unknown with priority (%d) too low.\n", priority); |
| 268 |
} |
| 269 |
#endif |
| 270 |
return -1; |
| 271 |
} |
| 272 |
|
| 273 |
// Plays a 2d sound |
| 274 |
int lnxsound::PlaySound2d(play_information *play_info, int sound_index, float f_volume, float f_pan, bool f_looped) { |
| 275 |
sound_buffer_info *sb; |
| 276 |
int16_t sound_slot; |
| 277 |
|
| 278 |
if (sound_device == 0) { |
| 279 |
return -1; |
| 280 |
} |
| 281 |
|
| 282 |
// calculate volume and pan |
| 283 |
f_volume = (f_volume < 0.0f) ? 0.0f : (f_volume > 1.0f) ? 1.0f : f_volume; |
| 284 |
play_info->left_volume = play_info->right_volume = f_volume; |
| 285 |
|
| 286 |
f_pan = (f_pan < -1.0f) ? -1.0f : (f_pan > 1.0f) ? 1.0f : f_pan; |
| 287 |
if (f_pan < 0.0) { |
| 288 |
play_info->right_volume += f_volume * f_pan; |
| 289 |
} else { |
| 290 |
play_info->left_volume -= f_volume * f_pan; |
| 291 |
} |
| 292 |
|
| 293 |
// do common processing. |
| 294 |
if (SoundFiles[Sounds[sound_index].sample_index].used == 0) { |
| •295 |
mprintf(0, "Tried to play %d sound, it DNE.\n", sound_index); |
| 296 |
return -1; |
| 297 |
} |
| 298 |
#ifdef _DEBUG |
| 299 |
sound_slot = FindFreeSoundSlot(sound_index, f_volume, play_info->priority); |
| 300 |
#else |
| 301 |
sound_slot = FindFreeSoundSlot(f_volume, play_info->priority); |
| 302 |
#endif |
| 303 |
if (sound_slot < 0) { |
| 304 |
// do prioritization code here. |
| 305 |
return -1; |
| 306 |
} |
| 307 |
sb = &sound_cache[sound_slot]; |
| 308 |
m_total_sounds_played++; |
| 309 |
sb->play_info = play_info; |
| 310 |
sb->m_unique_id = MakeUniqueId(sound_slot); |
| 311 |
sb->m_buffer_type = SBT_2D; |
| 312 |
sb->m_sound_index = sound_index; |
| 313 |
sb->m_status = SSF_UNUSED; |
| 314 |
|
| 315 |
ASSERT(sb->m_unique_id != -1); |
| 316 |
|
| 317 |
// play 2d sound |
| 318 |
sb->m_status = (f_looped) ? SSF_PLAY_LOOPING : SSF_PLAY_NORMAL; |
| 319 |
return sb->m_unique_id; |
| 320 |
} |
| 321 |
|
| 322 |
// This function limits the number of sounds cached to 255(8bits) and 256 bit is for invalid channel |
| 323 |
// The purpose is to create unique signatures for each sound played (and allow for |
| 324 |
// the slot_number to be quickly determined) |
| 325 |
inline int lnxsound::MakeUniqueId(int sound_slot) const { return ((((int)m_total_sounds_played) << 8) + sound_slot); } |
| 326 |
|
| 327 |
inline int lnxsound::ValidateUniqueId(int sound_uid) { |
| 328 |
if (sound_uid == sound_cache[sound_uid & 0x00FF].m_unique_id) { |
| 329 |
return sound_uid & 0x00FF; |
| 330 |
} else { |
| 331 |
return -1; |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
int lnxsound::PlayStream(play_information *play_info) { |
| 336 |
int16_t sound_slot; |
| 337 |
|
| 338 |
ASSERT(play_info != nullptr); |
| 339 |
|
| 340 |
float volume = std::max(play_info->left_volume, play_info->right_volume); |
| 341 |
|
| 342 |
if (sound_device == 0) |
| 343 |
return -1; |
| 344 |
|
| 345 |
#ifdef _DEBUG |
| 346 |
sound_slot = FindFreeSoundSlot(-1, volume, play_info->priority); |
| 347 |
#else |
| 348 |
sound_slot = FindFreeSoundSlot(volume, play_info->priority); |
| 349 |
#endif |
| 350 |
// Out of sound slots |
| 351 |
if (sound_slot < 0) { |
| 352 |
return -1; |
| 353 |
} |
| 354 |
|
| 355 |
m_total_sounds_played++; |
| 356 |
sound_cache[sound_slot].play_info = play_info; |
| 357 |
|
| 358 |
sound_cache[sound_slot].m_unique_id = MakeUniqueId(sound_slot); |
| 359 |
ASSERT(sound_cache[sound_slot].m_unique_id != -1); |
| 360 |
|
| 361 |
sound_cache[sound_slot].m_buffer_type = SBT_2D; |
| 362 |
sound_cache[sound_slot].m_status = SSF_PLAY_STREAMING; |
| 363 |
|
| 364 |
m_cur_sounds_played++; |
| 365 |
|
| 366 |
return (sound_cache[sound_slot].m_unique_id); |
| 367 |
} |
| 368 |
|
| 369 |
void lnxsound::SetListener(pos_state *cur_pos) { |
| 370 |
if (sound_device == 0) |
| 371 |
return; |
| 372 |
|
| 373 |
m_emulated_listener.orient = *cur_pos->orient; |
| 374 |
m_emulated_listener.position = *cur_pos->position; |
| 375 |
m_emulated_listener.velocity = *cur_pos->velocity; |
| 376 |
} |
| 377 |
|
| 378 |
int lnxsound::PlaySound3d(play_information *play_info, int sound_index, pos_state *cur_pos, float adjusted_volume, |
| 379 |
bool f_looped, float reverb) //, uint16_t frequency |
| 380 |
{ |
| 381 |
float volume = adjusted_volume; // Adjust base volume by sent volume, let 3d stuff do the rest |
| 382 |
|
| 383 |
if (sound_device == 0) |
| 384 |
return -1; |
| 385 |
|
| 386 |
ASSERT(Sounds[sound_index].used != 0); |
| 387 |
if (Sounds[sound_index].used == 0) |
| 388 |
return -1; |
| 389 |
|
| 390 |
float dist; |
| 391 |
vector dir_to_sound = *cur_pos->position - m_emulated_listener.position; |
| 392 |
float pan; |
| 393 |
|
| 394 |
dist = vm_NormalizeVector(&dir_to_sound); |
| 395 |
if (dist < .1f) { |
| 396 |
dir_to_sound = m_emulated_listener.orient.fvec; |
| 397 |
} |
| 398 |
|
| 399 |
if (dist >= Sounds[sound_index].max_distance) { |
| 400 |
return -1; |
| 401 |
} else if (dist > Sounds[sound_index].min_distance) { |
| 402 |
volume *= (1.0f - ((dist - Sounds[sound_index].min_distance) / |
| 403 |
(Sounds[sound_index].max_distance - Sounds[sound_index].min_distance))); |
| 404 |
} |
| 405 |
|
| 406 |
pan = (dir_to_sound * m_emulated_listener.orient.rvec); |
| 407 |
|
| 408 |
if (volume < 0.0f) |
| 409 |
volume = 0.0f; |
| 410 |
else if (volume > 1.0f) |
| 411 |
volume = 1.0f; |
| 412 |
|
| 413 |
if (pan < -1.0f) |
| 414 |
pan = -1.0f; |
| 415 |
else if (pan > 1.0f) |
| 416 |
pan = 1.0f; |
| 417 |
|
| 418 |
return PlaySound2d(play_info, sound_index, volume, pan, f_looped); |
| 419 |
} |
| 420 |
|
| 421 |
void lnxsound::AdjustSound(int sound_uid, float f_volume, float f_pan, uint16_t frequency) { |
| 422 |
int current_slot; |
| 423 |
|
| 424 |
if (sound_device == 0) |
| 425 |
return; |
| 426 |
|
| 427 |
if ((current_slot = ValidateUniqueId(sound_uid)) == -1) |
| 428 |
return; |
| 429 |
if (sound_cache[current_slot].m_status == SSF_UNUSED) |
| 430 |
return; |
| 431 |
|
| 432 |
sound_buffer_info *sb = &sound_cache[current_slot]; |
| 433 |
play_information *play_info = sb->play_info; |
| 434 |
|
| 435 |
play_info->left_volume = play_info->right_volume = f_volume; |
| 436 |
if (f_pan < 0.0) |
| 437 |
play_info->right_volume += f_volume * f_pan; |
| 438 |
else |
| 439 |
play_info->left_volume -= f_volume * f_pan; |
| 440 |
} |
| 441 |
|
| 442 |
void lnxsound::AdjustSound(int sound_uid, pos_state *cur_pos, float adjusted_volume, float reverb) { |
| 443 |
if (sound_device == 0) |
| 444 |
return; |
| 445 |
|
| 446 |
int current_slot; |
| 447 |
|
| 448 |
if ((current_slot = ValidateUniqueId(sound_uid)) == -1) |
| 449 |
return; |
| 450 |
if (sound_cache[current_slot].m_status == SSF_UNUSED) |
| 451 |
return; |
| 452 |
|
| 453 |
// We need to determine the pan and volume |
| 454 |
float volume; |
| 455 |
|
| 456 |
volume = adjusted_volume; |
| 457 |
|
| 458 |
float dist; |
| 459 |
vector dir_to_sound = *cur_pos->position - m_emulated_listener.position; |
| 460 |
float pan; |
| 461 |
|
| 462 |
dist = vm_NormalizeVector(&dir_to_sound); |
| 463 |
if (dist < .1f) { |
| 464 |
dir_to_sound = m_emulated_listener.orient.fvec; |
| 465 |
} |
| 466 |
|
| 467 |
if (dist >= Sounds[sound_cache[current_slot].m_sound_index].max_distance) { |
| 468 |
volume = 0.0f; |
| 469 |
} else if (dist > Sounds[sound_cache[current_slot].m_sound_index].min_distance) { |
| 470 |
volume *= (1.0f - ((dist - Sounds[sound_cache[current_slot].m_sound_index].min_distance) / |
| 471 |
(Sounds[sound_cache[current_slot].m_sound_index].max_distance - |
| 472 |
Sounds[sound_cache[current_slot].m_sound_index].min_distance))); |
| 473 |
} |
| 474 |
|
| 475 |
pan = (dir_to_sound * m_emulated_listener.orient.rvec); |
| 476 |
|
| 477 |
if (volume < 0.0f) |
| 478 |
volume = 0.0f; |
| 479 |
else if (volume > 1.0f) |
| 480 |
volume = 1.0f; |
| 481 |
|
| 482 |
if (pan < -1.0f) |
| 483 |
pan = -1.0f; |
| 484 |
else if (pan > 1.0f) |
| 485 |
pan = 1.0f; |
| 486 |
|
| 487 |
AdjustSound(sound_cache[current_slot].m_unique_id, volume, pan, 22050); |
| 488 |
} |
| 489 |
|
| 490 |
void lnxsound::StopAllSounds() { |
| 491 |
for (auto ¤t_slot : sound_cache) { |
| 492 |
if (current_slot.m_status != SSF_UNUSED) { |
| 493 |
StopSound(current_slot.m_unique_id); |
| 494 |
} |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
// Checks if a sound is playing (removes finished sound); |
| 499 |
bool lnxsound::IsSoundInstancePlaying(int sound_uid) { |
| 500 |
int current_slot; |
| 501 |
|
| 502 |
if (sound_device == 0) |
| 503 |
return false; |
| 504 |
|
| 505 |
if ((current_slot = ValidateUniqueId(sound_uid)) == -1) |
| 506 |
return false; |
| 507 |
|
| 508 |
if (sound_cache[current_slot].m_status != SSF_UNUSED) { |
| 509 |
return true; |
| 510 |
} |
| 511 |
|
| 512 |
return false; |
| 513 |
} |
| 514 |
|
| 515 |
int lnxsound::IsSoundPlaying(int sound_index) { |
| 516 |
if (sound_device == 0) |
| 517 |
return -1; |
| 518 |
|
| 519 |
for (auto ¤t_slot : sound_cache) { |
| 520 |
if ((current_slot.m_status != SSF_UNUSED) && (current_slot.m_sound_index == sound_index)) { |
| 521 |
return current_slot.m_unique_id; |
| 522 |
} |
| 523 |
} |
| 524 |
|
| 525 |
return -1; |
| 526 |
} |
| 527 |
|
| 528 |
// Stops 2d and 3d sounds |
| 529 |
void lnxsound::StopSound(int sound_uid, uint8_t f_immediately) { |
| 530 |
int current_slot; |
| 531 |
sound_buffer_info *sb; |
| 532 |
|
| 533 |
if (sound_device == 0) |
| 534 |
return; |
| 535 |
|
| 536 |
if ((current_slot = ValidateUniqueId(sound_uid)) == -1) |
| 537 |
return; |
| 538 |
|
| 539 |
sb = &sound_cache[current_slot]; |
| 540 |
|
| 541 |
if (sb->m_status == SSF_UNUSED) |
| 542 |
return; |
| 543 |
|
| 544 |
// update sound count. |
| 545 |
m_cur_sounds_played--; |
| 546 |
|
| 547 |
if (f_immediately == SKT_STOP_AFTER_LOOP) { |
| 548 |
sb->m_status &= ~SSF_PLAY_LOOPING; |
| 549 |
sb->m_status |= SSF_PLAY_NORMAL; |
| 550 |
return; |
| 551 |
} |
| 552 |
|
| 553 |
sound_cache[current_slot].m_status = SSF_UNUSED; |
| 554 |
} |
| 555 |
|
| 556 |
// Pause all sounds/resume all sounds |
| 557 |
void lnxsound::PauseSounds() { |
| 558 |
for (auto ¤t_slot : sound_cache) { |
| 559 |
sound_buffer_info *sb = ¤t_slot; |
| 560 |
if (sb->m_status != SSF_UNUSED && !(sb->m_status & SSF_PAUSED)) { |
| 561 |
sb->m_status |= SSF_PAUSED; |
| 562 |
} |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
void lnxsound::ResumeSounds() { |
| 567 |
for (auto ¤t_slot : sound_cache) { |
| 568 |
sound_buffer_info *sb = ¤t_slot; |
| 569 |
|
| 570 |
if (sb->m_status != SSF_UNUSED && (sb->m_status & SSF_PAUSED)) { |
| 571 |
current_slot.m_status &= (~SSF_PAUSED); |
| 572 |
} |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
void lnxsound::PauseSound(int sound_uid) { |
| 577 |
for (auto ¤t_slot : sound_cache) { |
| 578 |
sound_buffer_info *sb = ¤t_slot; |
| 579 |
if (sb->m_unique_id == sound_uid) { |
| 580 |
if (sb->m_status != SSF_UNUSED && !(sb->m_status & SSF_PAUSED)) { |
| 581 |
sb->m_status |= SSF_PAUSED; |
| 582 |
} |
| 583 |
break; |
| 584 |
} |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
void lnxsound::ResumeSound(int sound_uid) { |
| 589 |
for (auto ¤t_slot : sound_cache) { |
| 590 |
if (sound_uid == current_slot.m_unique_id) { |
| 591 |
if (current_slot.m_status != SSF_UNUSED && (current_slot.m_status & SSF_PAUSED)) { |
| 592 |
current_slot.m_status &= (~SSF_PAUSED); |
| 593 |
break; |
| 594 |
} |
| 595 |
} |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
bool lnxsound::CheckAndForceSoundDataAlloc(int sound_index) { |
| 600 |
int sound_file_index = Sounds[sound_index].sample_index; |
| 601 |
|
| 602 |
// ASSERT(sound_file_index >= 0 && sound_file_index < MAX_SOUND_FILES); |
| 603 |
|
| 604 |
if (sound_file_index < 0 || sound_file_index >= MAX_SOUND_FILES) { |
| 605 |
return false; |
| 606 |
} |
| 607 |
|
| 608 |
// Check if the sample data is already loaded |
| 609 |
if (SoundFiles[sound_file_index].sample_16bit != nullptr || SoundFiles[sound_file_index].sample_8bit != nullptr) |
| 610 |
return true; |
| 611 |
|
| 612 |
// If not, get the sound data |
| 613 |
int result = SoundLoadWaveFile(SoundFiles[sound_file_index].name, Sounds[sound_index].import_volume, sound_file_index, |
| 614 |
(m_sound_quality == SQT_HIGH), true); |
| 615 |
|
| 616 |
// Why would it load once (table load time) and not now? |
| 617 |
if (!result) |
| 618 |
return false; |
| 619 |
|
| 620 |
mprintf(0, "Sound %s loaded.\n", SoundFiles[sound_file_index].name); |
| 621 |
|
| 622 |
return true; |
| 623 |
} |
| 624 |
|
| 625 |
// Begin sound frame |
| 626 |
void lnxsound::SoundStartFrame() { |
| 627 |
float frame_time; |
| 628 |
|
| 629 |
if (m_timer_last_frametime == -1) { |
| 630 |
frame_time = 0.0f; |
| 631 |
} else { |
| 632 |
frame_time = (timer_GetMSTime() - m_timer_last_frametime) / 1000.0f; |
| 633 |
} |
| 634 |
m_timer_last_frametime = timer_GetMSTime(); |
| 635 |
|
| 636 |
// perform necessary functions if sound events are pending for frame, this doesn't have to do anything |
| 637 |
// if the mixer doesn't require such actions. Aureal does though. |
| 638 |
if (m_pending_actions) { |
| 639 |
mprintf(0, "pending actions\n"); |
| 640 |
} |
| 641 |
|
| 642 |
m_in_sound_frame = true; |
| 643 |
m_pending_actions = false; |
| 644 |
|
| 645 |
int counter = 0, loop_counter = 0, stream_counter = 0, buf_loop_counter = 0; |
| 646 |
|
| 647 |
#ifdef _DEBUG |
| 648 |
int n_p5 = 0, n_p4 = 0, n_p3 = 0, n_p2 = 0, n_p1 = 0, n_p0 = 0; |
| 649 |
#endif |
| 650 |
|
| 651 |
for (auto ¤t_slot : sound_cache) { |
| 652 |
sound_buffer_info *sb = ¤t_slot; |
| 653 |
if (sb->m_status != SSF_UNUSED) { |
| 654 |
counter++; |
| 655 |
if (sb->m_status & SSF_PLAY_LOOPING) { |
| 656 |
if (sb->m_status & SSF_BUFFERED_LOOP) |
| 657 |
buf_loop_counter++; |
| 658 |
loop_counter++; |
| 659 |
} |
| 660 |
if (sb->m_status & SSF_PLAY_STREAMING) |
| 661 |
stream_counter++; |
| 662 |
|
| 663 |
#ifdef _DEBUG |
| 664 |
if (sb->play_info->priority == SND_PRIORITY_CRITICAL) |
| 665 |
n_p5++; |
| 666 |
else if (sb->play_info->priority == SND_PRIORITY_HIGHEST) |
| 667 |
n_p4++; |
| 668 |
else if (sb->play_info->priority == SND_PRIORITY_HIGH) |
| 669 |
n_p3++; |
| 670 |
else if (sb->play_info->priority == SND_PRIORITY_NORMAL) |
| 671 |
n_p2++; |
| 672 |
else if (sb->play_info->priority == SND_PRIORITY_LOW) |
| 673 |
n_p1++; |
| 674 |
else if (sb->play_info->priority == SND_PRIORITY_LOWEST) |
| 675 |
n_p0++; |
| 676 |
#endif |
| 677 |
} |
| 678 |
} |
| 679 |
|
| 680 |
// update cache stress timer. |
| 681 |
if (counter < (MAX_SOUNDS_PLAYING_AT_ONCE * 3 / 4)) { |
| 682 |
m_cache_stress_timer += frame_time; |
| 683 |
} else { |
| 684 |
m_cache_stress_timer = 0.0f; |
| 685 |
} |
| 686 |
|
| 687 |
#ifdef _DEBUG |
| 688 |
mprintf_at(3, 2, 0, "LNS: %02d/%02d", counter, MAX_SOUNDS_PLAYING_AT_ONCE); |
| 689 |
mprintf_at(3, 3, 1, "Lp: %02d", loop_counter); |
| 690 |
mprintf_at(3, 4, 1, "St: %02d", stream_counter); |
| 691 |
mprintf_at(3, 5, 0, " Ot: %02d", counter - loop_counter - stream_counter); |
| 692 |
|
| 693 |
mprintf_at(3, 2, 20, "P5:%02d P4:%02d P3:%02d", n_p5, n_p4, n_p3); |
| 694 |
mprintf_at(3, 3, 20, "P2:%02d P1:%02d P0:%02d", n_p2, n_p1, n_p0); |
| 695 |
#endif |
| 696 |
} |
| 697 |
|
| 698 |
// End sound frame |
| 699 |
void lnxsound::SoundEndFrame() { |
| 700 |
CheckForErrors(); // handles errors. |
| 701 |
m_in_sound_frame = false; |
| 702 |
} |
| 703 |
|
| 704 |
// Sound System Error Handler. |
| 705 |
void lnxsound::CheckForErrors() { |
| 706 |
// if a fatal error occurred, quit and display an error |
| 707 |
// non-fatal errors should be put inside a logfile, or just mprinted out. |
| 708 |
switch (m_lib_error_code) { |
| 709 |
case SSL_ERROR_SAMPLE_NODATA: |
| 710 |
Error("%s\nSample had no data.", m_error_text); |
| 711 |
break; |
| 712 |
|
| 713 |
case SSL_ERROR_STREAMMIXER: |
| 714 |
Error("%s\nMixer alignment check failed.", m_error_text); |
| 715 |
break; |
| 716 |
|
| 717 |
case SSL_ERROR_GENERIC: |
| 718 |
Error("%s\nGeneric error.", m_error_text); |
| 719 |
break; |
| 720 |
} |
| 721 |
|
| 722 |
// must call! |
| 723 |
llsSystem::CheckForErrors(); |
| 724 |
} |
| 725 |
|
| 726 |
// returns the error string. |
| 727 |
const char *lnxsound::GetErrorStr() const { |
| 728 |
static char buffer[] = "No Error Given"; |
| 729 |
return buffer; |
| 730 |
} |
| 731 |
|
| 732 |
bool lnxsound::SetGlobalReverbProperties(float volume, float damping, float decay) { return false; } |
| 733 |
|
| 734 |
void lnxsound::StartStreaming() {} |
| 735 |
|
| 736 |
void lnxsound::EndStreaming() {} |
| 737 |
|
| 738 |
// may be called before init (must be to be valid, the card passed here will be initialized in InitSoundLib) |
| 739 |
void lnxsound::SetSoundCard(const char *name) {} |
| 740 |
|
| 741 |
// set special parameters for the 3d environment. |
| 742 |
// of strcuture passed, you must set the appropriate 'flags' value for values you wish to modify |
| 743 |
void lnxsound::SetEnvironmentValues(const t3dEnvironmentValues *env) {} |
| 744 |
|
| 745 |
// get special parameters for the 3d environment. |
| 746 |
// of strcuture passed, you must set the appropriate 'flags' value for values you wish to modify |
| 747 |
void lnxsound::GetEnvironmentValues(t3dEnvironmentValues *env) {} |
| 748 |
|
| 749 |
// enable special parameters for the 3d environment. |
| 750 |
// of strcuture passed, you must set the appropriate 'flags' value for values you wish to modify |
| 751 |
void lnxsound::SetEnvironmentToggles(const t3dEnvironmentToggles *env) {} |
| 752 |
|
| 753 |
// get states of special parameters for the 3d environment. |
| 754 |
// of strcuture passed, you must set the appropriate 'flags' value for values you wish to modify |
| 755 |
void lnxsound::GetEnvironmentToggles(t3dEnvironmentToggles *env) {} |
| 756 |
|
| 757 |
void lnxsound_SetError(int code) { ll_sound_ptr->SetError(code); } |
| 758 |
|
| 759 |
void lnxsound_ErrorText(const char *fmt, ...) { |
| 760 |
char buffer[384]; |
| 761 |
va_list args; |
| 762 |
va_start(args, fmt); |
| 763 |
std::vsnprintf(buffer, sizeof(buffer), fmt, args); |
| 764 |
va_end(args); |
| 765 |
|
| 766 |
ll_sound_ptr->ErrorText(buffer); |
| 767 |
} |
| 768 |
|
| 769 |
// A peroidic mixer that uses the primary buffer as a stream buffer |
| 770 |
static void StreamAudio(void *user_ptr, Uint8 *stream, int len) { |
| 771 |
((software_mixer *)user_ptr)->StreamMixer((char *)stream, len); |
| 772 |
} |