| 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/lib/ds3dlib.h $ |
| 21 | * $Revision: 49 $ |
| 22 | * $Date: 8/23/99 5:29p $ |
| 23 | * $Author: Samir $ |
| 24 | * |
| 25 | * Low-level library for 3d sound |
| 26 | * |
| 27 | * $Log: /DescentIII/Main/lib/ds3dlib.h $ |
| 28 | * |
| 29 | * 49 8/23/99 5:29p Samir |
| 30 | * incremental EAX 2.0 checkin |
| 31 | * |
| 32 | * 48 5/03/99 3:12a Samir |
| 33 | * fixed up aureal so it works (a little slow though...) |
| 34 | * |
| 35 | * 47 4/29/99 3:01p Samir |
| 36 | * added code for direct sound mixers only (and function for Aureal |
| 37 | * really) that will use direct sound looping for simple loops. |
| 38 | * |
| 39 | * 46 4/27/99 2:10p Samir |
| 40 | * added code to set the desired sound card given the descriptive name of |
| 41 | * a sound card. |
| 42 | * |
| 43 | * 45 4/23/99 7:51p Samir |
| 44 | * looping fixes for directsound. |
| 45 | * |
| 46 | * 44 4/22/99 10:33p Samir |
| 47 | * modifications so that DirectSound mixers use one thread for all looping |
| 48 | * and streaming sounds. It worked without crashing for about twenty |
| 49 | * minutes of playing from level 1 to level 2 of D3. We'll see. |
| 50 | * |
| 51 | * 43 4/13/99 4:09p Samir |
| 52 | * more priority stuff. |
| 53 | * |
| 54 | * 42 4/12/99 7:14p Samir |
| 55 | * prioritization code added. |
| 56 | * |
| 57 | * 41 4/10/99 5:08p Samir |
| 58 | * took out obsolete data from play_information structure that should save |
| 59 | * around 70 bytes per instance. |
| 60 | * |
| 61 | * 40 4/09/99 12:03p Samir |
| 62 | * took out windows.h again, this time made HWND a void pointer, and |
| 63 | * checked under both editor and main projects. |
| 64 | * |
| 65 | * 39 4/07/99 12:38a 3dsmax |
| 66 | * Misc changes to allow editor build, as per Samir's phone instructions. |
| 67 | * Will this break the game build? Who knows? (Matt, ChrisP) |
| 68 | * |
| 69 | * 38 4/06/99 8:29p Samir |
| 70 | * added error check system. |
| 71 | * |
| 72 | * 37 3/29/99 11:01a Samir |
| 73 | * added ability to change 3d environment. |
| 74 | * |
| 75 | * 36 3/17/99 4:20p Samir |
| 76 | * added functions to pause and resume individual sounds. |
| 77 | * |
| 78 | * 35 2/19/99 5:21p Kevin |
| 79 | * Fixed some connection DLLs and a Direct Sound bug with threads. |
| 80 | * |
| 81 | * 34 1/14/99 6:10p Samir |
| 82 | * added DirectSound buffer duplication code. |
| 83 | * |
| 84 | * 33 1/11/99 5:51p Samir |
| 85 | * reverb on the buffer level. |
| 86 | * |
| 87 | * 32 1/08/99 6:31p Samir |
| 88 | * added reverb |
| 89 | * |
| 90 | * 31 1/08/99 11:36a Samir |
| 91 | * implemented basic Aureal 2.0 support. |
| 92 | * |
| 93 | * 30 12/23/98 11:49a Samir |
| 94 | * reorganized code so it works with different APIs. |
| 95 | * |
| 96 | * 29 7/08/98 12:09p Samir |
| 97 | * use C RTL for thread creation. |
| 98 | * |
| 99 | * 28 7/06/98 1:48p Samir |
| 100 | * added A3d support. |
| 101 | * |
| 102 | * 27 6/29/98 4:15p Chris |
| 103 | * Streaming audio works |
| 104 | * |
| 105 | * 26 6/29/98 9:29a Chris |
| 106 | * Added some support for Direct Sound 3d |
| 107 | * |
| 108 | * 25 6/25/98 11:28a Chris |
| 109 | * Added some DS3d support |
| 110 | * |
| 111 | * 24 6/24/98 6:43p Chris |
| 112 | * Furthered the endless work on the sound system |
| 113 | * |
| 114 | * 23 6/24/98 4:39p Chris |
| 115 | * Improved the multiple mixer support |
| 116 | * |
| 117 | * 22 6/24/98 11:31a Chris |
| 118 | * Fixed an ordering bug |
| 119 | * |
| 120 | * 21 6/24/98 11:14a Chris |
| 121 | * Added more support and bug fixes |
| 122 | * |
| 123 | * 20 6/24/98 1:07a Chris |
| 124 | * Intermediate checkin |
| 125 | * |
| 126 | * 19 6/23/98 6:00p Chris |
| 127 | * DirectSound 16 mixer is active with 8 bit samples |
| 128 | * |
| 129 | * 18 6/22/98 12:00p Chris |
| 130 | * Working on sound system to make it in a nice shape. |
| 131 | * |
| 132 | * 17 6/19/98 3:09p Chris |
| 133 | * Improved IsSoundPlaying |
| 134 | * |
| 135 | * 16 6/19/98 3:03p Chris |
| 136 | * Made CheckAndForceSoundDataAlloc a SoundSystem function - useful for |
| 137 | * multiple mixers. Added IsSoundPlaying to the high level sound lib. |
| 138 | * |
| 139 | * 15 6/16/98 3:48p Chris |
| 140 | * Updated the sound system and added the start of sound streaming |
| 141 | * |
| 142 | * 14 2/26/98 5:11p Chris |
| 143 | * Removed a unused data element from the llsoundlib |
| 144 | * |
| 145 | * 13 2/26/98 3:08p Chris |
| 146 | * More NT stuff |
| 147 | * |
| 148 | * 12 2/25/98 3:19p Jeff |
| 149 | * more NT fixes |
| 150 | * |
| 151 | * 11 2/25/98 2:47p Jeff |
| 152 | * CHRIS: Added some NT support for sound code. It is pretty hokey right |
| 153 | * now. |
| 154 | * |
| 155 | * 10 2/25/98 12:13p Chris |
| 156 | * NT support? Not sure... I do not have the Editor in NT. |
| 157 | * |
| 158 | * 9 2/15/98 7:12p Chris |
| 159 | * More improvements to the sound lib |
| 160 | * |
| 161 | * 8 1/23/98 4:21p Chris |
| 162 | * Changed the low-level sound code from multimedia timers to a thread |
| 163 | * that sleeps(); |
| 164 | * |
| 165 | * 7 1/02/98 5:32p Chris |
| 166 | * More radical changes to the sound system |
| 167 | * |
| 168 | * 6 12/31/97 2:58a Chris |
| 169 | * Another major revision to the SoundLib. This cleaned up the code after |
| 170 | * removing the direct sound secondary buffers. |
| 171 | * |
| 172 | * 5 12/22/97 6:19p Chris |
| 173 | * Working on new mixer functionality |
| 174 | * |
| 175 | * 4 12/19/97 4:08p Chris |
| 176 | * New dynamically loading sounds |
| 177 | * |
| 178 | * 3 12/10/97 4:47p Chris |
| 179 | * Revision 1.0 of new sound library (no hardware -- uses primary buffer |
| 180 | * streaming) |
| 181 | * |
| 182 | * 2 11/21/97 1:10p Chris |
| 183 | * Incremental Improvements |
| 184 | * |
| 185 | * 18 6/11/97 1:07p Samir |
| 186 | * The removal of gameos and replaced with oeApplication, oeDatabase |
| 187 | * |
| 188 | * 17 5/22/97 5:15p Chris |
| 189 | * |
| 190 | * 16 4/29/97 7:26a Chris |
| 191 | * Improved the sound code. It is healthy. |
| 192 | * |
| 193 | * 15 4/28/97 11:21a Chris |
| 194 | * Incremental improvements is the sound system |
| 195 | * |
| 196 | * 14 4/24/97 3:25a Chris |
| 197 | * // Added some more support for 3d sounds |
| 198 | * |
| 199 | * 13 4/03/97 8:37p Chris |
| 200 | * Added the Main_OS object pass in the sound library |
| 201 | * initialization code. |
| 202 | * |
| 203 | * 12 4/01/97 10:00a Chris |
| 204 | * |
| 205 | * 11 3/17/97 5:12p Chris |
| 206 | * Allowed new sound code to be demo'ed. |
| 207 | * |
| 208 | * 10 3/14/97 12:10p Chris |
| 209 | * Made a nice C++ abstract type for the low-level. It should make the |
| 210 | * MacMan very joyful. :) |
| 211 | * |
| 212 | * 9 2/07/97 5:48p Matt |
| 213 | * Renamed vector.h to vecmat.h to fix DevStudio problem |
| 214 | * |
| 215 | * $NoKeywords: $ |
| 216 | */ |
| 217 | |
| 218 | #ifndef __ds3dlib_h__ |
| 219 | #define __ds3dlib_h__ |
| 220 | |
| 221 | #include <windows.h> |
| 222 | // #include <mmsystem.h> // Multi-media system support |
| 223 | // #include "dsound.h" // Direct sound header file |
| 224 | |
| 225 | #include "ssl_lib.h" // Shared sound header (between high and low-levels) |
| 226 | #include "vecmat.h" |
| 227 | |
| 228 | #ifndef DS3DLIB_INTERNAL_H |
| 229 | typedef void *LPDIRECTSOUND; |
| 230 | typedef void *LPDIRECTSOUNDBUFFER; |
| 231 | typedef void *LPDIRECTSOUND3DLISTENER; |
| 232 | typedef void *LPDIRECTSOUND3DBUFFER; |
| 233 | typedef void *LPDIRECTSOUNDCAPTURE; |
| 234 | typedef void *LPDIRECTSOUNDCAPTUREBUFFER; |
| 235 | typedef void *LPDIRECTSOUNDNOTIFY; |
| 236 | // typedef void *HWND; |
| 237 | class sound_buffer_info; |
| 238 | struct tPSBInfo; |
| 239 | #endif |
| 240 | |
| 241 | // Sound cache list |
| 242 | class sound_buffer_cache { |
| 243 | public: |
| 244 | class sound_buffer_info *m_sound_cache; // List of all sounds current sounds |
| 245 | uint16_t m_max_sounds_played; // Maximum sounds played at any given moment |
| 246 | uint16_t m_cur_sounds_played; // Current number of sounds playing |
| 247 | unsigned m_loop_method; // what method do we do looping sounds (direct sound only) |
| 248 | |
| 249 | public: |
| 250 | sound_buffer_info *FindSoundBuffer(int sound_index); |
| 251 | }; |
| 252 | |
| 253 | class emulated_listener { |
| 254 | public: |
| 255 | matrix orient; |
| 256 | vector position; |
| 257 | vector velocity; |
| 258 | }; |
| 259 | |
| 260 | // Sound library object |
| 261 | |
| 262 | // Commonly passed arguments: |
| 263 | // sound_index -- index of a particular sample in the SoundArray |
| 264 | // sound_uid -- unique id of a particular sound being played |
| 265 | // sound_slot -- "channel" that a sample is being played on (derivable from sound_uid) |
| 266 | |
| 267 | class win_llsSystem : public llsSystem { |
| 268 | private: |
| 269 | // Windows and Direct Sound information |
| 270 | LPDIRECTSOUND m_lp_ds; // pointer to direct sound object |
| 271 | void *m_hwnd_main; // Handle to main window -- needed for direct sound |
| 272 | |
| 273 | // For DS3D |
| 274 | LPDIRECTSOUND3DLISTENER m_lp_listener; |
| 275 | |
| 276 | // For 3d sounds and non-3D mixers |
| 277 | emulated_listener m_emulated_listener; |
| 278 | |
| 279 | // For DS and DS3d mixers |
| 280 | void LoopStartStreaming(sound_buffer_info *sb, int buffer_type, float volume, float pan, pos_state *cur_pos); |
| 281 | void DSStartStreaming(sound_buffer_info *sb, float volume, float pan); |
| 282 | |
| 283 | friend void __cdecl LoopStreamTimer(void *user_ptr); |
| 284 | |
| 285 | // Creates a sound buffer |
| 286 | HRESULT CreateDSBuffer(int buffer_type, LPDIRECTSOUNDBUFFER *lp_lp_dsb, LPDIRECTSOUND3DBUFFER *lp_lp_dsb_3d, |
| 287 | DWORD sound_bytes, DWORD frequency, bool f_is_stereo, bool f_is_16_bit); |
| 288 | |
| 289 | // Finds a free slot for a new sound, slot_uid is an SBID_xxx value. |
| 290 | #ifdef _DEBUG |
| 291 | int16_t FindFreeSoundSlot(int sound_index, float volume, int priority); |
| 292 | #else |
| 293 | int16_t FindFreeSoundSlot(float volume, int priority); |
| 294 | #endif |
| 295 | |
| 296 | // updates a directsound buffer |
| 297 | void update_directsound_sb(sound_buffer_info *sb, bool update_looping = false); |
| 298 | |
| 299 | // used to clean up direct sound buffer stuff |
| 300 | void cleanup_directsound_looping_sb(sound_buffer_info *sb); |
| 301 | |
| 302 | // Makes a unique sound id from a sound slot number |
| 303 | inline int MakeUniqueId(int sound_slot); |
| 304 | // Validates and converts a unique sound id to a sound slot number |
| 305 | inline int ValidateUniqueId(int sound_uid); |
| 306 | |
| 307 | friend void StreamMixer(char *ptr, int len); |
| 308 | friend void StreamFill(int start_byte, int num_bytes); |
| 309 | friend void __cdecl StreamTimer(void *user_ptr); |
| 310 | |
| 311 | bool StartDSCleaner(void); |
| 312 | friend void __cdecl DSCleanerThread(void *user_ptr); |
| 313 | |
| 314 | bool StartStreaming(void); |
| 315 | |
| 316 | private: |
| 317 | // Total samples played since start of library |
| 318 | uint16_t m_total_sounds_played; // Used for unique ids and for stats |
| 319 | |
| 320 | // Sound library status |
| 321 | uint8_t m_f_sound_lib_init; // Flag is set if sound library is initialized -- cmphack |
| 322 | uint8_t m_f_sound_lib_status; // Paused or running -- |
| 323 | // cmphack |
| 324 | |
| 325 | // May not need some of these |
| 326 | int m_primary_frequency; // Set to the primary buffers frequency -- cmphack |
| 327 | int m_primary_alignment; |
| 328 | int m_current_frequency; // -- cmphack |
| 329 | |
| 330 | uint8_t m_primary_bit_depth; // 16 or 8 bits per sample -- cmphack |
| 331 | char m_sound_quality; |
| 332 | |
| 333 | // muted looped sound system |
| 334 | int64_t m_timer_last_frametime; |
| 335 | float m_cache_stress_timer; |
| 336 | |
| 337 | // clean interface |
| 338 | #ifdef DS3DLIB_INTERNAL_H // following data is available to internal functions |
| 339 | public: |
| 340 | #else |
| 341 | private: |
| 342 | #endif |
| 343 | bool CreateSoundBuffer(sound_buffer_info *sb, bool f_is_stereo, int size = -1, bool dynamic = false); |
| 344 | bool LoadSoundBuffer(sound_buffer_info *sb); |
| 345 | bool PlaySoundBuffer(sound_buffer_info *sb, tPSBInfo *psb); |
| 346 | bool DuplicateSoundBuffer(sound_buffer_info *sb); |
| 347 | |
| 348 | bool m_in_sound_frame; |
| 349 | bool m_pending_actions; // are there sound events pending (outside of of start/end) |
| 350 | char m_mixer_type; |
| 351 | |
| 352 | // Public functions |
| 353 | public: |
| 354 | // Initializes the object, but does not activate the sound library |
| 355 | win_llsSystem(void); |
| 356 | // Destructor -- calls the cleanup code. |
| 357 | ~win_llsSystem(void); |
| 358 | |
| 359 | // Starts the sound library |
| 360 | virtual int InitSoundLib(char mixer_type, oeApplication *sos, uint8_t max_sounds_played); |
| 361 | |
| 362 | // Cleans up after the Sound Library |
| 363 | virtual void DestroySoundLib(void); |
| 364 | |
| 365 | public: // only for win32 instances |
| 366 | int SetSoundPos(int sound_uid, int pos); |
| 367 | int GetSoundPos(int sound_uid); |
| 368 | |
| 369 | public: |
| 370 | // reserve sound slots., returns an ID to this slot. |
| 371 | |
| 372 | virtual void SetSoundCard(const char *name); |
| 373 | |
| 374 | virtual void SetListener(pos_state *cur_pos); |
| 375 | virtual int PlaySound3d(play_information *play_info, int sound_index, pos_state *cur_pos, float master_volume, |
| 376 | bool f_looped, float reverb = 0.5f); //, uint16_t frequency) |
| 377 | virtual void AdjustSound(int sound_uid, float f_volume, float f_pan, uint16_t frequency); |
| 378 | virtual void AdjustSound(int sound_uid, pos_state *cur_pos, float adjusted_volume, float reverb = 0.5f); |
| 379 | |
| 380 | // Sample cache and list of samples |
| 381 | class sound_buffer_cache m_sound_mixer; // Lists all stored samples (playing, cached, or unused) |
| 382 | |
| 383 | // Plays a 2d sound |
| 384 | virtual int PlaySound2d(play_information *play_info, int sound_index, float volume, float pan, bool f_looped = false); |
| 385 | virtual int PlayStream(play_information *play_info); |
| 386 | virtual void StopAllSounds(void); |
| 387 | |
| 388 | virtual bool SetSoundQuality(char quality); |
| 389 | virtual char GetSoundQuality(void); |
| 390 | |
| 391 | virtual bool SetSoundMixer(char mixer_type); |
| 392 | virtual char GetSoundMixer(void); |
| 393 | |
| 394 | // Checks if a sound is playing |
| 395 | virtual bool IsSoundInstancePlaying(int sound_uid); |
| 396 | virtual int IsSoundPlaying(int sound_index); |
| 397 | |
| 398 | // Locks and unlocks sounds (used when changing play_info data) |
| 399 | virtual bool LockSound(int sound_uid); |
| 400 | virtual bool UnlockSound(int sound_uid); |
| 401 | |
| 402 | // Stops 2d and 3d sounds |
| 403 | virtual void StopSound(int sound_uid, uint8_t f_immediately = SKT_STOP_IMMEDIATELY); |
| 404 | |
| 405 | // Pause all sounds/resume all sounds |
| 406 | virtual void PauseSounds(void); |
| 407 | virtual void ResumeSounds(void); |
| 408 | virtual void PauseSound(int sound_uid); |
| 409 | virtual void ResumeSound(int sound_uid); |
| 410 | |
| 411 | // Loads the actual sound data into memory |
| 412 | virtual bool CheckAndForceSoundDataAlloc(int sound_file_index); |
| 413 | |
| 414 | // Begin sound frame |
| 415 | virtual void SoundStartFrame(void); |
| 416 | |
| 417 | // End sound frame |
| 418 | virtual void SoundEndFrame(void); |
| 419 | |
| 420 | // environmental sound interface |
| 421 | // volume modifier (0-1), damping(0-1), 1 = complete, 0 = none |
| 422 | // decay 0.1 to 100 seconds, how long it takes for a sound to die. |
| 423 | virtual bool SetGlobalReverbProperties(float volume, float damping, float decay); |
| 424 | |
| 425 | // set special parameters for the 3d environment. |
| 426 | // of strcuture passed, you must set the appropriate 'flags' value for values you wish to modify |
| 427 | virtual void SetEnvironmentValues(const t3dEnvironmentValues *env); |
| 428 | |
| 429 | // get special parameters for the 3d environment. |
| 430 | // of strcuture passed, you must set the appropriate 'flags' value for values you wish to modify |
| 431 | virtual void GetEnvironmentValues(t3dEnvironmentValues *env); |
| 432 | |
| 433 | // enable special parameters for the 3d environment. |
| 434 | // of strcuture passed, you must set the appropriate 'flags' value for values you wish to modify |
| 435 | virtual void SetEnvironmentToggles(const t3dEnvironmentToggles *env); |
| 436 | |
| 437 | // get states of special parameters for the 3d environment. |
| 438 | // of strcuture passed, you must set the appropriate 'flags' value for values you wish to modify |
| 439 | virtual void GetEnvironmentToggles(t3dEnvironmentToggles *env); |
| 440 | |
| 441 | protected: |
| 442 | virtual void CheckForErrors(); // part of sound system error handler. |
| 443 | |
| 444 | ///////////////////////////////////////////////////////////////////////////////// |
| 445 | public: |
| 446 | // set auxillary 3d sound properties |
| 447 | virtual bool SoundPropertySupport() const; |
| 448 | |
| 449 | // sound obstruction from 0 to 1.0 (1.0 = fully obstructed) |
| 450 | virtual void SetSoundProperties(int sound_uid, float obstruction); |
| 451 | }; |
| 452 | |
| 453 | #endif |