7 #define WIN32_LEAN_AND_MEAN
18 #include "gr.h" // needed for piggy.h
29 #define MAX_SOUND_SLOTS 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
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
50 short segnum; // Used if SOF_LINK_TO_POS field is used
55 short objnum; // Used if SOF_LINK_TO_OBJ field is used
60 #define lp_segnum link.pos.segnum
61 #define lp_sidenum link.pos.sidenum
62 #define lp_position link.pos.position
64 #define lo_objnum link.obj.objnum
65 #define lo_objsignature link.obj.objsignature
67 #define MAX_SOUND_OBJECTS 16
68 sound_object SoundObjects[MAX_SOUND_OBJECTS];
69 short next_signature=0;
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;
78 WAVEFORMATEX waveformat;
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;
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];
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;
104 static int digi_initialised = 0;
105 static int digi_atexit_initialised=0;
107 static int digi_sounds_initialized = 0;
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);
116 void digi_reset_digi_sounds(void);
118 void digi_reset() { }
120 void digi_close(void) {
122 IDirectSound_Release(lpds);
123 digi_initialised = 0;
126 /* Initialise audio devices. */
131 if (!digi_initialised && g_hWnd){
133 memset(&waveformat, 0, sizeof(waveformat));
134 waveformat.wFormatTag=WAVE_FORMAT_PCM;
135 waveformat.wBitsPerSample=8;
136 waveformat.nChannels = 1;
137 waveformat.nSamplesPerSec = 11025;
138 waveformat.nBlockAlign =
139 waveformat.nChannels * (waveformat.wBitsPerSample/8);
140 waveformat.nAvgBytesPerSec =
141 waveformat.nSamplesPerSec * waveformat.nBlockAlign;
143 if ((hr = DirectSoundCreate(NULL, &lpds, NULL)) != DS_OK)
146 if ((hr = IDirectSound_SetCooperativeLevel(lpds, g_hWnd, DSSCL_PRIORITY)) //hWndMain
149 IDirectSound_Release(lpds);
153 memset(&dsbd, 0, sizeof(dsbd));
154 dsbd.dwSize = sizeof(dsbd);
155 dsbd.dwFlags = DSBCAPS_CTRLDEFAULT | DSBCAPS_GETCURRENTPOSITION2;
156 dsbd.dwBufferBytes = 8192;
158 dsbd.lpwfxFormat = &waveformat;
160 digi_initialised = 1;
163 if (!digi_atexit_initialised){
165 digi_atexit_initialised=1;
170 /* Find the sound which actually equates to a sound number */
171 int digi_xlat_sound(int soundno)
173 if ( soundno < 0 ) return -1;
176 soundno = AltSounds[soundno];
177 if ( soundno == 255 ) return -1;
179 return Sounds[soundno];
182 static int get_free_slot()
186 for (i=0; i<MAX_SOUND_SLOTS; i++)
188 if (!SoundSlots[i].playing) return i;
189 if (SoundSlots[i].lpsb) {
190 IDirectSoundBuffer_GetStatus(SoundSlots[i].lpsb, &s);
191 if (!(s & DSBSTATUS_PLAYING)) IDirectSoundBuffer_Release(SoundSlots[i].lpsb);
192 SoundSlots[i].playing = 0;
193 SoundSlots[i].lpsb = NULL;
200 int D1vol2DSvol(fix d1v){
201 //multiplying by 1.5 doesn't help. DirectSound uses dB for volume, rather than a linear scale like d1 wants.
202 //I had to pull out a math book, but here is the code to fix it :) -Matt Mueller
208 // return log2(f2fl(d1v))*1000;//no log2? hm.
209 return log(f2fl(d1v))/log(2)*1000.0;
212 int digi_start_sound(int soundnum, fix volume, fix pan)
218 if (!digi_initialised) return -1;
220 //added on 980905 by adb from original source to add sound kill system
221 // play at most digi_max_channel samples, if possible kill sample with low volume
225 if ( (SampleHandles[next_handle] >= 0) && (SoundSlots[SampleHandles[next_handle]].playing) )
227 if ( (SoundSlots[SampleHandles[next_handle]].volume > digi_volume) && (ntries<digi_max_channels) )
229 //mprintf(( 0, "Not stopping loud sound %d.\n", next_handle ));
231 if ( next_handle >= digi_max_channels )
236 //mprintf(( 0, "[SS:%d]", next_handle ));
237 SoundSlots[SampleHandles[next_handle]].playing = 0;
238 SampleHandles[next_handle] = -1;
242 slot = get_free_slot();
243 if (slot<0) return -1;
245 SoundSlots[slot].soundno = soundnum;
246 SoundSlots[slot].samples = GameSounds[soundnum].data;
247 SoundSlots[slot].length = GameSounds[soundnum].length;
248 SoundSlots[slot].volume = fixmul(digi_volume, volume);
249 SoundSlots[slot].pan = pan;
250 SoundSlots[slot].position = 0;
251 SoundSlots[slot].looped = 0;
252 SoundSlots[slot].playing = 1;
254 memset(&waveformat, 0, sizeof(waveformat));
255 waveformat.wFormatTag=WAVE_FORMAT_PCM;
256 waveformat.wBitsPerSample=8;
257 waveformat.nChannels = 1;
258 waveformat.nSamplesPerSec = 11025;
259 waveformat.nBlockAlign =
260 waveformat.nChannels * (waveformat.wBitsPerSample/8);
261 waveformat.nAvgBytesPerSec =
262 waveformat.nSamplesPerSec * waveformat.nBlockAlign;
264 memset(&dsbd, 0, sizeof(dsbd));
265 dsbd.dwSize = sizeof(dsbd);
266 dsbd.dwFlags = DSBCAPS_CTRLDEFAULT | DSBCAPS_GETCURRENTPOSITION2;
268 dsbd.dwBufferBytes = SoundSlots[slot].length;
269 dsbd.lpwfxFormat = &waveformat;
271 hr = IDirectSound_CreateSoundBuffer(lpds, &dsbd, &SoundSlots[slot].lpsb, NULL);
273 printf("Createsoundbuffer failed! hr=0x%X\n", (int)hr);
279 IDirectSoundBuffer_Lock(SoundSlots[slot].lpsb, 0, GameSounds[soundnum].length,
280 (void **)&ptr1, &len1, (void **)&ptr2, &len2, 0);
281 memcpy(ptr1,GameSounds[soundnum].data, MIN(len1, GameSounds[soundnum].length));
282 IDirectSoundBuffer_Unlock(SoundSlots[slot].lpsb, ptr1, len1, ptr2, len2);
284 IDirectSoundBuffer_SetPan(SoundSlots[slot].lpsb, ((int)(f2fl(pan) * 20000.0))-10000);
285 // IDirectSoundBuffer_SetVolume(SoundSlots[slot].lpsb, MIN(((int)(f2fl(SoundSlots[slot].volume) * 15000.0)) - 10000, 0));//nope
286 IDirectSoundBuffer_SetVolume(SoundSlots[slot].lpsb, D1vol2DSvol(SoundSlots[slot].volume));
288 IDirectSoundBuffer_Play(SoundSlots[slot].lpsb, 0, 0, 0);
290 //added on 980905 by adb to add sound kill system from original sos digi.c
291 reset_sounds_on_channel(slot);
292 SampleHandles[next_handle] = slot;
294 if ( next_handle >= digi_max_channels )
301 //added on 980905 by adb to add sound kill system from original sos digi.c
302 void reset_sounds_on_channel( int channel )
306 for (i=0; i<digi_max_channels; i++)
307 if (SampleHandles[i] == channel)
308 SampleHandles[i] = -1;
312 int digi_start_sound_object(int obj)
317 if (!digi_initialised) return -1;
318 slot = get_free_slot();
320 if (slot<0) return -1;
323 SoundSlots[slot].soundno = SoundObjects[obj].soundnum;
324 SoundSlots[slot].samples = GameSounds[SoundObjects[obj].soundnum].data;
325 SoundSlots[slot].length = GameSounds[SoundObjects[obj].soundnum].length;
326 SoundSlots[slot].volume = fixmul(digi_volume, SoundObjects[obj].volume);
327 SoundSlots[slot].pan = SoundObjects[obj].pan;
328 SoundSlots[slot].position = 0;
329 SoundSlots[slot].looped = (SoundObjects[obj].flags & SOF_PLAY_FOREVER);
330 SoundSlots[slot].playing = 1;
332 memset(&waveformat, 0, sizeof(waveformat));
333 waveformat.wFormatTag=WAVE_FORMAT_PCM;
334 waveformat.wBitsPerSample=8;
335 waveformat.nChannels = 1;
336 waveformat.nSamplesPerSec = 11025;
337 waveformat.nBlockAlign =
338 waveformat.nChannels * (waveformat.wBitsPerSample/8);
339 waveformat.nAvgBytesPerSec =
340 waveformat.nSamplesPerSec * waveformat.nBlockAlign;
342 memset(&dsbd, 0, sizeof(dsbd));
343 dsbd.dwSize = sizeof(dsbd);
344 dsbd.dwFlags = DSBCAPS_CTRLDEFAULT | DSBCAPS_GETCURRENTPOSITION2;
346 dsbd.dwBufferBytes = SoundSlots[slot].length;
347 dsbd.lpwfxFormat = &waveformat;
349 hr = IDirectSound_CreateSoundBuffer(lpds, &dsbd, &SoundSlots[slot].lpsb, NULL);
356 IDirectSoundBuffer_Lock(SoundSlots[slot].lpsb, 0, SoundSlots[slot].length,
357 (void **)&ptr1, &len1, (void **)&ptr2, &len2, 0);
358 memcpy(ptr1, SoundSlots[slot].samples, MIN(len1,(int)SoundSlots[slot].length));
359 IDirectSoundBuffer_Unlock(SoundSlots[slot].lpsb, (void *)ptr1, len1, (void *)ptr2, len2);
361 IDirectSoundBuffer_SetPan(SoundSlots[slot].lpsb, ((int)(f2fl(SoundSlots[slot].pan) * 20000))-10000);
362 // IDirectSoundBuffer_SetVolume(SoundSlots[slot].lpsb, MIN(((int)(f2fl(SoundSlots[slot].volume) * 15000.0)) - 10000, 0));//nope
363 IDirectSoundBuffer_SetVolume(SoundSlots[slot].lpsb,D1vol2DSvol(SoundSlots[slot].volume));
364 IDirectSoundBuffer_Play(SoundSlots[slot].lpsb, 0, 0, SoundSlots[slot].looped?DSBPLAY_LOOPING:0);
366 SoundObjects[obj].signature = next_signature++;
367 SoundObjects[obj].handle = slot;
369 SoundObjects[obj].flags |= SOF_PLAYING;
370 //added on 980905 by adb to add sound kill system from original sos digi.c
371 reset_sounds_on_channel(slot);
378 // Play the given sound number.
379 // Volume is max at F1_0.
380 void digi_play_sample( int soundno, fix max_volume )
383 if ( Newdemo_state == ND_STATE_RECORDING )
384 newdemo_record_sound( soundno );
386 soundno = digi_xlat_sound(soundno);
388 if (!digi_initialised) return;
390 if (soundno < 0 ) return;
392 digi_start_sound(soundno, max_volume, F0_5);
395 // Play the given sound number. If the sound is already playing,
397 void digi_play_sample_once( int soundno, fix max_volume )
402 if ( Newdemo_state == ND_STATE_RECORDING )
403 newdemo_record_sound( soundno );
405 soundno = digi_xlat_sound(soundno);
407 if (!digi_initialised) return;
409 if (soundno < 0 ) return;
411 for (i=0; i < MAX_SOUND_SLOTS; i++)
412 if (SoundSlots[i].soundno == soundno) {
413 SoundSlots[i].playing = 0;
414 if (SoundSlots[i].lpsb) {
416 IDirectSoundBuffer_GetStatus(SoundSlots[i].lpsb, &s);
417 if (s & DSBSTATUS_PLAYING) IDirectSoundBuffer_Stop(SoundSlots[i].lpsb);
418 IDirectSoundBuffer_Release(SoundSlots[i].lpsb);
419 SoundSlots[i].playing = 0;
420 SoundSlots[i].lpsb = NULL;
423 digi_start_sound(soundno, max_volume, F0_5);
427 void digi_play_sample_3d( int soundno, int angle, int volume, int no_dups ) // Volume from 0-0x7fff
432 if ( Newdemo_state == ND_STATE_RECORDING ) {
434 newdemo_record_sound_3d_once( soundno, angle, volume );
436 newdemo_record_sound_3d( soundno, angle, volume );
439 soundno = digi_xlat_sound(soundno);
441 if (!digi_initialised) return;
442 if (soundno < 0 ) return;
444 if (volume < MIN_VOLUME ) return;
445 digi_start_sound(soundno, volume, angle);
448 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 )
450 vms_vector vector_to_sound;
451 fix angle_from_ear, cosang,sinang;
458 max_distance = (max_distance*5)/4; // Make all sounds travel 1.25 times as far.
460 // Warning: Made the vm_vec_normalized_dir be vm_vec_normalized_dir_quick and got illegal values to acos in the fang computation.
461 distance = vm_vec_normalized_dir_quick( &vector_to_sound, sound_pos, listener_pos );
463 if (distance < max_distance ) {
464 int num_search_segs = f2i(max_distance/20);
465 if ( num_search_segs < 1 ) num_search_segs = 1;
467 path_distance = find_connected_distance(listener_pos, listener_seg, sound_pos, sound_seg, num_search_segs, WID_RENDPAST_FLAG );
468 if ( path_distance > -1 ) {
469 *volume = max_volume - fixdiv(path_distance,max_distance);
470 //mprintf( (0, "Sound path distance %.2f, volume is %d / %d\n", f2fl(distance), *volume, max_volume ));
472 angle_from_ear = vm_vec_delta_ang_norm(&listener->rvec,&vector_to_sound,&listener->uvec);
473 fix_sincos(angle_from_ear,&sinang,&cosang);
474 //mprintf( (0, "volume is %.2f\n", f2fl(*volume) ));
475 if (Config_channels_reversed) cosang *= -1;
476 *pan = (cosang + F1_0)/2;
484 int digi_link_sound_to_object2( int org_soundnum, short objnum, int forever, fix max_volume, fix max_distance )
490 soundnum = digi_xlat_sound(org_soundnum);
492 if ( max_volume < 0 ) return -1;
493 // if ( max_volume > F1_0 ) max_volume = F1_0;
495 if (!digi_initialised) return -1;
496 if (soundnum < 0 ) return -1;
497 if (GameSounds[soundnum].data==NULL) {
501 if ((objnum<0)||(objnum>Highest_object_index))
505 // Hack to keep sounds from building up...
506 digi_get_sound_loc( &Viewer->orient, &Viewer->pos, Viewer->segnum, &Objects[objnum].pos, Objects[objnum].segnum, max_volume,&volume, &pan, max_distance );
507 digi_play_sample_3d( org_soundnum, pan, volume, 0 );
511 for (i=0; i<MAX_SOUND_OBJECTS; i++ )
512 if (SoundObjects[i].flags==0)
515 if (i==MAX_SOUND_OBJECTS) {
516 mprintf((1, "Too many sound objects!\n" ));
520 SoundObjects[i].signature=next_signature++;
521 SoundObjects[i].flags = SOF_USED | SOF_LINK_TO_OBJ;
523 SoundObjects[i].flags |= SOF_PLAY_FOREVER;
524 SoundObjects[i].lo_objnum = objnum;
525 SoundObjects[i].lo_objsignature = Objects[objnum].signature;
526 SoundObjects[i].max_volume = max_volume;
527 SoundObjects[i].max_distance = max_distance;
528 SoundObjects[i].volume = 0;
529 SoundObjects[i].pan = 0;
530 SoundObjects[i].soundnum = soundnum;
532 objp = &Objects[SoundObjects[i].lo_objnum];
533 digi_get_sound_loc( &Viewer->orient, &Viewer->pos, Viewer->segnum,
534 &objp->pos, objp->segnum, SoundObjects[i].max_volume,
535 &SoundObjects[i].volume, &SoundObjects[i].pan, SoundObjects[i].max_distance );
537 if (!forever || SoundObjects[i].volume >= MIN_VOLUME)
538 digi_start_sound_object(i);
540 return SoundObjects[i].signature;
543 int digi_link_sound_to_object( int soundnum, short objnum, int forever, fix max_volume )
544 { return digi_link_sound_to_object2( soundnum, objnum, forever, max_volume, 256*F1_0); }
546 int digi_link_sound_to_pos2( int org_soundnum, short segnum, short sidenum, vms_vector * pos, int forever, fix max_volume, fix max_distance )
551 soundnum = digi_xlat_sound(org_soundnum);
553 if ( max_volume < 0 ) return -1;
554 // if ( max_volume > F1_0 ) max_volume = F1_0;
556 if (!digi_initialised) return -1;
557 if (soundnum < 0 ) return -1;
558 if (GameSounds[soundnum].data==NULL) {
563 if ((segnum<0)||(segnum>Highest_segment_index))
567 // Hack to keep sounds from building up...
568 digi_get_sound_loc( &Viewer->orient, &Viewer->pos, Viewer->segnum, pos, segnum, max_volume, &volume, &pan, max_distance );
569 digi_play_sample_3d( org_soundnum, pan, volume, 0 );
573 for (i=0; i<MAX_SOUND_OBJECTS; i++ )
574 if (SoundObjects[i].flags==0)
577 if (i==MAX_SOUND_OBJECTS) {
578 mprintf((1, "Too many sound objects!\n" ));
583 SoundObjects[i].signature=next_signature++;
584 SoundObjects[i].flags = SOF_USED | SOF_LINK_TO_POS;
586 SoundObjects[i].flags |= SOF_PLAY_FOREVER;
587 SoundObjects[i].lp_segnum = segnum;
588 SoundObjects[i].lp_sidenum = sidenum;
589 SoundObjects[i].lp_position = *pos;
590 SoundObjects[i].soundnum = soundnum;
591 SoundObjects[i].max_volume = max_volume;
592 SoundObjects[i].max_distance = max_distance;
593 SoundObjects[i].volume = 0;
594 SoundObjects[i].pan = 0;
595 digi_get_sound_loc( &Viewer->orient, &Viewer->pos, Viewer->segnum,
596 &SoundObjects[i].lp_position, SoundObjects[i].lp_segnum,
597 SoundObjects[i].max_volume,
598 &SoundObjects[i].volume, &SoundObjects[i].pan, SoundObjects[i].max_distance );
600 if (!forever || SoundObjects[i].volume >= MIN_VOLUME)
601 digi_start_sound_object(i);
603 return SoundObjects[i].signature;
606 int digi_link_sound_to_pos( int soundnum, short segnum, short sidenum, vms_vector * pos, int forever, fix max_volume )
608 return digi_link_sound_to_pos2( soundnum, segnum, sidenum, pos, forever, max_volume, F1_0 * 256 );
611 void digi_kill_sound_linked_to_segment( int segnum, int sidenum, int soundnum )
615 soundnum = digi_xlat_sound(soundnum);
617 if (!digi_initialised) return;
621 for (i=0; i<MAX_SOUND_OBJECTS; i++ ) {
622 if ( (SoundObjects[i].flags & SOF_USED) && (SoundObjects[i].flags & SOF_LINK_TO_POS) ) {
623 if ((SoundObjects[i].lp_segnum == segnum) && (SoundObjects[i].soundnum==soundnum ) && (SoundObjects[i].lp_sidenum==sidenum) ) {
624 if ( SoundObjects[i].flags & SOF_PLAYING ) {
625 SoundSlots[SoundObjects[i].handle].playing = 0;
626 if (SoundSlots[SoundObjects[i].handle].lpsb) {
628 IDirectSoundBuffer_GetStatus(SoundSlots[SoundObjects[i].handle].lpsb, &s);
629 if (s & DSBSTATUS_PLAYING) IDirectSoundBuffer_Stop(SoundSlots[SoundObjects[i].handle].lpsb);
630 IDirectSoundBuffer_Release(SoundSlots[SoundObjects[i].handle].lpsb);
631 SoundSlots[SoundObjects[i].handle].playing = 0;
632 SoundSlots[SoundObjects[i].handle].lpsb = NULL;
635 SoundObjects[i].flags = 0; // Mark as dead, so some other sound can use this sound
640 // If this assert happens, it means that there were 2 sounds
641 // that got deleted. Weird, get John.
643 mprintf( (1, "ERROR: More than 1 sounds were deleted from seg %d\n", segnum ));
647 void digi_kill_sound_linked_to_object( int objnum )
651 if (!digi_initialised) return;
655 for (i=0; i<MAX_SOUND_OBJECTS; i++ ) {
656 if ( (SoundObjects[i].flags & SOF_USED) && (SoundObjects[i].flags & SOF_LINK_TO_OBJ ) ) {
657 if (SoundObjects[i].lo_objnum == objnum) {
658 if ( SoundObjects[i].flags & SOF_PLAYING ) {
659 SoundSlots[SoundObjects[i].handle].playing = 0;
660 if (SoundSlots[SoundObjects[i].handle].lpsb) {
662 IDirectSoundBuffer_GetStatus(SoundSlots[SoundObjects[i].handle].lpsb, &s);
663 if (s & DSBSTATUS_PLAYING) IDirectSoundBuffer_Stop(SoundSlots[SoundObjects[i].handle].lpsb);
664 IDirectSoundBuffer_Release(SoundSlots[SoundObjects[i].handle].lpsb);
665 SoundSlots[SoundObjects[i].handle].playing = 0;
666 SoundSlots[SoundObjects[i].handle].lpsb = NULL;
669 SoundObjects[i].flags = 0; // Mark as dead, so some other sound can use this sound
674 // If this assert happens, it means that there were 2 sounds
675 // that got deleted. Weird, get John.
677 mprintf( (1, "ERROR: More than 1 sounds were deleted from object %d\n", objnum ));
681 void digi_sync_sounds()
684 int oldvolume, oldpan;
686 if (!digi_initialised) return;
688 for (i=0; i<MAX_SOUND_OBJECTS; i++ ) {
689 if ( SoundObjects[i].flags & SOF_USED ) {
690 oldvolume = SoundObjects[i].volume;
691 oldpan = SoundObjects[i].pan;
693 if ( !(SoundObjects[i].flags & SOF_PLAY_FOREVER) ) {
694 // Check if its done.
695 if (SoundObjects[i].flags & SOF_PLAYING) {
696 if (!SoundSlots[SoundObjects[i].handle].playing) {
697 SoundObjects[i].flags = 0; // Mark as dead, so some other sound can use this sound
698 continue; // Go on to next sound...
703 if ( SoundObjects[i].flags & SOF_LINK_TO_POS ) {
704 digi_get_sound_loc( &Viewer->orient, &Viewer->pos, Viewer->segnum,
705 &SoundObjects[i].lp_position, SoundObjects[i].lp_segnum,
706 SoundObjects[i].max_volume,
707 &SoundObjects[i].volume, &SoundObjects[i].pan, SoundObjects[i].max_distance );
709 } else if ( SoundObjects[i].flags & SOF_LINK_TO_OBJ ) {
712 objp = &Objects[SoundObjects[i].lo_objnum];
714 if ((objp->type==OBJ_NONE) || (objp->signature!=SoundObjects[i].lo_objsignature)) {
715 // The object that this is linked to is dead, so just end this sound if it is looping.
716 if ( (SoundObjects[i].flags & SOF_PLAYING) && (SoundObjects[i].flags & SOF_PLAY_FOREVER)) {
717 SoundSlots[SoundObjects[i].handle].playing = 0;
718 if (SoundSlots[SoundObjects[i].handle].lpsb) {
720 IDirectSoundBuffer_GetStatus(SoundSlots[SoundObjects[i].handle].lpsb, &s);
721 if (s & DSBSTATUS_PLAYING) IDirectSoundBuffer_Stop(SoundSlots[SoundObjects[i].handle].lpsb);
722 IDirectSoundBuffer_Release(SoundSlots[SoundObjects[i].handle].lpsb);
723 SoundSlots[SoundObjects[i].handle].playing = 0;
724 SoundSlots[SoundObjects[i].handle].lpsb = NULL;
727 SoundObjects[i].flags = 0; // Mark as dead, so some other sound can use this sound
728 continue; // Go on to next sound...
730 digi_get_sound_loc( &Viewer->orient, &Viewer->pos, Viewer->segnum,
731 &objp->pos, objp->segnum, SoundObjects[i].max_volume,
732 &SoundObjects[i].volume, &SoundObjects[i].pan, SoundObjects[i].max_distance );
736 if (oldvolume != SoundObjects[i].volume) {
737 if ( SoundObjects[i].volume < MIN_VOLUME ) {
738 // Sound is too far away, so stop it from playing.
739 if ((SoundObjects[i].flags & SOF_PLAYING)&&(SoundObjects[i].flags & SOF_PLAY_FOREVER)) {
740 SoundSlots[SoundObjects[i].handle].playing = 0;
741 if (SoundSlots[SoundObjects[i].handle].lpsb) {
743 IDirectSoundBuffer_GetStatus(SoundSlots[SoundObjects[i].handle].lpsb, &s);
744 if (s & DSBSTATUS_PLAYING) IDirectSoundBuffer_Stop(SoundSlots[SoundObjects[i].handle].lpsb);
745 IDirectSoundBuffer_Release(SoundSlots[SoundObjects[i].handle].lpsb);
746 SoundSlots[SoundObjects[i].handle].playing = 0;
747 SoundSlots[SoundObjects[i].handle].lpsb = NULL;
749 SoundObjects[i].flags &= ~SOF_PLAYING; // Mark sound as not playing
752 if (!(SoundObjects[i].flags & SOF_PLAYING)) {
753 digi_start_sound_object(i);
755 SoundSlots[SoundObjects[i].handle].volume = fixmuldiv(SoundObjects[i].volume,digi_volume,F1_0);
760 if (oldpan != SoundObjects[i].pan) {
761 if (SoundObjects[i].flags & SOF_PLAYING)
762 SoundSlots[SoundObjects[i].handle].pan = SoundObjects[i].pan;
768 void digi_init_sounds()
772 if (!digi_initialised) return;
774 digi_reset_digi_sounds();
776 for (i=0; i<MAX_SOUND_OBJECTS; i++ ) {
777 if (digi_sounds_initialized) {
778 if ( SoundObjects[i].flags & SOF_PLAYING ) {
779 SoundSlots[SoundObjects[i].handle].playing=0;
780 if (SoundSlots[SoundObjects[i].handle].lpsb) {
782 IDirectSoundBuffer_GetStatus(SoundSlots[SoundObjects[i].handle].lpsb, &s);
783 if (s & DSBSTATUS_PLAYING) IDirectSoundBuffer_Stop(SoundSlots[SoundObjects[i].handle].lpsb);
784 IDirectSoundBuffer_Release(SoundSlots[SoundObjects[i].handle].lpsb);
785 SoundSlots[SoundObjects[i].handle].playing = 0;
786 SoundSlots[SoundObjects[i].handle].lpsb = NULL;
790 SoundObjects[i].flags = 0; // Mark as dead, so some other sound can use this sound
792 digi_sounds_initialized = 1;
795 //added on 980905 by adb from original source to make sfx volume work
796 void digi_set_digi_volume( int dvolume )
798 dvolume = fixmuldiv( dvolume, SOUND_MAX_VOLUME, 0x7fff);
799 if ( dvolume > SOUND_MAX_VOLUME )
800 digi_volume = SOUND_MAX_VOLUME;
801 else if ( dvolume < 0 )
804 digi_volume = dvolume;
806 if ( !digi_initialised ) return;
812 void digi_set_volume( int dvolume, int mvolume )
814 digi_set_digi_volume(dvolume);
815 digi_set_midi_volume(mvolume);
818 int digi_is_sound_playing(int soundno)
822 soundno = digi_xlat_sound(soundno);
824 for (i = 0; i < MAX_SOUND_SLOTS; i++)
825 //changed on 980905 by adb: added SoundSlots[i].playing &&
826 if (SoundSlots[i].playing && SoundSlots[i].soundno == soundno)
833 void digi_pause_all() { }
834 void digi_resume_all() { }
835 void digi_stop_all() { }
837 //added on 980905 by adb to make sound channel setting work
838 void digi_set_max_channels(int n) {
839 digi_max_channels = n;
841 if ( digi_max_channels < 1 )
842 digi_max_channels = 1;
843 if ( digi_max_channels > (MAX_SOUND_SLOTS-MAX_SOUND_OBJECTS) )
844 digi_max_channels = (MAX_SOUND_SLOTS-MAX_SOUND_OBJECTS);
846 if ( !digi_initialised ) return;
848 digi_reset_digi_sounds();
851 int digi_get_max_channels() {
852 return digi_max_channels;
856 void digi_reset_digi_sounds() {
860 for (i=0; i< MAX_SOUND_SLOTS; i++) {
861 SoundSlots[i].playing=0;
862 if (SoundSlots[i].lpsb) {
863 IDirectSoundBuffer_GetStatus(SoundSlots[i].lpsb, &s);
864 if (s & DSBSTATUS_PLAYING) IDirectSoundBuffer_Stop(SoundSlots[i].lpsb);
865 IDirectSoundBuffer_Release(SoundSlots[i].lpsb);
866 SoundSlots[i].playing = 0;
867 SoundSlots[i].lpsb = NULL;
872 //added on 980905 by adb to reset sound kill system
873 memset(SampleHandles, 255, sizeof(SampleHandles));
879 int digi_midi_song_playing = 0;
880 static int digi_initialised = 0;
881 int midi_volume = 255;
883 int digi_get_settings() { return 0; }
884 int digi_init() { digi_initialised = 1; return 0; }
888 void digi_play_sample( int sndnum, fix max_volume ) {}
889 void digi_play_sample_once( int sndnum, fix max_volume ) {}
890 int digi_link_sound_to_object( int soundnum, short objnum, int forever, fix max_volume ) { return 0; }
891 int digi_link_sound_to_pos( int soundnum, short segnum, short sidenum, vms_vector * pos, int forever, fix max_volume ) { return 0; }
892 // Same as above, but you pass the max distance sound can be heard. The old way uses f1_0*256 for max_distance.
893 int digi_link_sound_to_object2( int soundnum, short objnum, int forever, fix max_volume, fix max_distance ) { return 0; }
894 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; }
896 void digi_play_sample_3d( int soundno, int angle, int volume, int no_dups ) {} // Volume from 0-0x7fff
898 void digi_init_sounds() {}
899 void digi_sync_sounds() {}
900 void digi_kill_sound_linked_to_segment( int segnum, int sidenum, int soundnum ) {}
901 void digi_kill_sound_linked_to_object( int objnum ) {}
903 void digi_set_digi_volume( int dvolume ) {}
904 void digi_set_volume( int dvolume, int mvolume ) {}
906 int digi_is_sound_playing(int soundno) { return 0; }
908 void digi_pause_all() {}
909 void digi_resume_all() {}
910 void digi_stop_all() {}
912 void digi_set_max_channels(int n) {}
913 int digi_get_max_channels() { return 0; }
918 // MIDI stuff follows.
920 void digi_stop_current_song()
922 if ( digi_midi_song_playing ) {
925 digi_midi_song_playing = 0;
929 void digi_set_midi_volume( int n )
940 // scale up from 0-127 to 0-0xffff
941 mm_volume = (midi_volume << 1) | (midi_volume & 1);
942 mm_volume |= (mm_volume << 8);
945 midiOutSetVolume((HMIDIOUT)hmp->hmidi, mm_volume | mm_volume << 16);
948 void digi_play_midi_song( char * filename, char * melodic_bank, char * drum_bank, int loop )
950 if (!digi_initialised) return;
952 digi_stop_current_song();
954 //added on 5/20/99 by Victor Rachels to fix crash/etc
955 if(filename == NULL) return;
956 if(midi_volume < 1) return;
957 //end this section addition - VR
959 if ((hmp = hmp_open(filename))) {
961 digi_midi_song_playing = 1;
962 digi_set_midi_volume(midi_volume);
965 printf("hmp_open failed\n");
968 void digi_stop_current_song() {}
969 void digi_set_midi_volume( int n ) {}
970 void digi_play_midi_song( char * filename, char * melodic_bank, char * drum_bank, int loop ) {}