2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
47 #define SOF_USED 1 // Set if this sample is used
48 #define SOF_PLAYING 2 // Set if this sample is playing on a channel
49 #define SOF_LINK_TO_OBJ 4 // Sound is linked to a moving object. If object dies, then finishes play and quits.
50 #define SOF_LINK_TO_POS 8 // Sound is linked to segment, pos
51 #define SOF_PLAY_FOREVER 16 // Play forever (or until level is stopped), otherwise plays once
52 #define SOF_PERMANENT 32 // Part of the level, like a waterfall or fan
54 typedef struct sound_object {
55 short signature; // A unique signature to this sound
56 ubyte flags; // Used to tell if this slot is used and/or currently playing, and how long.
57 ubyte pad; // Keep alignment
58 fix max_volume; // Max volume that this sound is playing at
59 fix max_distance; // The max distance that this sound can be heard at...
60 int volume; // Volume that this sound is playing at
61 int pan; // Pan value that this sound is playing at
62 int channel; // What channel this is playing on, -1 if not playing
63 short soundnum; // The sound number that is playing
64 int loop_start; // The start point of the loop. -1 means no loop
65 int loop_end; // The end point of the loop
68 short segnum; // Used if SOF_LINK_TO_POS field is used
73 short objnum; // Used if SOF_LINK_TO_OBJ field is used
79 #define MAX_SOUND_OBJECTS 150
80 sound_object SoundObjects[MAX_SOUND_OBJECTS];
81 short next_signature=0;
83 int N_active_sound_objects=0;
85 int digi_sounds_initialized=0;
89 /* Find the sound which actually equates to a sound number */
90 int digi_xlat_sound(int soundno)
97 soundno = AltSounds[soundno];
102 Assert(Sounds[soundno] != 255); //if hit this, probably using undefined sound
104 return Sounds[soundno];
107 int digi_unxlat_sound(int soundno)
110 ubyte *table = (digi_lomem?AltSounds:Sounds);
112 if ( soundno < 0 ) return -1;
114 for (i=0;i<MAX_SOUNDS;i++)
115 if (table[i] == soundno)
123 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 )
126 vms_vector vector_to_sound;
127 fix angle_from_ear, cosang,sinang;
134 max_distance = (max_distance*5)/4; // Make all sounds travel 1.25 times as far.
136 // Warning: Made the vm_vec_normalized_dir be vm_vec_normalized_dir_quick and got illegal values to acos in the fang computation.
137 distance = vm_vec_normalized_dir_quick( &vector_to_sound, sound_pos, listener_pos );
139 if (distance < max_distance ) {
140 int num_search_segs = f2i(max_distance/20);
141 if ( num_search_segs < 1 ) num_search_segs = 1;
143 path_distance = find_connected_distance(listener_pos, listener_seg, sound_pos, sound_seg, num_search_segs, WID_RENDPAST_FLAG+WID_FLY_FLAG );
144 if ( path_distance > -1 ) {
145 *volume = max_volume - fixdiv(path_distance,max_distance);
146 //mprintf( (0, "Sound path distance %.2f, volume is %d / %d\n", f2fl(distance), *volume, max_volume ));
148 angle_from_ear = vm_vec_delta_ang_norm(&listener->rvec,&vector_to_sound,&listener->uvec);
149 fix_sincos(angle_from_ear,&sinang,&cosang);
150 //mprintf( (0, "volume is %.2f\n", f2fl(*volume) ));
151 if (Config_channels_reversed.intval) cosang *= -1;
152 *pan = (cosang + F1_0)/2;
161 void digi_play_sample_once( int soundno, fix max_volume )
166 if ( Newdemo_state == ND_STATE_RECORDING )
167 newdemo_record_sound( soundno );
169 soundno = digi_xlat_sound(soundno);
171 if (soundno < 0 ) return;
173 channel=digi_find_channel(soundno);
175 digi_stop_sound( channel );
177 // start the sample playing
178 digi_start_sound( soundno, max_volume, 0xffff/2, 0, -1, -1, -1 );
182 void digi_play_sample( int soundno, fix max_volume )
185 if ( Newdemo_state == ND_STATE_RECORDING )
186 newdemo_record_sound( soundno );
188 soundno = digi_xlat_sound(soundno);
190 if (soundno < 0 ) return;
192 // start the sample playing
193 digi_start_sound( soundno, max_volume, 0xffff/2, 0, -1, -1, -1 );
197 void digi_play_sample_3d( int soundno, int angle, int volume, int no_dups )
203 if ( Newdemo_state == ND_STATE_RECORDING ) {
205 newdemo_record_sound_3d_once( soundno, angle, volume );
207 newdemo_record_sound_3d( soundno, angle, volume );
210 soundno = digi_xlat_sound(soundno);
212 if (soundno < 0 ) return;
214 if (volume < 10 ) return;
216 // start the sample playing
217 digi_start_sound( soundno, volume, angle, 0, -1, -1, -1 );
222 void SoundQ_process();
225 void digi_init_sounds()
231 digi_stop_all_channels();
233 digi_stop_looping_sound();
234 for (i=0; i<MAX_SOUND_OBJECTS; i++ ) {
235 SoundObjects[i].channel = -1;
236 SoundObjects[i].flags = 0; // Mark as dead, so some other sound can use this sound
238 N_active_sound_objects = 0;
239 digi_sounds_initialized = 1;
242 extern int digi_max_channels;
244 // plays a sample that loops forever.
245 // Call digi_stop_channe(channel) to stop it.
246 // Call digi_set_channel_volume(channel, volume) to change volume.
247 // if loop_start is -1, entire sample loops
248 // Returns the channel that sound is playing on, or -1 if can't play.
249 // This could happen because of no sound drivers loaded or not enough channels.
250 int digi_looping_sound = -1;
251 int digi_looping_volume = 0;
252 int digi_looping_start = -1;
253 int digi_looping_end = -1;
254 int digi_looping_channel = -1;
256 void digi_play_sample_looping_sub()
258 if ( digi_looping_sound > -1 )
259 digi_looping_channel = digi_start_sound( digi_looping_sound, digi_looping_volume, 0xFFFF/2, 1, digi_looping_start, digi_looping_end, -1 );
262 void digi_play_sample_looping( int soundno, fix max_volume,int loop_start, int loop_end )
264 soundno = digi_xlat_sound(soundno);
266 if (soundno < 0 ) return;
268 if (digi_looping_channel>-1)
269 digi_stop_sound( digi_looping_channel );
271 digi_looping_sound = soundno;
272 digi_looping_volume = max_volume;
273 digi_looping_start = loop_start;
274 digi_looping_end = loop_end;
275 digi_play_sample_looping_sub();
278 void digi_change_looping_volume( fix volume )
280 if ( digi_looping_channel > -1 )
281 digi_set_channel_volume( digi_looping_channel, volume );
282 digi_looping_volume = volume;
285 void digi_stop_looping_sound()
287 if ( digi_looping_channel > -1 )
288 digi_stop_sound( digi_looping_channel );
289 digi_looping_channel = -1;
290 digi_looping_sound = -1;
293 void digi_pause_looping_sound()
295 if ( digi_looping_channel > -1 )
296 digi_stop_sound( digi_looping_channel );
297 digi_looping_channel = -1;
300 void digi_unpause_looping_sound()
302 digi_play_sample_looping_sub();
305 //hack to not start object when loading level
306 int Dont_start_sound_objects = 0;
308 void digi_start_sound_object(int i)
310 // start sample structures
311 SoundObjects[i].channel = -1;
313 if ( SoundObjects[i].volume <= 0 )
316 if ( Dont_start_sound_objects )
319 // -- MK, 2/22/96 -- if ( Newdemo_state == ND_STATE_RECORDING )
320 // -- MK, 2/22/96 -- newdemo_record_sound_3d_once( digi_unxlat_sound(SoundObjects[i].soundnum), SoundObjects[i].pan, SoundObjects[i].volume );
322 // only use up to half the sound channels for "permanent" sounts
323 if ((SoundObjects[i].flags & SOF_PERMANENT) && (N_active_sound_objects >= max(1, digi_get_max_channels() / 4)))
326 // start the sample playing
328 SoundObjects[i].channel = digi_start_sound( SoundObjects[i].soundnum,
329 SoundObjects[i].volume,
331 SoundObjects[i].flags & SOF_PLAY_FOREVER,
332 SoundObjects[i].loop_start,
333 SoundObjects[i].loop_end, i );
335 if (SoundObjects[i].channel > -1 )
336 N_active_sound_objects++;
339 //sounds longer than this get their 3d aspects updated
340 #define SOUND_3D_THRESHHOLD (digi_sample_rate * 3 / 2) //1.5 seconds
342 int digi_link_sound_to_object3( int org_soundnum, short objnum, int forever, fix max_volume, fix max_distance, int loop_start, int loop_end )
349 soundnum = digi_xlat_sound(org_soundnum);
351 if ( max_volume < 0 ) return -1;
352 // if ( max_volume > F1_0 ) max_volume = F1_0;
354 if (soundnum < 0 ) return -1;
355 if (GameSounds[soundnum].data==NULL) {
359 if ((objnum<0)||(objnum>Highest_object_index))
362 if ( !forever ) { // && GameSounds[soundnum - SOUND_OFFSET].length < SOUND_3D_THRESHHOLD) {
363 // Hack to keep sounds from building up...
364 digi_get_sound_loc( &Viewer->orient, &Viewer->pos, Viewer->segnum, &Objects[objnum].pos, Objects[objnum].segnum, max_volume,&volume, &pan, max_distance );
365 digi_play_sample_3d( org_soundnum, pan, volume, 0 );
370 if ( Newdemo_state == ND_STATE_RECORDING ) {
371 newdemo_record_link_sound_to_object3( org_soundnum, objnum, max_volume, max_distance, loop_start, loop_end );
375 for (i=0; i<MAX_SOUND_OBJECTS; i++ )
376 if (SoundObjects[i].flags==0)
379 if (i==MAX_SOUND_OBJECTS) {
380 mprintf((1, "Too many sound objects!\n" ));
384 SoundObjects[i].signature=next_signature++;
385 SoundObjects[i].flags = SOF_USED | SOF_LINK_TO_OBJ;
387 SoundObjects[i].flags |= SOF_PLAY_FOREVER;
388 SoundObjects[i].link_type.obj.objnum = objnum;
389 SoundObjects[i].link_type.obj.objsignature = Objects[objnum].signature;
390 SoundObjects[i].max_volume = max_volume;
391 SoundObjects[i].max_distance = max_distance;
392 SoundObjects[i].volume = 0;
393 SoundObjects[i].pan = 0;
394 SoundObjects[i].soundnum = soundnum;
395 SoundObjects[i].loop_start = loop_start;
396 SoundObjects[i].loop_end = loop_end;
398 if (Dont_start_sound_objects) { //started at level start
400 SoundObjects[i].flags |= SOF_PERMANENT;
401 SoundObjects[i].channel = -1;
404 objp = &Objects[SoundObjects[i].link_type.obj.objnum];
405 digi_get_sound_loc( &Viewer->orient, &Viewer->pos, Viewer->segnum,
406 &objp->pos, objp->segnum, SoundObjects[i].max_volume,
407 &SoundObjects[i].volume, &SoundObjects[i].pan, SoundObjects[i].max_distance );
409 digi_start_sound_object(i);
411 // If it's a one-shot sound effect, and it can't start right away, then
412 // just cancel it and be done with it.
413 if ( (SoundObjects[i].channel < 0) && (!(SoundObjects[i].flags & SOF_PLAY_FOREVER)) ) {
414 SoundObjects[i].flags = 0;
419 return SoundObjects[i].signature;
422 int digi_link_sound_to_object2( int org_soundnum, short objnum, int forever, fix max_volume, fix max_distance )
424 return digi_link_sound_to_object3( org_soundnum, objnum, forever, max_volume, max_distance, -1, -1 );
428 int digi_link_sound_to_object( int soundnum, short objnum, int forever, fix max_volume )
430 return digi_link_sound_to_object2( soundnum, objnum, forever, max_volume, 256*F1_0 );
433 int digi_link_sound_to_pos2( int org_soundnum, short segnum, short sidenum, vms_vector * pos, int forever, fix max_volume, fix max_distance )
439 soundnum = digi_xlat_sound(org_soundnum);
441 if ( max_volume < 0 ) return -1;
442 // if ( max_volume > F1_0 ) max_volume = F1_0;
444 if (soundnum < 0 ) return -1;
445 if (GameSounds[soundnum].data==NULL) {
450 if ((segnum<0)||(segnum>Highest_segment_index))
453 if ( !forever ) { //&& GameSounds[soundnum - SOUND_OFFSET].length < SOUND_3D_THRESHHOLD) {
454 // Hack to keep sounds from building up...
455 digi_get_sound_loc( &Viewer->orient, &Viewer->pos, Viewer->segnum, pos, segnum, max_volume, &volume, &pan, max_distance );
456 digi_play_sample_3d( org_soundnum, pan, volume, 0 );
460 for (i=0; i<MAX_SOUND_OBJECTS; i++ )
461 if (SoundObjects[i].flags==0)
464 if (i==MAX_SOUND_OBJECTS) {
465 mprintf((1, "Too many sound objects!\n" ));
470 SoundObjects[i].signature=next_signature++;
471 SoundObjects[i].flags = SOF_USED | SOF_LINK_TO_POS;
473 SoundObjects[i].flags |= SOF_PLAY_FOREVER;
474 SoundObjects[i].link_type.pos.segnum = segnum;
475 SoundObjects[i].link_type.pos.sidenum = sidenum;
476 SoundObjects[i].link_type.pos.position = *pos;
477 SoundObjects[i].soundnum = soundnum;
478 SoundObjects[i].max_volume = max_volume;
479 SoundObjects[i].max_distance = max_distance;
480 SoundObjects[i].volume = 0;
481 SoundObjects[i].pan = 0;
482 SoundObjects[i].loop_start = SoundObjects[i].loop_end = -1;
484 if (Dont_start_sound_objects) { //started at level start
486 SoundObjects[i].flags |= SOF_PERMANENT;
488 SoundObjects[i].channel = -1;
492 digi_get_sound_loc( &Viewer->orient, &Viewer->pos, Viewer->segnum,
493 &SoundObjects[i].link_type.pos.position, SoundObjects[i].link_type.pos.segnum, SoundObjects[i].max_volume,
494 &SoundObjects[i].volume, &SoundObjects[i].pan, SoundObjects[i].max_distance );
496 digi_start_sound_object(i);
498 // If it's a one-shot sound effect, and it can't start right away, then
499 // just cancel it and be done with it.
500 if ( (SoundObjects[i].channel < 0) && (!(SoundObjects[i].flags & SOF_PLAY_FOREVER)) ) {
501 SoundObjects[i].flags = 0;
506 return SoundObjects[i].signature;
509 int digi_link_sound_to_pos( int soundnum, short segnum, short sidenum, vms_vector * pos, int forever, fix max_volume )
511 return digi_link_sound_to_pos2( soundnum, segnum, sidenum, pos, forever, max_volume, F1_0 * 256 );
514 //if soundnum==-1, kill any sound
515 void digi_kill_sound_linked_to_segment( int segnum, int sidenum, int soundnum )
520 soundnum = digi_xlat_sound(soundnum);
525 for (i=0; i<MAX_SOUND_OBJECTS; i++ ) {
526 if ( (SoundObjects[i].flags & SOF_USED) && (SoundObjects[i].flags & SOF_LINK_TO_POS) ) {
527 if ((SoundObjects[i].link_type.pos.segnum == segnum) && (SoundObjects[i].link_type.pos.sidenum==sidenum) && (soundnum==-1 || SoundObjects[i].soundnum==soundnum )) {
528 if ( SoundObjects[i].channel > -1 ) {
529 digi_stop_sound( SoundObjects[i].channel );
530 N_active_sound_objects--;
532 SoundObjects[i].channel = -1;
533 SoundObjects[i].flags = 0; // Mark as dead, so some other sound can use this sound
538 // If this assert happens, it means that there were 2 sounds
539 // that got deleted. Weird, get John.
541 mprintf( (1, "ERROR: More than 1 sounds were deleted from seg %d\n", segnum ));
545 void digi_kill_sound_linked_to_object( int objnum )
553 if ( Newdemo_state == ND_STATE_RECORDING ) {
554 newdemo_record_kill_sound_linked_to_object( objnum );
558 for (i=0; i<MAX_SOUND_OBJECTS; i++ ) {
559 if ( (SoundObjects[i].flags & SOF_USED) && (SoundObjects[i].flags & SOF_LINK_TO_OBJ ) ) {
560 if (SoundObjects[i].link_type.obj.objnum == objnum) {
561 if ( SoundObjects[i].channel > -1 ) {
562 digi_stop_sound( SoundObjects[i].channel );
563 N_active_sound_objects--;
565 SoundObjects[i].channel = -1;
566 SoundObjects[i].flags = 0; // Mark as dead, so some other sound can use this sound
571 // If this assert happens, it means that there were 2 sounds
572 // that got deleted. Weird, get John.
574 mprintf( (1, "ERROR: More than 1 sounds were deleted from object %d\n", objnum ));
579 // John's new function, 2/22/96.
580 void digi_record_sound_objects()
584 for (i=0; i<MAX_SOUND_OBJECTS; i++ ) {
585 if ( (SoundObjects[i].flags & SOF_USED)&&
586 (SoundObjects[i].flags & SOF_LINK_TO_OBJ)&&
587 (SoundObjects[i].flags & SOF_PLAY_FOREVER)
591 newdemo_record_link_sound_to_object3( digi_unxlat_sound(SoundObjects[i].soundnum), SoundObjects[i].link_type.obj.objnum,
592 SoundObjects[i].max_volume, SoundObjects[i].max_distance, SoundObjects[i].loop_start, SoundObjects[i].loop_end );
597 int was_recording = 0;
599 void digi_sync_sounds()
602 int oldvolume, oldpan;
604 if ( Newdemo_state == ND_STATE_RECORDING) {
605 if ( !was_recording ) {
606 digi_record_sound_objects();
615 for (i=0; i<MAX_SOUND_OBJECTS; i++ ) {
616 if ( SoundObjects[i].flags & SOF_USED ) {
617 oldvolume = SoundObjects[i].volume;
618 oldpan = SoundObjects[i].pan;
620 if ( !(SoundObjects[i].flags & SOF_PLAY_FOREVER) ) {
621 // Check if its done.
622 if (SoundObjects[i].channel > -1 ) {
623 if ( !digi_is_channel_playing(SoundObjects[i].channel) ) {
624 digi_end_sound( SoundObjects[i].channel );
625 SoundObjects[i].flags = 0; // Mark as dead, so some other sound can use this sound
626 N_active_sound_objects--;
627 continue; // Go on to next sound...
632 if ( SoundObjects[i].flags & SOF_LINK_TO_POS ) {
633 digi_get_sound_loc( &Viewer->orient, &Viewer->pos, Viewer->segnum,
634 &SoundObjects[i].link_type.pos.position, SoundObjects[i].link_type.pos.segnum, SoundObjects[i].max_volume,
635 &SoundObjects[i].volume, &SoundObjects[i].pan, SoundObjects[i].max_distance );
637 } else if ( SoundObjects[i].flags & SOF_LINK_TO_OBJ ) {
641 if ( Newdemo_state == ND_STATE_PLAYBACK ) {
643 objnum = newdemo_find_object( SoundObjects[i].link_type.obj.objsignature );
645 objp = &Objects[objnum];
650 objp = &Objects[SoundObjects[i].link_type.obj.objnum];
653 if ((objp->type==OBJ_NONE) || (objp->signature!=SoundObjects[i].link_type.obj.objsignature)) {
654 // The object that this is linked to is dead, so just end this sound if it is looping.
655 if ( SoundObjects[i].channel>-1 ) {
656 if (SoundObjects[i].flags & SOF_PLAY_FOREVER)
657 digi_stop_sound( SoundObjects[i].channel );
659 digi_end_sound( SoundObjects[i].channel );
660 N_active_sound_objects--;
662 SoundObjects[i].flags = 0; // Mark as dead, so some other sound can use this sound
663 continue; // Go on to next sound...
665 digi_get_sound_loc( &Viewer->orient, &Viewer->pos, Viewer->segnum,
666 &objp->pos, objp->segnum, SoundObjects[i].max_volume,
667 &SoundObjects[i].volume, &SoundObjects[i].pan, SoundObjects[i].max_distance );
671 if (oldvolume != SoundObjects[i].volume) {
672 if ( SoundObjects[i].volume < 1 ) {
673 // Sound is too far away, so stop it from playing.
675 if ( SoundObjects[i].channel>-1 ) {
676 if (SoundObjects[i].flags & SOF_PLAY_FOREVER)
677 digi_stop_sound( SoundObjects[i].channel );
679 digi_end_sound( SoundObjects[i].channel );
680 N_active_sound_objects--;
681 SoundObjects[i].channel = -1;
684 if (! (SoundObjects[i].flags & SOF_PLAY_FOREVER)) {
685 SoundObjects[i].flags = 0; // Mark as dead, so some other sound can use this sound
690 if (SoundObjects[i].channel<0) {
691 digi_start_sound_object(i);
693 digi_set_channel_volume( SoundObjects[i].channel, SoundObjects[i].volume );
698 if (oldpan != SoundObjects[i].pan) {
699 if (SoundObjects[i].channel>-1)
700 digi_set_channel_pan( SoundObjects[i].channel, SoundObjects[i].pan );
707 // digi_sound_debug();
711 void digi_pause_digi_sounds()
716 digi_pause_looping_sound();
718 for (i=0; i<MAX_SOUND_OBJECTS; i++ ) {
719 if ( (SoundObjects[i].flags & SOF_USED) && (SoundObjects[i].channel>-1) ) {
720 digi_stop_sound( SoundObjects[i].channel );
721 if (! (SoundObjects[i].flags & SOF_PLAY_FOREVER))
722 SoundObjects[i].flags = 0; // Mark as dead, so some other sound can use this sound
723 N_active_sound_objects--;
724 SoundObjects[i].channel = -1;
728 digi_stop_all_channels();
732 void digi_pause_all()
735 digi_pause_digi_sounds();
738 void digi_resume_digi_sounds()
740 digi_sync_sounds(); //don't think we really need to do this, but can't hurt
741 digi_unpause_looping_sound();
744 extern void digi_resume_midi();
746 void digi_resume_all()
749 digi_unpause_looping_sound();
752 // Called by the code in digi.c when another sound takes this sound object's
753 // slot because the sound was done playing.
754 void digi_end_soundobj(int i)
756 Assert( SoundObjects[i].flags & SOF_USED );
757 Assert( SoundObjects[i].channel > -1 );
759 N_active_sound_objects--;
760 SoundObjects[i].channel = -1;
763 void digi_stop_digi_sounds()
767 digi_stop_looping_sound();
769 for (i=0; i<MAX_SOUND_OBJECTS; i++ ) {
770 if ( SoundObjects[i].flags & SOF_USED ) {
771 if ( SoundObjects[i].channel > -1 ) {
772 digi_stop_sound( SoundObjects[i].channel );
773 N_active_sound_objects--;
775 SoundObjects[i].flags = 0; // Mark as dead, so some other sound can use this sound
779 digi_stop_all_channels();
785 digi_stop_current_song();
787 digi_stop_digi_sounds();
791 int verify_sound_channel_free( int channel )
794 for (i=0; i<MAX_SOUND_OBJECTS; i++ ) {
795 if ( SoundObjects[i].flags & SOF_USED ) {
796 if ( SoundObjects[i].channel == channel ) {
797 mprintf(( 0, "ERROR STARTING SOUND CHANNEL ON USED SLOT!!\n" ));
805 void digi_sound_debug()
808 int n_active_sound_objs=0;
811 for (i=0; i<MAX_SOUND_OBJECTS; i++ ) {
812 if ( SoundObjects[i].flags & SOF_USED ) {
814 if ( SoundObjects[i].channel > -1 )
815 n_active_sound_objs++;
818 mprintf_at(( 0, 0, 0, "DIGI: Active Sound Objects: %d,%d/%d (%d max) \n", n_active_sound_objs,N_active_sound_objects, n_sound_objs, MAX_SOUND_OBJECTS ));
819 mprintf_at(( 0, 1, 0, "DIGI: Looping sound: %s, snd=%d, vol=%d, ch=%d \n", (digi_looping_sound>-1?"ON":"OFF"), digi_looping_sound, digi_looping_volume, digi_looping_channel ));
825 typedef struct sound_q {
831 #define MAX_LIFE F1_0*30 // After being queued for 30 seconds, don't play it
832 sound_q SoundQ[MAX_Q];
833 int SoundQ_head, SoundQ_tail, SoundQ_num;
838 SoundQ_head = SoundQ_tail = 0;
850 // Current playing sound is stopped, so take it off the Queue
851 SoundQ_head = (SoundQ_head+1);
852 if ( SoundQ_head >= MAX_Q ) SoundQ_head = 0;
857 void SoundQ_process()
859 fix curtime = timer_get_approx_seconds();
861 if ( SoundQ_channel > -1 ) {
862 if ( digi_is_channel_playing(SoundQ_channel) )
867 if ( SoundQ_num > 0 ) {
868 mprintf(( 0, "SQ:%d ", SoundQ_num ));
871 while ( SoundQ_head != SoundQ_tail ) {
872 sound_q * q = &SoundQ[SoundQ_head];
874 if ( q->time_added+MAX_LIFE > curtime ) {
875 SoundQ_channel = digi_start_sound(q->soundnum, F1_0+1, 0xFFFF/2, 0, -1, -1, -1 );
878 // expired; remove from Queue
885 void digi_start_sound_queued( short soundnum, fix volume )
889 soundnum = digi_xlat_sound(soundnum);
891 if (soundnum < 0 ) return;
894 if ( i>=MAX_Q ) i = 0;
896 // Make sure its loud so it doesn't get cancelled!
897 if ( volume < F1_0+1 )
900 if ( i != SoundQ_head ) {
901 SoundQ[SoundQ_tail].time_added = timer_get_approx_seconds();
902 SoundQ[SoundQ_tail].soundnum = soundnum;
906 mprintf(( 0, "Sound Q full!\n" ));