home *** CD-ROM | disk | FTP | other *** search
/ Amiga ACS 1998 #6 / amigaacscoverdisc1998-061998.iso / games / descent / source / main / digi.c < prev    next >
Text File  |  1998-06-08  |  59KB  |  1,971 lines

  1. /*
  2. THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
  3. SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
  4. END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
  5. ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
  6. IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
  7. SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
  8. FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
  9. CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
  10. AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
  11. COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
  12. */
  13. /*
  14.  * $Source: f:/miner/source/main/rcs/digi.c $
  15.  * $Revision: 2.5 $
  16.  * $Author: john $
  17.  * $Date: 1996/01/05 16:51:51 $
  18.  * 
  19.  * Routines to access digital sound hardware
  20.  * 
  21.  * $Log: digi.c $
  22.  * Revision 2.5  1996/01/05  16:51:51  john
  23.  * Made the midi handler lock down to +4K.
  24.  * 
  25.  * Revision 2.4  1996/01/05  16:46:54  john
  26.  * Made code lock down midicallback +4K.
  27.  * 
  28.  * Revision 2.3  1995/05/26  16:16:03  john
  29.  * Split SATURN into define's for requiring cd, using cd, etc.
  30.  * Also started adding all the Rockwell stuff.
  31.  * 
  32.  * Revision 2.2  1995/03/30  16:36:04  mike
  33.  * text localization.
  34.  * 
  35.  * Revision 2.1  1995/03/15  11:41:23  john
  36.  * Better Saturn CD-ROM support.
  37.  * 
  38.  * Revision 2.0  1995/02/27  11:29:15  john
  39.  * New version 2.0, which has no anonymous unions, builds with
  40.  * Watcom 10.0, and doesn't require parsing BITMAPS.TBL.
  41.  * 
  42.  * Revision 1.172  1995/02/15  09:57:53  john
  43.  * Fixed bug with loading new banks while song is playing.
  44.  * 
  45.  * Revision 1.171  1995/02/14  16:36:26  john
  46.  * Fixed bug with countdown voice getting cut off.
  47.  * 
  48.  * Revision 1.170  1995/02/13  20:34:51  john
  49.  * Lintized
  50.  * 
  51.  * Revision 1.169  1995/02/13  15:18:21  john
  52.  * Added code to reset sounds.
  53.  * 
  54.  * Revision 1.168  1995/02/13  12:53:11  john
  55.  * Fixed bug with demos recording wrong volumes.
  56.  * 
  57.  * Revision 1.167  1995/02/11  15:04:29  john
  58.  * Made FM files load as if you have FM card.
  59.  * 
  60.  * Revision 1.166  1995/02/11  12:41:35  john
  61.  * Added new song method, with FM bank switching..
  62.  * 
  63.  * Revision 1.165  1995/02/10  16:24:58  john
  64.  * MAde previous change only reverse for RAP10.
  65.  * 
  66.  * Revision 1.164  1995/02/10  13:47:37  john
  67.  * Made digi init before midi to maybe fix RAP10 bug.
  68.  * 
  69.  * Revision 1.163  1995/02/08  21:05:22  john
  70.  * Added code that loads patches for every FM song.
  71.  * 
  72.  * Revision 1.162  1995/02/03  17:17:51  john
  73.  * Made digi lowmem default to off.
  74.  * 
  75.  * Revision 1.161  1995/02/03  17:08:22  john
  76.  * Changed sound stuff to allow low memory usage.
  77.  * Also, changed so that Sounds isn't an array of digi_sounds, it
  78.  * is a ubyte pointing into GameSounds, this way the digi.c code that
  79.  * locks sounds won't accidentally unlock a sound that is already playing, but
  80.  * since it's Sounds[soundno] is different, it would erroneously be unlocked.
  81.  * 
  82.  * Revision 1.160  1995/02/01  22:37:06  john
  83.  * Reduced sound travel distance to 1.25 times original.
  84.  * 
  85.  * Revision 1.159  1995/02/01  22:20:41  john
  86.  * Added digi_is_sound_playing.
  87.  * 
  88.  * Revision 1.158  1995/01/28  15:56:56  john
  89.  * Made sounds carry 1.5 times farther.
  90.  * 
  91.  * Revision 1.157  1995/01/27  17:17:09  john
  92.  * Made max sounds relate better to MAX_SOUND_OBJECTS
  93.  * 
  94.  * Revision 1.156  1995/01/25  12:18:06  john
  95.  * Fixed bug with not closing MIDI files when midi volume is 0.
  96.  * 
  97.  * Revision 1.155  1995/01/24  17:52:17  john
  98.  * MAde midi music stop playing when volume is 0.
  99.  * 
  100.  * Revision 1.154  1995/01/21  21:22:31  mike
  101.  * Correct bogus error message.
  102.  * 
  103.  * Revision 1.153  1995/01/17  14:53:38  john
  104.  * IFDEF'D out digital drums.
  105.  * 
  106.  * Revision 1.152  1995/01/11  16:26:50  john
  107.  * Restored MIDI pausing to actually setting volume to 0,.
  108.  * 
  109.  * Revision 1.151  1995/01/10  16:38:46  john
  110.  * Made MIDI songs pause, not lower volume when pausing, and
  111.  * had to fix an HMI bug by clearing the TRACKINFO array or something.
  112.  * 
  113.  * Revision 1.150  1995/01/05  19:46:27  john
  114.  * Added code to reset the midi tracks to use the ones 
  115.  * in the midi file before each song is played.
  116.  * 
  117.  * Revision 1.149  1994/12/21  15:08:59  matt
  118.  * Bumped MAX_SOUND_OBJECTS back up to match v1.00 of shareware
  119.  * 
  120.  * Revision 1.148  1994/12/20  18:22:54  john
  121.  * Added code to support non-looping songs, and put
  122.  * it in for endlevel and credits.
  123.  * 
  124.  * Revision 1.147  1994/12/19  17:58:19  john
  125.  * Changed Assert for too many sounds to gracefully exit.
  126.  * 
  127.  * Revision 1.146  1994/12/14  16:03:27  john
  128.  * Made the digi/midi deinit in reverse order for anything
  129.  * other than GUS.
  130.  * 
  131.  * Revision 1.145  1994/12/14  14:51:06  john
  132.  * Added assert.
  133.  * 
  134.  * Revision 1.144  1994/12/14  12:14:40  john
  135.  * Relplaced a bunch of (a*b)/c with fixmuldiv
  136.  * to get rid of a overflow bug mike found.
  137.  * 
  138.  * Revision 1.143  1994/12/13  17:30:16  john
  139.  * Made the timer rate be changed right after initializing it.
  140.  * 
  141.  * Revision 1.142  1994/12/13  14:08:35  john
  142.  * Made Pause key set midi volume to 0.
  143.  * Made digi_init set midi volume to proper level.
  144.  * 
  145.  * Revision 1.141  1994/12/13  12:42:31  jasen
  146.  * Fixed sound priority bugs... -john
  147.  * 
  148.  * Revision 1.140  1994/12/13  12:11:38  john
  149.  * Added debugging code.
  150.  * 
  151.  * Revision 1.139  1994/12/13  11:45:19  john
  152.  * Disabled interrupts around the midisetvolume because awe32
  153.  * hangs if you don't.
  154.  * 
  155.  * Revision 1.138  1994/12/13  11:33:45  john
  156.  * MAde so that sounds with volumes > f1_0 don't cut off.
  157.  * 
  158.  * Revision 1.137  1994/12/13  02:24:29  matt
  159.  * digi_init() now doesn't return error when no sound card
  160.  * 
  161.  * Revision 1.136  1994/12/13  00:46:27  john
  162.  * Split digi and midi volume into 2 seperate functions.
  163.  * 
  164.  * Revision 1.135  1994/12/12  22:19:20  john
  165.  * Made general midi versions of files load...
  166.  * .hmq instead of .hmp.
  167.  * 
  168.  * Revision 1.134  1994/12/12  21:32:49  john
  169.  * Made volume work better by making sure volumes are valid
  170.  * and set correctly at program startup.
  171.  * 
  172.  * Revision 1.133  1994/12/12  20:52:35  john
  173.  * Fixed bug with pause calling set mastervolume to 0.
  174.  * 
  175.  * Revision 1.132  1994/12/12  20:39:52  john
  176.  * Changed so that instead of using MasterVolume for 
  177.  * digital sounds, I just scale the volume I play the
  178.  * sound by.
  179.  * 
  180.  * Revision 1.131  1994/12/12  13:58:21  john
  181.  * MAde -nomusic work.
  182.  * Fixed GUS hang at exit by deinitializing digi before midi.
  183.  * 
  184.  * Revision 1.130  1994/12/11  23:29:39  john
  185.  * *** empty log message ***
  186.  * 
  187.  * Revision 1.129  1994/12/11  23:18:07  john
  188.  * Added -nomusic.
  189.  * Added RealFrameTime.
  190.  * Put in a pause when sound initialization error.
  191.  * Made controlcen countdown and framerate use RealFrameTime.
  192.  * 
  193.  * Revision 1.128  1994/12/11  13:35:26  john
  194.  * Let sounds play louder than F1_0.  btw, 4*f1_0 is maxed.
  195.  * 
  196.  * Revision 1.127  1994/12/11  00:43:45  john
  197.  * Fixed bug with sounds taking over channels that sound objects own.
  198.  * 
  199.  * Revision 1.126  1994/12/10  20:35:03  john
  200.  * Added digi_kill_sound_linked_to_object.
  201.  * 
  202.  * Revision 1.125  1994/12/10  15:44:20  john
  203.  * Added max_distance passing for sound objects.
  204.  * 
  205.  * Revision 1.124  1994/12/09  20:16:37  john
  206.  * Made it so that when Mike's AI code passes bogus values, the
  207.  * digi code will saturate them.
  208.  * 
  209.  * Revision 1.123  1994/12/09  17:07:52  john
  210.  * Fixed abrupt fan cutoff.
  211.  * 
  212.  * Revision 1.122  1994/12/08  17:19:14  yuan
  213.  * Cfiling stuff.
  214.  * 
  215.  * Revision 1.121  1994/12/06  19:24:08  john
  216.  * CLosed HMI timer after making our not call it.
  217.  * 
  218.  * Revision 1.120  1994/12/06  19:00:42  john
  219.  * Moved digi_reset_sounds to reset instead of digi_close.
  220.  * 
  221.  * Revision 1.119  1994/12/06  18:23:48  matt
  222.  * Don't pause midi songs, becuase it can lock up
  223.  * 
  224.  * Revision 1.118  1994/12/06  16:07:09  john
  225.  * MAde the gus pats only load if using midi==midi_gus.
  226.  * 
  227.  * Revision 1.117  1994/12/06  10:17:07  john
  228.  * MAde digi_close call reset_sounds.
  229.  * 
  230.  * Revision 1.116  1994/12/05  23:36:50  john
  231.  * Took out lock down of GETDS, because, I, john, was wrong, and it
  232.  * was me, not the HMI people, that was calling GETDS in an interrupt.
  233.  * I un-formally apologize to HMI.
  234.  * 
  235.  * Revision 1.115  1994/12/05  22:50:09  john
  236.  * Put in code to lock down GETDS, because the HMI
  237.  * people don't lock it down, even tho they use it 
  238.  * in their timerhandler interrupt.
  239.  * 
  240.  * Revision 1.114  1994/12/05  18:54:09  john
  241.  * *** empty log message ***
  242.  * 
  243.  * Revision 1.113  1994/12/05  18:52:11  john
  244.  * Locked down the TrackMap array, since SOS references it in an interupt.
  245.  * Added -noloadpats command line option.
  246.  * 
  247.  * Revision 1.112  1994/12/05  16:55:45  john
  248.  * Made so that digi_init_System is  called before 
  249.  * the timer system calls the timer handler.
  250.  * 
  251.  * Revision 1.111  1994/12/05  12:56:19  john
  252.  * Made the intruments patches only load for FM devices.
  253.  * 
  254.  * Revision 1.110  1994/12/05  12:17:16  john
  255.  * Added code that locks/unlocks digital sounds on demand.
  256.  * 
  257.  * Revision 1.109  1994/12/04  14:30:10  john
  258.  * Added hooks for music..
  259.  * 
  260.  * Revision 1.108  1994/12/02  13:58:27  matt
  261.  * Put in Int3()s when try to play a non-existant sound
  262.  * 
  263.  * Revision 1.107  1994/12/02  10:35:58  john
  264.  * Took out loadpats.c
  265.  * 
  266.  * Revision 1.106  1994/12/01  02:22:33  john
  267.  * Incorporated LOADPATS.EXE into our code.
  268.  * 
  269.  * Revision 1.105  1994/12/01  00:59:51  john
  270.  * Fixed some pot. bugs with closing.
  271.  * 
  272.  * Revision 1.104  1994/11/30  23:54:40  rob
  273.  * Tweaked some volume params due to a fix in find_connected_distance.
  274.  * 
  275.  * Revision 1.103  1994/11/30  19:36:44  john
  276.  * Made Gravis Ultrasound work again.  Made the scores blink
  277.  * at a constant rate.  Revamped the newmenu background storage,
  278.  * which hopefully fixed some bugs.  Made menus in ame not pause
  279.  * sound, except for the pause key.               ^== Game!
  280.  * 
  281.  * Revision 1.102  1994/11/30  15:14:25  rob
  282.  * Removed unused include file..
  283.  * 
  284.  * Revision 1.101  1994/11/30  15:08:45  john
  285.  * Changed some open/close stuff with sound.
  286.  * 
  287.  * Revision 1.100  1994/11/29  13:35:41  john
  288.  * Test code.
  289.  * 
  290.  * Revision 1.99  1994/11/29  03:46:32  john
  291.  * Added joystick sensitivity; Added sound channels to detail menu.  Removed -maxchannels
  292.  * command line arg.
  293.  * 
  294.  * Revision 1.98  1994/11/28  18:37:59  john
  295.  * Made sample play once work.
  296.  * 
  297.  * Revision 1.97  1994/11/28  18:35:19  john
  298.  * Made the digi_max_channels cut of an old sound instead of
  299.  * not playing a new sound.
  300.  * 
  301.  * Revision 1.96  1994/11/28  01:32:02  mike
  302.  * use quick form of matrix function.
  303.  * 
  304.  * Revision 1.95  1994/11/27  23:12:14  matt
  305.  * Made changes for new mprintf calling convention
  306.  * 
  307.  * Revision 1.94  1994/11/22  17:13:31  john
  308.  * Fixed bug with the digital drums hanging.
  309.  * 
  310.  * Revision 1.93  1994/11/21  19:09:21  john
  311.  * Made so if digi and midi both 0, same as -nosound.
  312.  * 
  313.  * Revision 1.92  1994/11/21  16:46:46  john
  314.  * Limited digital channels to 10.
  315.  * 
  316.  * Revision 1.91  1994/11/21  16:28:42  john
  317.  * Fixed bug with digi_close hanging.
  318.  * 
  319.  * Revision 1.90  1994/11/21  15:40:28  john
  320.  * Locked down the instrument data.
  321.  * 
  322.  * Revision 1.89  1994/11/21  14:43:55  john
  323.  * Fixed some bugs with setting volumes even when -nosound was used. Duh!
  324.  * 
  325.  * Revision 1.88  1994/11/21  11:55:26  john
  326.  * Fixed some sound pausing in menus bugs.
  327.  * 
  328.  * Revision 1.87  1994/11/21  11:02:21  john
  329.  * Made fan sounds be 1/2 the volume so that they
  330.  * don'
  331.  * don't carry as far.
  332.  * 
  333.  * Revision 1.86  1994/11/20  17:47:51  john
  334.  * Fixed a potential bug with sound initializing.
  335.  * 
  336.  * Revision 1.85  1994/11/20  17:29:07  john
  337.  * Fixed bug with page fault during digi_close.
  338.  * 
  339.  * Revision 1.84  1994/11/19  15:19:24  mike
  340.  * rip out unused code and data.
  341.  * 
  342.  * Revision 1.83  1994/11/16  23:38:33  mike
  343.  * new improved boss teleportation behavior.
  344.  * 
  345.  * Revision 1.82  1994/11/14  18:12:46  john
  346.  * Took out some sound objects stuff.
  347.  * 
  348.  */
  349.  
  350.  
  351. #pragma off (unreferenced)
  352. static char rcsid[] = "$Id: digi.c 2.5 1996/01/05 16:51:51 john Exp $";
  353. #pragma on (unreferenced)
  354.  
  355. #include<stdlib.h>
  356. #include<stdio.h>
  357. #include<dos.h>
  358. #include<fcntl.h> 
  359. #include<malloc.h> 
  360. #include<bios.h>
  361. #include<io.h>
  362. #include<conio.h> 
  363. #include<string.h>
  364. #include<ctype.h>
  365.  
  366. #include "fix.h"
  367. #include "object.h"
  368. #include "mono.h"
  369. #include "timer.h"
  370. #include "joy.h"
  371. #include "digi.h"
  372. #include "sounds.h"
  373. #include "args.h"
  374. #include "key.h"
  375. #include "newdemo.h"
  376. #include "game.h"
  377. #include "dpmi.h"
  378. #include "error.h"
  379. #include "wall.h"
  380. #include "cfile.h"
  381. #include "piggy.h"
  382. #include "text.h"
  383.  
  384.  
  385. #pragma pack (4);                        // Use 32-bit packing!
  386. #pragma off (check_stack);            // No stack checking!
  387. //*************************************************
  388. //#include "sos.h"
  389. //#include "sosm.h"
  390. //The above two includes are part of a commercial 
  391. //sound library, so they cannot be included in a public 
  392. //release of the source code. -KRB
  393. #include "no_sos.h" //Added by KRB
  394. //*************************************************
  395. #include "kconfig.h"
  396. //#include "soscomp.h"
  397.  
  398. #define DIGI_PAUSE_BROKEN 1        //if this is defined, don't pause MIDI songs
  399.  
  400. #define _DIGI_SAMPLE_FLAGS (_VOLUME | _PANNING )
  401.  
  402. #define _DIGI_MAX_VOLUME (16384)    //16384
  403.  
  404. // patch files
  405. #define  _MELODIC_PATCH       "melodic.bnk"
  406. #define  _DRUM_PATCH          "drum.bnk"
  407. #define  _DIGDRUM_PATCH       "drum32.dig"
  408.  
  409.  
  410. static int    Digi_initialized         = 0;
  411. static int    digi_atexit_called    = 0;            // Set to 1 if atexit(digi_close) was called
  412.  
  413. int digi_driver_board                = 0;
  414. int digi_driver_port                    = 0;
  415. int digi_driver_irq                    = 0;
  416. int digi_driver_dma                    = 0;
  417. int digi_midi_type                    = 0;            // Midi driver type
  418. int digi_midi_port                    = 0;            // Midi driver port
  419. static int digi_max_channels        = 8;
  420. static int digi_driver_rate        = 11025;            // rate to use driver at
  421. static int digi_dma_buffersize    = 4096;            // size of the dma buffer to use (4k)
  422. int digi_timer_rate                    = 9943;            // rate for the timer to go off to handle the driver system (120 Hz)
  423. int digi_lomem                         = 0;
  424. static int digi_volume                = _DIGI_MAX_VOLUME;        // Max volume
  425. static int midi_volume                = 128/2;                        // Max volume
  426. static int midi_system_initialized        = 0;
  427. static int digi_system_initialized        = 0;
  428. static int timer_system_initialized        = 0;
  429. static int digi_sound_locks[MAX_SOUNDS];
  430. char digi_last_midi_song[16] = "";
  431. char digi_last_melodic_bank[16] = "";
  432. char digi_last_drum_bank[16] = "";
  433. LPSTR digi_driver_path = NULL;//Was _NULL -KRB
  434. static WORD                        hSOSDigiDriver = 0xffff;            // handle for the SOS driver being used 
  435. static WORD                     hSOSMidiDriver = 0xffff;            // handle for the loaded MIDI driver
  436. static WORD                        hTimerEventHandle = 0xffff;        // handle for the timer function
  437.  
  438. static void * lpInstruments = NULL;        // pointer to the instrument file
  439. static int InstrumentSize = 0;
  440. static void * lpDrums = NULL;                // pointer to the drum file
  441. static int DrumSize = 0;
  442. // track mapping structure, this is used to map which track goes
  443. // out which device. this can also be mapped by the name of the 
  444. // midi track. to map by the name of the midi track use the define
  445. // _MIDI_MAP_TRACK for each of the tracks 
  446. /*
  447. static _SOS_MIDI_TRACK_DEVICE   sSOSTrackMap = { 
  448.    _MIDI_MAP_TRACK, _MIDI_MAP_TRACK, _MIDI_MAP_TRACK, _MIDI_MAP_TRACK, 
  449.    _MIDI_MAP_TRACK, _MIDI_MAP_TRACK, _MIDI_MAP_TRACK, _MIDI_MAP_TRACK, 
  450.    _MIDI_MAP_TRACK, _MIDI_MAP_TRACK, _MIDI_MAP_TRACK, _MIDI_MAP_TRACK, 
  451.    _MIDI_MAP_TRACK, _MIDI_MAP_TRACK, _MIDI_MAP_TRACK, _MIDI_MAP_TRACK,
  452.    _MIDI_MAP_TRACK, _MIDI_MAP_TRACK, _MIDI_MAP_TRACK, _MIDI_MAP_TRACK, 
  453.    _MIDI_MAP_TRACK, _MIDI_MAP_TRACK, _MIDI_MAP_TRACK, _MIDI_MAP_TRACK, 
  454.    _MIDI_MAP_TRACK, _MIDI_MAP_TRACK, _MIDI_MAP_TRACK, _MIDI_MAP_TRACK, 
  455.    _MIDI_MAP_TRACK, _MIDI_MAP_TRACK, _MIDI_MAP_TRACK, _MIDI_MAP_TRACK 
  456. };
  457. */
  458. // handle for the initialized MIDI song
  459. WORD     wSongHandle = 0xffff;         
  460. ubyte        *SongData=NULL;
  461. int        SongSize;
  462.  
  463.  
  464. #define SOF_USED                1         // Set if this sample is used
  465. #define SOF_PLAYING            2        // Set if this sample is playing on a channel
  466. #define SOF_LINK_TO_OBJ        4        // Sound is linked to a moving object. If object dies, then finishes play and quits.
  467. #define SOF_LINK_TO_POS        8        // Sound is linked to segment, pos
  468. #define SOF_PLAY_FOREVER    16        // Play forever (or until level is stopped), otherwise plays once
  469.  
  470. typedef struct sound_object {
  471.     short            signature;        // A unique signature to this sound
  472.     ubyte            flags;            // Used to tell if this slot is used and/or currently playing, and how long.
  473.     fix            max_volume;        // Max volume that this sound is playing at
  474.     fix            max_distance;    // The max distance that this sound can be heard at...
  475.     int            volume;            // Volume that this sound is playing at
  476.     int            pan;                // Pan value that this sound is playing at
  477.     WORD            handle;            // What handle this sound is playing on.  Valid only if SOF_PLAYING is set.
  478.     short            soundnum;        // The sound number that is playing
  479.     union {    
  480.         struct {
  481.             short            segnum;                // Used if SOF_LINK_TO_POS field is used
  482.             short            sidenum;                
  483.             vms_vector    position;
  484.         };
  485.         struct {
  486.             short            objnum;                // Used if SOF_LINK_TO_OBJ field is used
  487.             short            objsignature;
  488.         };
  489.     };
  490. } sound_object;
  491.  
  492. #define MAX_SOUND_OBJECTS 16
  493. sound_object SoundObjects[MAX_SOUND_OBJECTS];
  494. short next_signature=0;
  495.  
  496. int digi_sounds_initialized=0;
  497. //this block commented out by KRB
  498.  
  499.  
  500. void * testLoadFile( char * szFileName, int * length );
  501.  
  502. VOID _far sosMIDICallback( WORD PassedSongHandle );
  503. VOID sosEndMIDICallback();
  504.  
  505. //NOT_MIDI_CHECKushort MIDI_CRC;
  506. //NOT_MIDI_CHECKubyte MIDI_SAVED_DATA[100*1024];
  507. /*
  508.  
  509.  
  510. */
  511.  
  512.  
  513. int digi_xlat_sound(int soundno)
  514. {
  515. /*
  516.     if ( soundno < 0 ) return -1;
  517.  
  518.     if ( digi_lomem )    {
  519.         soundno = AltSounds[soundno];
  520.         if ( soundno == 255 ) return -1;
  521.     }
  522.     return Sounds[soundno];
  523. */
  524.     return 0;//KRB Comment out...
  525.  
  526. }
  527.  
  528.  
  529. void digi_close_midi()
  530. {
  531. /*
  532.     if (digi_midi_type>0)    {
  533.         if (wSongHandle < 0xffff)    {
  534.            // stop the last MIDI song from playing
  535.             sosMIDIStopSong( wSongHandle );
  536.            // uninitialize the last MIDI song
  537.             sosMIDIUnInitSong( wSongHandle );
  538.             wSongHandle = 0xffff;
  539.         }
  540.         if (SongData)     {
  541.             if (!dpmi_unlock_region(SongData, SongSize))    {
  542.                 mprintf( (1, "Error unlocking midi file" ));
  543.             }
  544.             free(SongData);
  545.             SongData = NULL;
  546.         }
  547.        // reset the midi driver and
  548.        // uninitialize the midi driver and tell it to free the memory allocated
  549.        // for the driver
  550.         if ( hSOSMidiDriver < 0xffff )    {
  551.            sosMIDIResetDriver( hSOSMidiDriver );
  552.            sosMIDIUnInitDriver( hSOSMidiDriver, _TRUE  );
  553.             hSOSMidiDriver = 0xffff;
  554.         }
  555.  
  556.         if ( midi_system_initialized )    {
  557.            // uninitialize the MIDI system
  558.            sosMIDIUnInitSystem(); 
  559.             midi_system_initialized = 0;
  560.         }
  561.     }
  562. */
  563. }
  564.  
  565. void digi_close_digi()
  566. {
  567. /*
  568.     if (digi_driver_board>0)    {
  569.         if ( hTimerEventHandle < 0xffff )    {
  570.              sosTIMERRemoveEvent( hTimerEventHandle );
  571.             hTimerEventHandle = 0xffff;
  572.         }
  573.         if ( hSOSDigiDriver < 0xffff )
  574.            sosDIGIUnInitDriver( hSOSDigiDriver, _TRUE, _TRUE );
  575.     }
  576. */
  577. }
  578.  
  579.  
  580. void digi_close()
  581. {
  582. /*
  583.     if (!Digi_initialized) return;
  584.     Digi_initialized = 0;
  585.  
  586.     if ( timer_system_initialized )    {
  587.         // Remove timer...
  588.         timer_set_function( NULL );
  589.     }
  590.  
  591.     if ( digi_driver_board == _GUS_8_ST )    {
  592.         digi_close_digi();
  593.         digi_close_midi();
  594.     } else {
  595.         digi_close_midi();
  596.         digi_close_digi();
  597.     }
  598.  
  599.     if ( digi_system_initialized )    {
  600.        // uninitialize the DIGI system
  601.         sosDIGIUnInitSystem();
  602.         digi_system_initialized = 0;
  603.     }
  604.  
  605.     if ( timer_system_initialized )    {
  606.         // Remove timer...
  607.         timer_system_initialized = 0;
  608.         sosTIMERUnInitSystem( 0 );
  609.     }
  610. */
  611. }
  612.  
  613. extern int loadpats( char * filename );
  614.  
  615. int digi_load_fm_banks( char * melodic_file, char * drum_file )
  616. {    
  617. /*
  618.    WORD     wError;                 // error code returned from functions
  619.  
  620.     // set the instrument file for the MIDI driver to use, since we are using
  621.     // the FM driver two instrument files are needed, the first is for 
  622.     // all melodic instruments and the second is for events on channel 10
  623.     // which is the drum track.
  624.     // set the drum instruments
  625.     if (lpInstruments)    {
  626.         dpmi_unlock_region(lpInstruments, InstrumentSize);
  627.         free( lpInstruments );
  628.     }
  629.             
  630.     lpInstruments = testLoadFile( melodic_file, &InstrumentSize );
  631.     if ( !lpInstruments )    {
  632.         printf( "%s '%s'\n", TXT_SOUND_ERROR_OPEN, melodic_file );
  633.         return 0;
  634.     }
  635.  
  636.     if (!dpmi_lock_region(lpInstruments, InstrumentSize))    {
  637.         printf( "%s '%s', ptr=0x%8x, len=%d bytes\n", TXT_SOUND_ERROR_LOCK, melodic_file, lpInstruments, InstrumentSize );
  638.         return 0;
  639.     }
  640.     
  641.     if( ( wError =  sosMIDISetInsData( hSOSMidiDriver, lpInstruments, 0x01  ) ) )     {
  642.         printf( "%s %s \n", TXT_SOUND_ERROR_HMI, sosGetErrorString( wError ) );
  643.         return 0;
  644.     }
  645.     
  646.     if (lpDrums)    {
  647.         dpmi_unlock_region(lpDrums, DrumSize);
  648.         free( lpDrums );
  649.     }
  650.             
  651.     lpDrums = testLoadFile( drum_file, &DrumSize );
  652.     if ( !lpDrums )    {
  653.         printf( "%s '%s'\n", TXT_SOUND_ERROR_OPEN, drum_file );
  654.         return 0;
  655.     }
  656.  
  657.     if (!dpmi_lock_region(lpDrums, DrumSize))    {
  658.         printf( "%s  '%s', ptr=0x%8x, len=%d bytes\n", TXT_SOUND_ERROR_LOCK_DRUMS, drum_file, lpDrums, DrumSize );
  659.         return 0;
  660.     }
  661.     
  662.      // set the drum instruments
  663.     if( ( wError =  sosMIDISetInsData( hSOSMidiDriver, lpDrums, 0x01  ) ) )    {
  664.         printf( "%s %s\n", TXT_SOUND_ERROR_HMI, sosGetErrorString( wError ) );
  665.         return 0;
  666.     }
  667.     
  668.     return 1;
  669. */
  670.     return 0;//KRB Comment out...
  671.  
  672. }
  673.  
  674. int digi_init_midi()
  675. {
  676. /*
  677.    WORD     wError;                 // error code returned from functions
  678.     _SOS_MIDI_INIT_DRIVER            sSOSMIDIInitDriver;    // structure for the MIDI driver initialization function 
  679.     _SOS_MIDI_HARDWARE                sSOSMIDIHardware;     // structure for the MIDI driver hardware
  680.  
  681.     if ( digi_midi_type > 0 )    {
  682.         // Lock the TrackMap array, since HMI references it in an interrupt.
  683.         if (!dpmi_lock_region ( &sSOSTrackMap, sizeof(sSOSTrackMap)) )    {
  684.             printf( "%s\n", TXT_SOUND_ERROR_MIDI);
  685.             digi_close();
  686.             return 1;
  687.         }
  688.  
  689. //        if (!dpmi_lock_region ((void near *)sosMIDICallback, (char *)sosEndMIDICallback - (char near *)sosMIDICallback))    {
  690.         if (!dpmi_lock_region ((void near *)sosMIDICallback, 4*1024) )    {
  691.             printf( "%s\n", TXT_SOUND_ERROR_MIDI_CALLBACK);
  692.             digi_close();
  693.             return 1;
  694.         }
  695.  
  696.        // initialize the MIDI system
  697.        sosMIDIInitSystem( digi_driver_path, _SOS_DEBUG_NORMAL );
  698.         midi_system_initialized = 1;
  699.  
  700.        // set the pointer to the driver memory for the MIDI driver to 
  701.        // _NULL. this will tell the load driver routine to allocate new
  702.        // memory for the MIDI driver
  703.        sSOSMIDIInitDriver.lpDriverMemory  = _NULL;
  704.         sSOSMIDIInitDriver.sDIGIInitInfo = _NULL;
  705.     
  706.         sSOSMIDIHardware.wPort = digi_midi_port;
  707.     
  708.        // load and initialize the MIDI driver 
  709.        if( ( wError = sosMIDIInitDriver( digi_midi_type, &sSOSMIDIHardware, &sSOSMIDIInitDriver, &hSOSMidiDriver ) ) )    {
  710.           printf( "SOUND: (HMI) '%s'\n", sosGetErrorString( wError ) );
  711.             digi_close();
  712.           return 1;
  713.        }
  714.         sosMIDIEnableChannelStealing( _FALSE );
  715.     }
  716.     return 0;
  717. */
  718.     return 0;//KRB Comment out...
  719.  
  720. }
  721.  
  722. int digi_init_digi()
  723. {
  724. /*
  725.    WORD     wError;                 // error code returned from functions
  726.     _SOS_INIT_DRIVER                    sSOSInitDriver;            // structure used to initialize the driver
  727.     _SOS_HARDWARE                        sSOSHardwareSettings;    // device settings
  728.  
  729.     if ( digi_driver_board > 0 )    {
  730.         memset( &sSOSInitDriver, 0, sizeof(_SOS_INIT_DRIVER) );
  731.         sSOSInitDriver.wBufferSize            = digi_dma_buffersize;
  732.         sSOSInitDriver.lpBuffer                = _NULL;
  733.         sSOSInitDriver.wAllocateBuffer    = _TRUE;
  734.         //sSOSInitDriver.wParam                 = 19;
  735.         sSOSInitDriver.wSampleRate            = digi_driver_rate;
  736.         sSOSInitDriver.lpDriverMemory        = _NULL;
  737.         sSOSInitDriver.lpTimerMemory        = _NULL;
  738.     
  739.         sSOSHardwareSettings.wPort = digi_driver_port;
  740.         sSOSHardwareSettings.wIRQ = digi_driver_irq;
  741.         sSOSHardwareSettings.wDMA = digi_driver_dma;
  742.  
  743.        if( ( wError = sosDIGIInitDriver(  digi_driver_board, &sSOSHardwareSettings, &sSOSInitDriver, &hSOSDigiDriver ) ) )
  744.        {
  745.           // display the error types
  746.           printf( "SOUND: (HMI) '%s'\n", sosGetErrorString( wError ) );
  747.             digi_close();
  748.           return 1;
  749.        }
  750.  
  751.        if( ( wError = sosTIMERRegisterEvent( 1193180/digi_timer_rate, sSOSInitDriver.lpFillHandler, &hTimerEventHandle ) ) )    {
  752.           // display the error types
  753.           printf( "SOUND: (HMI) '%s'\n", sosGetErrorString( wError ) );
  754.             digi_close();
  755.           return 1;
  756.         }
  757.     }
  758.     */
  759.     return 0;
  760. }
  761.  
  762. int digi_init()
  763. {
  764.     int i;
  765. /*
  766. #ifdef USE_CD
  767.     { 
  768.         FILE * fp;
  769.         fp = fopen( "hmimdrv.386", "rb" );    
  770.         if ( fp )
  771.             fclose(fp);
  772.         else
  773.             digi_driver_path = destsat_cdpath;
  774.     }
  775. #endif
  776.  
  777.     if ( FindArg( "-nomusic" )) 
  778.         digi_midi_type = 0;
  779.  
  780.     if ( (digi_midi_type<1) && (digi_driver_board<1) )    
  781.         return 0;
  782.  
  783.     if ( !FindArg( "-noloadpats" ) )    {
  784.         if ( digi_midi_type == _MIDI_GUS )    {
  785.             char fname[128];
  786.             strcpy( fname, "DESCENTG.INI" );
  787. #ifdef USE_CD
  788.             {
  789.                 FILE * fp = fopen( fname, "rb" );
  790.                 if ( fp )
  791.                     fclose(fp);
  792.                 else {
  793.                     strcpy( fname, destsat_cdpath );    
  794.                     strcat( fname, "DESCENTG.INI" );
  795.                 }
  796.             }
  797. #endif
  798.             loadpats(fname);
  799.         }
  800.     }
  801.     Digi_initialized = 1;
  802.  
  803.     // initialize the DIGI system and lock down all SOS memory
  804.     sosDIGIInitSystem( digi_driver_path, _SOS_DEBUG_NORMAL );
  805.     digi_system_initialized = 1;
  806.  
  807.     // Initialize timer, where we will call it from out own interrupt
  808.     // routine, and we will call DOS 18.2Hz ourselves.
  809.     sosTIMERInitSystem( 0, _SOS_DEBUG_NO_TIMER );
  810.     timer_set_function( sosTIMERHandler );        // New way
  811.     timer_system_initialized = 1;
  812.     
  813.     if ( digi_driver_board == _RAP10_8_MONO )    {
  814.         if (digi_init_digi()) return 1;
  815.         if (digi_init_midi()) return 1;
  816.     } else {
  817.         if (digi_init_midi()) return 1;
  818.         if (digi_init_digi()) return 1;
  819.     }
  820.  
  821.     if (!digi_atexit_called)    {
  822.         atexit( digi_close );
  823.         digi_atexit_called = 1;
  824.     }
  825.  
  826.     digi_init_sounds();
  827.     digi_set_midi_volume( midi_volume );
  828.  
  829.     for (i=0; i<MAX_SOUNDS; i++ )
  830.         digi_sound_locks[i] = 0;
  831.     digi_reset_digi_sounds();
  832.  
  833.     return 0;
  834. */
  835.     return 0;//KRB Comment out...
  836.  
  837. }
  838.  
  839. // Toggles sound system on/off
  840. void digi_reset()    
  841. {
  842. /*
  843.     if ( Digi_initialized )    {
  844.         digi_reset_digi_sounds();
  845.         digi_close();
  846.         mprintf( (0, "Sound system DISABLED.\n" ));
  847.     } else {
  848.         digi_init();
  849.         mprintf( (0, "Sound system ENABLED.\n" ));
  850.     }
  851. */
  852. }
  853.  
  854. int digi_total_locks = 0;
  855.  
  856. ubyte * digi_lock_sound_data( int soundnum )
  857. {
  858. /*
  859.     int i;
  860.  
  861.     if ( !Digi_initialized ) return NULL;
  862.     if ( digi_driver_board <= 0 )    return NULL;
  863.  
  864.     if ( digi_sound_locks[soundnum] == 0 )    {
  865.         digi_total_locks++;
  866.         //mprintf(( 0, "Total sound locks = %d\n", digi_total_locks ));
  867.         i = dpmi_lock_region( GameSounds[soundnum].data, GameSounds[soundnum].length );
  868.         if ( !i ) Error( "Error locking sound %d\n", soundnum );
  869.     }
  870.     digi_sound_locks[soundnum]++;
  871.     return GameSounds[soundnum].data;
  872. */
  873.     digi_total_locks=digi_total_locks;//blah -KRB
  874.     return 0;//KRB Comment out...
  875.  
  876. }
  877.  
  878. void digi_unlock_sound_data( int soundnum )
  879. {
  880. /*
  881.     int i;
  882.  
  883.     if ( !Digi_initialized ) return;
  884.     if ( digi_driver_board <= 0 )    return;
  885.  
  886.     Assert( digi_sound_locks[soundnum] > 0 );
  887.  
  888.     if ( digi_sound_locks[soundnum] == 1 )    {
  889.         digi_total_locks--;
  890.         //mprintf(( 0, "Total sound locks = %d\n", digi_total_locks ));
  891.         i = dpmi_unlock_region( GameSounds[soundnum].data, GameSounds[soundnum].length );
  892.         if ( !i ) Error( "Error unlocking sound %d\n", soundnum );
  893.     }
  894.     digi_sound_locks[soundnum]--;
  895. */
  896. }
  897. /*
  898. static int next_handle = 0;
  899. static WORD SampleHandles[32] = { 0xffff, 0xffff, 0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff };
  900. static int SoundNums[32] = { -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 };
  901. static uint SoundVolumes[32] = { -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 };
  902. //This block commented out by KRB
  903. */
  904. void digi_reset_digi_sounds()
  905. {
  906. /*
  907.     int i;
  908.  
  909.     if ( !Digi_initialized ) return;
  910.     if ( digi_driver_board <= 0 )    return;
  911.  
  912.     for (i=0; i<32; i++ )    {
  913.         if (SampleHandles[i] < _MAX_VOICES )    {
  914.             if ( !sosDIGISampleDone( hSOSDigiDriver, SampleHandles[i])  )        {
  915.                 //mprintf(( 0, "Stopping sound %d.\n", next_handle ));
  916.                 sosDIGIStopSample(hSOSDigiDriver, SampleHandles[i]);
  917.             }
  918.             SampleHandles[i] = 0xffff;
  919.         }
  920.         if ( SoundNums[i] != -1 )    {
  921.             digi_unlock_sound_data(SoundNums[i]);
  922.             SoundNums[i] = -1;
  923.         }
  924.     }
  925.     for (i=0; i<MAX_SOUNDS; i++ )    {
  926.         Assert( digi_sound_locks[i] == 0 );
  927.     }
  928. */
  929. }
  930.  
  931. void reset_sounds_on_channel( int channel )
  932. {
  933. /*
  934.     int i;
  935.  
  936.     if ( !Digi_initialized ) return;
  937.     if ( digi_driver_board <= 0 )    return;
  938.  
  939.     for (i=0; i<digi_max_channels; i++ )    {
  940.         if (SampleHandles[i] == channel )    {
  941.             SampleHandles[i] = 0xffff;
  942.             if ( SoundNums[i] != -1 )    {
  943.                 digi_unlock_sound_data(SoundNums[i]);
  944.                 SoundNums[i] = -1;
  945.             }
  946.         }
  947.     }
  948. */
  949. }
  950.  
  951.  
  952. void digi_set_max_channels(int n)
  953. {
  954. /*
  955.     digi_max_channels    = n;
  956.  
  957.     if ( digi_max_channels < 1 ) 
  958.         digi_max_channels = 1;
  959.     if ( digi_max_channels > (32-MAX_SOUND_OBJECTS) ) 
  960.         digi_max_channels = (32-MAX_SOUND_OBJECTS);
  961.  
  962.     if ( !Digi_initialized ) return;
  963.     if ( digi_driver_board <= 0 )    return;
  964.  
  965.     digi_reset_digi_sounds();
  966. */
  967. }
  968.  
  969. int digi_get_max_channels()
  970. {
  971.     //return digi_max_channels;
  972.     return 0;//KRB Comment out...
  973.  
  974. }
  975.  
  976.  
  977. //WORD digi_start_sound(_SOS_START_SAMPLE * sampledata, short soundnum ) //This is the original modified below by KRB
  978. WORD digi_start_sound(void * sampledata, short soundnum )
  979.  
  980. {
  981. /*
  982.     int i, ntries;
  983.     WORD sHandle;
  984.  
  985.     if ( !Digi_initialized ) return 0xFFFF;
  986.     if ( digi_driver_board <= 0 )    return 0xFFFF;
  987.  
  988.     soundnum  = soundnum;
  989.     ntries = 0;
  990.  
  991. TryNextChannel:
  992.     if ( (SampleHandles[next_handle] < _MAX_VOICES) && (!sosDIGISampleDone( hSOSDigiDriver, SampleHandles[next_handle]))  )        {
  993.         if ( (SoundVolumes[next_handle] > digi_volume) && (ntries<digi_max_channels) )    {
  994.             //mprintf(( 0, "Not stopping loud sound %d.\n", next_handle ));
  995.             next_handle++;
  996.             if ( next_handle >= digi_max_channels )
  997.                 next_handle = 0;
  998.             ntries++;
  999.             goto TryNextChannel;
  1000.         }
  1001.         //mprintf(( 0, "[SS:%d]", next_handle ));
  1002.         sosDIGIStopSample(hSOSDigiDriver, SampleHandles[next_handle]);
  1003.         SampleHandles[next_handle] = 0xffff;
  1004.     }
  1005.  
  1006.     if ( SoundNums[next_handle] != -1 )    {
  1007.         digi_unlock_sound_data(SoundNums[next_handle]);
  1008.         SoundNums[next_handle] = -1;
  1009.     }
  1010.  
  1011. //    sampledata->lpSamplePtr = sound_expand_sound( soundnum,  &i );
  1012. //    sampledata->wSampleID = i;
  1013.     
  1014.     digi_lock_sound_data(soundnum);
  1015.     sHandle = sosDIGIStartSample( hSOSDigiDriver, sampledata );
  1016.     if ( sHandle == _ERR_NO_SLOTS )    {
  1017.         mprintf(( 1, "NOT ENOUGH SOUND SLOTS!!!\n" ));
  1018.         digi_unlock_sound_data(soundnum);
  1019.         return sHandle;
  1020.     }
  1021.     //mprintf(( 0, "Starting sound on channel %d\n", sHandle ));
  1022.  
  1023. #ifndef NDEBUG
  1024.     verify_sound_channel_free( sHandle );
  1025.     Assert( sHandle != _ERR_NO_SLOTS );
  1026. #endif
  1027.  
  1028.     for (i=0; i<digi_max_channels; i++ )    {
  1029.         if (SampleHandles[i] == sHandle )    {
  1030.             SampleHandles[i] = 0xffff;
  1031.             if ( SoundNums[i] != -1 )    {
  1032.                 digi_unlock_sound_data(SoundNums[i]);
  1033.                 SoundNums[i] = -1;
  1034.             }
  1035.         }
  1036.     }
  1037.     SampleHandles[next_handle] = sHandle;
  1038.     SoundNums[next_handle] = soundnum;
  1039.     SoundVolumes[next_handle]  = sampledata->wVolume;
  1040. //    mprintf(( 0, "Starting sample %d at volume %d\n", next_handle, sampledata->wVolume  ));
  1041. //    if (SoundVolumes[next_handle] > digi_volume)
  1042. //        mprintf(( 0, "Starting loud sample %d\n", next_handle ));
  1043.     next_handle++;
  1044.     if ( next_handle >= digi_max_channels )
  1045.         next_handle = 0;
  1046.     //mprintf(( 0, "%d samples playing\n", sosDIGISamplesPlaying(hSOSDigiDriver) ));
  1047.     return sHandle;
  1048. */
  1049.     return 0;//KRB Comment out...
  1050.  
  1051. }
  1052.  
  1053. int digi_is_sound_playing(int soundno)
  1054. {
  1055. /*
  1056.     WORD SampleHandle;
  1057.     soundno = digi_xlat_sound(soundno);
  1058.  
  1059.     if (!Digi_initialized) return 0;
  1060.     if (digi_driver_board<1) return 0;
  1061.  
  1062.     if (soundno < 0 ) return 0;
  1063.     if (GameSounds[soundno].data==NULL) {
  1064.         Int3();
  1065.         return 0;
  1066.     }
  1067.  
  1068.     SampleHandle = sosDIGIGetSampleHandle( hSOSDigiDriver, soundno );
  1069.     if ( (SampleHandle < _MAX_VOICES) && (!sosDIGISampleDone( hSOSDigiDriver, SampleHandle)) )
  1070.         return 1;
  1071.     return 0;
  1072.     */
  1073.     return 0;//KRB Comment out...
  1074. }
  1075.  
  1076. void digi_play_sample_once( int soundno, fix max_volume )    
  1077. {
  1078. /*
  1079.     WORD SampleHandle;
  1080.     digi_sound *snd;
  1081.     _SOS_START_SAMPLE sSOSSampleData;
  1082.  
  1083. #ifdef NEWDEMO
  1084.     if ( Newdemo_state == ND_STATE_RECORDING )
  1085.         newdemo_record_sound( soundno );
  1086. #endif
  1087.     soundno = digi_xlat_sound(soundno);
  1088.  
  1089.     if (!Digi_initialized) return;
  1090.     if (digi_driver_board<1) return;
  1091.     if (soundno < 0 ) return;
  1092.     snd = &GameSounds[soundno];
  1093.     if (snd==NULL) {
  1094.         Int3();
  1095.         return;
  1096.     }
  1097.         
  1098.     SampleHandle = sosDIGIGetSampleHandle( hSOSDigiDriver, soundno );
  1099.     if ( (SampleHandle < _MAX_VOICES) && (!sosDIGISampleDone( hSOSDigiDriver, SampleHandle)) )    {
  1100.         sosDIGIStopSample(hSOSDigiDriver, SampleHandle);
  1101.         //while ( !sosDIGISampleDone( hSOSDigiDriver, SampleHandle));
  1102.         //return;
  1103.     }
  1104.  
  1105.     memset( &sSOSSampleData, 0, sizeof(_SOS_START_SAMPLE));
  1106.     sSOSSampleData.wLoopCount                 = 0x00;
  1107.     sSOSSampleData.wChannel                 = _CENTER_CHANNEL;
  1108.     sSOSSampleData.wVolume                    = fixmuldiv(max_volume,digi_volume,F1_0);
  1109.     sSOSSampleData.wSampleID                = soundno;
  1110.    sSOSSampleData.dwSampleSize             = ( LONG )snd->length;
  1111.     sSOSSampleData.lpSamplePtr                = snd->data;
  1112.     sSOSSampleData.lpCallback                = _NULL;        //sosDIGISampleCallback;
  1113.     sSOSSampleData.wSamplePanLocation    = 0xffff/2;
  1114.     sSOSSampleData.wSamplePanSpeed         = 0;
  1115.     sSOSSampleData.wSampleFlags            = _DIGI_SAMPLE_FLAGS;
  1116.  
  1117. //    if ( sosDIGISamplesPlaying(hSOSDigiDriver) >= digi_max_channels )
  1118. //        return;
  1119.     
  1120.    // start the sample playing
  1121.     digi_start_sound( &sSOSSampleData, soundno );
  1122.     */
  1123. }
  1124.  
  1125.  
  1126. void digi_play_sample( int soundno, fix max_volume )
  1127. {
  1128. /*
  1129.     digi_sound *snd;
  1130.     _SOS_START_SAMPLE sSOSSampleData;
  1131.  
  1132. #ifdef NEWDEMO
  1133.     if ( Newdemo_state == ND_STATE_RECORDING )
  1134.         newdemo_record_sound( soundno );
  1135. #endif
  1136.     soundno = digi_xlat_sound(soundno);
  1137.  
  1138.     if (!Digi_initialized) return;
  1139.     if (digi_driver_board<1) return;
  1140.     if (soundno < 0 ) return;
  1141.     snd = &GameSounds[soundno];
  1142.     if (snd==NULL) {
  1143.         Int3();
  1144.         return;
  1145.     }
  1146.  
  1147.     //mprintf( (0, "Playing sample of length %d\n", snd->length ));
  1148.     memset( &sSOSSampleData, 0, sizeof(_SOS_START_SAMPLE));
  1149.     sSOSSampleData.wLoopCount                 = 0x00;
  1150.     sSOSSampleData.wChannel                 = _CENTER_CHANNEL;
  1151.     sSOSSampleData.wVolume                    = fixmuldiv(max_volume,digi_volume,F1_0);
  1152.     sSOSSampleData.wSampleID                = soundno;
  1153.    sSOSSampleData.dwSampleSize             = ( LONG )snd->length;
  1154.     sSOSSampleData.lpSamplePtr                = snd->data;
  1155.     sSOSSampleData.lpCallback                = _NULL;        //sosDIGISampleCallback;
  1156.     sSOSSampleData.wSamplePanLocation    = 0xffff/2;
  1157.     sSOSSampleData.wSamplePanSpeed         = 0;
  1158.     sSOSSampleData.wSampleFlags            = _DIGI_SAMPLE_FLAGS;
  1159.  
  1160. //    if ( sosDIGISamplesPlaying(hSOSDigiDriver) >= digi_max_channels )
  1161. //        return;
  1162.  
  1163.    // start the sample playing
  1164.     digi_start_sound( &sSOSSampleData, soundno );
  1165. */
  1166. }
  1167.  
  1168.  
  1169. void digi_play_sample_3d( int soundno, int angle, int volume, int no_dups )    
  1170. {
  1171. /*
  1172.     _SOS_START_SAMPLE sSOSSampleData;
  1173.     digi_sound *snd;
  1174.  
  1175.     no_dups = 1;
  1176.  
  1177. #ifdef NEWDEMO
  1178.     if ( Newdemo_state == ND_STATE_RECORDING )        {
  1179.         if ( no_dups )
  1180.             newdemo_record_sound_3d_once( soundno, angle, volume );
  1181.         else
  1182.             newdemo_record_sound_3d( soundno, angle, volume );
  1183.     }
  1184. #endif
  1185.     soundno = digi_xlat_sound(soundno);
  1186.  
  1187.     if (!Digi_initialized) return;
  1188.     if (digi_driver_board<1) return;
  1189.     if (soundno < 0 ) return;
  1190.     snd = &GameSounds[soundno];
  1191.     if (snd==NULL) {
  1192.         Int3();
  1193.         return;
  1194.     }
  1195.  
  1196.     if (volume < 10 ) return;
  1197.  
  1198.     memset( &sSOSSampleData, 0, sizeof(_SOS_START_SAMPLE));
  1199.     sSOSSampleData.wLoopCount         = 0x00;
  1200.     sSOSSampleData.wChannel         = _CENTER_CHANNEL;
  1201.     sSOSSampleData.wSampleID        = soundno;
  1202.     sSOSSampleData.dwSampleSize     = ( LONG )snd->length;
  1203.     sSOSSampleData.dwSampleByteLength     = ( LONG )snd->length;
  1204.     sSOSSampleData.lpSamplePtr        = snd->data;
  1205.     sSOSSampleData.lpCallback        = _NULL;        //sosDIGISampleCallback;
  1206.     sSOSSampleData.wSamplePanLocation    = angle;            // 0 - 0xFFFF
  1207.     sSOSSampleData.wSamplePanSpeed         = 0;
  1208.     sSOSSampleData.wVolume                    = fixmuldiv(volume,digi_volume,F1_0);;                    // 0 - 0x7fff
  1209.     sSOSSampleData.wSampleFlags            = _DIGI_SAMPLE_FLAGS;
  1210.  
  1211.    // start the sample playing
  1212.     digi_start_sound( &sSOSSampleData, soundno );
  1213. */
  1214. }
  1215.  
  1216. void digi_set_midi_volume( int mvolume )
  1217. {
  1218. /*
  1219.     int old_volume = midi_volume;
  1220.  
  1221.     if ( mvolume > 127 )
  1222.         midi_volume = 127;
  1223.     else if ( mvolume < 0 )
  1224.         midi_volume = 0;
  1225.     else
  1226.         midi_volume = mvolume;
  1227.  
  1228.     if ( (digi_midi_type > 0) && (hSOSMidiDriver < 0xffff) )        {
  1229.         if (  (old_volume < 1) && ( midi_volume > 1 ) )    {
  1230.             if (wSongHandle == 0xffff )
  1231.                 digi_play_midi_song( digi_last_midi_song, digi_last_melodic_bank, digi_last_drum_bank, 1 );
  1232.         }
  1233.         _disable();
  1234.         sosMIDISetMasterVolume(midi_volume);
  1235.         _enable();
  1236.     }
  1237. */
  1238. }
  1239.  
  1240. void digi_set_digi_volume( int dvolume )
  1241. {
  1242. /*
  1243.     dvolume = fixmuldiv( dvolume, _DIGI_MAX_VOLUME, 0x7fff);
  1244.     if ( dvolume > _DIGI_MAX_VOLUME )
  1245.         digi_volume = _DIGI_MAX_VOLUME;
  1246.     else if ( dvolume < 0 )
  1247.         digi_volume = 0;
  1248.     else
  1249.         digi_volume = dvolume;
  1250.  
  1251.     if ( !Digi_initialized ) return;
  1252.     if ( digi_driver_board <= 0 )    return;
  1253.  
  1254.     digi_sync_sounds();
  1255. */
  1256. }
  1257.  
  1258.  
  1259. // 0-0x7FFF 
  1260. void digi_set_volume( int dvolume, int mvolume )    
  1261. {
  1262. /*
  1263.     digi_set_midi_volume( mvolume );
  1264.     digi_set_digi_volume( dvolume );
  1265. //    mprintf(( 1, "Volume: 0x%x and 0x%x\n", digi_volume, midi_volume ));
  1266. */
  1267. }
  1268.  
  1269. // allocate memory for file, load file, create far pointer
  1270. // with DS in selector.
  1271. void * testLoadFile( char * szFileName, int * length )
  1272. {
  1273. /*
  1274.    PSTR  pDataPtr;
  1275.     CFILE * fp;
  1276.     
  1277.    // open file
  1278.    fp  =  cfopen( szFileName, "rb" );
  1279.     if ( !fp ) return NULL;
  1280.  
  1281.    *length  =  cfilelength(fp);
  1282.  
  1283.    pDataPtr =  malloc( *length );
  1284.     if ( !pDataPtr ) return NULL;
  1285.  
  1286.    // read in driver
  1287.    cfread( pDataPtr, *length, 1, fp);
  1288.  
  1289.    // close driver file
  1290.    cfclose( fp );
  1291.  
  1292.    // return 
  1293.    return( pDataPtr );
  1294.    */
  1295.    return NULL;//KRB Comment out...
  1296. }
  1297.  
  1298.  
  1299. // ALL VARIABLES IN HERE MUST BE LOCKED DOWN!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1300. VOID _far sosMIDICallback( WORD PassedSongHandle )
  1301. {
  1302.     //sosMIDIStartSong(PassedSongHandle);
  1303.     return;//KRB comment out
  1304.  
  1305. VOID sosEndMIDICallback()        // Used to mark the end of sosMIDICallBack
  1306. {
  1307. }
  1308.  
  1309. void digi_stop_current_song()
  1310. {
  1311. /*
  1312.     // Stop last song...
  1313.     if (wSongHandle < 0xffff )    {
  1314.        // stop the last MIDI song from playing
  1315.         sosMIDIStopSong( wSongHandle );
  1316.        // uninitialize the last MIDI song
  1317.         sosMIDIUnInitSong( wSongHandle );
  1318.         wSongHandle = 0xffff;
  1319.     }
  1320.     if (SongData)     {
  1321.         if (!dpmi_unlock_region(SongData, SongSize))    {
  1322.             mprintf( (1, "Error unlocking midi file" ));
  1323.         }
  1324.         free(SongData);
  1325.         SongData = NULL;
  1326.     }
  1327. */
  1328. }
  1329.  
  1330.  
  1331. void digi_play_midi_song( char * filename, char * melodic_bank, char * drum_bank, int loop )
  1332. {
  1333. /*
  1334.     int i;
  1335.     char fname[128];
  1336.    WORD     wError;                 // error code returned from functions
  1337.     CFILE        *fp;
  1338.  
  1339.     // structure to pass sosMIDIInitSong
  1340.     _SOS_MIDI_INIT_SONG     sSOSInitSong;
  1341.  
  1342.     if (!Digi_initialized) return;
  1343.     if ( digi_midi_type <= 0 )    return;
  1344.  
  1345.     digi_stop_current_song();
  1346.  
  1347.     if ( filename == NULL )    return;
  1348.  
  1349.     strcpy( digi_last_midi_song, filename );
  1350.     strcpy( digi_last_melodic_bank, melodic_bank );
  1351.     strcpy( digi_last_drum_bank, drum_bank );
  1352.  
  1353.     fp = NULL;
  1354.  
  1355.     if ( (digi_midi_type==_MIDI_FM)||(digi_midi_type==_MIDI_OPL3) )    {    
  1356.         int sl;
  1357.         sl = strlen( filename );
  1358.         strcpy( fname, filename );    
  1359.         fname[sl-1] = 'q';
  1360.         fp = cfopen( fname, "rb" );
  1361.     }
  1362.  
  1363.     if ( !fp  )    {
  1364.         fp = cfopen( filename, "rb" );
  1365.         if (!fp) {
  1366.              mprintf( (1, "Error opening midi file, '%s'", filename ));
  1367.              return;
  1368.         }
  1369.     }
  1370.     if ( midi_volume < 1 )        {
  1371.         cfclose(fp);
  1372.         return;                // Don't play song if volume == 0;
  1373.     }
  1374.     SongSize = cfilelength( fp );
  1375.     SongData    = malloc( SongSize );
  1376.     if (SongData==NULL)    {
  1377.         cfclose(fp);
  1378.         mprintf( (1, "Error mallocing %d bytes for '%s'", SongSize, filename ));
  1379.         return;
  1380.     }
  1381.     if ( cfread (  SongData, SongSize, 1, fp )!=1)    {
  1382.         mprintf( (1, "Error reading midi file, '%s'", filename ));
  1383.         cfclose(fp);
  1384.         free(SongData);
  1385.         SongData=NULL;
  1386.         return;
  1387.     }
  1388.     cfclose(fp);
  1389.  
  1390.     if ( (digi_midi_type==_MIDI_FM)||(digi_midi_type==_MIDI_OPL3) )    {    
  1391.         if ( !digi_load_fm_banks(melodic_bank, drum_bank) )    {
  1392.             return;
  1393.         }
  1394.     }
  1395.         
  1396.     if (!dpmi_lock_region(SongData, SongSize))    {
  1397.         mprintf( (1, "Error locking midi file, '%s'", filename ));
  1398.         free(SongData);
  1399.         SongData=NULL;
  1400.         return;
  1401.     }
  1402.  
  1403.     // setup the song initialization structure
  1404.     sSOSInitSong.lpSongData = SongData;
  1405.     if ( loop )
  1406.         sSOSInitSong.lpSongCallback = sosMIDICallback;
  1407.     else
  1408.         sSOSInitSong.lpSongCallback = _NULL;
  1409.  
  1410.     for ( i=0; i<32; i++ )
  1411.         sSOSTrackMap.wTrackDevice[i] = _MIDI_MAP_TRACK;
  1412.             
  1413.     for ( i=0; i<_SOS_MIDI_MAX_TRACKS; i++ )
  1414.         _lpSOSMIDITrack[0][i] = _NULL;
  1415.  
  1416.     //initialize the song
  1417.     if( ( wError = sosMIDIInitSong( &sSOSInitSong, &sSOSTrackMap, &wSongHandle ) ) )    {
  1418.         mprintf( (1, "\nHMI Error : %s", sosGetErrorString( wError ) ));
  1419.         free(SongData);
  1420.         SongData=NULL;
  1421.         return;
  1422.     }
  1423.  
  1424.     Assert( wSongHandle == 0 );
  1425.      
  1426.   // start the song playing
  1427.    if( ( wError = sosMIDIStartSong( wSongHandle ) ) ) {
  1428.         mprintf( (1, "\nHMI Error : %s", sosGetErrorString( wError ) ));
  1429.        // uninitialize the last MIDI song
  1430.         sosMIDIUnInitSong( wSongHandle );
  1431.         free(SongData);
  1432.         SongData=NULL;
  1433.         return;
  1434.    }
  1435.    */
  1436. }
  1437.  
  1438. void digi_get_sound_loc( vms_matrix * listener, vms_vector * listener_pos, int listener_seg, vms_vector * sound_pos, int sound_seg, fix max_volume, int *volume, int *pan, fix max_distance )    
  1439. {      
  1440. /*
  1441.     vms_vector    vector_to_sound;
  1442.     fix angle_from_ear, cosang,sinang;
  1443.     fix distance;
  1444.     fix path_distance;
  1445.  
  1446.     *volume = 0;
  1447.     *pan = 0;
  1448.  
  1449.     max_distance = (max_distance*5)/4;        // Make all sounds travel 1.25 times as far.
  1450.  
  1451.     //    Warning: Made the vm_vec_normalized_dir be vm_vec_normalized_dir_quick and got illegal values to acos in the fang computation.
  1452.     distance = vm_vec_normalized_dir_quick( &vector_to_sound, sound_pos, listener_pos );
  1453.         
  1454.     if (distance < max_distance )    {
  1455.         int num_search_segs = f2i(max_distance/20);
  1456.         if ( num_search_segs < 1 ) num_search_segs = 1;
  1457.  
  1458.         path_distance = find_connected_distance(listener_pos, listener_seg, sound_pos, sound_seg, num_search_segs, WID_RENDPAST_FLAG );
  1459.         if ( path_distance > -1 )    {
  1460.             *volume = max_volume - fixdiv(path_distance,max_distance);
  1461.             //mprintf( (0, "Sound path distance %.2f, volume is %d / %d\n", f2fl(distance), *volume, max_volume ));
  1462.             if (*volume > 0 )    {
  1463.                 angle_from_ear = vm_vec_delta_ang_norm(&listener->rvec,&vector_to_sound,&listener->uvec);
  1464.                 fix_sincos(angle_from_ear,&sinang,&cosang);
  1465.                 //mprintf( (0, "volume is %.2f\n", f2fl(*volume) ));
  1466.                 if (Config_channels_reversed) cosang *= -1;
  1467.                 *pan = (cosang + F1_0)/2;
  1468.             } else {
  1469.                 *volume = 0;
  1470.             }
  1471.         }
  1472.     }                                                                                      
  1473.     */
  1474. }
  1475.  
  1476.  
  1477. void digi_init_sounds()
  1478. {
  1479. /*
  1480.     int i;
  1481.  
  1482.     if (!Digi_initialized) return;
  1483.     if (digi_driver_board<1) return;
  1484.  
  1485.     digi_reset_digi_sounds();
  1486.  
  1487.     for (i=0; i<MAX_SOUND_OBJECTS; i++ )    {
  1488.         if (digi_sounds_initialized) {
  1489.             if ( SoundObjects[i].flags & SOF_PLAYING )    {
  1490.                 sosDIGIStopSample( hSOSDigiDriver, SoundObjects[i].handle );
  1491.             }
  1492.         }
  1493.         SoundObjects[i].flags = 0;    // Mark as dead, so some other sound can use this sound
  1494.     }
  1495.     digi_sounds_initialized = 1;
  1496.     */
  1497. }
  1498.  
  1499. void digi_start_sound_object(int i)
  1500. {
  1501. /*
  1502.     // start sample structures 
  1503.     _SOS_START_SAMPLE sSOSSampleData;
  1504.  
  1505.     memset( &sSOSSampleData, 0, sizeof(_SOS_START_SAMPLE));
  1506.  
  1507.     if (!Digi_initialized) return;
  1508.     if (digi_driver_board<1) return;
  1509.  
  1510.     if (!dpmi_lock_region( GameSounds[SoundObjects[i].soundnum].data, GameSounds[SoundObjects[i].soundnum].length ))
  1511.         Error( "Error locking sound object %d\n", SoundObjects[i].soundnum );
  1512.     
  1513.     // Sound is not playing, so we must start it again
  1514.     SoundObjects[i].signature=next_signature++;
  1515.     sSOSSampleData.wChannel         = _CENTER_CHANNEL;
  1516.     sSOSSampleData.wSampleID        = SoundObjects[i].soundnum;
  1517.     sSOSSampleData.dwSampleSize     = ( LONG )GameSounds[SoundObjects[i].soundnum].length;
  1518.     sSOSSampleData.lpSamplePtr        = GameSounds[SoundObjects[i].soundnum].data;
  1519.     sSOSSampleData.lpCallback        = _NULL;        //sosDIGISampleCallback;
  1520.     sSOSSampleData.wSamplePanLocation    = SoundObjects[i].pan;            // 0 - 0xFFFF
  1521.     sSOSSampleData.wSamplePanSpeed         = 0;
  1522.     sSOSSampleData.wVolume                    = fixmuldiv(SoundObjects[i].volume,digi_volume,F1_0);                    // 0 - 0x7fff
  1523.     sSOSSampleData.wSampleFlags            = _DIGI_SAMPLE_FLAGS;
  1524.     if (SoundObjects[i].flags & SOF_PLAY_FOREVER )    {
  1525.         sSOSSampleData.wLoopCount         = -1;                // loop forever.
  1526.         sSOSSampleData.wSampleFlags |= _LOOPING;         // Mark it as a looper.
  1527.     }
  1528.             // start the sample playing
  1529.     
  1530.     //SoundObjects[i].handle = digi_start_sound( &sSOSSampleData );    
  1531.     SoundObjects[i].handle = sosDIGIStartSample( hSOSDigiDriver, &sSOSSampleData );
  1532.     if (SoundObjects[i].handle != _ERR_NO_SLOTS )        {
  1533.         SoundObjects[i].flags |= SOF_PLAYING;
  1534.         //mprintf(( 0, "Starting sound object %d on channel %d\n", i, SoundObjects[i].handle ));
  1535.         reset_sounds_on_channel( SoundObjects[i].handle );
  1536.     }
  1537. //    else
  1538. //        mprintf( (1, "[Out of channels: %i] ", i ));
  1539.  
  1540. */
  1541.  
  1542. }
  1543.  
  1544. int digi_link_sound_to_object2( int org_soundnum, short objnum, int forever, fix max_volume, fix  max_distance )
  1545. {
  1546. /*
  1547.     int i,volume,pan;
  1548.     object * objp;
  1549.     int soundnum;
  1550.  
  1551.     soundnum = digi_xlat_sound(org_soundnum);
  1552.  
  1553.     if ( max_volume < 0 ) return -1;
  1554. //    if ( max_volume > F1_0 ) max_volume = F1_0;
  1555.  
  1556.     if (!Digi_initialized) return -1;
  1557.     if (soundnum < 0 ) return -1;
  1558.     if (GameSounds[soundnum].data==NULL) {
  1559.         Int3();
  1560.         return -1;
  1561.     }
  1562.     if ((objnum<0)||(objnum>Highest_object_index))
  1563.         return -1;
  1564.     if (digi_driver_board<1) return -1;
  1565.  
  1566.     if ( !forever )    {
  1567.         // Hack to keep sounds from building up...
  1568.         digi_get_sound_loc( &Viewer->orient, &Viewer->pos, Viewer->segnum, &Objects[objnum].pos, Objects[objnum].segnum, max_volume,&volume, &pan, max_distance );
  1569.         digi_play_sample_3d( org_soundnum, pan, volume, 0 );
  1570.         return -1;
  1571.     }
  1572.  
  1573.     for (i=0; i<MAX_SOUND_OBJECTS; i++ )
  1574.         if (SoundObjects[i].flags==0)
  1575.             break;
  1576.     
  1577.     if (i==MAX_SOUND_OBJECTS) {
  1578.         mprintf((1, "Too many sound objects!\n" ));
  1579.         return -1;
  1580.     }
  1581.  
  1582.     SoundObjects[i].signature=next_signature++;
  1583.     SoundObjects[i].flags = SOF_USED | SOF_LINK_TO_OBJ;
  1584.     if ( forever )
  1585.         SoundObjects[i].flags |= SOF_PLAY_FOREVER;
  1586.     SoundObjects[i].objnum = objnum;
  1587.     SoundObjects[i].objsignature = Objects[objnum].signature;
  1588.     SoundObjects[i].max_volume = max_volume;
  1589.     SoundObjects[i].max_distance = max_distance;
  1590.     SoundObjects[i].volume = 0;
  1591.     SoundObjects[i].pan = 0;
  1592.     SoundObjects[i].soundnum = soundnum;
  1593.  
  1594.     objp = &Objects[SoundObjects[i].objnum];
  1595.     digi_get_sound_loc( &Viewer->orient, &Viewer->pos, Viewer->segnum, 
  1596.                        &objp->pos, objp->segnum, SoundObjects[i].max_volume,
  1597.                        &SoundObjects[i].volume, &SoundObjects[i].pan, SoundObjects[i].max_distance );
  1598.  
  1599.     digi_start_sound_object(i);
  1600.  
  1601.     return SoundObjects[i].signature;
  1602. */
  1603.     return 0;//KRB -Comment out
  1604. }
  1605.  
  1606. int digi_link_sound_to_object( int soundnum, short objnum, int forever, fix max_volume )
  1607. {                                                                                                    // 10 segs away
  1608.     //return digi_link_sound_to_object2( soundnum, objnum, forever, max_volume, 256*F1_0  );
  1609.     return 0;//KRB comment out 98
  1610. }
  1611.  
  1612. int digi_link_sound_to_pos2( int org_soundnum, short segnum, short sidenum, vms_vector * pos, int forever, fix max_volume, fix max_distance )
  1613. {
  1614. /*
  1615.     int i, volume, pan;
  1616.     int soundnum;
  1617.  
  1618.     soundnum = digi_xlat_sound(org_soundnum);
  1619.  
  1620.     if ( max_volume < 0 ) return -1;
  1621. //    if ( max_volume > F1_0 ) max_volume = F1_0;
  1622.  
  1623.     if (!Digi_initialized) return -1;
  1624.     if (soundnum < 0 ) return -1;
  1625.     if (GameSounds[soundnum].data==NULL) {
  1626.         Int3();
  1627.         return -1;
  1628.     }
  1629.     if (digi_driver_board<1) return -1;
  1630.  
  1631.     if ((segnum<0)||(segnum>Highest_segment_index))
  1632.         return -1;
  1633.  
  1634.     if ( !forever )    {
  1635.         // Hack to keep sounds from building up...
  1636.         digi_get_sound_loc( &Viewer->orient, &Viewer->pos, Viewer->segnum, pos, segnum, max_volume, &volume, &pan, max_distance );
  1637.         digi_play_sample_3d( org_soundnum, pan, volume, 0 );
  1638.         return -1;
  1639.     }
  1640.  
  1641.     for (i=0; i<MAX_SOUND_OBJECTS; i++ )
  1642.         if (SoundObjects[i].flags==0)
  1643.             break;
  1644.     
  1645.     if (i==MAX_SOUND_OBJECTS) {
  1646.         mprintf((1, "Too many sound objects!\n" ));
  1647.         return -1;
  1648.     }
  1649.  
  1650.  
  1651.     SoundObjects[i].signature=next_signature++;
  1652.     SoundObjects[i].flags = SOF_USED | SOF_LINK_TO_POS;
  1653.     if ( forever )
  1654.         SoundObjects[i].flags |= SOF_PLAY_FOREVER;
  1655.     SoundObjects[i].segnum = segnum;
  1656.     SoundObjects[i].sidenum = sidenum;
  1657.     SoundObjects[i].position = *pos;
  1658.     SoundObjects[i].soundnum = soundnum;
  1659.     SoundObjects[i].max_volume = max_volume;
  1660.     SoundObjects[i].max_distance = max_distance;
  1661.     SoundObjects[i].volume = 0;
  1662.     SoundObjects[i].pan = 0;
  1663.     digi_get_sound_loc( &Viewer->orient, &Viewer->pos, Viewer->segnum, 
  1664.                        &SoundObjects[i].position, SoundObjects[i].segnum, SoundObjects[i].max_volume,
  1665.                        &SoundObjects[i].volume, &SoundObjects[i].pan, SoundObjects[i].max_distance );
  1666.     
  1667.     digi_start_sound_object(i);
  1668.  
  1669.     return SoundObjects[i].signature;
  1670.     */
  1671.     return 0;//KRB comment out '98
  1672. }
  1673.  
  1674. int digi_link_sound_to_pos( int soundnum, short segnum, short sidenum, vms_vector * pos, int forever, fix max_volume )
  1675. {
  1676.     //return digi_link_sound_to_pos2( soundnum, segnum, sidenum, pos, forever, max_volume, F1_0 * 256 );
  1677.     return 0;//KRB comment out project...    
  1678. }
  1679.  
  1680.  
  1681. void digi_kill_sound_linked_to_segment( int segnum, int sidenum, int soundnum )
  1682. {
  1683. /*
  1684.     int i,killed;
  1685.  
  1686.     soundnum = digi_xlat_sound(soundnum);
  1687.  
  1688.     if (!Digi_initialized) return;
  1689.     if (digi_driver_board<1) return;
  1690.  
  1691.     killed = 0;
  1692.  
  1693.     for (i=0; i<MAX_SOUND_OBJECTS; i++ )    {
  1694.         if ( (SoundObjects[i].flags & SOF_USED) && (SoundObjects[i].flags & SOF_LINK_TO_POS) )    {
  1695.             if ((SoundObjects[i].segnum == segnum) && (SoundObjects[i].soundnum==soundnum ) && (SoundObjects[i].sidenum==sidenum) )    {
  1696.                 if ( SoundObjects[i].flags & SOF_PLAYING )    {
  1697.                     sosDIGIStopSample( hSOSDigiDriver, SoundObjects[i].handle );
  1698.                 }
  1699.                 SoundObjects[i].flags = 0;    // Mark as dead, so some other sound can use this sound
  1700.                 killed++;
  1701.             }
  1702.         }
  1703.     }
  1704.     // If this assert happens, it means that there were 2 sounds
  1705.     // that got deleted. Weird, get John.
  1706.     if ( killed > 1 )    {
  1707.         mprintf( (1, "ERROR: More than 1 sounds were deleted from seg %d\n", segnum ));
  1708.     }
  1709. */
  1710. }
  1711.  
  1712. void digi_kill_sound_linked_to_object( int objnum )
  1713. {
  1714. /*
  1715.     int i,killed;
  1716.  
  1717.     if (!Digi_initialized) return;
  1718.     if (digi_driver_board<1) return;
  1719.  
  1720.     killed = 0;
  1721.  
  1722.     for (i=0; i<MAX_SOUND_OBJECTS; i++ )    {
  1723.         if ( (SoundObjects[i].flags & SOF_USED) && (SoundObjects[i].flags & SOF_LINK_TO_OBJ ) )    {
  1724.             if (SoundObjects[i].objnum == objnum)    {
  1725.                 if ( SoundObjects[i].flags & SOF_PLAYING )    {
  1726.                     sosDIGIStopSample( hSOSDigiDriver, SoundObjects[i].handle );
  1727.                 }
  1728.                 SoundObjects[i].flags = 0;    // Mark as dead, so some other sound can use this sound
  1729.                 killed++;
  1730.             }
  1731.         }
  1732.     }
  1733.     // If this assert happens, it means that there were 2 sounds
  1734.     // that got deleted. Weird, get John.
  1735.     if ( killed > 1 )    {
  1736.         mprintf( (1, "ERROR: More than 1 sounds were deleted from object %d\n", objnum ));
  1737.     }
  1738. */
  1739. }
  1740.  
  1741.  
  1742. //--unused-- void digi_kill_sound( int signature )
  1743. //--unused-- {
  1744. //--unused--     int i;
  1745. //--unused--     if (!Digi_initialized) return;
  1746. //--unused-- 
  1747. //--unused--     for (i=0; i<MAX_SOUND_OBJECTS; i++ )    {
  1748. //--unused--         if ( SoundObjects[i].flags & SOF_USED )    {
  1749. //--unused--             if (SoundObjects[i].signature == signature )    {
  1750. //--unused--                 if ( SoundObjects[i].flags & SOF_PLAYING )    {
  1751. //--unused--                     sosDIGIStopSample( hSOSDigiDriver, SoundObjects[i].handle );
  1752. //--unused--                 }
  1753. //--unused--                 SoundObjects[i].flags = 0;    // Mark as dead, so some other sound can use this sound
  1754. //--unused--             }
  1755. //--unused--         }
  1756. //--unused--     }
  1757. //--unused-- }
  1758.  
  1759. void digi_sync_sounds()
  1760. {
  1761. /*
  1762.     int i;
  1763.     int oldvolume, oldpan;
  1764.  
  1765.     if (!Digi_initialized) return;
  1766.     if (digi_driver_board<1) return;
  1767.  
  1768. //NOT_MIDI_CHECK    if (SongData)    {
  1769. //NOT_MIDI_CHECK        ushort new_crc;
  1770. //NOT_MIDI_CHECK        new_crc = netmisc_calc_checksum( &SongData, SongSize );
  1771. //NOT_MIDI_CHECK        if ( new_crc != MIDI_CRC )    {
  1772. //NOT_MIDI_CHECK            for (i=0; i<SongSize; i++ )    {
  1773. //NOT_MIDI_CHECK                if ( MIDI_SAVED_DATA[i] != SongData[i] )    {
  1774. //NOT_MIDI_CHECK                    mprintf(( 0, "MIDI[%d] CHANGED FROM 0x%x to 0x%x\n", i, MIDI_SAVED_DATA[i], SongData[i] ));
  1775. //NOT_MIDI_CHECK                    MIDI_SAVED_DATA[i] = SongData[i];
  1776. //NOT_MIDI_CHECK                }
  1777. //NOT_MIDI_CHECK            }        
  1778. //NOT_MIDI_CHECK            //Int3();        // Midi data changed!!!!
  1779. //NOT_MIDI_CHECK            MIDI_CRC=new_crc;
  1780. //NOT_MIDI_CHECK        }
  1781. //NOT_MIDI_CHECK    }
  1782.  
  1783.     for (i=0; i<MAX_SOUND_OBJECTS; i++ )    {
  1784.         if ( SoundObjects[i].flags & SOF_USED )    {
  1785.             oldvolume = SoundObjects[i].volume;
  1786.             oldpan = SoundObjects[i].pan;
  1787.  
  1788.             if ( !(SoundObjects[i].flags & SOF_PLAY_FOREVER) )    {
  1789.                  // Check if its done.
  1790.                 if (SoundObjects[i].flags & SOF_PLAYING) {
  1791.                     if (sosDIGISampleDone(hSOSDigiDriver, SoundObjects[i].handle) )    {
  1792.                         SoundObjects[i].flags = 0;    // Mark as dead, so some other sound can use this sound
  1793.                         continue;        // Go on to next sound...
  1794.                     }
  1795.                 }
  1796.             }            
  1797.         
  1798.             if ( SoundObjects[i].flags & SOF_LINK_TO_POS )    {
  1799.                 digi_get_sound_loc( &Viewer->orient, &Viewer->pos, Viewer->segnum, 
  1800.                                 &SoundObjects[i].position, SoundObjects[i].segnum, SoundObjects[i].max_volume,
  1801.                                 &SoundObjects[i].volume, &SoundObjects[i].pan, SoundObjects[i].max_distance );
  1802.  
  1803.             } else if ( SoundObjects[i].flags & SOF_LINK_TO_OBJ )    {
  1804.                 object * objp;
  1805.     
  1806.                 objp = &Objects[SoundObjects[i].objnum];
  1807.         
  1808.                 if ((objp->type==OBJ_NONE) || (objp->signature!=SoundObjects[i].objsignature))    {
  1809.                     // The object that this is linked to is dead, so just end this sound if it is looping.
  1810.                     if ( (SoundObjects[i].flags & SOF_PLAYING)  && (SoundObjects[i].flags & SOF_PLAY_FOREVER))    {
  1811.                         sosDIGIStopSample( hSOSDigiDriver, SoundObjects[i].handle );
  1812.                     }
  1813.                     SoundObjects[i].flags = 0;    // Mark as dead, so some other sound can use this sound
  1814.                     continue;        // Go on to next sound...
  1815.                 } else {
  1816.                     digi_get_sound_loc( &Viewer->orient, &Viewer->pos, Viewer->segnum, 
  1817.                                     &objp->pos, objp->segnum, SoundObjects[i].max_volume,
  1818.                                    &SoundObjects[i].volume, &SoundObjects[i].pan, SoundObjects[i].max_distance );
  1819.                 }
  1820.             }
  1821.              
  1822.             if (oldvolume != SoundObjects[i].volume)     {
  1823.                 if ( SoundObjects[i].volume < 1 )    {
  1824.                     // Sound is too far away, so stop it from playing.
  1825.                     if ((SoundObjects[i].flags & SOF_PLAYING)&&(SoundObjects[i].flags & SOF_PLAY_FOREVER))    {
  1826.                         sosDIGIStopSample( hSOSDigiDriver, SoundObjects[i].handle );
  1827.                         SoundObjects[i].flags &= ~SOF_PLAYING;        // Mark sound as not playing
  1828.                     }
  1829.                 } else {
  1830.                     if (!(SoundObjects[i].flags & SOF_PLAYING))    {
  1831.                         digi_start_sound_object(i);
  1832.                     } else {
  1833.                         sosDIGISetSampleVolume( hSOSDigiDriver, SoundObjects[i].handle, fixmuldiv(SoundObjects[i].volume,digi_volume,F1_0) );
  1834.                     }
  1835.                 }
  1836.             }
  1837.                 
  1838.             if (oldpan != SoundObjects[i].pan)     {
  1839.                 if (SoundObjects[i].flags & SOF_PLAYING)
  1840.                     sosDIGISetPanLocation( hSOSDigiDriver, SoundObjects[i].handle, SoundObjects[i].pan );
  1841.             }
  1842.  
  1843.  
  1844.         }
  1845.     }
  1846.  
  1847. */
  1848. }
  1849.  
  1850.  
  1851. int sound_paused = 0;
  1852.  
  1853. void digi_pause_all()
  1854. {
  1855. /*
  1856.     int i;
  1857.  
  1858.     if (!Digi_initialized) return;
  1859.  
  1860.     if (sound_paused==0)    {
  1861.         if ( digi_midi_type > 0 )    {
  1862.             // pause here
  1863.             //if (wSongHandle < 0xFFFF)    {
  1864.             //    // stop the last MIDI song from playing
  1865.             //    _disable();
  1866.             //    sosMIDIPauseSong( wSongHandle, _TRUE );
  1867.             //    _enable();
  1868.               //    mprintf(( 0, "Paused song %d\n", wSongHandle ));
  1869.            //}
  1870.             _disable();
  1871.             sosMIDISetMasterVolume(0);
  1872.             _enable();
  1873.         }
  1874.         if (digi_driver_board>0)    {
  1875.             for (i=0; i<MAX_SOUND_OBJECTS; i++ )    {
  1876.                 if ( (SoundObjects[i].flags & SOF_USED) && (SoundObjects[i].flags & SOF_PLAYING)&& (SoundObjects[i].flags & SOF_PLAY_FOREVER) )    {
  1877.                     sosDIGIStopSample( hSOSDigiDriver, SoundObjects[i].handle );
  1878.                     SoundObjects[i].flags &= ~SOF_PLAYING;        // Mark sound as not playing
  1879.                 }            
  1880.             }
  1881.         }
  1882.     }
  1883.     sound_paused++;
  1884. */
  1885. }
  1886.  
  1887. void digi_resume_all()
  1888. {
  1889. /*
  1890.     if (!Digi_initialized) return;
  1891.  
  1892.     Assert( sound_paused > 0 );
  1893.  
  1894.     if (sound_paused==1)    {
  1895.         // resume sound here
  1896.         if ( digi_midi_type > 0 )    {
  1897.             //if (wSongHandle < 0xffff )    {
  1898.             //   // stop the last MIDI song from playing
  1899.             //    _disable();
  1900.             //    sosMIDIResumeSong( wSongHandle );
  1901.             //    _enable();
  1902.             //    mprintf(( 0, "Resumed song %d\n", wSongHandle ));
  1903.             //}
  1904.             _disable();
  1905.             sosMIDISetMasterVolume(midi_volume);
  1906.             _enable();
  1907.         }
  1908.     }
  1909.     sound_paused--;
  1910. */
  1911. }
  1912.  
  1913.  
  1914. void digi_stop_all()
  1915. {
  1916.     /*
  1917.     int i;
  1918.  
  1919.     if (!Digi_initialized) return;
  1920.  
  1921.     if ( digi_midi_type > 0 )    {
  1922.         if (wSongHandle < 0xffff )    {
  1923.            // stop the last MIDI song from playing
  1924.             sosMIDIStopSong( wSongHandle );
  1925.            // uninitialize the last MIDI song
  1926.             sosMIDIUnInitSong( wSongHandle );
  1927.             wSongHandle = 0xffff;
  1928.         }
  1929.         if (SongData)     {
  1930.             free(SongData);
  1931.             SongData = NULL;
  1932.         }
  1933.     }
  1934.  
  1935.     if (digi_driver_board>0)    {
  1936.         for (i=0; i<MAX_SOUND_OBJECTS; i++ )    {
  1937.             if ( SoundObjects[i].flags & SOF_USED )    {
  1938.                 if ( SoundObjects[i].flags & SOF_PLAYING )    {
  1939.                     sosDIGIStopSample( hSOSDigiDriver, SoundObjects[i].handle );
  1940.                 }
  1941.                 SoundObjects[i].flags = 0;    // Mark as dead, so some other sound can use this sound
  1942.             }
  1943.         }
  1944.     }
  1945.     */
  1946. }
  1947.  
  1948. #ifndef NDEBUG
  1949. int verify_sound_channel_free( int channel )
  1950. {
  1951.     /*
  1952.     int i;
  1953.     if (digi_driver_board>0)    {
  1954.         for (i=0; i<MAX_SOUND_OBJECTS; i++ )    {
  1955.             if ( SoundObjects[i].flags & SOF_USED )    {
  1956.                 if ( SoundObjects[i].flags & SOF_PLAYING )    {
  1957.                     if ( SoundObjects[i].handle == channel )    {
  1958.                         mprintf(( 0, "ERROR STARTING SOUND CHANNEL ON USED SLOT!!\n" ));
  1959.                         Int3();    // Get John!
  1960.                     }
  1961.                 }
  1962.             }
  1963.         }
  1964.     }
  1965.     */
  1966.     return 0;
  1967. }
  1968. #endif
  1969. 
  1970.