Debian Codegraph @

sndlib/hlsoundlib.cpp

raw symbols SCIP
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 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/sndlib/hlsoundlib.cpp $
21 * $Revision: 116 $
22 * $Date: 4/19/00 5:07p $
23 * $Author: Matt $
24 *
25 * High level sound library for Descent 3
26 *
27 * $Log: /DescentIII/Main/sndlib/hlsoundlib.cpp $
28 *
29 * 116 4/19/00 5:07p Matt
30 * From Duane for 1.4
31 * Added checks, asserts, and fixes for bad return values
32 *
33 * 115 3/20/00 12:30p Matt
34 * Merge of Duane's post-1.3 changes.
35 * Optimizations.
36 * A bunch of 3D sound code ifdef'd out for Mac.
37 *
38 * 114 2/09/00 4:59p Jason
39 * fixed access violation in VerifyUniqueID
40 *
41 * 113 10/22/99 10:40p Jeff
42 * commented out an mprintf put in by mac merge
43 *
44 * 112 10/21/99 3:27p Kevin
45 * Mac merge!
46 *
47 * 111 8/23/99 5:29p Samir
48 * incremental EAX 2.0 checkin
49 *
50 * 110 8/19/99 7:10p Jeff
51 * hooked in linux sound lib
52 *
53 * 109 8/13/99 2:00p Samir
54 * more aureal and geometry fixes.
55 *
56 * 108 8/11/99 3:12p Samir
57 * fixes for aureal support.
58 *
59 * 107 7/28/99 2:05p Kevin
60 * Macintosh Changes
61 *
62 * 106 5/07/99 1:32p Jeff
63 * kill a sound if the object is ghosted (aka OBJ_DUMMY)
64 *
65 * 105 4/27/99 2:10p Samir
66 * added code to set the desired sound card given the descriptive name of
67 * a sound card.
68 *
69 * 104 4/23/99 7:50p Samir
70 * looping fixes for directsound.
71 *
72 * 103 4/14/99 1:46a Jeff
73 * fixed case mismatched #includes
74 *
75 * 102 4/12/99 7:14p Samir
76 * prioritization code added.
77 *
78 * 101 4/10/99 5:09p Samir
79 * beginning to add prioritization of sounds code. currently non
80 * functional, but allows me to change all calls to Play3dSound so that I
81 * can test later.
82 *
83 * 100 4/06/99 8:31p Samir
84 * moved free memory code to low level library.
85 *
86 * 99 4/03/99 6:24p Nate
87 * ? Fixed a editor sound crash ?
88 *
89 * 98 4/01/99 4:28p Samir
90 * hardware geometry integration if it's available.
91 *
92 * 97 4/01/99 11:57a Chris
93 * Made the holy cow messed print the name of the sound that couldn't be
94 * played
95 *
96 * 96 3/03/99 5:07p Samir
97 * music volume will shutoff music if 0, restart if greater only after a
98 * change in vollume.
99 *
100 * 95 3/03/99 5:44a Chris
101 * Sound is based on the viewer_object
102 *
103 * 94 2/24/99 12:27p Chris
104 * Fixed problems with GB finding robots/room he couldn't get to. Fixed
105 * problems with forcefields(sound prop. and path finding). Fixed
106 * problems with small portals.
107 *
108 * 93 2/21/99 5:48p Matt
109 * Added SetVolumeObject()
110 *
111 * 92 2/16/99 3:38p Chris
112 * FIXED: Ship sounds being all messed up. :) (3d dist = 0) So, use the
113 * rvec... So, sounds could get muffled
114 *
115 * 91 2/14/99 3:27p Chris
116 * FIxed a load/save bug... hehe
117 *
118 * 90 2/12/99 2:10p Chris
119 *
120 * 89 2/12/99 1:54p Sean
121 *
122 * 88 2/12/99 1:33p Sean
123 *
124 * 87 2/12/99 1:18p Sean
125 *
126 * 86 2/12/99 1:10p Sean
127 * Encapsulated the last_portal code
128 *
129 * 85 2/12/99 11:22a Chris
130 * Updated what BOA uses for BOA_cost_array. It is not just the distance
131 * from the center of the room to the center of the portal. It was this +
132 * the dist to the next room's center. The reason for this change was it
133 * makes it easier to compute the exact BOA_DIST for sounds. This is
134 * because the two distances are seperated (so from the start pos, we do a
135 * vm_Vect dist from the start point to the portal)... Hmmm....
136 *
137 * 84 2/09/99 12:40p Chris
138 * More aipath stuff has been merged with the new BOA
139 *
140 * 83 2/09/99 9:59a Chris
141 * Massive BOA update :) Terrain happy now. Vis happy now. Sound happy
142 * now.
143 *
144 * 82 1/30/99 3:44p Chris
145 * Added support for time-offset'ed 3d sounds (like start at 1.2 seconds
146 * into the sound)
147 *
148 * 81 1/29/99 12:48p Matt
149 * Rewrote the doorway system
150 *
151 * 80 1/26/99 5:16p Matt
152 * Added StopObjectSound()
153 *
154 * 79 1/12/99 4:04p Samir
155 * added environmental audio on a per room basis.
156 *
157 * 78 1/08/99 6:31p Samir
158 * added reverb
159 *
160 * 77 12/22/98 11:08a Chris
161 * Improved algorithm for 3d sounds
162 *
163 * 76 12/02/98 4:34p Samir
164 * made high level playstream set a variable needed to call low level
165 * stream playback.
166 *
167 * 75 11/16/98 4:29p Chris
168 * Added pause new flag to the high level system
169 *
170 * 74 11/10/98 1:52p Jason
171 * added dedicated server support
172 *
173 * 73 10/23/98 7:05p Samir
174 * added pause and resume
175 *
176 * 72 10/22/98 8:31p Chris
177 * Sounds use GlobalAlloc and GlobalFree
178 *
179 * 71 10/22/98 9:22a Chris
180 * Fixes problems with hl-uids only being valid for the first 256 sounds
181 *
182 * 70 10/20/98 2:53a Kevin
183 * gunboy crap
184 *
185 * 69 10/19/98 11:57a Chris
186 * Update the sound system to use the import volume
187 *
188 * 68 10/17/98 12:46p Kevin
189 * Beta 4 fixes
190 *
191 * 67 10/14/98 5:12p Samir
192 * added a null SOUND_MIXER.
193 *
194 * 66 10/12/98 1:27p Jeff
195 * all mem_free's do set the value to NULL after they are complete
196 *
197 * 65 10/02/98 3:39p Chris
198 * Fixed a bug in Stopallsounds (it always acted like you where in the
199 * game)
200 *
201 * 64 9/14/98 6:28p Jason
202 * first pass at getting dedicated server working
203 *
204 * 63 9/11/98 3:08p Chris
205 * Enabled the production sound
206 *
207 * 62 9/08/98 12:05p Jason
208 * moved doorway.h out of room.h
209 *
210 * 61 8/26/98 1:48a Chris
211 * Fixed odd non-stopping sounds when ESC out of the game - etc.
212 *
213 * 60 8/18/98 12:18p Chris
214 * Added f_in_game
215 *
216 * 59 7/09/98 8:34p Samir
217 * added argument to callback for streams.
218 *
219 * 58 7/09/98 11:32a Samir
220 * close stream system after init check in KillSoundLib.
221 *
222 * 57 7/08/98 6:27p Samir
223 * stream library integrated with highlevel sound system.
224 *
225 * 56 6/29/98 10:44p Chris
226 *
227 * 55 6/29/98 10:44p Chris
228 * Added support for SIF_ONCE_PER_OBJ
229 *
230 * 54 6/29/98 10:12a Chris
231 * Further improved the sound system
232 *
233 * 53 6/29/98 9:29a Chris
234 * Added some support for Direct Sound 3d
235 *
236 * 52 6/24/98 12:09p Chris
237 * Update
238 *
239 * 51 6/22/98 12:00p Chris
240 * Working on sound system to make it in a nice shape.
241 *
242 * 50 6/19/98 3:09p Chris
243 * Improved IsSoundPlaying
244 *
245 * 49 6/19/98 3:03p Chris
246 * Made CheckAndForceSoundDataAlloc a SoundSystem function - useful for
247 * multiple mixers. Added IsSoundPlaying to the high level sound lib.
248 *
249 * 48 6/17/98 4:39p Chris
250 * Added the playing of 8bit samples with the 16 bit sound mixer
251 *
252 * 47 6/16/98 4:46p Chris
253 * Revision 2 of the stream audio stuff
254 *
255 * 46 6/16/98 3:48p Chris
256 * Updated the sound system and added the start of sound streaming
257 *
258 * 45 5/14/98 7:32p Samir
259 * Removed an ASSert and replaced with a mprintf
260 *
261 * 44 5/04/98 6:31p Craig
262 * Added more bug checking
263 *
264 * 43 4/24/98 3:02p Chris
265 * Removed GlobalAlloc and GlobalFree
266 *
267 * 42 4/23/98 12:00p Jeff
268 * Fixed the GlobalFree problem
269 *
270 * 41 4/22/98 6:19p Chris
271 * Fixed a lockup bug
272 *
273 * 40 4/22/98 4:31p Chris
274 * Added DebugBlockPrint
275 *
276 * 39 4/06/98 3:43p Jason
277 * Removed an ASSERT and make it an if-block
278 *
279 * 38 3/20/98 4:03p Chris
280 * Made non-looping non-audible 3d sounds not play.
281 *
282 * 37 3/20/98 3:06p Chris
283 * Removed some mprintf's
284 *
285 * 36 3/20/98 2:59p Chris
286 * Added a wall hit sound for the player and added support for a base
287 * volume for 3d sounds
288 *
289 * 35 2/27/98 5:31p Chris
290 * Changed how master_volume is set.
291 *
292 * 34 2/23/98 6:32p Chris
293 * Added support for a hlsoundlib unique id. This is what all the public
294 * functions use. Also, looping sounds are now correctly turned off and
295 * on.
296 *
297 * 33 2/23/98 5:03p Chris
298 * Added the global alloc calls (deifned them to malloc and free when not
299 * Win32)
300 *
301 * 32 2/17/98 1:17p Chris
302 *
303 * 31 2/17/98 1:11p Chris
304 * Bails on invalid sound indices
305 *
306 * 30 2/16/98 4:34a Chris
307 *
308 * 29 2/16/98 4:23a Chris
309 *
310 * 28 2/16/98 3:54a Chris
311 *
312 * 27 2/16/98 3:20a Luke
313 *
314 * 26 2/16/98 2:47a Chris
315 * Fixed problems with 3d looping sounds
316 *
317 * 25 2/15/98 8:13p Luke
318 *
319 * 24 2/15/98 7:12p Chris
320 * More improvements to the sound lib
321 *
322 * 23 2/15/98 6:41p Chris
323 * Added Update2dSound
324 *
325 * 22 2/13/98 5:15p Chris
326 * Fixed multiple problems with looping sounds not being able to be
327 * stopped
328 *
329 * 21 2/04/98 6:10p Matt
330 * Changed object room number to indicate a terrain cell via a flag. Got
331 * rid of the object flag which used to indicate terrain.
332 *
333 * 20 1/19/98 10:04a Matt
334 * Added new object handle system
335 *
336 * 19 1/14/98 7:56p Chris
337 * Fixed bugs dealing with llsound system being full.
338 *
339 * 18 1/14/98 5:40p Chris
340 * Fixed problem of the hlsound system crashing when the llsound lib
341 * filled up.
342 *
343 * 17 1/13/98 5:08p Chris
344 * Increased sound object limit
345 *
346 * 16 1/08/98 1:11p Chris
347 * Forgot to OR SND_OUTSIDE of some sounds
348 *
349 * 15 1/06/98 2:12p Chris
350 * Added muffled sounds v.1 and made 3d sounds keep their roomnum.
351 *
352 * 14 1/05/98 4:36p Chris
353 * Added working ambient sounds to objects.
354 *
355 * 13 1/05/98 3:58p Chris
356 * Further improvements to the high level sound lib
357 *
358 * 12 1/02/98 5:32p Chris
359 * More radical changes to the sound system
360 *
361 * 11 12/31/97 2:58a Chris
362 * Another major revision to the SoundLib. This cleaned up the code after
363 * removing the direct sound secondary buffers.
364 *
365 * 10 12/30/97 2:15p Chris
366 * Adding further support for software 3d stuff
367 *
368 * 9 12/22/97 6:19p Chris
369 * Moved weapon battery firing sound off the projectile (weapon) and into
370 * the weapon battery.
371 *
372 * 8 12/12/97 11:43a Chris
373 * Added support to toggle sndlib on/off
374 *
375 * 7 12/11/97 1:33p Chris
376 * Added support for fixed 3d position updating sounds.
377 *
378 * 6 12/10/97 4:47p Chris
379 * Revision 1.0 of new sound library (no hardware -- uses primary buffer
380 * streaming)
381 *
382 * 5 11/21/97 1:10p Chris
383 * Incremental Improvements
384 *
385 * 4 11/21/97 12:33p Chris
386 * Fixed memory leak when -nosound was added.
387 *
388 * 3 11/07/97 5:43p Jason
389 * added -nosound command switch
390 *
391 * 2 7/16/97 5:15p Chris
392 * Fixed use of updating 3d sounds
393 *
394 * 23 6/11/97 1:07p Samir
395 * The removal of gameos and replaced with oeApplication, oeDatabase
396 *
397 * 22 5/22/97 5:15p Chris
398 *
399 * 21 5/15/97 1:59a Chris
400 *
401 * 20 4/29/97 8:03a Chris
402 * Improved the sound code. High-level sound now
403 * fully uses the flags in the sound page and it
404 * resulted in a simpilier coding interface :)
405 *
406 * 19 4/28/97 11:21a Chris
407 * Incremental improvements is the sound system
408 *
409 * 18 4/24/97 3:41a Chris
410 * Improved 3d sound support. :)
411 *
412 * 17 4/23/97 5:38p Chris
413 * Updated some 3d sound code
414 *
415 * 16 4/03/97 8:37p Chris
416 * Added the Main_OS object pass in the sound library
417 * initialization code.
418 *
419 * 15 4/01/97 9:32a Chris
420 * Getting sounds to work with the paging system.
421 *
422 * 14 3/27/97 11:44a Chris
423 * On a re-initialize, I now make sure that the old initializaton is
424 * cleared out
425 *
426 * 13 3/21/97 7:02 PM Jeremy
427 * changed a "== SIF_UNUSED" to "= SIF_UNUSED" in StopAllSounds as it was
428 * not really doing an assignment as intended.
429 *
430 * 12 3/21/97 4:06 PM Jeremy
431 * changed name of low level mac sound header
432 *
433 * 11 3/14/97 5:22 PM Jeremy
434 * unincluded malloc.h, changed #include of <pserror.h> to "pserror.h"
435 * since it's not an ANSI header, but one of ours.
436 *
437 * 10 3/14/97 12:17p Chris
438 * Abstract type for the low level sound lib
439 *
440 * 9 2/07/97 5:48p Matt
441 * Renamed vector.h to vecmat.h to fix DevStudio problem
442 *
443 * $NoKeywords: $
444 */
445 // High level sound object. This code is 100% machine independant and
446 // completely encapsulates the lowel-level machine-dependant sound code.
447
448 #include <cstring>
449
450 #include "hlsoundlib.h"
451 #include "ssl_lib.h"
452 #include "mono.h"
453 #include "pserror.h"
454 #include "vecmat.h"
455 #include "args.h"
456 #include "sounds.h"
457 #include "game.h"
458 #include "room.h"
459 #include "BOA.h"
460 #include "streamaudio.h"
461 #include "doorway.h"
462 #include "dedicated_server.h"
463 #include "sndrender.h"
464 #include "voice.h"
465 #include "descent.h"
466
467 #include "sdlsound.h"
468
469 hlsSystem Sound_system;
470 char Sound_quality = SQT_NORMAL;
471 char Sound_mixer = SOUND_MIXER_SOFTWARE_16;
472 char Sound_card_name[256] = "";
473 struct tEnvAudioPresets {
474 float volume;
475 float decay;
476 float damping;
477 }
478 EnvAudio_Presets[N_ENVAUDIO_PRESETS] = {
479 {0.0F, 0.0F, 0.0F}, // Use for reverb off.
480 {0.25F, 0.1F, 0.0F}, {0.417F, 0.4F, 0.666F}, {0.653F, 1.499F, 0.166F}, {0.208F, 0.478F, 0.0F},
481 {0.5F, 2.309F, 0.888F}, {0.403F, 4.279F, 0.5F}, {0.5F, 3.961F, 0.5F}, {0.5F, 2.886F, 1.304F},
482 {0.361F, 7.284F, 0.332F}, {0.5F, 10.0F, 0.3F}, {0.153F, 0.259F, 2.0F}, {0.361F, 1.493F, 0.0F},
483 {0.444F, 2.697F, 0.638F}, {0.25F, 1.752F, 0.776F}, {0.111F, 3.145F, 0.472F}, {0.111F, 2.767F, 0.224F},
484 {0.194F, 7.841F, 0.472F}, {1.0F, 1.499F, 0.5F}, {0.097F, 2.767F, 0.224F}, {0.208F, 1.652F, 1.5F},
485 {0.652F, 2.886F, 0.25F}, {1.0F, 1.499F, 0.0F}, {0.875F, 8.392F, 1.388F}, {0.139F, 17.234F, 0.666F},
486 {0.486F, 7.563F, 0.806F}};
487
488 //////////////////////////////////////////////////////////////////////////////
489 hlsSystem::hlsSystem() {
490 m_f_hls_system_init = 0;
491 m_ll_sound_ptr = NULL;
492 m_sounds_played = 0;
493 m_master_volume = 1.0;
494 m_pause_new = false;
495 n_lls_sounds = MAX_SOUNDS_MIXED;
496 }
497 bool hlsSystem::IsActive(void) {
498 if (m_f_hls_system_init)
499 return true;
500 return false;
501 }
502 void hlsSystem::SetLLSoundQuantity(int n_sounds) {
503 if (n_sounds > MAX_SOUNDS_MIXED) {
504 n_sounds = MAX_SOUNDS_MIXED;
505 }
506 n_lls_sounds = n_sounds;
507 mprintf(1, "SNDLIB: Allow %d sounds to be mixed.\n", n_sounds);
508 if (m_f_hls_system_init) {
509 InitSoundLib(NULL, Sound_mixer, Sound_quality, false);
510 }
511 }
512 int hlsSystem::GetLLSoundQuantity() { return n_lls_sounds; }
513 // Start and clean-up after the sound library
514 int hlsSystem::InitSoundLib(oeApplication *sos, char mixer_type, char quality, bool f_kill_sound_list) {
515 int status;
516 // Turn off sound if desired
517 if ((FindArg("-nosound")) || Dedicated_server) {
518 m_ll_sound_ptr = NULL;
519 return 0;
520 }
521 if (m_f_hls_system_init) {
522 KillSoundLib(f_kill_sound_list);
523 }
524
525 // Create and initialize the low-level sound library
526 if (m_ll_sound_ptr == NULL)
527 m_ll_sound_ptr = new lnxsound;
528 ASSERT(m_ll_sound_ptr);
529 if (m_ll_sound_ptr == NULL)
530 return 0;
531 if (Sound_card_name[0]) {
532 m_ll_sound_ptr->SetSoundCard(Sound_card_name);
533 } else {
534 m_ll_sound_ptr->SetSoundCard(NULL);
535 }
536 status = m_ll_sound_ptr->InitSoundLib(mixer_type, sos, n_lls_sounds);
537 // Initialize the high-level sound library
538 // Set the flag to initialized if all was o.k.
539 if (status) {
540 m_f_hls_system_init = 1;
541 SetSoundQuality(quality);
542 Sound_quality = GetSoundQuality();
543 Sound_mixer = GetSoundMixer();
544
545 // invoke high level stream system
546 AudioStream::InitSystem(this->m_ll_sound_ptr);
547 // set current environment
548 m_cur_environment = ENVAUD_PRESET_NONE;
549 }
550 // All done
551 return status;
552 }
553 // Kills the sound library
554 void hlsSystem::KillSoundLib(bool f_kill_sound_list) {
555 int i;
556 if (m_f_hls_system_init) {
557 mprintf(1, "m_sounds_played %d\n", m_sounds_played);
558 // clean up stream system
559 AudioStream::Shutdown();
560 for (i = 0; i < MAX_SOUND_OBJECTS; i++) {
561 m_sound_objects[i].m_obj_type_flags = SIF_UNUSED;
562 m_sound_objects[i].m_hlsound_uid = -1;
563 }
564 m_ll_sound_ptr->DestroySoundLib();
565 m_f_hls_system_init = 0;
566 }
567 if (m_ll_sound_ptr) {
568 delete m_ll_sound_ptr;
569 m_ll_sound_ptr = NULL;
570 }
571 // This is independant of the actual sound_lib status. It happened because sounds
572 // use the manage system.
573 // moved this from beginning of function because won't the data freed here screw up streaming/looping sounds?
574 // before the sound library is closed?
575 if (f_kill_sound_list) {
576 for (int i = 0; i < MAX_SOUND_FILES; i++) {
577 SoundLoadFree(i);
578 }
579 }
580 }
581 // Pause and Resume the library
582 // Pauses all sounds
583 void hlsSystem::PauseSounds(bool f_all_pause) {
584 if (!m_f_hls_system_init)
585 return;
586 m_pause_new = f_all_pause;
587 AudioStream::PauseAll();
588 m_ll_sound_ptr->PauseSounds();
589 }
590 // Resumes all sounds
591 void hlsSystem::ResumeSounds() {
592 if (!m_f_hls_system_init)
593 return;
594 m_pause_new = false;
595 m_ll_sound_ptr->ResumeSounds();
596 AudioStream::ResumeAll();
597 }
598 // int hlsSystem::SetSoundPos(int sound_uid, int pos)
599 //{
600 // if(!m_f_hls_system_init) return -1;
601 //
602 // return m_ll_sound_ptr->SetSoundPos(sound_uid, pos);
603 // return -1;
604 //}
605 // int hlsSystem::GetSoundPos(int sound_uid)
606 //{
607 // if(!m_f_hls_system_init) return -1;
608 //
609 // return m_ll_sound_ptr->GetSoundPos(sound_uid);
610 // return -1;
611 //}
612 // Stops all sound from playing
613 void hlsSystem::StopAllSounds() {
614 int i;
615 if (!m_f_hls_system_init)
616 return;
617 for (i = 0; i < MAX_SOUND_OBJECTS; i++) {
618 m_sound_objects[i].m_obj_type_flags = SIF_UNUSED;
619 m_sound_objects[i].m_hlsound_uid = -1;
620 }
621 m_ll_sound_ptr->StopAllSounds();
622 BeginSoundFrame(false);
623 EndSoundFrame();
624
625 SoundRenderReset();
626
627 mprintf(0, "Stopped all sounds\n");
628 }
629 // Code for the beginning and ending of a frame of action
630 // Begin_sound_frame(listener pos/orient/velocity)
631 // SyncSounds
632 // Do sound pos updates -- IF VOLUME IS LOW AND NOT FOREVER (or looping), THEN STOP SOUND
633 // compute echo / reverb
634 // indirect/direct path sounds
635
636 #include "findintersection.h"
637
638 void hlsSystem::BeginSoundFrame(bool f_in_game) {
639 bool hwsound_support; // if this is true, sound_render_system is being used
640 int16_t *sound_room_list; // these values are only meant for sound render system
641 int i, n, counter = 0;
642 pos_state listener_pos;
643 const int num_samples_this_frame = (int)(Frametime * 22050);
644 if (!m_f_hls_system_init)
645 return;
646 // determine if we're using hardware for sound support.
647 m_ll_sound_ptr->SoundStartFrame();
648 // non game sound frame, this, just updates currently playing sounds.
649 // placed here to leave early and ensure any further processing involves game only.
650 if (!f_in_game) {
651 sound_room_list = NULL;
652 for (i = 0, counter = 0; i < MAX_SOUND_OBJECTS; i++) {
653 if (m_sound_objects[i].m_obj_type_flags && m_sound_objects[i].m_sound_uid != -1) {
654 counter++;
655 ASSERT(m_sound_objects[i].m_sound_uid > -1);
656 if (!m_ll_sound_ptr->IsSoundInstancePlaying(m_sound_objects[i].m_sound_uid)) {
657 StopSound(i); // stop any sounds playing.
658 }
659 }
660 }
661
662 goto end_beginsoundframe; // this will handle any remaining processing
663 }
664 // FROM HERE ON, WE ASSUME WE'RE IN GAME.
665 hwsound_support = (sound_render_start_frame());
666 // define current environment of the listener.
667 listener_pos.velocity = &Viewer_object->mtype.phys_info.velocity;
668 listener_pos.position = &Viewer_object->pos;
669 listener_pos.orient = &Viewer_object->orient;
670 listener_pos.roomnum = Viewer_object->roomnum;
671 m_ll_sound_ptr->SetListener(&listener_pos);
672 // do environmental audio int current room.
673 if (!OBJECT_OUTSIDE(Viewer_object)) {
674 n = Rooms[Viewer_object->roomnum].env_reverb;
675 if (n >= 0) {
676 if (m_cur_environment != n) {
677 ASSERT(n < N_ENVAUDIO_PRESETS);
678 m_ll_sound_ptr->SetGlobalReverbProperties(EnvAudio_Presets[n].volume, EnvAudio_Presets[n].damping,
679 EnvAudio_Presets[n].decay);
680 m_cur_environment = n;
681 }
682 }
683 } else {
684 if (m_cur_environment != ENVAUD_PRESET_MOUNTAINS) {
685 n = ENVAUD_PRESET_MOUNTAINS;
686 m_ll_sound_ptr->SetGlobalReverbProperties(EnvAudio_Presets[n].volume, EnvAudio_Presets[n].damping,
687 EnvAudio_Presets[n].decay);
688 m_cur_environment = n;
689 }
690 }
691 // render all rooms within a certain range from player (traverse rooms through portals)
692 sound_room_list = hwsound_support ? sound_render_audible_rooms(&listener_pos) : NULL;
693 // SoundQ_process();
694 // if sound object is outside listener's cone of audibility, emulate the sound.
695 // if sound is audible, then determine its playback properties.
696 //
697 for (i = 0, counter = 0; i < MAX_SOUND_OBJECTS; i++) {
698 if (m_sound_objects[i].m_obj_type_flags) {
699 counter++;
700 // oldvolume = m_sound_objects[i].volume;
701 // oldpan = m_sound_objects[i].pan;
702 // mprintf(0, "SO %d playing: uid %d\n", i, m_sound_objects[i].m_sound_uid);
703 // Handle high-level only sounds
704 if (m_sound_objects[i].m_obj_type_flags & SIF_TOO_FAR) {
705 if (m_sound_objects[i].m_obj_type_flags & SIF_LOOPING) {
706 // while(m_sound_objects[i].play_info.m_samples_played >=
707 // Sounds[m_sound_objects[i].m_sound_index].loop_end)
708 // {
709 // m_sound_objects[i].play_info.m_samples_played -=
710 //(Sounds[m_sound_objects[i].m_sound_index].loop_end - Sounds[m_sound_objects[i].m_sound_index].loop_start);
711 // }
712 } else {
713 // Advance sound pointer and see if sound is done
714 m_sound_objects[i].play_info.m_samples_played += num_samples_this_frame;
715
716 if (m_sound_objects[i].play_info.m_samples_played >=
717 SoundFiles[Sounds[m_sound_objects[i].m_sound_index].sample_index].sample_length) {
718 StopSound(i);
719 continue;
720 }
721 }
722 if (m_sound_objects[i].m_obj_type_flags & SIF_OBJ_UPDATE) {
723 // If object not alive, kill its sound
724 object *obj_sound = ObjGet(m_sound_objects[i].m_link_info.object_handle);
725 if (!obj_sound || obj_sound->type == OBJ_DUMMY) {
726 StopSound(i);
727 continue; // Go on to next sound...
728 }
729 }
730 // determine new properties of sound if it's still playing, its volume, other properties.
731 if (Emulate3dSound(i)) {
732 // sound is audible now.
733 if (m_sound_objects[i].m_sound_uid != -1) {
734 m_sound_objects[i].m_obj_type_flags &= (~SIF_TOO_FAR);
735 } else {
736 // mprintf(0, "LLSound full 3\n");
737 }
738 }
739 continue;
740 }
741 ASSERT(m_sound_objects[i].m_sound_uid > -1);
742 if (!m_ll_sound_ptr->IsSoundInstancePlaying(m_sound_objects[i].m_sound_uid)) {
743 StopSound(i);
744 continue; // Go on to next sound...
745 }
746 if ((Sounds[m_sound_objects[i].m_sound_index].flags & SPF_LISTENER_UPDATE) &&
747 !(m_sound_objects[i].m_obj_type_flags & SIF_NO_3D_EFFECTS)) {
748 if (m_sound_objects[i].m_obj_type_flags & SIF_OBJ_UPDATE) {
749 object *obj_sound = ObjGet(m_sound_objects[i].m_link_info.object_handle);
750 if (!obj_sound || obj_sound->type == OBJ_DUMMY) {
751 StopSound(i);
752 continue; // Go on to next sound...
753 }
754 }
755 if ((m_sound_objects[i].m_obj_type_flags & (SIF_PLAYING_3D | SIF_OBJ_UPDATE))) {
756 bool f_audible;
757 vector virtual_pos;
758 vector virtual_vel;
759 float adjusted_volume;
760 f_audible = ComputePlayInfo(i, &virtual_pos, &virtual_vel, &adjusted_volume);
761 if (!f_audible) {
762 m_ll_sound_ptr->StopSound(m_sound_objects[i].m_sound_uid, SKT_STOP_IMMEDIATELY);
763 m_sound_objects[i].m_sound_uid = 0;
764 m_sound_objects[i].m_obj_type_flags |= SIF_TOO_FAR;
765 } else {
766 matrix orient = Identity_matrix;
767 pos_state cur_pos;
768 cur_pos.velocity = &virtual_vel;
769 cur_pos.position = &virtual_pos;
770 cur_pos.orient = &orient;
771 m_ll_sound_ptr->AdjustSound(m_sound_objects[i].m_sound_uid, &cur_pos, adjusted_volume);
772
773 // handle additional 3d sound support
774 sound_object *so = &m_sound_objects[i];
775 if (m_ll_sound_ptr->SoundPropertySupport() && (so->m_obj_type_flags & SIF_OBJ_UPDATE)) {
776 float obstruction = 0.0f;
777 object *game_obj;
778
779 // sound must be in same room as listener
780 // if so, then use FVI to determine obstruction properties.
781 // and then set the obstruction properties.
782 if (OBJECT_OUTSIDE(Viewer_object))
783 continue;
784 game_obj = ObjGet(so->m_link_info.object_handle);
785
786 if (game_obj && game_obj->roomnum == Viewer_object->roomnum) {
787 // inside and same room? go for it.
788 fvi_info hit_data;
789 fvi_query fq;
790
791 // mprintf(0, "Obstruction test.\n");
792
793 hit_data.hit_type[0] = HIT_WALL;
794 fq.p1 = &Viewer_object->pos;
795 fq.p0 = &game_obj->pos;
796 fq.startroom = game_obj->roomnum;
797 fq.rad = .5;
798 fq.thisobjnum = so->m_link_info.object_handle & HANDLE_OBJNUM_MASK;
799 fq.ignore_obj_list = NULL;
800 fq.flags = 0;
801 fvi_FindIntersection(&fq, &hit_data);
802
803 if (hit_data.hit_type[0] == HIT_WALL) {
804 obstruction = 1.0f;
805 }
806 m_ll_sound_ptr->SetSoundProperties(so->m_sound_uid, obstruction);
807 } else {
808 m_ll_sound_ptr->SetSoundProperties(so->m_sound_uid, 0.0f);
809 }
810 }
811 }
812 }
813 }
814 }
815 }
816 // mprintf(0, "BeginSoundFrame: used sound_objects %d\n", counter);
817 end_beginsoundframe:
818 AudioStream::Frame();
819 mprintf_at(3, 1, 0, "HNS: %04d", counter);
820 }
821
822 // Plays the deffered 3d stuff
823 void hlsSystem::EndSoundFrame() {
824 if (!m_f_hls_system_init)
825 return;
826 sound_render_end_frame();
827 m_ll_sound_ptr->SoundEndFrame();
828 }
829 // Allows for changes in a currently playing sound
830 int hlsSystem::Update2dSound(int hlsound_uid, float volume, float pan) {
831 int sound_obj_index;
832
833 if (!m_f_hls_system_init)
834 return -1;
835 if (volume < 0.0f)
836 volume = 0.0f;
837 else if (volume > 1.0f)
838 volume = 1.0f;
839 if (pan < -1.0f)
840 pan = -1.0f;
841 else if (pan > 1.0f)
842 pan = 1.0f;
843 if (!m_f_hls_system_init)
844 return -1;
845 sound_obj_index = ValidateUniqueId(hlsound_uid);
846 if (sound_obj_index == -1)
847 return -1;
848 m_ll_sound_ptr->AdjustSound(m_sound_objects[sound_obj_index].m_sound_uid, volume, pan, 22050);
849 return hlsound_uid;
850 }
851 bool hlsSystem::ComputePlayInfo(int sound_obj_index, vector *virtual_pos, vector *virtual_vel, float *adjusted_volume) {
852 int sound_index;
853
854 int sound_seg;
855 int ear_seg;
856 vector sound_pos;
857 m_sound_objects[sound_obj_index].play_info.sample_skip_interval = 0;
858 vector dir_to_sound;
859 float dist = 0;
860 if (m_master_volume <= 0.0)
861 return false;
862
863 *adjusted_volume = (m_master_volume * m_sound_objects[sound_obj_index].volume_3d);
864 if (*adjusted_volume <= 0.0) {
865 *adjusted_volume = 0.0;
866 return false;
867 }
868
869 sound_index = m_sound_objects[sound_obj_index].m_sound_index;
870 ASSERT(sound_index >= 0 && sound_index < MAX_SOUNDS);
871
872 if ((Sounds[m_sound_objects[sound_obj_index].m_sound_index].flags & SPF_LISTENER_UPDATE) &&
873 !(m_sound_objects[sound_obj_index].m_obj_type_flags & SIF_NO_3D_EFFECTS) &&
874 (m_sound_objects[sound_obj_index].m_obj_type_flags & SIF_OBJ_UPDATE)) { // this is an updating object sound
875 object *objp = ObjGet(m_sound_objects[sound_obj_index].m_link_info.object_handle);
876 if (!objp || objp->type == OBJ_DUMMY) { // Couldn't find object for given handle
877 // Int3(); // get Chris//removed for outside testers
878 return false;
879 }
880 if (objp->movement_type == MT_PHYSICS || objp->movement_type == MT_WALKING)
881 *virtual_vel = objp->mtype.phys_info.velocity;
882 else
883 *virtual_vel = Zero_vector;
884
885 sound_pos = objp->pos;
886 sound_seg = objp->roomnum;
887 } else {
888 *virtual_vel = Zero_vector;
889 sound_pos = m_sound_objects[sound_obj_index].m_link_info.pos_info.pos;
890 sound_seg = m_sound_objects[sound_obj_index].m_link_info.pos_info.segnum;
891 }
892
893 // sound_seg == -1 (which just means that the roomnum/segnum hasn't been
894 // initialized to a proper value yet )causes crashes when BOA_INDEX()
895 // calls TERRAIN_REGION() with that value. (By pure luck on 32bit platforms
896 // the overflow and truncation will likely use an address that doesn't crash,
897 // but it's still invalid). At least one case that could cause this was fixed,
898 // if there are others, the ASSERT should tell us about it
899 // (and if assertions are disabled, return false to handle this gracefully)
900 ASSERT(sound_seg != -1 && "invalid (unset) roomnum/segnum!");
901 if (sound_seg == -1)
902 return false;
903
904 sound_seg = BOA_INDEX(sound_seg);
905 ear_seg = BOA_INDEX(Viewer_object->roomnum);
906 if (!BOA_IsSoundAudible(sound_seg, ear_seg))
907 return false;
908
909 if (sound_seg != ear_seg && !(sound_seg == Highest_room_index + 1 && ear_seg > Highest_room_index) &&
910 !(ear_seg == Highest_room_index + 1 && sound_seg > Highest_room_index)) {
911 int cur_room = sound_seg;
912 int last_room;
913
914 do {
915 last_room = cur_room;
916
917 if (cur_room <= Highest_room_index && (Rooms[cur_room].flags & RF_DOOR) && (cur_room != sound_seg)) {
918 float door_position = DoorwayGetPosition(&Rooms[cur_room]);
919 // Closed doors antenuate a lot
920 if (door_position == 0.0) {
921 m_sound_objects[sound_obj_index].play_info.sample_skip_interval = 4;
922 *adjusted_volume *= 0.2f;
923 } else {
924 *adjusted_volume *= (0.6f + (0.4 * door_position));
925 }
926 }
927 cur_room = BOA_NEXT_ROOM(cur_room, ear_seg);
928 int last_portal;
929 if (BOA_INDEX(last_room) == BOA_INDEX(cur_room) || cur_room == BOA_NO_PATH)
930 return false;
931 if (BOA_INDEX(last_room) != BOA_INDEX(cur_room)) {
932 last_portal = BOA_DetermineStartRoomPortal(last_room, NULL, cur_room, NULL);
933 if (last_portal == -1) {
934 return false;
935 }
936 }
937 if (last_room == sound_seg) {
938 if (cur_room == ear_seg) {
939 dir_to_sound = sound_pos - Viewer_object->pos;
940 dist = vm_NormalizeVector(&dir_to_sound);
941 } else if ((cur_room != last_room) && (cur_room != BOA_NO_PATH)) {
942 int this_portal = BOA_DetermineStartRoomPortal(cur_room, NULL, last_room, NULL);
943 dist = BOA_cost_array[cur_room][this_portal];
944
945 if (last_room > Highest_room_index) {
946 vector pnt = Rooms[cur_room].portals[this_portal].path_pnt;
947 dist += vm_VectorDistance(&sound_pos, &pnt);
948 } else {
949 dist += vm_VectorDistance(&sound_pos, &Rooms[last_room].portals[last_portal].path_pnt);
950 }
951 }
952 } else if (cur_room == ear_seg) {
953 dist += BOA_cost_array[last_room][last_portal];
954
955 if (last_room > Highest_room_index) {
956 int this_portal = BOA_DetermineStartRoomPortal(cur_room, NULL, last_room, NULL);
957 vector pnt = Rooms[cur_room].portals[this_portal].path_pnt;
958 dist += vm_VectorDistance(&Viewer_object->pos, &pnt);
959 } else {
960 dist += vm_VectorDistance(&Viewer_object->pos, &Rooms[last_room].portals[last_portal].path_pnt);
961 }
962 } else if ((cur_room != last_room) && (cur_room != BOA_NO_PATH)) {
963 int this_portal = BOA_DetermineStartRoomPortal(cur_room, NULL, last_room, NULL);
964 dist += BOA_cost_array[last_room][last_portal] + BOA_cost_array[cur_room][this_portal];
965 }
966 } while ((cur_room != ear_seg) && (cur_room != last_room) && (cur_room != BOA_NO_PATH));
967 if (cur_room == BOA_NO_PATH) {
968 *adjusted_volume = 0.0;
969 } else if ((last_room != ear_seg) && (last_room != sound_seg)) {
970 dir_to_sound = Rooms[last_room].path_pnt - Viewer_object->pos;
971 vm_NormalizeVector(&dir_to_sound);
972 }
973 } else
974 {
975 dir_to_sound = sound_pos - Viewer_object->pos;
976 dist = vm_NormalizeVector(&dir_to_sound);
977 }
978 if (dist >= Sounds[sound_index].max_distance)
979 return false;
980
981 if (*adjusted_volume <= 0.0) {
982 *adjusted_volume = 0.0;
983 return false;
984 }
985 if (dist == 0.0f) {
986 dir_to_sound = Viewer_object->orient.fvec;
987 }
988 if ((m_sound_objects[sound_obj_index].play_info.sample_skip_interval == 0) && (*adjusted_volume > 0.0f) &&
989 (dir_to_sound * Viewer_object->orient.fvec < -.5))
990 m_sound_objects[sound_obj_index].play_info.sample_skip_interval = 1;
991 *virtual_pos = Viewer_object->pos + (dir_to_sound * dist);
992 return true;
993 }
994 bool hlsSystem::Emulate3dSound(int sound_obj_index) {
995 bool f_audible;
996 vector virtual_pos;
997 vector virtual_vel;
998 float adjusted_volume;
999 f_audible = ComputePlayInfo(sound_obj_index, &virtual_pos, &virtual_vel, &adjusted_volume);
1000
1001 if (f_audible) {
1002 pos_state cur_pos;
1003 matrix orient = Identity_matrix;
1004 cur_pos.velocity = &virtual_vel;
1005 cur_pos.position = &virtual_pos;
1006 cur_pos.orient = &orient;
1007 m_sound_objects[sound_obj_index].m_sound_uid = m_ll_sound_ptr->PlaySound3d(
1008 &m_sound_objects[sound_obj_index].play_info, m_sound_objects[sound_obj_index].m_sound_index, &cur_pos,
1009 adjusted_volume, (m_sound_objects[sound_obj_index].m_obj_type_flags & SIF_LOOPING) != 0);
1010 if (m_sound_objects[sound_obj_index].m_sound_uid == -1)
1011 f_audible = false;
1012 } else {
1013 m_sound_objects[sound_obj_index].m_sound_uid =
1014 0; // This is a dummy value -- it just cannot be -1 (that would mean that the low level sound system is full)
1015 }
1016 return f_audible;
1017 }
1018 // Functions that play a 3d sound -- includes the 2d emulation of 3d sound
1019 int hlsSystem::Play3dSound(int sound_index, pos_state *cur_pos, float volume, int flags, float offset) {
1020 return Play3dSound(sound_index, SND_PRIORITY_NORMAL, cur_pos, volume, flags, offset);
1021 }
1022 // Functions that play a 3d sound -- includes the 2d emulation of 3d sound
1023 int hlsSystem::Play3dSound(int sound_index, object *cur_obj, float volume, int flags, float offset) {
1024 return Play3dSound(sound_index, SND_PRIORITY_NORMAL, cur_obj, volume, flags, offset);
1025 }
1026 int hlsSystem::Play3dSound(int sound_index, int priority, pos_state *cur_pos, float volume, int flags, float offset) {
1027 if (!m_f_hls_system_init)
1028 return -1;
1029 return Play3dSound(sound_index, cur_pos, NULL, priority, volume, flags, offset);
1030 }
1031 // Functions that play a 3d sound -- includes the 2d emulation of 3d sound
1032 int hlsSystem::Play3dSound(int sound_index, int priority, object *cur_obj, float volume, int flags, float offset) {
1033 pos_state cur_pos;
1034 if (!m_f_hls_system_init)
1035 return -1;
1036 cur_pos.position = &cur_obj->pos;
1037 cur_pos.orient = &cur_obj->orient;
1038 cur_pos.velocity = &cur_obj->mtype.phys_info.velocity;
1039 cur_pos.roomnum = cur_obj->roomnum;
1040 return Play3dSound(sound_index, &cur_pos, cur_obj, priority, volume, flags, offset);
1041 }
1042 // Functions that play a 3d sound -- includes the 2d emulation of 3d sound
1043 int hlsSystem::Play3dSound(int sound_index, pos_state *cur_pos, object *cur_obj, int priority, float volume, int flags,
1044 float offset) {
1045 int i;
1046 int sound_uid;
1047 int sound_obj_index;
1048 bool f_audible;
1049 if (!m_f_hls_system_init)
1050 return -1;
1051 if (sound_index == SOUND_NONE_INDEX)
1052 return -1;
1053 if (sound_index < 0)
1054 return -1;
1055 if (sound_index >= MAX_SOUNDS || Sounds[sound_index].used == 0)
1056 return -1;
1057 // if the position doesn't belong to any valid room or cell,
1058 // all this would fail anyway (in Emulate3dSound() -> ComputePlayInfo()),
1059 // so might as well give up now; furthermore, this prevents m_sound_objects[i]
1060 // from remaining in an half-initialized state below (esp. for looping sounds
1061 // where StopSound() wouldn't be called after Emulate3dSound() returns false)
1062 if (cur_pos->roomnum == -1)
1063 return -1;
1064 // initialize sound.
1065 Sound_system.CheckAndForceSoundDataAlloc(sound_index);
1066 int sample_offset = offset * 22050.0f;
1067 if (sample_offset >= SoundFiles[Sounds[sound_index].sample_index].np_sample_length)
1068 return -1;
1069 // Handles exclusive sounds
1070 if (Sounds[sound_index].flags & SPF_PLAYS_EXCLUSIVELY) {
1071 sound_uid = m_ll_sound_ptr->IsSoundPlaying(sound_index);
1072 if (sound_uid != -1)
1073 return -1;
1074 }
1075 // Handles play once sounds
1076 if (Sounds[sound_index].flags & SPF_PLAYS_ONCE) {
1077 for (sound_obj_index = 0; sound_obj_index < MAX_SOUND_OBJECTS; sound_obj_index++) {
1078 if ((m_sound_objects[sound_obj_index].m_sound_index == sound_index) &&
1079 ((sound_uid = m_sound_objects[sound_obj_index].m_sound_uid) != -1)) {
1080 StopSound(sound_obj_index);
1081 }
1082 }
1083 }
1084 // Handles play once per object
1085 if ((Sounds[sound_index].flags & SPF_ONCE_PER_OBJ) && cur_obj) {
1086 for (sound_obj_index = 0; sound_obj_index < MAX_SOUND_OBJECTS; sound_obj_index++) {
1087 if ((m_sound_objects[sound_obj_index].m_sound_index == sound_index) &&
1088 ((sound_uid = m_sound_objects[sound_obj_index].m_sound_uid) != -1)) {
1089 if (m_sound_objects[sound_obj_index].m_obj_type_flags & SIF_OBJ_UPDATE) {
1090 if (m_sound_objects[sound_obj_index].m_link_info.object_handle == cur_obj->handle) {
1091 return -1;
1092 }
1093 }
1094 }
1095 }
1096 }
1097 // find free sound slot.
1098 for (i = 0; i < MAX_SOUND_OBJECTS; i++) {
1099 if (m_sound_objects[i].m_obj_type_flags == SIF_UNUSED)
1100 break; //
1101 }
1102 // no free slots? hmmm....
1103 if (i >= MAX_SOUND_OBJECTS) {
1104 mprintf(2, "HLSOUNDLIB HOLY COW: Over %d sounds trying to play(beyond max) - %s\n", MAX_SOUND_OBJECTS,
1105 Sounds[sound_index].name);
1106 // Int3();
1107 return -1;
1108 }
1109 // Set the current sound
1110 m_sound_objects[i].m_sound_index = sound_index;
1111 // Insert the passed flags
1112 m_sound_objects[i].m_obj_type_flags = flags;
1113 // Determine if the sound is linked to the object
1114 if ((cur_obj != NULL) && (Sounds[sound_index].flags & SPF_LISTENER_UPDATE) &&
1115 (Sounds[sound_index].flags & SPF_OBJ_UPDATE)) {
1116 m_sound_objects[i].m_obj_type_flags |= SIF_OBJ_UPDATE;
1117 m_sound_objects[i].m_link_info.object_handle = cur_obj->handle;
1118 } else {
1119 m_sound_objects[i].m_link_info.pos_info.pos = *cur_pos->position;
1120 m_sound_objects[i].m_link_info.pos_info.orient = *cur_pos->orient;
1121 m_sound_objects[i].m_link_info.pos_info.segnum = cur_pos->roomnum;
1122 // NOTE: Velocity is zero for all non object-linked sounds
1123 }
1124 m_sound_objects[i].volume_3d = volume;
1125 // m_sound_objects[i].m_sound_uid = m_ll_sound_ptr->PlaySound3d(sound_index, cur_pos, 22050);
1126 // Increment the total number of sounds played
1127 m_sounds_played++;
1128 // Initialize the play information to nice values
1129 memset(&m_sound_objects[i].play_info, 0, sizeof(play_information));
1130 m_sound_objects[i].play_info.samples_per_22khz_sample = 1.0;
1131 m_sound_objects[i].play_info.sample_skip_interval = 0;
1132 m_sound_objects[i].play_info.priority = priority; // Set sound's priority rating
1133 m_sound_objects[i].m_hlsound_uid = MakeUniqueId(i);
1134 if (Sounds[sound_index].flags & SPF_LOOPED)
1135 m_sound_objects[i].m_obj_type_flags |= SIF_LOOPING;
1136 if (!m_pause_new) {
1137 f_audible = Emulate3dSound(i);
1138 if ((!f_audible) && (!(m_sound_objects[i].m_obj_type_flags & SIF_LOOPING))) {
1139 StopSound(i, SKT_STOP_IMMEDIATELY);
1140 return -1;
1141 }
1142 } else {
1143 f_audible = false;
1144 m_sound_objects[i].m_sound_uid = -1;
1145 }
1146 if (m_sound_objects[i].m_sound_uid == -1) {
1147 m_sound_objects[i].m_obj_type_flags |= SIF_TOO_FAR;
1148 // mprintf(0, "LLSound full 2\n");
1149 } else if (!f_audible) {
1150 m_sound_objects[i].m_obj_type_flags |= SIF_TOO_FAR;
1151 }
1152 m_sound_objects[i].m_obj_type_flags |= SIF_PLAYING_3D;
1153 if (m_pause_new) {
1154 PauseSounds(true);
1155 }
1156
1157 return m_sound_objects[i].m_hlsound_uid;
1158 }
1159 // General purpose 2d sound play function
1160 int hlsSystem::Play2dSound(int sound_index, float volume, float pan, uint16_t frequency) {
1161 return hlsSystem::Play2dSound(sound_index, SND_PRIORITY_NORMAL, volume, pan, frequency);
1162 }
1163 int hlsSystem::Play2dSound(int sound_index, int priority, float volume, float pan, uint16_t frequency) {
1164 int i = 0;
1165 int sound_uid;
1166 int sound_obj_index;
1167 if (!m_f_hls_system_init)
1168 return -1;
1169 if (sound_index == SOUND_NONE_INDEX)
1170 return -1;
1171 if (m_pause_new)
1172 return -1;
1173 if (sound_index < 0) {
1174 return -1;
1175 }
1176 Sound_system.CheckAndForceSoundDataAlloc(sound_index);
1177 // Handles exclusive sounds
1178 if (Sounds[sound_index].flags & SPF_PLAYS_EXCLUSIVELY) {
1179 sound_uid = m_ll_sound_ptr->IsSoundPlaying(sound_index);
1180 if (sound_uid != -1)
1181 return -1;
1182 }
1183 // Handles play once sounds
1184 if (Sounds[sound_index].flags & SPF_PLAYS_ONCE) {
1185 for (sound_obj_index = 0; sound_obj_index < MAX_SOUND_OBJECTS; sound_obj_index++) {
1186 if ((m_sound_objects[sound_obj_index].m_sound_index == sound_index) &&
1187 ((sound_uid = m_sound_objects[sound_obj_index].m_sound_uid) != -1)) {
1188 StopSound(sound_obj_index);
1189 }
1190 }
1191 }
1192 for (i = 0; i < MAX_SOUND_OBJECTS; i++) {
1193 if (m_sound_objects[i].m_obj_type_flags == SIF_UNUSED)
1194 break;
1195 }
1196 // mprintf(0, "HL %d\n", i);
1197 // ASSERT(i < MAX_SOUND_OBJECTS);
1198 if (i >= MAX_SOUND_OBJECTS) {
1199 mprintf(3, "Play2DSound: Max Sounds Objects used\n");
1200 // Int3();
1201 return -1;
1202 }
1203 // Increment the total number of sounds played
1204 m_sounds_played++;
1205 // Initialize the play information to nice values
1206 memset(&m_sound_objects[i].play_info, 0, sizeof(play_information));
1207 m_sound_objects[i].play_info.samples_per_22khz_sample = 1.0;
1208 m_sound_objects[i].m_hlsound_uid = MakeUniqueId(i);
1209 m_sound_objects[i].play_info.priority = SND_PRIORITY_NORMAL;
1210 // static_skip++;
1211 // if (static_skip > 2)
1212 // static_skip = 0;
1213 //
1214 // m_sound_objects[i].play_info.sample_skip_interval = static_skip;
1215 //
1216 // if(m_sound_objects[i].play_info.sample_skip_interval == 0)
1217 // mprintf(0, "22.5k\n");
1218 // else if (m_sound_objects[i].play_info.sample_skip_interval == 1)
1219 // mprintf(0, "11.25k\n");
1220 // else
1221 // mprintf(0, "5.75k\n");
1222 m_sound_objects[i].m_sound_index = sound_index;
1223 m_sound_objects[i].m_sound_uid =
1224 m_ll_sound_ptr->PlaySound2d(&m_sound_objects[i].play_info, sound_index, volume * m_master_volume, pan,
1225 (Sounds[sound_index].flags & SPF_LOOPED));
1226 // ASSERT(m_sound_objects[i].m_sound_uid != -1);
1227 if (m_sound_objects[i].m_sound_uid == -1) {
1228 m_sound_objects[i].m_obj_type_flags = SIF_UNUSED;
1229 m_sound_objects[i].m_hlsound_uid = -1;
1230 mprintf(1, "Play2DSound: $%d Unplayed\n", i);
1231 return -1;
1232 }
1233
1234 m_sound_objects[i].m_obj_type_flags |= SIF_PLAYING_2D;
1235
1236 if (Sounds[m_sound_objects[i].m_sound_index].flags & SPF_LOOPED)
1237 m_sound_objects[i].m_obj_type_flags |= SIF_LOOPING;
1238 return m_sound_objects[i].m_hlsound_uid;
1239 }
1240 int hlsSystem::PlayStream(int unique_handle, void *data, int size, int stream_format, float volume,
1241 void *stream_callback(void *user_data, int handle, int *size)) {
1242 int i = 0;
1243 ASSERT(stream_format == SIF_STREAMING_8_M || stream_format == SIF_STREAMING_16_M ||
1244 stream_format == SIF_STREAMING_8_S || stream_format == SIF_STREAMING_16_S);
1245 if (!m_f_hls_system_init)
1246 return -1;
1247 if (data == NULL || size <= 0)
1248 return -1;
1249 for (i = 0; i < MAX_SOUND_OBJECTS; i++) {
1250 if (m_sound_objects[i].m_obj_type_flags == SIF_UNUSED)
1251 break;
1252 }
1253 if (i >= MAX_SOUND_OBJECTS) {
1254 mprintf(2, "PlayStream:Max Sounds Objects\n");
1255 // Int3();
1256 return -1;
1257 }
1258 // Increment the total number of sounds played
1259 m_sounds_played++;
1260 // Initialize the play information to nice values
1261 memset(&m_sound_objects[i].play_info, 0, sizeof(play_information));
1262 m_sound_objects[i].play_info.samples_per_22khz_sample = 1.0;
1263 m_sound_objects[i].play_info.left_volume = m_sound_objects[i].play_info.right_volume = volume * m_master_volume;
1264 m_sound_objects[i].play_info.m_stream_cback = stream_callback;
1265 m_sound_objects[i].play_info.m_stream_data = data;
1266 m_sound_objects[i].play_info.m_stream_format = stream_format;
1267 m_sound_objects[i].play_info.m_stream_size = size;
1268 m_sound_objects[i].play_info.m_stream_handle = unique_handle;
1269 m_sound_objects[i].play_info.m_stream_bufsize = size;
1270 m_sound_objects[i].m_hlsound_uid = MakeUniqueId(i);
1271 m_sound_objects[i].m_sound_index = -1;
1272
1273 m_sound_objects[i].m_sound_uid = m_ll_sound_ptr->PlayStream(&m_sound_objects[i].play_info);
1274 // ASSERT(m_sound_objects[i].m_sound_uid != -1);
1275 if (m_sound_objects[i].m_sound_uid == -1) {
1276 m_sound_objects[i].m_obj_type_flags = SIF_UNUSED;
1277 m_sound_objects[i].m_hlsound_uid = -1;
1278 mprintf(2, "LLSound full 1\n");
1279 return -1;
1280 }
1281
1282 m_sound_objects[i].m_obj_type_flags |= SIF_PLAYING_2D | stream_format;
1283 if (m_pause_new) {
1284 PauseSounds(true);
1285 }
1286 return m_sound_objects[i].m_hlsound_uid;
1287 }
1288 // Stop a looping sound and plays end snipit
1289 void hlsSystem::StopSoundLooping(int hlsound_uid) {
1290 int sound_object_index;
1291 if (m_f_hls_system_init < 1)
1292 return; // DAJ -1FIX
1293 sound_object_index = ValidateUniqueId(hlsound_uid);
1294 if (sound_object_index == -1)
1295 return;
1296 StopSound(sound_object_index, SKT_STOP_AFTER_LOOP);
1297 }
1298 void hlsSystem::StopSoundImmediate(int hlsound_uid) {
1299 int sound_object_index;
1300 if (m_f_hls_system_init < 1)
1301 return; // DAJ -1FIX
1302 sound_object_index = ValidateUniqueId(hlsound_uid);
1303 if (sound_object_index == -1)
1304 return;
1305 StopSound(sound_object_index, SKT_STOP_IMMEDIATELY);
1306 }
1307 // Forcefully ends a sound
1308 void hlsSystem::StopSound(int sound_obj_index, uint8_t f_stop_priority) {
1309 if (!m_f_hls_system_init)
1310 return;
1311 m_sound_objects[sound_obj_index].m_obj_type_flags &= (~SIF_LOOPING);
1312
1313 if (m_sound_objects[sound_obj_index].m_sound_uid != -1) {
1314 mprintf(1, "stopSound %d \n", m_sound_objects[sound_obj_index].m_sound_uid);
1315 m_ll_sound_ptr->StopSound(m_sound_objects[sound_obj_index].m_sound_uid, f_stop_priority);
1316 }
1317 if (f_stop_priority == SKT_STOP_IMMEDIATELY || m_sound_objects[sound_obj_index].m_sound_uid == -1) {
1318 m_sound_objects[sound_obj_index].m_obj_type_flags = SIF_UNUSED;
1319 m_sound_objects[sound_obj_index].m_hlsound_uid = -1;
1320 }
1321 }
1322 // Stops all sounds attached to an object
1323 void hlsSystem::StopObjectSound(int objhandle) {
1324 int i;
1325 for (i = 0; i < MAX_SOUND_OBJECTS; i++)
1326 if (m_sound_objects[i].m_obj_type_flags & SIF_OBJ_UPDATE)
1327 if (m_sound_objects[i].m_link_info.object_handle == objhandle)
1328 StopSound(i, SKT_STOP_IMMEDIATELY);
1329 }
1330 // Set the volume for all the sounds attached to an object
1331 void hlsSystem::SetVolumeObject(int objhandle, float volume) {
1332 for (int i = 0; i < MAX_SOUND_OBJECTS; i++)
1333 if (m_sound_objects[i].m_obj_type_flags & SIF_OBJ_UPDATE)
1334 if (m_sound_objects[i].m_link_info.object_handle == objhandle)
1335 m_sound_objects[i].volume_3d = volume;
1336 }
1337 // Queued sound functions
1338 // Adds a sound to a queue
1339 void hlsSystem::Add2dSoundQueued(int q_num, int sound_index, float volume, float pan, uint16_t frequency) {
1340 if (!m_f_hls_system_init)
1341 return;
1342 }
1343 // Cleans a queue (if sound is playing it will finish normally)
1344 void hlsSystem::KillQueue(int q_num) {
1345 if (!m_f_hls_system_init)
1346 return;
1347 }
1348 // Kills all sound queues
1349 void hlsSystem::KillAllQueues() {
1350 if (!m_f_hls_system_init)
1351 return;
1352 }
1353 // Midi play stuff
1354 void hlsSystem::PlayMidi() {
1355 if (!m_f_hls_system_init)
1356 return;
1357 }
1358 void hlsSystem::StopMidi() {
1359 if (!m_f_hls_system_init)
1360 return;
1361 }
1362 void hlsSystem::PauseMidi() {
1363 if (!m_f_hls_system_init)
1364 return;
1365 }
1366 void hlsSystem::ResumeMidi() {
1367 if (!m_f_hls_system_init)
1368 return;
1369 }
1370 void hlsSystem::SetMidiVolume() {
1371 if (!m_f_hls_system_init)
1372 return;
1373 }
1374 void hlsSystem::GetMidiVolume() {
1375 if (!m_f_hls_system_init)
1376 return;
1377 }
1378 // Sets the master volume
1379 void hlsSystem::SetMasterVolume(float volume) {
1380 extern void StreamVolume(float master_volume);
1381 ASSERT(volume >= 0.0 && volume <= 1.0);
1382
1383 if (volume == 0) {
1384 StopAllSounds();
1385 CloseVoices();
1386 KillSoundLib(true);
1387 m_master_volume = 0;
1388 } else {
1389 if (m_master_volume == 0) {
1390 InitSoundLib(Descent, Sound_mixer, Sound_quality, false);
1391 InitVoices();
1392 }
1393 }
1394 m_master_volume = volume;
1395 StreamVolume(m_master_volume);
1396 }
1397
1398 // Gets the master volume
1399 float hlsSystem::GetMasterVolume(void) { return m_master_volume; }
1400 inline int hlsSystem::MakeUniqueId(int sound_obj_index) { return ((((int)m_sounds_played) << 12) + sound_obj_index); }
1401 inline int hlsSystem::ValidateUniqueId(int hl_sound_uid) {
1402 ASSERT(MAX_SOUND_OBJECTS <= 0x0FFF);
1403 int index = hl_sound_uid & 0x0FFF;
1404 if (index < 0 || index >= MAX_SOUND_OBJECTS)
1405 return -1;
1406
1407 if (hl_sound_uid == m_sound_objects[index].m_hlsound_uid) {
1408 return index;
1409 } else {
1410 return -1;
1411 }
1412 }
1413 bool hlsSystem::CheckAndForceSoundDataAlloc(int sound_index) {
1414 if (!m_f_hls_system_init)
1415 return false;
1416 return m_ll_sound_ptr->CheckAndForceSoundDataAlloc(sound_index);
1417 }
1418 bool hlsSystem::IsSoundPlaying(int hlsound_uid) {
1419 int hl_index;
1420 if (!m_f_hls_system_init)
1421 return false;
1422 hl_index = ValidateUniqueId(hlsound_uid);
1423 if (hl_index != -1) {
1424 return m_ll_sound_ptr->IsSoundInstancePlaying(m_sound_objects[hl_index].m_sound_uid);
1425 }
1426 return false;
1427 }
1428 bool hlsSystem::SetSoundQuality(char quality) {
1429 if (!m_f_hls_system_init)
1430 return false;
1431 m_ll_sound_ptr->SetSoundQuality(quality);
1432 Sound_quality = m_ll_sound_ptr->GetSoundQuality();
1433 return true;
1434 }
1435 char hlsSystem::GetSoundQuality(void) {
1436 if (!m_f_hls_system_init)
1437 return -1;
1438 return (Sound_quality = m_ll_sound_ptr->GetSoundQuality());
1439 }
1440 bool hlsSystem::SetSoundMixer(char mixer) {
1441 if (!m_f_hls_system_init)
1442 return false;
1443 m_ll_sound_ptr->SetSoundMixer(mixer);
1444 Sound_mixer = m_ll_sound_ptr->GetSoundMixer();
1445 return true;
1446 }
1447 char hlsSystem::GetSoundMixer(void) {
1448 if (!m_f_hls_system_init)
1449 return SOUND_MIXER_NONE;
1450 return (Sound_mixer = m_ll_sound_ptr->GetSoundMixer());
1451 }