1 /* $Id: digi.c,v 1.11 2004-05-22 23:01:20 btb Exp $ */
8 #define WIN32_LEAN_AND_MEAN
19 #include "gr.h" // needed for piggy.h
33 #define MAX_SOUND_SLOTS 32
37 #define SOF_USED 1 // Set if this sample is used
38 #define SOF_PLAYING 2 // Set if this sample is playing on a channel
39 #define SOF_LINK_TO_OBJ 4 // Sound is linked to a moving object. If object dies, then finishes play and quits.
40 #define SOF_LINK_TO_POS 8 // Sound is linked to segment, pos
41 #define SOF_PLAY_FOREVER 16 // Play forever (or until level is stopped), otherwise plays once
43 typedef struct sound_object {
44 short signature; // A unique signature to this sound
45 ubyte flags; // Used to tell if this slot is used and/or currently playing, and how long.
46 fix max_volume; // Max volume that this sound is playing at
47 fix max_distance; // The max distance that this sound can be heard at...
48 int volume; // Volume that this sound is playing at
49 int pan; // Pan value that this sound is playing at
50 int handle; // What handle this sound is playing on. Valid only if SOF_PLAYING is set.
51 short soundnum; // The sound number that is playing
54 short segnum; // Used if SOF_LINK_TO_POS field is used
59 short objnum; // Used if SOF_LINK_TO_OBJ field is used
64 #define lp_segnum link.pos.segnum
65 #define lp_sidenum link.pos.sidenum
66 #define lp_position link.pos.position
68 #define lo_objnum link.obj.objnum
69 #define lo_objsignature link.obj.objsignature
71 #define MAX_SOUND_OBJECTS 16
72 sound_object SoundObjects[MAX_SOUND_OBJECTS];
73 short next_signature=0;
76 //added/changed on 980905 by adb to make sfx volume work
77 #define SOUND_MAX_VOLUME F1_0
78 int digi_volume = SOUND_MAX_VOLUME;
82 WAVEFORMATEX waveformat;
89 int playing; // Is there a sample playing on this channel?
90 int looped; // Play this sample looped?
91 fix pan; // 0 = far left, 1 = far right
92 fix volume; // 0 = nothing, 1 = fully on
93 //changed on 980905 by adb from char * to unsigned char *
94 unsigned char *samples;
96 unsigned int length; // Length of the sample
97 unsigned int position; // Position we are at at the moment.
98 LPDIRECTSOUNDBUFFER lpsb;
99 } SoundSlots[MAX_SOUND_SLOTS];
103 int midi_volume = 255;
104 int digi_midi_song_playing = 0;
105 int digi_last_midi_song = 0;
106 int digi_last_midi_song_loop = 0;
108 static int digi_initialised = 0;
109 static int digi_atexit_initialised=0;
111 static int digi_sounds_initialized = 0;
113 //added on 980905 by adb to add rotating/volume based sound kill system
114 static int digi_max_channels = 16;
115 static int next_handle = 0;
116 int SampleHandles[32];
117 void reset_sounds_on_channel(int channel);
120 void digi_reset_digi_sounds(void);
122 void digi_reset() { }
124 void digi_close(void)
128 digi_reset_digi_sounds();
129 IDirectSound_Release(lpds);
131 digi_initialised = 0;
134 /* Initialise audio devices. */
139 if (!digi_initialised && g_hWnd){
141 memset(&waveformat, 0, sizeof(waveformat));
142 waveformat.wFormatTag=WAVE_FORMAT_PCM;
143 waveformat.wBitsPerSample=8;
144 waveformat.nChannels = 1;
145 waveformat.nSamplesPerSec = digi_sample_rate; //11025;
146 waveformat.nBlockAlign = waveformat.nChannels * (waveformat.wBitsPerSample / 8);
147 waveformat.nAvgBytesPerSec = waveformat.nSamplesPerSec * waveformat.nBlockAlign;
149 if ((hr = DirectSoundCreate(NULL, &lpds, NULL)) != DS_OK)
152 if ((hr = IDirectSound_SetCooperativeLevel(lpds, g_hWnd, DSSCL_PRIORITY)) //hWndMain
155 IDirectSound_Release(lpds);
159 memset(&dsbd, 0, sizeof(dsbd));
160 dsbd.dwSize = sizeof(dsbd);
161 dsbd.dwFlags = DSBCAPS_CTRLDEFAULT | DSBCAPS_GETCURRENTPOSITION2;
162 dsbd.dwBufferBytes = 8192;
164 dsbd.lpwfxFormat = &waveformat;
166 digi_initialised = 1;
169 if (!digi_atexit_initialised){
171 digi_atexit_initialised=1;
176 // added 2000/01/15 Matt Mueller -- remove some duplication (and fix a big memory leak, in the kill=0 one case)
177 static int DS_release_slot(int slot, int kill)
179 if (SoundSlots[slot].lpsb)
183 IDirectSoundBuffer_GetStatus(SoundSlots[slot].lpsb, &s);
184 if (s & DSBSTATUS_PLAYING)
187 IDirectSoundBuffer_Stop(SoundSlots[slot].lpsb);
191 IDirectSoundBuffer_Release(SoundSlots[slot].lpsb);
192 SoundSlots[slot].lpsb = NULL;
194 SoundSlots[slot].playing = 0;
198 static int get_free_slot()
202 for (i=0; i<MAX_SOUND_SLOTS; i++)
204 if (DS_release_slot(i, 0))
210 int D1vol2DSvol(fix d1v){
211 //multiplying by 1.5 doesn't help. DirectSound uses dB for volume, rather than a linear scale like d1 wants.
212 //I had to pull out a math book, but here is the code to fix it :) -Matt Mueller
218 // return log2(f2fl(d1v))*1000;//no log2? hm.
219 return log(f2fl(d1v))/log(2)*1000.0;
222 int digi_start_sound(int soundnum, fix volume, fix pan)
228 if (!digi_initialised)
231 // added on 980905 by adb from original source to add sound kill system
232 // play at most digi_max_channel samples, if possible kill sample with low volume
236 if ((SampleHandles[next_handle] >= 0) && (SoundSlots[SampleHandles[next_handle]].playing))
238 if ((SoundSlots[SampleHandles[next_handle]].volume > digi_volume) && (ntries < digi_max_channels))
240 //mprintf((0, "Not stopping loud sound %d.\n", next_handle));
242 if (next_handle >= digi_max_channels)
247 //mprintf((0, "[SS:%d]", next_handle));
248 SampleHandles[next_handle] = -1;
252 slot = get_free_slot();
256 SoundSlots[slot].soundno = soundnum;
257 SoundSlots[slot].samples = Sounddat(soundnum)->data;
258 SoundSlots[slot].length = Sounddat(soundnum)->length;
259 SoundSlots[slot].volume = fixmul(digi_volume, volume);
260 SoundSlots[slot].pan = pan;
261 SoundSlots[slot].position = 0;
262 SoundSlots[slot].looped = 0;
263 SoundSlots[slot].playing = 1;
265 memset(&waveformat, 0, sizeof(waveformat));
266 waveformat.wFormatTag = WAVE_FORMAT_PCM;
267 waveformat.wBitsPerSample = Sounddat(soundnum)->bits;
268 waveformat.nChannels = 1;
269 waveformat.nSamplesPerSec = Sounddat(soundnum)->freq; //digi_sample_rate;
270 waveformat.nBlockAlign = waveformat.nChannels * (waveformat.wBitsPerSample / 8);
271 waveformat.nAvgBytesPerSec = waveformat.nSamplesPerSec * waveformat.nBlockAlign;
273 memset(&dsbd, 0, sizeof(dsbd));
274 dsbd.dwSize = sizeof(dsbd);
275 dsbd.dwFlags = DSBCAPS_CTRLDEFAULT | DSBCAPS_GETCURRENTPOSITION2;
277 dsbd.dwBufferBytes = SoundSlots[slot].length;
278 dsbd.lpwfxFormat = &waveformat;
280 hr = IDirectSound_CreateSoundBuffer(lpds, &dsbd, &SoundSlots[slot].lpsb, NULL);
283 printf("Createsoundbuffer failed! hr=0x%X\n", (int)hr);
291 IDirectSoundBuffer_Lock(SoundSlots[slot].lpsb, 0, Sounddat(soundnum)->length,
292 &ptr1, &len1, &ptr2, &len2, 0);
293 memcpy(ptr1, Sounddat(soundnum)->data, MIN(len1, Sounddat(soundnum)->length));
294 IDirectSoundBuffer_Unlock(SoundSlots[slot].lpsb, ptr1, len1, ptr2, len2);
297 IDirectSoundBuffer_SetPan(SoundSlots[slot].lpsb, ((int)(f2fl(pan) * 20000.0)) - 10000);
298 IDirectSoundBuffer_SetVolume(SoundSlots[slot].lpsb, D1vol2DSvol(SoundSlots[slot].volume));
299 IDirectSoundBuffer_Play(SoundSlots[slot].lpsb, 0, 0, 0);
301 // added on 980905 by adb to add sound kill system from original sos digi.c
302 reset_sounds_on_channel(slot);
303 SampleHandles[next_handle] = slot;
305 if (next_handle >= digi_max_channels)
312 //added on 980905 by adb to add sound kill system from original sos digi.c
313 void reset_sounds_on_channel( int channel )
317 for (i=0; i<digi_max_channels; i++)
318 if (SampleHandles[i] == channel)
319 SampleHandles[i] = -1;
323 int digi_start_sound_object(int obj)
328 if (!digi_initialised)
331 slot = get_free_slot();
337 SoundSlots[slot].soundno = SoundObjects[obj].soundnum;
338 SoundSlots[slot].samples = Sounddat(SoundObjects[obj].soundnum)->data;
339 SoundSlots[slot].length = Sounddat(SoundObjects[obj].soundnum).length;
340 SoundSlots[slot].volume = fixmul(digi_volume, SoundObjects[obj].volume);
341 SoundSlots[slot].pan = SoundObjects[obj].pan;
342 SoundSlots[slot].position = 0;
343 SoundSlots[slot].looped = (SoundObjects[obj].flags & SOF_PLAY_FOREVER);
344 SoundSlots[slot].playing = 1;
346 memset(&waveformat, 0, sizeof(waveformat));
347 waveformat.wFormatTag = WAVE_FORMAT_PCM;
348 waveformat.wBitsPerSample = Sounddat(SoundObjects[obj].soundnum)->bits;
349 waveformat.nChannels = 1;
350 waveformat.nSamplesPerSec = Sounddat(SoundObjects[obj].soundnum)->freq; //digi_sample_rate;
351 waveformat.nBlockAlign = waveformat.nChannels * (waveformat.wBitsPerSample / 8);
352 waveformat.nAvgBytesPerSec = waveformat.nSamplesPerSec * waveformat.nBlockAlign;
354 memset(&dsbd, 0, sizeof(dsbd));
355 dsbd.dwSize = sizeof(dsbd);
356 dsbd.dwFlags = DSBCAPS_CTRLDEFAULT | DSBCAPS_GETCURRENTPOSITION2;
358 dsbd.dwBufferBytes = SoundSlots[slot].length;
359 dsbd.lpwfxFormat = &waveformat;
361 hr = IDirectSound_CreateSoundBuffer(lpds, &dsbd, &SoundSlots[slot].lpsb, NULL);
371 IDirectSoundBuffer_Lock(SoundSlots[slot].lpsb, 0, SoundSlots[slot].length,
372 &ptr1, &len1, &ptr2, &len2, 0);
373 memcpy(ptr1, SoundSlots[slot].samples, MIN(len1, (int)SoundSlots[slot].length));
374 IDirectSoundBuffer_Unlock(SoundSlots[slot].lpsb, ptr1, len1, ptr2, len2);
376 IDirectSoundBuffer_SetPan(SoundSlots[slot].lpsb, ((int)(f2fl(SoundSlots[slot].pan) * 20000)) - 10000);
377 IDirectSoundBuffer_SetVolume(SoundSlots[slot].lpsb, D1vol2DSvol(SoundSlots[slot].volume));
378 IDirectSoundBuffer_Play(SoundSlots[slot].lpsb, 0, 0, SoundSlots[slot].looped ? DSBPLAY_LOOPING : 0);
380 SoundObjects[obj].signature = next_signature++;
381 SoundObjects[obj].handle = slot;
383 SoundObjects[obj].flags |= SOF_PLAYING;
384 // added on 980905 by adb to add sound kill system from original sos digi.c
385 reset_sounds_on_channel(slot);
392 // Play the given sound number.
393 // Volume is max at F1_0.
394 void digi_play_sample( int soundno, fix max_volume )
397 if (Newdemo_state == ND_STATE_RECORDING)
398 newdemo_record_sound(soundno);
400 if (!digi_initialised)
402 if (digi_xlat_sound(soundno) < 0)
405 if (soundno < 0 ) return;
407 digi_start_sound(soundno, max_volume, F0_5);
410 // Play the given sound number. If the sound is already playing,
412 void digi_play_sample_once( int soundno, fix max_volume )
417 if (Newdemo_state == ND_STATE_RECORDING)
418 newdemo_record_sound(soundno);
420 if (!digi_initialised)
422 if(digi_xlat_sound(soundno) < 0)
425 for (i = 0; i < MAX_SOUND_SLOTS; i++)
426 if (SoundSlots[i].soundno == soundno)
428 DS_release_slot(i, 1);
431 digi_start_sound(soundno, max_volume, F0_5);
434 void digi_play_sample_3d( int soundno, int angle, int volume, int no_dups ) // Volume from 0-0x7fff
439 if (Newdemo_state == ND_STATE_RECORDING)
442 newdemo_record_sound_3d_once(soundno, angle, volume);
444 newdemo_record_sound_3d(soundno, angle, volume);
447 if (!digi_initialised)
449 if (digi_xlat_sound(soundno) < 0)
451 if (volume < MIN_VOLUME)
454 digi_start_sound(soundno, volume, angle);
457 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 )
459 vms_vector vector_to_sound;
460 fix angle_from_ear, cosang,sinang;
467 max_distance = (max_distance * 5) / 4; // Make all sounds travel 1.25 times as far.
469 // Warning: Made the vm_vec_normalized_dir be vm_vec_normalized_dir_quick and got illegal values to acos in the fang computation.
470 distance = vm_vec_normalized_dir_quick( &vector_to_sound, sound_pos, listener_pos );
472 if (distance < max_distance ) {
473 int num_search_segs = f2i(max_distance/20);
474 if ( num_search_segs < 1 ) num_search_segs = 1;
476 path_distance = find_connected_distance(listener_pos, listener_seg, sound_pos, sound_seg, num_search_segs, WID_RENDPAST_FLAG );
477 if ( path_distance > -1 ) {
478 *volume = max_volume - fixdiv(path_distance,max_distance);
479 //mprintf( (0, "Sound path distance %.2f, volume is %d / %d\n", f2fl(distance), *volume, max_volume ));
481 angle_from_ear = vm_vec_delta_ang_norm(&listener->rvec,&vector_to_sound,&listener->uvec);
482 fix_sincos(angle_from_ear,&sinang,&cosang);
483 //mprintf( (0, "volume is %.2f\n", f2fl(*volume) ));
484 if (Config_channels_reversed) cosang *= -1;
485 *pan = (cosang + F1_0)/2;
493 int digi_link_sound_to_object2(int soundnum, short objnum, int forever, fix max_volume, fix max_distance)
500 if (!digi_initialised)
502 if (digi_xlat_sound(soundnum) < 0)
504 if (Sounddat(soundnum)->data == NULL)
509 if ((objnum < 0) || (objnum > Highest_object_index))
514 // Hack to keep sounds from building up...
515 digi_get_sound_loc(&Viewer->orient, &Viewer->pos, Viewer->segnum, &Objects[objnum].pos, Objects[objnum].segnum, max_volume, &volume, &pan, max_distance);
516 digi_play_sample_3d(soundnum, pan, volume, 0);
520 for (i = 0; i < MAX_SOUND_OBJECTS; i++)
521 if (SoundObjects[i].flags == 0)
524 if (i == MAX_SOUND_OBJECTS)
526 mprintf((1, "Too many sound objects!\n"));
530 SoundObjects[i].signature = next_signature++;
531 SoundObjects[i].flags = SOF_USED | SOF_LINK_TO_OBJ;
533 SoundObjects[i].flags |= SOF_PLAY_FOREVER;
534 SoundObjects[i].lo_objnum = objnum;
535 SoundObjects[i].lo_objsignature = Objects[objnum].signature;
536 SoundObjects[i].max_volume = max_volume;
537 SoundObjects[i].max_distance = max_distance;
538 SoundObjects[i].volume = 0;
539 SoundObjects[i].pan = 0;
540 SoundObjects[i].soundnum = soundnum;
541 objp = &Objects[SoundObjects[i].lo_objnum];
542 digi_get_sound_loc(&Viewer->orient, &Viewer->pos, Viewer->segnum,
543 &objp->pos, objp->segnum, SoundObjects[i].max_volume,
544 &SoundObjects[i].volume, &SoundObjects[i].pan, SoundObjects[i].max_distance);
546 if (!forever || SoundObjects[i].volume >= MIN_VOLUME)
547 digi_start_sound_object(i);
549 return SoundObjects[i].signature;
552 int digi_link_sound_to_object( int soundnum, short objnum, int forever, fix max_volume )
554 return digi_link_sound_to_object2(soundnum, objnum, forever, max_volume, 256 * F1_0);
557 int digi_link_sound_to_pos2(int soundnum, short segnum, short sidenum, vms_vector *pos, int forever, fix max_volume, fix max_distance)
563 if (!digi_initialised)
565 if (digi_xlat_sound(soundnum) < 0)
567 if (Sounddat(soundnum)->data == NULL)
575 // Hack to keep sounds from building up...
576 digi_get_sound_loc(&Viewer->orient, &Viewer->pos, Viewer->segnum, pos, segnum, max_volume, &volume, &pan, max_distance);
577 digi_play_sample_3d(org_soundnum, pan, volume, 0);
581 for (i = 0; i < MAX_SOUND_OBJECTS; i++)
582 if (SoundObjects[i].flags == 0)
585 if (i == MAX_SOUND_OBJECTS)
587 mprintf((1, "Too many sound objects!\n"));
592 SoundObjects[i].signature = next_signature++;
593 SoundObjects[i].flags = SOF_USED | SOF_LINK_TO_POS;
595 SoundObjects[i].flags |= SOF_PLAY_FOREVER;
596 SoundObjects[i].lp_segnum = segnum;
597 SoundObjects[i].lp_sidenum = sidenum;
598 SoundObjects[i].lp_position = *pos;
599 SoundObjects[i].soundnum = soundnum;
600 SoundObjects[i].max_volume = max_volume;
601 SoundObjects[i].max_distance = max_distance;
602 SoundObjects[i].volume = 0;
603 SoundObjects[i].pan = 0;
604 digi_get_sound_loc(&Viewer->orient, &Viewer->pos, Viewer->segnum,
605 &SoundObjects[i].lp_position, SoundObjects[i].lp_segnum,
606 SoundObjects[i].max_volume,
607 &SoundObjects[i].volume, &SoundObjects[i].pan, SoundObjects[i].max_distance);
609 if (!forever || SoundObjects[i].volume >= MIN_VOLUME)
610 digi_start_sound_object(i);
612 return SoundObjects[i].signature;
615 int digi_link_sound_to_pos( int soundnum, short segnum, short sidenum, vms_vector * pos, int forever, fix max_volume )
617 return digi_link_sound_to_pos2(soundnum, segnum, sidenum, pos, forever, max_volume, F1_0 * 256);
620 void digi_kill_sound_linked_to_segment( int segnum, int sidenum, int soundnum )
624 if (!digi_initialised)
629 for (i = 0; i < MAX_SOUND_OBJECTS; i++)
630 if ((SoundObjects[i].flags & SOF_USED) &&
631 (SoundObjects[i].flags & SOF_LINK_TO_POS) &&
632 (SoundObjects[i].lp_segnum == segnum) &&
633 (SoundObjects[i].soundnum == soundnum) &&
634 (SoundObjects[i].lp_sidenum==sidenum))
636 if (SoundObjects[i].flags & SOF_PLAYING)
638 DS_release_slot(SoundObjects[i].handle, 1);
640 SoundObjects[i].flags = 0; // Mark as dead, so some other sound can use this sound
644 // If this assert happens, it means that there were 2 sounds
645 // that got deleted. Weird, get John.
648 mprintf((1, "ERROR: More than 1 sounds were deleted from seg %d\n", segnum));
652 void digi_kill_sound_linked_to_object( int objnum )
656 if (!digi_initialised) return;
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 DS_release_slot(SoundObjects[i].handle, 1);
666 SoundObjects[i].flags = 0; // Mark as dead, so some other sound can use this sound
671 // If this assert happens, it means that there were 2 sounds
672 // that got deleted. Weird, get John.
674 mprintf( (1, "ERROR: More than 1 sounds were deleted from object %d\n", objnum ));
678 void digi_sync_sounds()
681 int oldvolume, oldpan;
683 if (!digi_initialised) return;
685 for (i=0; i<MAX_SOUND_OBJECTS; i++ ) {
686 if ( SoundObjects[i].flags & SOF_USED ) {
687 oldvolume = SoundObjects[i].volume;
688 oldpan = SoundObjects[i].pan;
690 if ( !(SoundObjects[i].flags & SOF_PLAY_FOREVER) ) {
691 // Check if its done.
692 if (SoundObjects[i].flags & SOF_PLAYING) {
693 if (!SoundSlots[SoundObjects[i].handle].playing) {
694 SoundObjects[i].flags = 0; // Mark as dead, so some other sound can use this sound
695 continue; // Go on to next sound...
700 if ( SoundObjects[i].flags & SOF_LINK_TO_POS ) {
701 digi_get_sound_loc( &Viewer->orient, &Viewer->pos, Viewer->segnum,
702 &SoundObjects[i].lp_position, SoundObjects[i].lp_segnum,
703 SoundObjects[i].max_volume,
704 &SoundObjects[i].volume, &SoundObjects[i].pan, SoundObjects[i].max_distance );
706 } else if ( SoundObjects[i].flags & SOF_LINK_TO_OBJ ) {
709 objp = &Objects[SoundObjects[i].lo_objnum];
711 if ((objp->type==OBJ_NONE) || (objp->signature!=SoundObjects[i].lo_objsignature)) {
712 // The object that this is linked to is dead, so just end this sound if it is looping.
713 if ( (SoundObjects[i].flags & SOF_PLAYING) && (SoundObjects[i].flags & SOF_PLAY_FOREVER)) {
714 DS_release_slot(SoundObjects[i].handle, 1);
716 SoundObjects[i].flags = 0; // Mark as dead, so some other sound can use this sound
717 continue; // Go on to next sound...
719 digi_get_sound_loc( &Viewer->orient, &Viewer->pos, Viewer->segnum,
720 &objp->pos, objp->segnum, SoundObjects[i].max_volume,
721 &SoundObjects[i].volume, &SoundObjects[i].pan, SoundObjects[i].max_distance );
725 if (oldvolume != SoundObjects[i].volume) {
726 if ( SoundObjects[i].volume < MIN_VOLUME ) {
727 // Sound is too far away, so stop it from playing.
728 if ((SoundObjects[i].flags & SOF_PLAYING)&&(SoundObjects[i].flags & SOF_PLAY_FOREVER)) {
729 DS_release_slot(SoundObjects[i].handle, 1);
730 SoundObjects[i].flags &= ~SOF_PLAYING; // Mark sound as not playing
733 if (!(SoundObjects[i].flags & SOF_PLAYING)) {
734 digi_start_sound_object(i);
736 SoundSlots[SoundObjects[i].handle].volume = fixmuldiv(SoundObjects[i].volume,digi_volume,F1_0);
737 IDirectSoundBuffer_SetVolume(SoundSlots[SoundObjects[i].handle].lpsb, D1vol2DSvol(SoundSlots[SoundObjects[i].handle].volume));
742 if (oldpan != SoundObjects[i].pan) {
743 if (SoundObjects[i].flags & SOF_PLAYING)
745 SoundSlots[SoundObjects[i].handle].pan = SoundObjects[i].pan;
746 IDirectSoundBuffer_SetPan(SoundSlots[SoundObjects[i].handle].lpsb, ((int)(f2fl(SoundObjects[i].pan) * 20000.0)) - 10000);
753 void digi_init_sounds()
757 if (!digi_initialised) return;
759 digi_reset_digi_sounds();
761 for (i=0; i<MAX_SOUND_OBJECTS; i++ ) {
762 if (digi_sounds_initialized) {
763 if ( SoundObjects[i].flags & SOF_PLAYING ) {
764 DS_release_slot(SoundObjects[i].handle, 1);
767 SoundObjects[i].flags = 0; // Mark as dead, so some other sound can use this sound
769 digi_sounds_initialized = 1;
772 //added on 980905 by adb from original source to make sfx volume work
773 void digi_set_digi_volume( int dvolume )
775 dvolume = fixmuldiv( dvolume, SOUND_MAX_VOLUME, 0x7fff);
776 if ( dvolume > SOUND_MAX_VOLUME )
777 digi_volume = SOUND_MAX_VOLUME;
778 else if ( dvolume < 0 )
781 digi_volume = dvolume;
783 if ( !digi_initialised ) return;
789 void digi_set_volume( int dvolume, int mvolume )
791 digi_set_digi_volume(dvolume);
792 digi_set_midi_volume(mvolume);
795 int digi_is_sound_playing(int soundno)
799 soundno = digi_xlat_sound(soundno);
801 for (i = 0; i < MAX_SOUND_SLOTS; i++)
802 //changed on 980905 by adb: added SoundSlots[i].playing &&
803 if (SoundSlots[i].playing && SoundSlots[i].soundno == soundno)
810 void digi_pause_all() { }
811 void digi_resume_all() { }
812 void digi_stop_all() { }
814 //added on 980905 by adb to make sound channel setting work
815 void digi_set_max_channels(int n) {
816 digi_max_channels = n;
818 if ( digi_max_channels < 1 )
819 digi_max_channels = 1;
820 if ( digi_max_channels > (MAX_SOUND_SLOTS-MAX_SOUND_OBJECTS) )
821 digi_max_channels = (MAX_SOUND_SLOTS-MAX_SOUND_OBJECTS);
823 if ( !digi_initialised ) return;
825 digi_reset_digi_sounds();
828 int digi_get_max_channels() {
829 return digi_max_channels;
833 void digi_reset_digi_sounds() {
836 for (i=0; i< MAX_SOUND_SLOTS; i++) {
837 DS_release_slot(i, 1);
840 //added on 980905 by adb to reset sound kill system
841 memset(SampleHandles, 255, sizeof(SampleHandles));
847 int digi_midi_song_playing = 0;
848 static int digi_initialised = 0;
849 int midi_volume = 255;
851 int digi_get_settings() { return 0; }
852 int digi_init() { digi_initialised = 1; return 0; }
856 void digi_play_sample( int sndnum, fix max_volume ) {}
857 void digi_play_sample_once( int sndnum, fix max_volume ) {}
858 int digi_link_sound_to_object( int soundnum, short objnum, int forever, fix max_volume ) { return 0; }
859 int digi_link_sound_to_pos( int soundnum, short segnum, short sidenum, vms_vector * pos, int forever, fix max_volume ) { return 0; }
860 // Same as above, but you pass the max distance sound can be heard. The old way uses f1_0*256 for max_distance.
861 int digi_link_sound_to_object2( int soundnum, short objnum, int forever, fix max_volume, fix max_distance ) { return 0; }
862 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; }
864 void digi_play_sample_3d( int soundno, int angle, int volume, int no_dups ) {} // Volume from 0-0x7fff
866 void digi_init_sounds() {}
867 void digi_sync_sounds() {}
868 void digi_kill_sound_linked_to_segment( int segnum, int sidenum, int soundnum ) {}
869 void digi_kill_sound_linked_to_object( int objnum ) {}
871 void digi_set_digi_volume( int dvolume ) {}
872 void digi_set_volume( int dvolume, int mvolume ) {}
874 int digi_is_sound_playing(int soundno) { return 0; }
876 void digi_pause_all() {}
877 void digi_resume_all() {}
878 void digi_stop_all() {}
880 void digi_set_max_channels(int n) {}
881 int digi_get_max_channels() { return 0; }
886 // MIDI stuff follows.
888 void digi_stop_current_song()
890 if ( digi_midi_song_playing ) {
893 digi_midi_song_playing = 0;
897 void digi_set_midi_volume( int n )
908 // scale up from 0-127 to 0-0xffff
909 mm_volume = (midi_volume << 1) | (midi_volume & 1);
910 mm_volume |= (mm_volume << 8);
913 midiOutSetVolume((HMIDIOUT)hmp->hmidi, mm_volume | mm_volume << 16);
916 void digi_play_midi_song( char * filename, char * melodic_bank, char * drum_bank, int loop )
918 if (!digi_initialised) return;
920 digi_stop_current_song();
922 //added on 5/20/99 by Victor Rachels to fix crash/etc
923 if(filename == NULL) return;
924 if(midi_volume < 1) return;
925 //end this section addition - VR
927 if ((hmp = hmp_open(filename))) {
929 digi_midi_song_playing = 1;
930 digi_set_midi_volume(midi_volume);
933 printf("hmp_open failed\n");
936 void digi_stop_current_song() {}
937 void digi_set_midi_volume( int n ) {}
938 void digi_play_midi_song( char * filename, char * melodic_bank, char * drum_bank, int loop ) {}