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)
124 digi_reset_digi_sounds();
125 IDirectSound_Release(lpds);
127 digi_initialised = 0;
130 /* Initialise audio devices. */
135 if (!digi_initialised && g_hWnd){
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;
147 if ((hr = DirectSoundCreate(NULL, &lpds, NULL)) != DS_OK)
150 if ((hr = IDirectSound_SetCooperativeLevel(lpds, g_hWnd, DSSCL_PRIORITY)) //hWndMain
153 IDirectSound_Release(lpds);
157 memset(&dsbd, 0, sizeof(dsbd));
158 dsbd.dwSize = sizeof(dsbd);
159 dsbd.dwFlags = DSBCAPS_CTRLDEFAULT | DSBCAPS_GETCURRENTPOSITION2;
160 dsbd.dwBufferBytes = 8192;
162 dsbd.lpwfxFormat = &waveformat;
164 digi_initialised = 1;
167 if (!digi_atexit_initialised){
169 digi_atexit_initialised=1;
174 /* Find the sound which actually equates to a sound number */
175 int digi_xlat_sound(int soundno)
177 if ( soundno < 0 ) return -1;
180 soundno = AltSounds[soundno];
181 if ( soundno == 255 ) return -1;
183 return Sounds[soundno];
186 // added 2000/01/15 Matt Mueller -- remove some duplication (and fix a big memory leak, in the kill=0 one case)
187 static int DS_release_slot(int slot, int kill)
189 if (SoundSlots[slot].lpsb)
193 IDirectSoundBuffer_GetStatus(SoundSlots[slot].lpsb, &s);
194 if (s & DSBSTATUS_PLAYING)
197 IDirectSoundBuffer_Stop(SoundSlots[slot].lpsb);
201 IDirectSoundBuffer_Release(SoundSlots[slot].lpsb);
202 SoundSlots[slot].playing = 0;
203 SoundSlots[slot].lpsb = NULL;
211 static int get_free_slot()
215 for (i=0; i<MAX_SOUND_SLOTS; i++)
217 if (!SoundSlots[i].playing) return i;
218 if (DS_release_slot(i, 0))
224 int D1vol2DSvol(fix d1v){
225 //multiplying by 1.5 doesn't help. DirectSound uses dB for volume, rather than a linear scale like d1 wants.
226 //I had to pull out a math book, but here is the code to fix it :) -Matt Mueller
232 // return log2(f2fl(d1v))*1000;//no log2? hm.
233 return log(f2fl(d1v))/log(2)*1000.0;
236 int digi_start_sound(int soundnum, fix volume, fix pan)
242 if (!digi_initialised) return -1;
244 //added on 980905 by adb from original source to add sound kill system
245 // play at most digi_max_channel samples, if possible kill sample with low volume
249 if ( (SampleHandles[next_handle] >= 0) && (SoundSlots[SampleHandles[next_handle]].playing) )
251 if ( (SoundSlots[SampleHandles[next_handle]].volume > digi_volume) && (ntries<digi_max_channels) )
253 //mprintf(( 0, "Not stopping loud sound %d.\n", next_handle ));
255 if ( next_handle >= digi_max_channels )
260 //mprintf(( 0, "[SS:%d]", next_handle ));
261 SoundSlots[SampleHandles[next_handle]].playing = 0;
262 DS_release_slot(SampleHandles[next_handle], 1);
263 SampleHandles[next_handle] = -1;
267 slot = get_free_slot();
268 if (slot<0) return -1;
270 SoundSlots[slot].soundno = soundnum;
271 SoundSlots[slot].samples = GameSounds[soundnum].data;
272 SoundSlots[slot].length = GameSounds[soundnum].length;
273 SoundSlots[slot].volume = fixmul(digi_volume, volume);
274 SoundSlots[slot].pan = pan;
275 SoundSlots[slot].position = 0;
276 SoundSlots[slot].looped = 0;
277 SoundSlots[slot].playing = 1;
279 memset(&waveformat, 0, sizeof(waveformat));
280 waveformat.wFormatTag=WAVE_FORMAT_PCM;
281 waveformat.wBitsPerSample=8;
282 waveformat.nChannels = 1;
283 waveformat.nSamplesPerSec = digi_sample_rate; //11025;
284 waveformat.nBlockAlign =
285 waveformat.nChannels * (waveformat.wBitsPerSample/8);
286 waveformat.nAvgBytesPerSec =
287 waveformat.nSamplesPerSec * waveformat.nBlockAlign;
289 memset(&dsbd, 0, sizeof(dsbd));
290 dsbd.dwSize = sizeof(dsbd);
291 dsbd.dwFlags = DSBCAPS_CTRLDEFAULT | DSBCAPS_GETCURRENTPOSITION2;
293 dsbd.dwBufferBytes = SoundSlots[slot].length;
294 dsbd.lpwfxFormat = &waveformat;
296 hr = IDirectSound_CreateSoundBuffer(lpds, &dsbd, &SoundSlots[slot].lpsb, NULL);
298 printf("Createsoundbuffer failed! hr=0x%X\n", (int)hr);
304 IDirectSoundBuffer_Lock(SoundSlots[slot].lpsb, 0, GameSounds[soundnum].length,
305 (void **)&ptr1, &len1, (void **)&ptr2, &len2, 0);
306 memcpy(ptr1,GameSounds[soundnum].data, MIN(len1, GameSounds[soundnum].length));
307 IDirectSoundBuffer_Unlock(SoundSlots[slot].lpsb, ptr1, len1, ptr2, len2);
309 IDirectSoundBuffer_SetPan(SoundSlots[slot].lpsb, ((int)(f2fl(pan) * 20000.0))-10000);
310 // IDirectSoundBuffer_SetVolume(SoundSlots[slot].lpsb, MIN(((int)(f2fl(SoundSlots[slot].volume) * 15000.0)) - 10000, 0));//nope
311 IDirectSoundBuffer_SetVolume(SoundSlots[slot].lpsb, D1vol2DSvol(SoundSlots[slot].volume));
313 IDirectSoundBuffer_Play(SoundSlots[slot].lpsb, 0, 0, 0);
315 //added on 980905 by adb to add sound kill system from original sos digi.c
316 reset_sounds_on_channel(slot);
317 SampleHandles[next_handle] = slot;
319 if ( next_handle >= digi_max_channels )
326 //added on 980905 by adb to add sound kill system from original sos digi.c
327 void reset_sounds_on_channel( int channel )
331 for (i=0; i<digi_max_channels; i++)
332 if (SampleHandles[i] == channel)
333 SampleHandles[i] = -1;
337 int digi_start_sound_object(int obj)
342 if (!digi_initialised) return -1;
343 slot = get_free_slot();
345 if (slot<0) return -1;
348 SoundSlots[slot].soundno = SoundObjects[obj].soundnum;
349 SoundSlots[slot].samples = GameSounds[SoundObjects[obj].soundnum].data;
350 SoundSlots[slot].length = GameSounds[SoundObjects[obj].soundnum].length;
351 SoundSlots[slot].volume = fixmul(digi_volume, SoundObjects[obj].volume);
352 SoundSlots[slot].pan = SoundObjects[obj].pan;
353 SoundSlots[slot].position = 0;
354 SoundSlots[slot].looped = (SoundObjects[obj].flags & SOF_PLAY_FOREVER);
355 SoundSlots[slot].playing = 1;
357 memset(&waveformat, 0, sizeof(waveformat));
358 waveformat.wFormatTag=WAVE_FORMAT_PCM;
359 waveformat.wBitsPerSample=8;
360 waveformat.nChannels = 1;
361 waveformat.nSamplesPerSec = digi_sample_rate; // 11025;
362 waveformat.nBlockAlign =
363 waveformat.nChannels * (waveformat.wBitsPerSample/8);
364 waveformat.nAvgBytesPerSec =
365 waveformat.nSamplesPerSec * waveformat.nBlockAlign;
367 memset(&dsbd, 0, sizeof(dsbd));
368 dsbd.dwSize = sizeof(dsbd);
369 dsbd.dwFlags = DSBCAPS_CTRLDEFAULT | DSBCAPS_GETCURRENTPOSITION2;
371 dsbd.dwBufferBytes = SoundSlots[slot].length;
372 dsbd.lpwfxFormat = &waveformat;
374 hr = IDirectSound_CreateSoundBuffer(lpds, &dsbd, &SoundSlots[slot].lpsb, NULL);
381 IDirectSoundBuffer_Lock(SoundSlots[slot].lpsb, 0, SoundSlots[slot].length,
382 (void **)&ptr1, &len1, (void **)&ptr2, &len2, 0);
383 memcpy(ptr1, SoundSlots[slot].samples, MIN(len1,(int)SoundSlots[slot].length));
384 IDirectSoundBuffer_Unlock(SoundSlots[slot].lpsb, (void *)ptr1, len1, (void *)ptr2, len2);
386 IDirectSoundBuffer_SetPan(SoundSlots[slot].lpsb, ((int)(f2fl(SoundSlots[slot].pan) * 20000))-10000);
387 // IDirectSoundBuffer_SetVolume(SoundSlots[slot].lpsb, MIN(((int)(f2fl(SoundSlots[slot].volume) * 15000.0)) - 10000, 0));//nope
388 IDirectSoundBuffer_SetVolume(SoundSlots[slot].lpsb,D1vol2DSvol(SoundSlots[slot].volume));
389 IDirectSoundBuffer_Play(SoundSlots[slot].lpsb, 0, 0, SoundSlots[slot].looped?DSBPLAY_LOOPING:0);
391 SoundObjects[obj].signature = next_signature++;
392 SoundObjects[obj].handle = slot;
394 SoundObjects[obj].flags |= SOF_PLAYING;
395 //added on 980905 by adb to add sound kill system from original sos digi.c
396 reset_sounds_on_channel(slot);
403 // Play the given sound number.
404 // Volume is max at F1_0.
405 void digi_play_sample( int soundno, fix max_volume )
408 if ( Newdemo_state == ND_STATE_RECORDING )
409 newdemo_record_sound( soundno );
411 soundno = digi_xlat_sound(soundno);
413 if (!digi_initialised) return;
415 if (soundno < 0 ) return;
417 digi_start_sound(soundno, max_volume, F0_5);
420 // Play the given sound number. If the sound is already playing,
422 void digi_play_sample_once( int soundno, fix max_volume )
427 if ( Newdemo_state == ND_STATE_RECORDING )
428 newdemo_record_sound( soundno );
430 soundno = digi_xlat_sound(soundno);
432 if (!digi_initialised) return;
434 if (soundno < 0 ) return;
436 for (i=0; i < MAX_SOUND_SLOTS; i++)
437 if (SoundSlots[i].soundno == soundno) {
438 SoundSlots[i].playing = 0;
439 DS_release_slot(i, 1);
441 digi_start_sound(soundno, max_volume, F0_5);
445 void digi_play_sample_3d( int soundno, int angle, int volume, int no_dups ) // Volume from 0-0x7fff
450 if ( Newdemo_state == ND_STATE_RECORDING ) {
452 newdemo_record_sound_3d_once( soundno, angle, volume );
454 newdemo_record_sound_3d( soundno, angle, volume );
457 soundno = digi_xlat_sound(soundno);
459 if (!digi_initialised) return;
460 if (soundno < 0 ) return;
462 if (volume < MIN_VOLUME ) return;
463 digi_start_sound(soundno, volume, angle);
466 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 )
468 vms_vector vector_to_sound;
469 fix angle_from_ear, cosang,sinang;
476 max_distance = (max_distance*5)/4; // Make all sounds travel 1.25 times as far.
478 // Warning: Made the vm_vec_normalized_dir be vm_vec_normalized_dir_quick and got illegal values to acos in the fang computation.
479 distance = vm_vec_normalized_dir_quick( &vector_to_sound, sound_pos, listener_pos );
481 if (distance < max_distance ) {
482 int num_search_segs = f2i(max_distance/20);
483 if ( num_search_segs < 1 ) num_search_segs = 1;
485 path_distance = find_connected_distance(listener_pos, listener_seg, sound_pos, sound_seg, num_search_segs, WID_RENDPAST_FLAG );
486 if ( path_distance > -1 ) {
487 *volume = max_volume - fixdiv(path_distance,max_distance);
488 //mprintf( (0, "Sound path distance %.2f, volume is %d / %d\n", f2fl(distance), *volume, max_volume ));
490 angle_from_ear = vm_vec_delta_ang_norm(&listener->rvec,&vector_to_sound,&listener->uvec);
491 fix_sincos(angle_from_ear,&sinang,&cosang);
492 //mprintf( (0, "volume is %.2f\n", f2fl(*volume) ));
493 if (Config_channels_reversed) cosang *= -1;
494 *pan = (cosang + F1_0)/2;
502 int digi_link_sound_to_object2( int org_soundnum, short objnum, int forever, fix max_volume, fix max_distance )
508 soundnum = digi_xlat_sound(org_soundnum);
510 if ( max_volume < 0 ) return -1;
511 // if ( max_volume > F1_0 ) max_volume = F1_0;
513 if (!digi_initialised) return -1;
514 if (soundnum < 0 ) return -1;
515 if (GameSounds[soundnum].data==NULL) {
519 if ((objnum<0)||(objnum>Highest_object_index))
523 // Hack to keep sounds from building up...
524 digi_get_sound_loc( &Viewer->orient, &Viewer->pos, Viewer->segnum, &Objects[objnum].pos, Objects[objnum].segnum, max_volume,&volume, &pan, max_distance );
525 digi_play_sample_3d( org_soundnum, pan, volume, 0 );
529 for (i=0; i<MAX_SOUND_OBJECTS; i++ )
530 if (SoundObjects[i].flags==0)
533 if (i==MAX_SOUND_OBJECTS) {
534 mprintf((1, "Too many sound objects!\n" ));
538 SoundObjects[i].signature=next_signature++;
539 SoundObjects[i].flags = SOF_USED | SOF_LINK_TO_OBJ;
541 SoundObjects[i].flags |= SOF_PLAY_FOREVER;
542 SoundObjects[i].lo_objnum = objnum;
543 SoundObjects[i].lo_objsignature = Objects[objnum].signature;
544 SoundObjects[i].max_volume = max_volume;
545 SoundObjects[i].max_distance = max_distance;
546 SoundObjects[i].volume = 0;
547 SoundObjects[i].pan = 0;
548 SoundObjects[i].soundnum = soundnum;
550 objp = &Objects[SoundObjects[i].lo_objnum];
551 digi_get_sound_loc( &Viewer->orient, &Viewer->pos, Viewer->segnum,
552 &objp->pos, objp->segnum, SoundObjects[i].max_volume,
553 &SoundObjects[i].volume, &SoundObjects[i].pan, SoundObjects[i].max_distance );
555 if (!forever || SoundObjects[i].volume >= MIN_VOLUME)
556 digi_start_sound_object(i);
558 return SoundObjects[i].signature;
561 int digi_link_sound_to_object( int soundnum, short objnum, int forever, fix max_volume )
562 { return digi_link_sound_to_object2( soundnum, objnum, forever, max_volume, 256*F1_0); }
564 int digi_link_sound_to_pos2( int org_soundnum, short segnum, short sidenum, vms_vector * pos, int forever, fix max_volume, fix max_distance )
569 soundnum = digi_xlat_sound(org_soundnum);
571 if ( max_volume < 0 ) return -1;
572 // if ( max_volume > F1_0 ) max_volume = F1_0;
574 if (!digi_initialised) return -1;
575 if (soundnum < 0 ) return -1;
576 if (GameSounds[soundnum].data==NULL) {
581 if ((segnum<0)||(segnum>Highest_segment_index))
585 // Hack to keep sounds from building up...
586 digi_get_sound_loc( &Viewer->orient, &Viewer->pos, Viewer->segnum, pos, segnum, max_volume, &volume, &pan, max_distance );
587 digi_play_sample_3d( org_soundnum, pan, volume, 0 );
591 for (i=0; i<MAX_SOUND_OBJECTS; i++ )
592 if (SoundObjects[i].flags==0)
595 if (i==MAX_SOUND_OBJECTS) {
596 mprintf((1, "Too many sound objects!\n" ));
601 SoundObjects[i].signature=next_signature++;
602 SoundObjects[i].flags = SOF_USED | SOF_LINK_TO_POS;
604 SoundObjects[i].flags |= SOF_PLAY_FOREVER;
605 SoundObjects[i].lp_segnum = segnum;
606 SoundObjects[i].lp_sidenum = sidenum;
607 SoundObjects[i].lp_position = *pos;
608 SoundObjects[i].soundnum = soundnum;
609 SoundObjects[i].max_volume = max_volume;
610 SoundObjects[i].max_distance = max_distance;
611 SoundObjects[i].volume = 0;
612 SoundObjects[i].pan = 0;
613 digi_get_sound_loc( &Viewer->orient, &Viewer->pos, Viewer->segnum,
614 &SoundObjects[i].lp_position, SoundObjects[i].lp_segnum,
615 SoundObjects[i].max_volume,
616 &SoundObjects[i].volume, &SoundObjects[i].pan, SoundObjects[i].max_distance );
618 if (!forever || SoundObjects[i].volume >= MIN_VOLUME)
619 digi_start_sound_object(i);
621 return SoundObjects[i].signature;
624 int digi_link_sound_to_pos( int soundnum, short segnum, short sidenum, vms_vector * pos, int forever, fix max_volume )
626 return digi_link_sound_to_pos2( soundnum, segnum, sidenum, pos, forever, max_volume, F1_0 * 256 );
629 void digi_kill_sound_linked_to_segment( int segnum, int sidenum, int soundnum )
633 soundnum = digi_xlat_sound(soundnum);
635 if (!digi_initialised) return;
639 for (i=0; i<MAX_SOUND_OBJECTS; i++ ) {
640 if ( (SoundObjects[i].flags & SOF_USED) && (SoundObjects[i].flags & SOF_LINK_TO_POS) ) {
641 if ((SoundObjects[i].lp_segnum == segnum) && (SoundObjects[i].soundnum==soundnum ) && (SoundObjects[i].lp_sidenum==sidenum) ) {
642 if ( SoundObjects[i].flags & SOF_PLAYING ) {
643 SoundSlots[SoundObjects[i].handle].playing = 0;
644 DS_release_slot(SoundObjects[i].handle, 1);
646 SoundObjects[i].flags = 0; // Mark as dead, so some other sound can use this sound
651 // If this assert happens, it means that there were 2 sounds
652 // that got deleted. Weird, get John.
654 mprintf( (1, "ERROR: More than 1 sounds were deleted from seg %d\n", segnum ));
658 void digi_kill_sound_linked_to_object( int objnum )
662 if (!digi_initialised) return;
666 for (i=0; i<MAX_SOUND_OBJECTS; i++ ) {
667 if ( (SoundObjects[i].flags & SOF_USED) && (SoundObjects[i].flags & SOF_LINK_TO_OBJ ) ) {
668 if (SoundObjects[i].lo_objnum == objnum) {
669 if ( SoundObjects[i].flags & SOF_PLAYING ) {
670 SoundSlots[SoundObjects[i].handle].playing = 0;
671 DS_release_slot(SoundObjects[i].handle, 1);
673 SoundObjects[i].flags = 0; // Mark as dead, so some other sound can use this sound
678 // If this assert happens, it means that there were 2 sounds
679 // that got deleted. Weird, get John.
681 mprintf( (1, "ERROR: More than 1 sounds were deleted from object %d\n", objnum ));
685 void digi_sync_sounds()
688 int oldvolume, oldpan;
690 if (!digi_initialised) return;
692 for (i=0; i<MAX_SOUND_OBJECTS; i++ ) {
693 if ( SoundObjects[i].flags & SOF_USED ) {
694 oldvolume = SoundObjects[i].volume;
695 oldpan = SoundObjects[i].pan;
697 if ( !(SoundObjects[i].flags & SOF_PLAY_FOREVER) ) {
698 // Check if its done.
699 if (SoundObjects[i].flags & SOF_PLAYING) {
700 if (!SoundSlots[SoundObjects[i].handle].playing) {
701 SoundObjects[i].flags = 0; // Mark as dead, so some other sound can use this sound
702 continue; // Go on to next sound...
707 if ( SoundObjects[i].flags & SOF_LINK_TO_POS ) {
708 digi_get_sound_loc( &Viewer->orient, &Viewer->pos, Viewer->segnum,
709 &SoundObjects[i].lp_position, SoundObjects[i].lp_segnum,
710 SoundObjects[i].max_volume,
711 &SoundObjects[i].volume, &SoundObjects[i].pan, SoundObjects[i].max_distance );
713 } else if ( SoundObjects[i].flags & SOF_LINK_TO_OBJ ) {
716 objp = &Objects[SoundObjects[i].lo_objnum];
718 if ((objp->type==OBJ_NONE) || (objp->signature!=SoundObjects[i].lo_objsignature)) {
719 // The object that this is linked to is dead, so just end this sound if it is looping.
720 if ( (SoundObjects[i].flags & SOF_PLAYING) && (SoundObjects[i].flags & SOF_PLAY_FOREVER)) {
721 SoundSlots[SoundObjects[i].handle].playing = 0;
722 DS_release_slot(SoundObjects[i].handle, 1);
724 SoundObjects[i].flags = 0; // Mark as dead, so some other sound can use this sound
725 continue; // Go on to next sound...
727 digi_get_sound_loc( &Viewer->orient, &Viewer->pos, Viewer->segnum,
728 &objp->pos, objp->segnum, SoundObjects[i].max_volume,
729 &SoundObjects[i].volume, &SoundObjects[i].pan, SoundObjects[i].max_distance );
733 if (oldvolume != SoundObjects[i].volume) {
734 if ( SoundObjects[i].volume < MIN_VOLUME ) {
735 // Sound is too far away, so stop it from playing.
736 if ((SoundObjects[i].flags & SOF_PLAYING)&&(SoundObjects[i].flags & SOF_PLAY_FOREVER)) {
737 SoundSlots[SoundObjects[i].handle].playing = 0;
738 DS_release_slot(SoundObjects[i].handle, 1);
739 SoundObjects[i].flags &= ~SOF_PLAYING; // Mark sound as not playing
742 if (!(SoundObjects[i].flags & SOF_PLAYING)) {
743 digi_start_sound_object(i);
745 SoundSlots[SoundObjects[i].handle].volume = fixmuldiv(SoundObjects[i].volume,digi_volume,F1_0);
750 if (oldpan != SoundObjects[i].pan) {
751 if (SoundObjects[i].flags & SOF_PLAYING)
752 SoundSlots[SoundObjects[i].handle].pan = SoundObjects[i].pan;
758 void digi_init_sounds()
762 if (!digi_initialised) return;
764 digi_reset_digi_sounds();
766 for (i=0; i<MAX_SOUND_OBJECTS; i++ ) {
767 if (digi_sounds_initialized) {
768 if ( SoundObjects[i].flags & SOF_PLAYING ) {
769 SoundSlots[SoundObjects[i].handle].playing=0;
770 DS_release_slot(SoundObjects[i].handle, 1);
773 SoundObjects[i].flags = 0; // Mark as dead, so some other sound can use this sound
775 digi_sounds_initialized = 1;
778 //added on 980905 by adb from original source to make sfx volume work
779 void digi_set_digi_volume( int dvolume )
781 dvolume = fixmuldiv( dvolume, SOUND_MAX_VOLUME, 0x7fff);
782 if ( dvolume > SOUND_MAX_VOLUME )
783 digi_volume = SOUND_MAX_VOLUME;
784 else if ( dvolume < 0 )
787 digi_volume = dvolume;
789 if ( !digi_initialised ) return;
795 void digi_set_volume( int dvolume, int mvolume )
797 digi_set_digi_volume(dvolume);
798 digi_set_midi_volume(mvolume);
801 int digi_is_sound_playing(int soundno)
805 soundno = digi_xlat_sound(soundno);
807 for (i = 0; i < MAX_SOUND_SLOTS; i++)
808 //changed on 980905 by adb: added SoundSlots[i].playing &&
809 if (SoundSlots[i].playing && SoundSlots[i].soundno == soundno)
816 void digi_pause_all() { }
817 void digi_resume_all() { }
818 void digi_stop_all() { }
820 //added on 980905 by adb to make sound channel setting work
821 void digi_set_max_channels(int n) {
822 digi_max_channels = n;
824 if ( digi_max_channels < 1 )
825 digi_max_channels = 1;
826 if ( digi_max_channels > (MAX_SOUND_SLOTS-MAX_SOUND_OBJECTS) )
827 digi_max_channels = (MAX_SOUND_SLOTS-MAX_SOUND_OBJECTS);
829 if ( !digi_initialised ) return;
831 digi_reset_digi_sounds();
834 int digi_get_max_channels() {
835 return digi_max_channels;
839 void digi_reset_digi_sounds() {
842 for (i=0; i< MAX_SOUND_SLOTS; i++) {
843 SoundSlots[i].playing=0;
844 DS_release_slot(i, 1);
847 //added on 980905 by adb to reset sound kill system
848 memset(SampleHandles, 255, sizeof(SampleHandles));
854 int digi_midi_song_playing = 0;
855 static int digi_initialised = 0;
856 int midi_volume = 255;
858 int digi_get_settings() { return 0; }
859 int digi_init() { digi_initialised = 1; return 0; }
863 void digi_play_sample( int sndnum, fix max_volume ) {}
864 void digi_play_sample_once( int sndnum, fix max_volume ) {}
865 int digi_link_sound_to_object( int soundnum, short objnum, int forever, fix max_volume ) { return 0; }
866 int digi_link_sound_to_pos( int soundnum, short segnum, short sidenum, vms_vector * pos, int forever, fix max_volume ) { return 0; }
867 // Same as above, but you pass the max distance sound can be heard. The old way uses f1_0*256 for max_distance.
868 int digi_link_sound_to_object2( int soundnum, short objnum, int forever, fix max_volume, fix max_distance ) { return 0; }
869 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; }
871 void digi_play_sample_3d( int soundno, int angle, int volume, int no_dups ) {} // Volume from 0-0x7fff
873 void digi_init_sounds() {}
874 void digi_sync_sounds() {}
875 void digi_kill_sound_linked_to_segment( int segnum, int sidenum, int soundnum ) {}
876 void digi_kill_sound_linked_to_object( int objnum ) {}
878 void digi_set_digi_volume( int dvolume ) {}
879 void digi_set_volume( int dvolume, int mvolume ) {}
881 int digi_is_sound_playing(int soundno) { return 0; }
883 void digi_pause_all() {}
884 void digi_resume_all() {}
885 void digi_stop_all() {}
887 void digi_set_max_channels(int n) {}
888 int digi_get_max_channels() { return 0; }
893 // MIDI stuff follows.
895 void digi_stop_current_song()
897 if ( digi_midi_song_playing ) {
900 digi_midi_song_playing = 0;
904 void digi_set_midi_volume( int n )
915 // scale up from 0-127 to 0-0xffff
916 mm_volume = (midi_volume << 1) | (midi_volume & 1);
917 mm_volume |= (mm_volume << 8);
920 midiOutSetVolume((HMIDIOUT)hmp->hmidi, mm_volume | mm_volume << 16);
923 void digi_play_midi_song( char * filename, char * melodic_bank, char * drum_bank, int loop )
925 if (!digi_initialised) return;
927 digi_stop_current_song();
929 //added on 5/20/99 by Victor Rachels to fix crash/etc
930 if(filename == NULL) return;
931 if(midi_volume < 1) return;
932 //end this section addition - VR
934 if ((hmp = hmp_open(filename))) {
936 digi_midi_song_playing = 1;
937 digi_set_midi_volume(midi_volume);
940 printf("hmp_open failed\n");
943 void digi_stop_current_song() {}
944 void digi_set_midi_volume( int n ) {}
945 void digi_play_midi_song( char * filename, char * melodic_bank, char * drum_bank, int loop ) {}