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 static int get_free_slot()
190 for (i=0; i<MAX_SOUND_SLOTS; i++)
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;
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
212 // return log2(f2fl(d1v))*1000;//no log2? hm.
213 return log(f2fl(d1v))/log(2)*1000.0;
216 int digi_start_sound(int soundnum, fix volume, fix pan)
222 if (!digi_initialised) return -1;
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
229 if ( (SampleHandles[next_handle] >= 0) && (SoundSlots[SampleHandles[next_handle]].playing) )
231 if ( (SoundSlots[SampleHandles[next_handle]].volume > digi_volume) && (ntries<digi_max_channels) )
233 //mprintf(( 0, "Not stopping loud sound %d.\n", next_handle ));
235 if ( next_handle >= digi_max_channels )
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;
247 slot = get_free_slot();
248 if (slot<0) return -1;
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;
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;
269 memset(&dsbd, 0, sizeof(dsbd));
270 dsbd.dwSize = sizeof(dsbd);
271 dsbd.dwFlags = DSBCAPS_CTRLDEFAULT | DSBCAPS_GETCURRENTPOSITION2;
273 dsbd.dwBufferBytes = SoundSlots[slot].length;
274 dsbd.lpwfxFormat = &waveformat;
276 hr = IDirectSound_CreateSoundBuffer(lpds, &dsbd, &SoundSlots[slot].lpsb, NULL);
278 printf("Createsoundbuffer failed! hr=0x%X\n", (int)hr);
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);
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));
293 IDirectSoundBuffer_Play(SoundSlots[slot].lpsb, 0, 0, 0);
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;
299 if ( next_handle >= digi_max_channels )
306 //added on 980905 by adb to add sound kill system from original sos digi.c
307 void reset_sounds_on_channel( int channel )
311 for (i=0; i<digi_max_channels; i++)
312 if (SampleHandles[i] == channel)
313 SampleHandles[i] = -1;
317 int digi_start_sound_object(int obj)
322 if (!digi_initialised) return -1;
323 slot = get_free_slot();
325 if (slot<0) return -1;
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;
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;
347 memset(&dsbd, 0, sizeof(dsbd));
348 dsbd.dwSize = sizeof(dsbd);
349 dsbd.dwFlags = DSBCAPS_CTRLDEFAULT | DSBCAPS_GETCURRENTPOSITION2;
351 dsbd.dwBufferBytes = SoundSlots[slot].length;
352 dsbd.lpwfxFormat = &waveformat;
354 hr = IDirectSound_CreateSoundBuffer(lpds, &dsbd, &SoundSlots[slot].lpsb, NULL);
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);
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);
371 SoundObjects[obj].signature = next_signature++;
372 SoundObjects[obj].handle = slot;
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);
383 // Play the given sound number.
384 // Volume is max at F1_0.
385 void digi_play_sample( int soundno, fix max_volume )
388 if ( Newdemo_state == ND_STATE_RECORDING )
389 newdemo_record_sound( soundno );
391 soundno = digi_xlat_sound(soundno);
393 if (!digi_initialised) return;
395 if (soundno < 0 ) return;
397 digi_start_sound(soundno, max_volume, F0_5);
400 // Play the given sound number. If the sound is already playing,
402 void digi_play_sample_once( int soundno, fix max_volume )
407 if ( Newdemo_state == ND_STATE_RECORDING )
408 newdemo_record_sound( soundno );
410 soundno = digi_xlat_sound(soundno);
412 if (!digi_initialised) return;
414 if (soundno < 0 ) return;
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) {
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;
428 digi_start_sound(soundno, max_volume, F0_5);
432 void digi_play_sample_3d( int soundno, int angle, int volume, int no_dups ) // Volume from 0-0x7fff
437 if ( Newdemo_state == ND_STATE_RECORDING ) {
439 newdemo_record_sound_3d_once( soundno, angle, volume );
441 newdemo_record_sound_3d( soundno, angle, volume );
444 soundno = digi_xlat_sound(soundno);
446 if (!digi_initialised) return;
447 if (soundno < 0 ) return;
449 if (volume < MIN_VOLUME ) return;
450 digi_start_sound(soundno, volume, angle);
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 )
455 vms_vector vector_to_sound;
456 fix angle_from_ear, cosang,sinang;
463 max_distance = (max_distance*5)/4; // Make all sounds travel 1.25 times as far.
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 );
468 if (distance < max_distance ) {
469 int num_search_segs = f2i(max_distance/20);
470 if ( num_search_segs < 1 ) num_search_segs = 1;
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 ));
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;
489 int digi_link_sound_to_object2( int org_soundnum, short objnum, int forever, fix max_volume, fix max_distance )
495 soundnum = digi_xlat_sound(org_soundnum);
497 if ( max_volume < 0 ) return -1;
498 // if ( max_volume > F1_0 ) max_volume = F1_0;
500 if (!digi_initialised) return -1;
501 if (soundnum < 0 ) return -1;
502 if (GameSounds[soundnum].data==NULL) {
506 if ((objnum<0)||(objnum>Highest_object_index))
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 );
516 for (i=0; i<MAX_SOUND_OBJECTS; i++ )
517 if (SoundObjects[i].flags==0)
520 if (i==MAX_SOUND_OBJECTS) {
521 mprintf((1, "Too many sound objects!\n" ));
525 SoundObjects[i].signature=next_signature++;
526 SoundObjects[i].flags = SOF_USED | SOF_LINK_TO_OBJ;
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;
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 );
542 if (!forever || SoundObjects[i].volume >= MIN_VOLUME)
543 digi_start_sound_object(i);
545 return SoundObjects[i].signature;
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); }
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 )
556 soundnum = digi_xlat_sound(org_soundnum);
558 if ( max_volume < 0 ) return -1;
559 // if ( max_volume > F1_0 ) max_volume = F1_0;
561 if (!digi_initialised) return -1;
562 if (soundnum < 0 ) return -1;
563 if (GameSounds[soundnum].data==NULL) {
568 if ((segnum<0)||(segnum>Highest_segment_index))
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 );
578 for (i=0; i<MAX_SOUND_OBJECTS; i++ )
579 if (SoundObjects[i].flags==0)
582 if (i==MAX_SOUND_OBJECTS) {
583 mprintf((1, "Too many sound objects!\n" ));
588 SoundObjects[i].signature=next_signature++;
589 SoundObjects[i].flags = SOF_USED | SOF_LINK_TO_POS;
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 );
605 if (!forever || SoundObjects[i].volume >= MIN_VOLUME)
606 digi_start_sound_object(i);
608 return SoundObjects[i].signature;
611 int digi_link_sound_to_pos( int soundnum, short segnum, short sidenum, vms_vector * pos, int forever, fix max_volume )
613 return digi_link_sound_to_pos2( soundnum, segnum, sidenum, pos, forever, max_volume, F1_0 * 256 );
616 void digi_kill_sound_linked_to_segment( int segnum, int sidenum, int soundnum )
620 soundnum = digi_xlat_sound(soundnum);
622 if (!digi_initialised) return;
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) {
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;
640 SoundObjects[i].flags = 0; // Mark as dead, so some other sound can use this sound
645 // If this assert happens, it means that there were 2 sounds
646 // 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 SoundSlots[SoundObjects[i].handle].playing = 0;
665 if (SoundSlots[SoundObjects[i].handle].lpsb) {
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;
674 SoundObjects[i].flags = 0; // Mark as dead, so some other sound can use this sound
679 // If this assert happens, it means that there were 2 sounds
680 // that got deleted. Weird, get John.
682 mprintf( (1, "ERROR: More than 1 sounds were deleted from object %d\n", objnum ));
686 void digi_sync_sounds()
689 int oldvolume, oldpan;
691 if (!digi_initialised) return;
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;
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...
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 );
714 } else if ( SoundObjects[i].flags & SOF_LINK_TO_OBJ ) {
717 objp = &Objects[SoundObjects[i].lo_objnum];
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) {
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;
732 SoundObjects[i].flags = 0; // Mark as dead, so some other sound can use this sound
733 continue; // Go on to next sound...
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 );
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) {
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;
754 SoundObjects[i].flags &= ~SOF_PLAYING; // Mark sound as not playing
757 if (!(SoundObjects[i].flags & SOF_PLAYING)) {
758 digi_start_sound_object(i);
760 SoundSlots[SoundObjects[i].handle].volume = fixmuldiv(SoundObjects[i].volume,digi_volume,F1_0);
765 if (oldpan != SoundObjects[i].pan) {
766 if (SoundObjects[i].flags & SOF_PLAYING)
767 SoundSlots[SoundObjects[i].handle].pan = SoundObjects[i].pan;
773 void digi_init_sounds()
777 if (!digi_initialised) return;
779 digi_reset_digi_sounds();
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) {
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;
795 SoundObjects[i].flags = 0; // Mark as dead, so some other sound can use this sound
797 digi_sounds_initialized = 1;
800 //added on 980905 by adb from original source to make sfx volume work
801 void digi_set_digi_volume( int dvolume )
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 )
809 digi_volume = dvolume;
811 if ( !digi_initialised ) return;
817 void digi_set_volume( int dvolume, int mvolume )
819 digi_set_digi_volume(dvolume);
820 digi_set_midi_volume(mvolume);
823 int digi_is_sound_playing(int soundno)
827 soundno = digi_xlat_sound(soundno);
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)
838 void digi_pause_all() { }
839 void digi_resume_all() { }
840 void digi_stop_all() { }
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;
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);
851 if ( !digi_initialised ) return;
853 digi_reset_digi_sounds();
856 int digi_get_max_channels() {
857 return digi_max_channels;
861 void digi_reset_digi_sounds() {
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;
877 //added on 980905 by adb to reset sound kill system
878 memset(SampleHandles, 255, sizeof(SampleHandles));
884 int digi_midi_song_playing = 0;
885 static int digi_initialised = 0;
886 int midi_volume = 255;
888 int digi_get_settings() { return 0; }
889 int digi_init() { digi_initialised = 1; return 0; }
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; }
901 void digi_play_sample_3d( int soundno, int angle, int volume, int no_dups ) {} // Volume from 0-0x7fff
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 ) {}
908 void digi_set_digi_volume( int dvolume ) {}
909 void digi_set_volume( int dvolume, int mvolume ) {}
911 int digi_is_sound_playing(int soundno) { return 0; }
913 void digi_pause_all() {}
914 void digi_resume_all() {}
915 void digi_stop_all() {}
917 void digi_set_max_channels(int n) {}
918 int digi_get_max_channels() { return 0; }
923 // MIDI stuff follows.
925 void digi_stop_current_song()
927 if ( digi_midi_song_playing ) {
930 digi_midi_song_playing = 0;
934 void digi_set_midi_volume( int n )
945 // scale up from 0-127 to 0-0xffff
946 mm_volume = (midi_volume << 1) | (midi_volume & 1);
947 mm_volume |= (mm_volume << 8);
950 midiOutSetVolume((HMIDIOUT)hmp->hmidi, mm_volume | mm_volume << 16);
953 void digi_play_midi_song( char * filename, char * melodic_bank, char * drum_bank, int loop )
955 if (!digi_initialised) return;
957 digi_stop_current_song();
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
964 if ((hmp = hmp_open(filename))) {
966 digi_midi_song_playing = 1;
967 digi_set_midi_volume(midi_volume);
970 printf("hmp_open failed\n");
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 ) {}