]> icculus.org git repositories - btb/d2x.git/blob - arch/win32/digi.c
call digi_reset_digi_sounds in digi_close to fix crashing (d1x r1.4)
[btb/d2x.git] / arch / win32 / digi.c
1 #define DIGI_SOUND
2 #define MIDI_SOUND
3
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <string.h>
7 #define WIN32_LEAN_AND_MEAN
8 #include <windows.h>
9 #include <mmsystem.h>
10 #include <dsound.h>
11
12 #include <math.h>
13
14 #include "error.h"
15 #include "mono.h"
16 #include "fix.h"
17 #include "vecmat.h"
18 #include "gr.h" // needed for piggy.h
19 #include "piggy.h"
20 #include "digi.h"
21 #include "sounds.h"
22 #include "wall.h"
23 #include "newdemo.h"
24 #include "kconfig.h"
25 #include "hmpfile.h"
26 hmp_file *hmp = NULL;
27
28 #ifdef DIGI_SOUND
29 #define MAX_SOUND_SLOTS 32
30 #define MIN_VOLUME 10
31
32
33 #define SOF_USED                        1               // Set if this sample is used
34 #define SOF_PLAYING                     2               // Set if this sample is playing on a channel
35 #define SOF_LINK_TO_OBJ         4               // Sound is linked to a moving object. If object dies, then finishes play and quits.
36 #define SOF_LINK_TO_POS         8               // Sound is linked to segment, pos
37 #define SOF_PLAY_FOREVER        16              // Play forever (or until level is stopped), otherwise plays once
38
39 typedef struct sound_object {
40         short           signature;              // A unique signature to this sound
41         ubyte           flags;                  // Used to tell if this slot is used and/or currently playing, and how long.
42         fix             max_volume;             // Max volume that this sound is playing at
43         fix             max_distance;           // The max distance that this sound can be heard at...
44         int             volume;                 // Volume that this sound is playing at
45         int             pan;                    // Pan value that this sound is playing at
46         int             handle;                 // What handle this sound is playing on.  Valid only if SOF_PLAYING is set.
47         short           soundnum;               // The sound number that is playing
48         union { 
49                 struct {
50                         short           segnum;                         // Used if SOF_LINK_TO_POS field is used
51                         short           sidenum;
52                         vms_vector      position;
53                 }pos;
54                 struct {
55                         short            objnum;                         // Used if SOF_LINK_TO_OBJ field is used
56                         short            objsignature;
57                 }obj;
58         }link;
59 } sound_object;
60 #define lp_segnum link.pos.segnum
61 #define lp_sidenum link.pos.sidenum
62 #define lp_position link.pos.position
63
64 #define lo_objnum link.obj.objnum
65 #define lo_objsignature link.obj.objsignature
66
67 #define MAX_SOUND_OBJECTS 16
68 sound_object SoundObjects[MAX_SOUND_OBJECTS];
69 short next_signature=0;
70
71
72 //added/changed on 980905 by adb to make sfx volume work
73 #define SOUND_MAX_VOLUME F1_0
74 int digi_volume = SOUND_MAX_VOLUME;
75 //end edit by adb
76
77 LPDIRECTSOUND lpds;
78 WAVEFORMATEX waveformat;
79 DSBUFFERDESC dsbd;
80
81 extern HWND g_hWnd;
82
83 struct sound_slot {
84  int soundno;
85  int playing;   // Is there a sample playing on this channel?
86  int looped;    // Play this sample looped?
87  fix pan;       // 0 = far left, 1 = far right
88  fix volume;    // 0 = nothing, 1 = fully on
89  //changed on 980905 by adb from char * to unsigned char * 
90  unsigned char *samples;
91  //end changes by adb
92  unsigned int length; // Length of the sample
93  unsigned int position; // Position we are at at the moment.
94  LPDIRECTSOUNDBUFFER lpsb;
95 } SoundSlots[MAX_SOUND_SLOTS];
96
97
98 int digi_lomem = 0;
99 int midi_volume = 255;
100 int digi_midi_song_playing = 0;
101 int digi_last_midi_song = 0;
102 int digi_last_midi_song_loop = 0;
103
104 static int digi_initialised = 0;
105 static int digi_atexit_initialised=0;
106
107 static int digi_sounds_initialized = 0;
108
109 //added on 980905 by adb to add rotating/volume based sound kill system
110 static int digi_max_channels = 16;
111 static int next_handle = 0;
112 int SampleHandles[32];
113 void reset_sounds_on_channel(int channel);
114 //end edit by adb
115
116 void digi_reset_digi_sounds(void);
117
118 void digi_reset() { }
119
120 void digi_close(void)
121 {
122         if(digi_initialised)
123         {
124                 digi_reset_digi_sounds();
125                 IDirectSound_Release(lpds);
126         }
127         digi_initialised = 0;
128 }
129
130 /* Initialise audio devices. */
131 int digi_init()
132 {
133  HRESULT hr;
134  
135  if (!digi_initialised && g_hWnd){
136
137          memset(&waveformat, 0, sizeof(waveformat));
138          waveformat.wFormatTag=WAVE_FORMAT_PCM;
139          waveformat.wBitsPerSample=8;
140          waveformat.nChannels = 1;
141          waveformat.nSamplesPerSec = digi_sample_rate; //11025;
142          waveformat.nBlockAlign =
143          waveformat.nChannels * (waveformat.wBitsPerSample/8);
144          waveformat.nAvgBytesPerSec =
145          waveformat.nSamplesPerSec * waveformat.nBlockAlign;    
146
147           if ((hr = DirectSoundCreate(NULL, &lpds, NULL)) != DS_OK)
148            return -1;
149
150           if ((hr = IDirectSound_SetCooperativeLevel(lpds, g_hWnd, DSSCL_PRIORITY)) //hWndMain
151                != DS_OK)
152            {
153             IDirectSound_Release(lpds);
154             return -1;
155            }
156         
157          memset(&dsbd, 0, sizeof(dsbd));
158          dsbd.dwSize = sizeof(dsbd);
159          dsbd.dwFlags = DSBCAPS_CTRLDEFAULT | DSBCAPS_GETCURRENTPOSITION2;
160          dsbd.dwBufferBytes = 8192;
161          dsbd.dwReserved=0;
162          dsbd.lpwfxFormat = &waveformat;
163
164          digi_initialised = 1;
165 }
166
167         if (!digi_atexit_initialised){
168                 atexit(digi_close);
169                 digi_atexit_initialised=1;
170         }
171  return 0;
172 }
173
174 /* Find the sound which actually equates to a sound number */
175 int digi_xlat_sound(int soundno)
176 {
177         if ( soundno < 0 ) return -1;
178
179         if ( digi_lomem )       {
180                 soundno = AltSounds[soundno];
181                 if ( soundno == 255 ) return -1;
182         }
183         return Sounds[soundno];
184 }
185
186 static int get_free_slot()
187 {
188  int i;
189  unsigned int s;
190  for (i=0; i<MAX_SOUND_SLOTS; i++)
191  {
192   if (!SoundSlots[i].playing) return i;
193   if (SoundSlots[i].lpsb) {
194    IDirectSoundBuffer_GetStatus(SoundSlots[i].lpsb, &s);
195    if (!(s & DSBSTATUS_PLAYING)) IDirectSoundBuffer_Release(SoundSlots[i].lpsb);
196    SoundSlots[i].playing = 0;
197    SoundSlots[i].lpsb = NULL;
198    return i;
199   }
200  }
201  return -1;
202 }
203
204 int D1vol2DSvol(fix d1v){
205 //multiplying by 1.5 doesn't help.  DirectSound uses dB for volume, rather than a linear scale like d1 wants.
206 //I had to pull out a math book, but here is the code to fix it :)  -Matt Mueller
207 //log x=y  <==>  x=a^y  
208 //   a
209          if (d1v<=0)
210                  return -10000;
211          else
212 //               return log2(f2fl(d1v))*1000;//no log2? hm.
213                  return log(f2fl(d1v))/log(2)*1000.0;
214 }
215
216 int digi_start_sound(int soundnum, fix volume, fix pan)
217 {
218  int ntries;
219  int slot;
220  HRESULT hr;
221
222  if (!digi_initialised) return -1;
223
224  //added on 980905 by adb from original source to add sound kill system
225  // play at most digi_max_channel samples, if possible kill sample with low volume
226  ntries = 0;
227
228 TryNextChannel:
229  if ( (SampleHandles[next_handle] >= 0) && (SoundSlots[SampleHandles[next_handle]].playing)  )
230  {
231   if ( (SoundSlots[SampleHandles[next_handle]].volume > digi_volume) && (ntries<digi_max_channels) )
232   {
233    //mprintf(( 0, "Not stopping loud sound %d.\n", next_handle ));
234    next_handle++;
235    if ( next_handle >= digi_max_channels )
236     next_handle = 0;
237    ntries++;
238    goto TryNextChannel;
239   }
240   //mprintf(( 0, "[SS:%d]", next_handle ));
241   SoundSlots[SampleHandles[next_handle]].playing = 0;
242         DS_release_slot(SampleHandles[next_handle], 1);
243   SampleHandles[next_handle] = -1;
244  }
245  //end edit by adb
246
247  slot = get_free_slot();
248  if (slot<0) return -1;
249
250  SoundSlots[slot].soundno = soundnum;
251  SoundSlots[slot].samples = GameSounds[soundnum].data;
252  SoundSlots[slot].length = GameSounds[soundnum].length;
253  SoundSlots[slot].volume = fixmul(digi_volume, volume);
254  SoundSlots[slot].pan = pan;
255  SoundSlots[slot].position = 0;
256  SoundSlots[slot].looped = 0;
257  SoundSlots[slot].playing = 1;
258
259  memset(&waveformat, 0, sizeof(waveformat));
260  waveformat.wFormatTag=WAVE_FORMAT_PCM;
261  waveformat.wBitsPerSample=8;
262  waveformat.nChannels = 1;
263  waveformat.nSamplesPerSec = digi_sample_rate; //11025;
264  waveformat.nBlockAlign =
265    waveformat.nChannels * (waveformat.wBitsPerSample/8);
266  waveformat.nAvgBytesPerSec =
267    waveformat.nSamplesPerSec * waveformat.nBlockAlign;
268
269  memset(&dsbd, 0, sizeof(dsbd));
270  dsbd.dwSize = sizeof(dsbd);
271  dsbd.dwFlags = DSBCAPS_CTRLDEFAULT | DSBCAPS_GETCURRENTPOSITION2;
272  dsbd.dwReserved=0;
273  dsbd.dwBufferBytes = SoundSlots[slot].length;
274  dsbd.lpwfxFormat = &waveformat;
275
276  hr = IDirectSound_CreateSoundBuffer(lpds, &dsbd, &SoundSlots[slot].lpsb, NULL);
277  if ( hr != DS_OK ) {
278   printf("Createsoundbuffer failed! hr=0x%X\n", (int)hr);
279   abort();
280  }
281  {
282   char *ptr1, *ptr2;
283   int len1, len2;
284   IDirectSoundBuffer_Lock(SoundSlots[slot].lpsb, 0, GameSounds[soundnum].length,
285          (void **)&ptr1, &len1, (void **)&ptr2, &len2, 0);
286   memcpy(ptr1,GameSounds[soundnum].data, MIN(len1, GameSounds[soundnum].length));
287   IDirectSoundBuffer_Unlock(SoundSlots[slot].lpsb, ptr1, len1, ptr2, len2);
288  }
289  IDirectSoundBuffer_SetPan(SoundSlots[slot].lpsb, ((int)(f2fl(pan) * 20000.0))-10000);
290 // IDirectSoundBuffer_SetVolume(SoundSlots[slot].lpsb, MIN(((int)(f2fl(SoundSlots[slot].volume) * 15000.0)) - 10000, 0));//nope
291  IDirectSoundBuffer_SetVolume(SoundSlots[slot].lpsb, D1vol2DSvol(SoundSlots[slot].volume));
292  
293  IDirectSoundBuffer_Play(SoundSlots[slot].lpsb, 0, 0, 0);
294
295  //added on 980905 by adb to add sound kill system from original sos digi.c
296  reset_sounds_on_channel(slot);
297  SampleHandles[next_handle] = slot;
298  next_handle++;
299  if ( next_handle >= digi_max_channels )
300   next_handle = 0;
301  //end edit by adb
302
303  return slot;
304 }
305
306  //added on 980905 by adb to add sound kill system from original sos digi.c
307 void reset_sounds_on_channel( int channel )
308 {
309  int i;
310
311  for (i=0; i<digi_max_channels; i++)
312   if (SampleHandles[i] == channel)
313    SampleHandles[i] = -1;
314 }
315 //end edit by adb
316
317 int digi_start_sound_object(int obj)
318 {
319  int slot;
320  HRESULT hr;
321
322  if (!digi_initialised) return -1;
323  slot = get_free_slot();
324
325  if (slot<0) return -1;
326
327
328  SoundSlots[slot].soundno = SoundObjects[obj].soundnum;
329  SoundSlots[slot].samples = GameSounds[SoundObjects[obj].soundnum].data;
330  SoundSlots[slot].length = GameSounds[SoundObjects[obj].soundnum].length;
331  SoundSlots[slot].volume = fixmul(digi_volume, SoundObjects[obj].volume);
332  SoundSlots[slot].pan = SoundObjects[obj].pan;
333  SoundSlots[slot].position = 0;
334  SoundSlots[slot].looped = (SoundObjects[obj].flags & SOF_PLAY_FOREVER);
335  SoundSlots[slot].playing = 1;
336
337  memset(&waveformat, 0, sizeof(waveformat));
338  waveformat.wFormatTag=WAVE_FORMAT_PCM;
339  waveformat.wBitsPerSample=8;
340  waveformat.nChannels = 1;
341  waveformat.nSamplesPerSec = digi_sample_rate; // 11025;
342  waveformat.nBlockAlign =
343  waveformat.nChannels * (waveformat.wBitsPerSample/8);
344  waveformat.nAvgBytesPerSec =
345  waveformat.nSamplesPerSec * waveformat.nBlockAlign;
346
347  memset(&dsbd, 0, sizeof(dsbd));
348  dsbd.dwSize = sizeof(dsbd);
349  dsbd.dwFlags = DSBCAPS_CTRLDEFAULT | DSBCAPS_GETCURRENTPOSITION2;
350  dsbd.dwReserved=0;
351  dsbd.dwBufferBytes = SoundSlots[slot].length;
352  dsbd.lpwfxFormat = &waveformat;
353
354  hr = IDirectSound_CreateSoundBuffer(lpds, &dsbd, &SoundSlots[slot].lpsb, NULL);
355  if ( hr != DS_OK ) {
356   abort();
357  }
358  {
359   char *ptr1, *ptr2;
360   int len1, len2;
361   IDirectSoundBuffer_Lock(SoundSlots[slot].lpsb, 0, SoundSlots[slot].length,
362          (void **)&ptr1, &len1, (void **)&ptr2, &len2, 0);
363   memcpy(ptr1, SoundSlots[slot].samples, MIN(len1,(int)SoundSlots[slot].length));
364   IDirectSoundBuffer_Unlock(SoundSlots[slot].lpsb, (void *)ptr1, len1, (void *)ptr2, len2);
365  }
366  IDirectSoundBuffer_SetPan(SoundSlots[slot].lpsb, ((int)(f2fl(SoundSlots[slot].pan) * 20000))-10000);
367 // IDirectSoundBuffer_SetVolume(SoundSlots[slot].lpsb, MIN(((int)(f2fl(SoundSlots[slot].volume) * 15000.0)) - 10000, 0));//nope
368  IDirectSoundBuffer_SetVolume(SoundSlots[slot].lpsb,D1vol2DSvol(SoundSlots[slot].volume));
369  IDirectSoundBuffer_Play(SoundSlots[slot].lpsb, 0, 0, SoundSlots[slot].looped?DSBPLAY_LOOPING:0);
370
371  SoundObjects[obj].signature = next_signature++;
372  SoundObjects[obj].handle = slot;
373
374  SoundObjects[obj].flags |= SOF_PLAYING;
375  //added on 980905 by adb to add sound kill system from original sos digi.c
376  reset_sounds_on_channel(slot);
377  //end edit by adb
378  
379  return 0;
380 }
381
382
383 // Play the given sound number.
384 // Volume is max at F1_0.
385 void digi_play_sample( int soundno, fix max_volume )
386 {
387 #ifdef NEWDEMO
388         if ( Newdemo_state == ND_STATE_RECORDING )
389                 newdemo_record_sound( soundno );
390 #endif
391         soundno = digi_xlat_sound(soundno);
392
393         if (!digi_initialised) return;
394
395         if (soundno < 0 ) return;
396
397         digi_start_sound(soundno, max_volume, F0_5);
398 }
399
400 // Play the given sound number. If the sound is already playing,
401 // restart it.
402 void digi_play_sample_once( int soundno, fix max_volume )
403 {
404         int i;
405
406 #ifdef NEWDEMO
407         if ( Newdemo_state == ND_STATE_RECORDING )
408                 newdemo_record_sound( soundno );
409 #endif
410         soundno = digi_xlat_sound(soundno);
411
412         if (!digi_initialised) return;
413
414         if (soundno < 0 ) return;
415
416         for (i=0; i < MAX_SOUND_SLOTS; i++)
417           if (SoundSlots[i].soundno == soundno) {
418              SoundSlots[i].playing = 0;
419               if (SoundSlots[i].lpsb) {
420                 unsigned int s;
421                 IDirectSoundBuffer_GetStatus(SoundSlots[i].lpsb, &s);
422                 if (s & DSBSTATUS_PLAYING) IDirectSoundBuffer_Stop(SoundSlots[i].lpsb);
423                 IDirectSoundBuffer_Release(SoundSlots[i].lpsb);
424                 SoundSlots[i].playing = 0;
425                 SoundSlots[i].lpsb = NULL;
426               }
427           }
428         digi_start_sound(soundno, max_volume, F0_5);
429
430 }
431
432 void digi_play_sample_3d( int soundno, int angle, int volume, int no_dups ) // Volume from 0-0x7fff
433 {
434         no_dups = 1;
435
436 #ifdef NEWDEMO
437         if ( Newdemo_state == ND_STATE_RECORDING )              {
438                 if ( no_dups )
439                         newdemo_record_sound_3d_once( soundno, angle, volume );
440                 else
441                         newdemo_record_sound_3d( soundno, angle, volume );
442         }
443 #endif
444         soundno = digi_xlat_sound(soundno);
445
446         if (!digi_initialised) return;
447         if (soundno < 0 ) return;
448
449         if (volume < MIN_VOLUME ) return;
450         digi_start_sound(soundno, volume, angle);
451 }
452
453 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 )
454 {         
455         vms_vector      vector_to_sound;
456         fix angle_from_ear, cosang,sinang;
457         fix distance;
458         fix path_distance;
459
460         *volume = 0;
461         *pan = 0;
462
463         max_distance = (max_distance*5)/4;              // Make all sounds travel 1.25 times as far.
464
465         //      Warning: Made the vm_vec_normalized_dir be vm_vec_normalized_dir_quick and got illegal values to acos in the fang computation.
466         distance = vm_vec_normalized_dir_quick( &vector_to_sound, sound_pos, listener_pos );
467                 
468         if (distance < max_distance )   {
469                 int num_search_segs = f2i(max_distance/20);
470                 if ( num_search_segs < 1 ) num_search_segs = 1;
471
472                 path_distance = find_connected_distance(listener_pos, listener_seg, sound_pos, sound_seg, num_search_segs, WID_RENDPAST_FLAG );
473                 if ( path_distance > -1 )       {
474                         *volume = max_volume - fixdiv(path_distance,max_distance);
475                         //mprintf( (0, "Sound path distance %.2f, volume is %d / %d\n", f2fl(distance), *volume, max_volume ));
476                         if (*volume > 0 )       {
477                                 angle_from_ear = vm_vec_delta_ang_norm(&listener->rvec,&vector_to_sound,&listener->uvec);
478                                 fix_sincos(angle_from_ear,&sinang,&cosang);
479                                 //mprintf( (0, "volume is %.2f\n", f2fl(*volume) ));
480                                 if (Config_channels_reversed) cosang *= -1;
481                                 *pan = (cosang + F1_0)/2;
482                         } else {
483                                 *volume = 0;
484                         }
485                 }
486         }                                                                                                                                                                         
487 }
488
489 int digi_link_sound_to_object2( int org_soundnum, short objnum, int forever, fix max_volume, fix  max_distance )
490 {
491         int i,volume,pan;
492         object * objp;
493         int soundnum;
494
495         soundnum = digi_xlat_sound(org_soundnum);
496
497         if ( max_volume < 0 ) return -1;
498 //      if ( max_volume > F1_0 ) max_volume = F1_0;
499
500         if (!digi_initialised) return -1;
501         if (soundnum < 0 ) return -1;
502         if (GameSounds[soundnum].data==NULL) {
503                 Int3();
504                 return -1;
505         }
506         if ((objnum<0)||(objnum>Highest_object_index))
507                 return -1;
508
509         if ( !forever ) {
510                 // Hack to keep sounds from building up...
511                 digi_get_sound_loc( &Viewer->orient, &Viewer->pos, Viewer->segnum, &Objects[objnum].pos, Objects[objnum].segnum, max_volume,&volume, &pan, max_distance );
512                 digi_play_sample_3d( org_soundnum, pan, volume, 0 );
513                 return -1;
514         }
515
516         for (i=0; i<MAX_SOUND_OBJECTS; i++ )
517                 if (SoundObjects[i].flags==0)
518                    break;
519
520         if (i==MAX_SOUND_OBJECTS) {
521                 mprintf((1, "Too many sound objects!\n" ));
522                 return -1;
523         }
524
525         SoundObjects[i].signature=next_signature++;
526         SoundObjects[i].flags = SOF_USED | SOF_LINK_TO_OBJ;
527         if ( forever )
528                 SoundObjects[i].flags |= SOF_PLAY_FOREVER;
529         SoundObjects[i].lo_objnum = objnum;
530         SoundObjects[i].lo_objsignature = Objects[objnum].signature;
531         SoundObjects[i].max_volume = max_volume;
532         SoundObjects[i].max_distance = max_distance;
533         SoundObjects[i].volume = 0;
534         SoundObjects[i].pan = 0;
535         SoundObjects[i].soundnum = soundnum;
536
537         objp = &Objects[SoundObjects[i].lo_objnum];
538         digi_get_sound_loc( &Viewer->orient, &Viewer->pos, Viewer->segnum, 
539                        &objp->pos, objp->segnum, SoundObjects[i].max_volume,
540                        &SoundObjects[i].volume, &SoundObjects[i].pan, SoundObjects[i].max_distance );
541
542         if (!forever || SoundObjects[i].volume >= MIN_VOLUME)
543                digi_start_sound_object(i);
544
545         return SoundObjects[i].signature;
546 }
547
548 int digi_link_sound_to_object( int soundnum, short objnum, int forever, fix max_volume )
549 { return digi_link_sound_to_object2( soundnum, objnum, forever, max_volume, 256*F1_0); }
550
551 int digi_link_sound_to_pos2( int org_soundnum, short segnum, short sidenum, vms_vector * pos, int forever, fix max_volume, fix max_distance )
552 {
553         int i, volume, pan;
554         int soundnum;
555
556         soundnum = digi_xlat_sound(org_soundnum);
557
558         if ( max_volume < 0 ) return -1;
559 //      if ( max_volume > F1_0 ) max_volume = F1_0;
560
561         if (!digi_initialised) return -1;
562         if (soundnum < 0 ) return -1;
563         if (GameSounds[soundnum].data==NULL) {
564                 Int3();
565                 return -1;
566         }
567
568         if ((segnum<0)||(segnum>Highest_segment_index))
569                 return -1;
570
571         if ( !forever ) {
572                 // Hack to keep sounds from building up...
573                 digi_get_sound_loc( &Viewer->orient, &Viewer->pos, Viewer->segnum, pos, segnum, max_volume, &volume, &pan, max_distance );
574                 digi_play_sample_3d( org_soundnum, pan, volume, 0 );
575                 return -1;
576         }
577
578         for (i=0; i<MAX_SOUND_OBJECTS; i++ )
579                 if (SoundObjects[i].flags==0)
580                         break;
581         
582         if (i==MAX_SOUND_OBJECTS) {
583                 mprintf((1, "Too many sound objects!\n" ));
584                 return -1;
585         }
586
587
588         SoundObjects[i].signature=next_signature++;
589         SoundObjects[i].flags = SOF_USED | SOF_LINK_TO_POS;
590         if ( forever )
591                 SoundObjects[i].flags |= SOF_PLAY_FOREVER;
592         SoundObjects[i].lp_segnum = segnum;
593         SoundObjects[i].lp_sidenum = sidenum;
594         SoundObjects[i].lp_position = *pos;
595         SoundObjects[i].soundnum = soundnum;
596         SoundObjects[i].max_volume = max_volume;
597         SoundObjects[i].max_distance = max_distance;
598         SoundObjects[i].volume = 0;
599         SoundObjects[i].pan = 0;
600         digi_get_sound_loc( &Viewer->orient, &Viewer->pos, Viewer->segnum, 
601                                            &SoundObjects[i].lp_position, SoundObjects[i].lp_segnum,
602                                            SoundObjects[i].max_volume,
603                        &SoundObjects[i].volume, &SoundObjects[i].pan, SoundObjects[i].max_distance );
604         
605         if (!forever || SoundObjects[i].volume >= MIN_VOLUME)
606                 digi_start_sound_object(i);
607
608         return SoundObjects[i].signature;
609 }
610
611 int digi_link_sound_to_pos( int soundnum, short segnum, short sidenum, vms_vector * pos, int forever, fix max_volume )
612 {
613         return digi_link_sound_to_pos2( soundnum, segnum, sidenum, pos, forever, max_volume, F1_0 * 256 );
614 }
615
616 void digi_kill_sound_linked_to_segment( int segnum, int sidenum, int soundnum )
617 {
618         int i,killed;
619
620         soundnum = digi_xlat_sound(soundnum);
621
622         if (!digi_initialised) return;
623
624         killed = 0;
625
626         for (i=0; i<MAX_SOUND_OBJECTS; i++ )    {
627                 if ( (SoundObjects[i].flags & SOF_USED) && (SoundObjects[i].flags & SOF_LINK_TO_POS) )  {
628                         if ((SoundObjects[i].lp_segnum == segnum) && (SoundObjects[i].soundnum==soundnum ) && (SoundObjects[i].lp_sidenum==sidenum) ) {
629                                 if ( SoundObjects[i].flags & SOF_PLAYING )      {
630                                         SoundSlots[SoundObjects[i].handle].playing = 0;
631                                          if (SoundSlots[SoundObjects[i].handle].lpsb) {
632                                            unsigned int s;
633                                            IDirectSoundBuffer_GetStatus(SoundSlots[SoundObjects[i].handle].lpsb, &s);
634                                            if (s & DSBSTATUS_PLAYING) IDirectSoundBuffer_Stop(SoundSlots[SoundObjects[i].handle].lpsb);
635                                            IDirectSoundBuffer_Release(SoundSlots[SoundObjects[i].handle].lpsb);
636                                            SoundSlots[SoundObjects[i].handle].playing = 0;
637                                            SoundSlots[SoundObjects[i].handle].lpsb = NULL;
638                                          }
639                                 }
640                                 SoundObjects[i].flags = 0;      // Mark as dead, so some other sound can use this sound
641                                 killed++;
642                         }
643                 }
644         }
645         // If this assert happens, it means that there were 2 sounds
646         // that got deleted. Weird, get John.
647         if ( killed > 1 )       {
648                 mprintf( (1, "ERROR: More than 1 sounds were deleted from seg %d\n", segnum ));
649         }
650 }
651
652 void digi_kill_sound_linked_to_object( int objnum )
653 {
654         int i,killed;
655
656         if (!digi_initialised) return;
657
658         killed = 0;
659
660         for (i=0; i<MAX_SOUND_OBJECTS; i++ )    {
661                 if ( (SoundObjects[i].flags & SOF_USED) && (SoundObjects[i].flags & SOF_LINK_TO_OBJ ) ) {
662                         if (SoundObjects[i].lo_objnum == objnum)   {
663                                 if ( SoundObjects[i].flags & SOF_PLAYING )      {
664                                      SoundSlots[SoundObjects[i].handle].playing = 0;
665                                          if (SoundSlots[SoundObjects[i].handle].lpsb) {
666                                            unsigned int s;
667                                            IDirectSoundBuffer_GetStatus(SoundSlots[SoundObjects[i].handle].lpsb, &s);
668                                            if (s & DSBSTATUS_PLAYING) IDirectSoundBuffer_Stop(SoundSlots[SoundObjects[i].handle].lpsb);
669                                            IDirectSoundBuffer_Release(SoundSlots[SoundObjects[i].handle].lpsb);
670                                            SoundSlots[SoundObjects[i].handle].playing = 0;
671                                            SoundSlots[SoundObjects[i].handle].lpsb = NULL;
672                                          }
673                                 }
674                                 SoundObjects[i].flags = 0;      // Mark as dead, so some other sound can use this sound
675                                 killed++;
676                         }
677                 }
678         }
679         // If this assert happens, it means that there were 2 sounds
680         // that got deleted. Weird, get John.
681         if ( killed > 1 )       {
682                 mprintf( (1, "ERROR: More than 1 sounds were deleted from object %d\n", objnum ));
683         }
684 }
685
686 void digi_sync_sounds()
687 {
688         int i;
689         int oldvolume, oldpan;
690
691         if (!digi_initialised) return;
692
693         for (i=0; i<MAX_SOUND_OBJECTS; i++ )    {
694                 if ( SoundObjects[i].flags & SOF_USED ) {
695                         oldvolume = SoundObjects[i].volume;
696                         oldpan = SoundObjects[i].pan;
697
698                         if ( !(SoundObjects[i].flags & SOF_PLAY_FOREVER) )      {
699                                 // Check if its done.
700                                 if (SoundObjects[i].flags & SOF_PLAYING) {
701                                         if (!SoundSlots[SoundObjects[i].handle].playing) {
702                                                 SoundObjects[i].flags = 0;      // Mark as dead, so some other sound can use this sound
703                                                 continue;               // Go on to next sound...
704                                         }
705                                 }
706                         }                       
707                 
708                         if ( SoundObjects[i].flags & SOF_LINK_TO_POS )  {
709                                 digi_get_sound_loc( &Viewer->orient, &Viewer->pos, Viewer->segnum, 
710                                                                 &SoundObjects[i].lp_position, SoundObjects[i].lp_segnum,
711                                                                 SoundObjects[i].max_volume,
712                                 &SoundObjects[i].volume, &SoundObjects[i].pan, SoundObjects[i].max_distance );
713
714                         } else if ( SoundObjects[i].flags & SOF_LINK_TO_OBJ )   {
715                                 object * objp;
716         
717                                 objp = &Objects[SoundObjects[i].lo_objnum];
718                 
719                                 if ((objp->type==OBJ_NONE) || (objp->signature!=SoundObjects[i].lo_objsignature))  {
720                                         // The object that this is linked to is dead, so just end this sound if it is looping.
721                                         if ( (SoundObjects[i].flags & SOF_PLAYING)  && (SoundObjects[i].flags & SOF_PLAY_FOREVER))      {
722                                              SoundSlots[SoundObjects[i].handle].playing = 0;
723                                              if (SoundSlots[SoundObjects[i].handle].lpsb) {
724                                                unsigned int s;
725                                                IDirectSoundBuffer_GetStatus(SoundSlots[SoundObjects[i].handle].lpsb, &s);
726                                                if (s & DSBSTATUS_PLAYING) IDirectSoundBuffer_Stop(SoundSlots[SoundObjects[i].handle].lpsb);
727                                                IDirectSoundBuffer_Release(SoundSlots[SoundObjects[i].handle].lpsb);
728                                                SoundSlots[SoundObjects[i].handle].playing = 0;
729                                                SoundSlots[SoundObjects[i].handle].lpsb = NULL;
730                                              }
731                                         }
732                                         SoundObjects[i].flags = 0;      // Mark as dead, so some other sound can use this sound
733                                         continue;               // Go on to next sound...
734                                 } else {
735                                         digi_get_sound_loc( &Viewer->orient, &Viewer->pos, Viewer->segnum, 
736                                         &objp->pos, objp->segnum, SoundObjects[i].max_volume,
737                                    &SoundObjects[i].volume, &SoundObjects[i].pan, SoundObjects[i].max_distance );
738                                 }
739                         }
740                          
741                         if (oldvolume != SoundObjects[i].volume)        {
742                                 if ( SoundObjects[i].volume < MIN_VOLUME )       {
743                                         // Sound is too far away, so stop it from playing.
744                                         if ((SoundObjects[i].flags & SOF_PLAYING)&&(SoundObjects[i].flags & SOF_PLAY_FOREVER))  {
745                                                 SoundSlots[SoundObjects[i].handle].playing = 0;
746                                                 if (SoundSlots[SoundObjects[i].handle].lpsb) {
747                                                   unsigned int s;
748                                                   IDirectSoundBuffer_GetStatus(SoundSlots[SoundObjects[i].handle].lpsb, &s);
749                                                   if (s & DSBSTATUS_PLAYING) IDirectSoundBuffer_Stop(SoundSlots[SoundObjects[i].handle].lpsb);
750                                                   IDirectSoundBuffer_Release(SoundSlots[SoundObjects[i].handle].lpsb);
751                                                   SoundSlots[SoundObjects[i].handle].playing = 0;
752                                                   SoundSlots[SoundObjects[i].handle].lpsb = NULL;
753                                                 }
754                                                 SoundObjects[i].flags &= ~SOF_PLAYING;          // Mark sound as not playing
755                                         }
756                                 } else {
757                                         if (!(SoundObjects[i].flags & SOF_PLAYING))     {
758                                                 digi_start_sound_object(i);
759                                         } else {
760                                                 SoundSlots[SoundObjects[i].handle].volume = fixmuldiv(SoundObjects[i].volume,digi_volume,F1_0);
761                                         }
762                                 }
763                         }
764                                 
765                         if (oldpan != SoundObjects[i].pan)      {
766                                 if (SoundObjects[i].flags & SOF_PLAYING)
767                                         SoundSlots[SoundObjects[i].handle].pan = SoundObjects[i].pan;
768                         }
769                 }
770         }
771 }
772
773 void digi_init_sounds()
774 {
775         int i;
776
777         if (!digi_initialised) return;
778
779         digi_reset_digi_sounds();
780
781         for (i=0; i<MAX_SOUND_OBJECTS; i++ )    {
782                 if (digi_sounds_initialized) {
783                         if ( SoundObjects[i].flags & SOF_PLAYING )      {
784                                 SoundSlots[SoundObjects[i].handle].playing=0;
785                                     if (SoundSlots[SoundObjects[i].handle].lpsb) {
786                                            unsigned int s;
787                                            IDirectSoundBuffer_GetStatus(SoundSlots[SoundObjects[i].handle].lpsb, &s);
788                                            if (s & DSBSTATUS_PLAYING) IDirectSoundBuffer_Stop(SoundSlots[SoundObjects[i].handle].lpsb);
789                                            IDirectSoundBuffer_Release(SoundSlots[SoundObjects[i].handle].lpsb);
790                                            SoundSlots[SoundObjects[i].handle].playing = 0;
791                                            SoundSlots[SoundObjects[i].handle].lpsb = NULL;
792                                     }
793                         }
794                 }
795                 SoundObjects[i].flags = 0;      // Mark as dead, so some other sound can use this sound
796         }
797         digi_sounds_initialized = 1;
798 }
799
800 //added on 980905 by adb from original source to make sfx volume work
801 void digi_set_digi_volume( int dvolume )
802 {
803         dvolume = fixmuldiv( dvolume, SOUND_MAX_VOLUME, 0x7fff);
804         if ( dvolume > SOUND_MAX_VOLUME )
805                 digi_volume = SOUND_MAX_VOLUME;
806         else if ( dvolume < 0 )
807                 digi_volume = 0;
808         else
809                 digi_volume = dvolume;
810
811         if ( !digi_initialised ) return;
812
813         digi_sync_sounds();
814 }
815 //end edit by adb
816
817 void digi_set_volume( int dvolume, int mvolume ) 
818
819         digi_set_digi_volume(dvolume);
820         digi_set_midi_volume(mvolume);
821 }
822
823 int digi_is_sound_playing(int soundno)
824 {
825         int i;
826
827         soundno = digi_xlat_sound(soundno);
828
829         for (i = 0; i < MAX_SOUND_SLOTS; i++)
830                   //changed on 980905 by adb: added SoundSlots[i].playing &&
831                   if (SoundSlots[i].playing && SoundSlots[i].soundno == soundno)
832                   //end changes by adb
833                         return 1;
834         return 0;
835 }
836
837
838 void digi_pause_all() { }
839 void digi_resume_all() { }
840 void digi_stop_all() { }
841
842  //added on 980905 by adb to make sound channel setting work
843 void digi_set_max_channels(int n) { 
844         digi_max_channels       = n;
845
846         if ( digi_max_channels < 1 ) 
847                 digi_max_channels = 1;
848         if ( digi_max_channels > (MAX_SOUND_SLOTS-MAX_SOUND_OBJECTS) ) 
849                 digi_max_channels = (MAX_SOUND_SLOTS-MAX_SOUND_OBJECTS);
850
851         if ( !digi_initialised ) return;
852
853         digi_reset_digi_sounds();
854 }
855
856 int digi_get_max_channels() { 
857         return digi_max_channels; 
858 }
859 // end edit by adb
860
861 void digi_reset_digi_sounds() {
862  int i;
863  unsigned int s;
864
865  for (i=0; i< MAX_SOUND_SLOTS; i++) {
866   SoundSlots[i].playing=0;
867   if (SoundSlots[i].lpsb) {
868    IDirectSoundBuffer_GetStatus(SoundSlots[i].lpsb, &s);
869    if (s & DSBSTATUS_PLAYING) IDirectSoundBuffer_Stop(SoundSlots[i].lpsb);
870    IDirectSoundBuffer_Release(SoundSlots[i].lpsb);
871    SoundSlots[i].playing = 0;
872    SoundSlots[i].lpsb = NULL;
873   }
874
875  }
876  
877  //added on 980905 by adb to reset sound kill system
878  memset(SampleHandles, 255, sizeof(SampleHandles));
879  next_handle = 0;
880  //end edit by adb
881 }
882 #else
883 int digi_lomem = 0;
884 int digi_midi_song_playing = 0;
885 static int digi_initialised = 0;
886 int midi_volume = 255;
887
888 int digi_get_settings() { return 0; }
889 int digi_init() { digi_initialised = 1; return 0; }
890 void digi_reset() {}
891 void digi_close() {}
892
893 void digi_play_sample( int sndnum, fix max_volume ) {}
894 void digi_play_sample_once( int sndnum, fix max_volume ) {}
895 int digi_link_sound_to_object( int soundnum, short objnum, int forever, fix max_volume ) { return 0; }
896 int digi_link_sound_to_pos( int soundnum, short segnum, short sidenum, vms_vector * pos, int forever, fix max_volume ) { return 0; }
897 // Same as above, but you pass the max distance sound can be heard.  The old way uses f1_0*256 for max_distance.
898 int digi_link_sound_to_object2( int soundnum, short objnum, int forever, fix max_volume, fix  max_distance ) { return 0; }
899 int digi_link_sound_to_pos2( int soundnum, short segnum, short sidenum, vms_vector * pos, int forever, fix max_volume, fix max_distance ) { return 0; }
900
901 void digi_play_sample_3d( int soundno, int angle, int volume, int no_dups ) {} // Volume from 0-0x7fff
902
903 void digi_init_sounds() {}
904 void digi_sync_sounds() {}
905 void digi_kill_sound_linked_to_segment( int segnum, int sidenum, int soundnum ) {}
906 void digi_kill_sound_linked_to_object( int objnum ) {}
907
908 void digi_set_digi_volume( int dvolume ) {}
909 void digi_set_volume( int dvolume, int mvolume ) {}
910
911 int digi_is_sound_playing(int soundno) { return 0; }
912
913 void digi_pause_all() {}
914 void digi_resume_all() {}
915 void digi_stop_all() {}
916
917 void digi_set_max_channels(int n) {}
918 int digi_get_max_channels() { return 0; }
919
920 #endif
921
922 #ifdef MIDI_SOUND
923 // MIDI stuff follows.
924
925 void digi_stop_current_song()
926 {
927         if ( digi_midi_song_playing ) {
928             hmp_close(hmp);
929             hmp = NULL;
930             digi_midi_song_playing = 0;
931         }
932 }
933
934 void digi_set_midi_volume( int n )
935 {
936         int mm_volume;
937
938         if (n < 0)
939                 midi_volume = 0;
940         else if (n > 127)
941                 midi_volume = 127;
942         else
943                 midi_volume = n;
944
945         // scale up from 0-127 to 0-0xffff
946         mm_volume = (midi_volume << 1) | (midi_volume & 1);
947         mm_volume |= (mm_volume << 8);
948
949         if (hmp)
950                 midiOutSetVolume((HMIDIOUT)hmp->hmidi, mm_volume | mm_volume << 16);
951 }
952
953 void digi_play_midi_song( char * filename, char * melodic_bank, char * drum_bank, int loop )
954 {       
955         if (!digi_initialised) return;
956
957         digi_stop_current_song();
958
959        //added on 5/20/99 by Victor Rachels to fix crash/etc
960         if(filename == NULL) return;
961         if(midi_volume < 1) return;
962        //end this section addition - VR
963
964         if ((hmp = hmp_open(filename))) {
965             hmp_play(hmp);
966             digi_midi_song_playing = 1;
967             digi_set_midi_volume(midi_volume);
968         }
969         else
970                 printf("hmp_open failed\n");
971 }
972 #else
973 void digi_stop_current_song() {}
974 void digi_set_midi_volume( int n ) {}
975 void digi_play_midi_song( char * filename, char * melodic_bank, char * drum_bank, int loop ) {}
976 #endif