Debian Codegraph @

similar/main/digiobj.cpp

raw
1 /*
2 * Portions of this file are copyright Rebirth contributors and licensed as
3 * described in COPYING.txt.
4 * Portions of this file are copyright Parallax Software and licensed
5 * according to the Parallax license below.
6 * See COPYING.txt for license details.
7
8 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
9 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
10 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
11 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
12 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
13 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
14 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
15 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
16 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
17 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
18 */
19
20 #include "dxxsconf.h"
21 #include <algorithm>
22 #include <stdexcept>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <fcntl.h>
26 #include <string.h>
27 #include <ctype.h>
28 #include <ranges>
29
30 #include "maths.h"
31 #include "object.h"
32 #include "timer.h"
33 #include "joy.h"
34 #include "digi.h"
35 #include "digi_audio.h"
36 #include "sounds.h"
37 #include "args.h"
38 #include "newdemo.h"
39 #include "gameseg.h"
40 #include "dxxerror.h"
41 #include "wall.h"
42 #include "piggy.h"
43 #include "kconfig.h"
44 #include "config.h"
45
46 #include "compiler-range_for.h"
47 #include "d_levelstate.h"
48 #include <iterator>
49 #include <utility>
50
51 using std::max;
52
53 namespace dcx {
54
55 namespace {
56
57 constexpr std::integral_constant<unsigned, 1> SOF_USED{}; // Set if this sample is used
58 constexpr std::integral_constant<unsigned, 4> SOF_LINK_TO_OBJ{}; // Sound is linked to a moving object. If object dies, then finishes play and quits.
59 constexpr std::integral_constant<unsigned, 8> SOF_LINK_TO_POS{}; // Sound is linked to segment, pos
60 constexpr std::integral_constant<unsigned, 16> SOF_PLAY_FOREVER{}; // Play forever (or until level is stopped), otherwise plays once
61 constexpr std::integral_constant<unsigned, 32> SOF_PERMANENT{}; // Part of the level, like a waterfall or fan
62
63 constexpr std::integral_constant<unsigned, 150> MAX_SOUND_OBJECTS{};
64
65 }
66
67 sound_channel SoundQ_channel;
68
69 struct sound_object
70 {
71 short signature; // A unique signature to this sound
72 ubyte flags; // Used to tell if this slot is used and/or currently playing, and how long.
73 ubyte pad; // Keep alignment
74 fix max_volume; // Max volume that this sound is playing at
75 vm_distance max_distance; // The max distance that this sound can be heard at...
76 int volume; // Volume that this sound is playing at
77 sound_pan pan; // Pan value that this sound is playing at
78 sound_channel channel; // What channel this is playing on, sound_channel::None if not playing
79 sound_effect soundnum; // The sound number that is playing
80 int loop_start; // The start point of the loop. -1 means no loop
81 int loop_end; // The end point of the loop
82 union link {
83 constexpr link() :
84 pos{}
85 {
86 }
87 struct {
88 segnum_t segnum; // Used if SOF_LINK_TO_POS field is used
89 sidenum_t sidenum;
90 vms_vector position;
91 } pos;
92 struct {
93 objnum_t objnum; // Used if SOF_LINK_TO_OBJ field is used
94 object_signature_t objsignature;
95 } obj;
96 } link_type;
97 };
98
99 namespace {
100
101 using sound_objects_t = std::array<sound_object, MAX_SOUND_OBJECTS>;
102 static sound_objects_t SoundObjects;
103 static short next_signature=0;
104
105 static int N_active_sound_objects;
106
107 static void digi_kill_sound(sound_object &s)
108 {
109 s.flags = 0; // Mark as dead, so some other sound can use this sound
110 if (s.channel != sound_channel::None)
111 {
112 N_active_sound_objects--;
113 digi_stop_sound(std::exchange(s.channel, sound_channel::None));
114 }
115 }
116
117 static std::pair<sound_objects_t::iterator, sound_objects_t::iterator> find_sound_object_flags0(sound_objects_t &SoundObjects)
118 {
119 const auto eso = SoundObjects.end();
120 const auto &&i{std::ranges::find(SoundObjects.begin(), eso, 0, &sound_object::flags)};
121 return {i, eso};
122 }
123
124 static std::pair<sound_objects_t::iterator, sound_objects_t::iterator> find_sound_object(sound_objects_t &SoundObjects, const unsigned soundnum, const vcobjidx_t obj, const sound_stack once)
125 {
126 if (once == sound_stack::allow_stacking)
127 return find_sound_object_flags0(SoundObjects);
128 const auto eso = SoundObjects.end();
129 sound_objects_t::iterator free_sound_object = eso;
130 for (auto i = SoundObjects.begin(); i != eso; ++i)
131 {
132 constexpr uint8_t used_obj = SOF_USED | SOF_LINK_TO_OBJ;
133 auto &so = *i;
134 const auto flags{so.flags};
135 if (flags == 0)
136 free_sound_object = i;
137 else if (so.soundnum == soundnum && (flags & used_obj) == used_obj)
138 {
139 /* No check for a signature match here. If the signature
140 * matches, this sound will be reclaimed to prevent
141 * stacking. If the signature does not match, then the old
142 * sound is tied to a dead object and needs to be removed.
143 */
144 if (so.link_type.obj.objnum == obj)
145 {
146 digi_kill_sound(so);
147 return {i, eso};
148 }
149 }
150 }
151 return {free_sound_object, eso};
152 }
153
154 }
155
156 }
157
158 namespace dsx {
159
160 /* Find the sound which actually equates to a sound number */
161 sound_effect digi_xlat_sound(sound_effect soundno)
162 {
163 if (soundno == sound_effect::None)
164 return sound_effect::None;
165
166 if (CGameArg.SysLowMem)
167 {
168 soundno = AltSounds[soundno];
169 if (soundno == sound_effect::None)
170 return soundno;
171 }
172
173 //Assert(Sounds[soundno] != 255); //if hit this, probably using undefined sound
174 return Sounds[soundno];
175 }
176
177 namespace {
178
179 static sound_effect digi_unxlat_sound(const sound_effect soundno)
180 {
181 if (soundno == sound_effect::None)
182 return soundno;
183
184 auto &table = (CGameArg.SysLowMem ? AltSounds : Sounds);
185 auto b = std::begin(table);
186 auto e = std::end(table);
187 auto i = std::find(b, e, soundno);
188 if (i != e)
189 return static_cast<sound_effect>(std::distance(b, i));
190 throw std::invalid_argument("sound not loaded");
191 }
192
193 static std::pair<int, sound_pan> digi_get_sound_loc(const vms_matrix &listener, const vms_vector &listener_pos, const vcsegptridx_t listener_seg, const vms_vector &sound_pos, const vcsegptridx_t sound_seg, fix max_volume, vm_distance max_distance)
194 {
195
196 vms_vector vector_to_sound;
197 fix angle_from_ear;
198
199 max_distance = (max_distance*5)/4; // Make all sounds travel 1.25 times as far.
200
201 // Warning: Made the vm_vec_normalized_dir be vm_vec_normalized_dir_quick and got illegal values to acos in the fang computation.
202 auto distance = vm_vec_normalized_dir_quick( vector_to_sound, sound_pos, listener_pos );
203
204 if (distance < max_distance ) {
205 int num_search_segs = f2i(max_distance/20);
206 if ( num_search_segs < 1 ) num_search_segs = 1;
207
208 const auto path_distance = find_connected_distance(listener_pos, listener_seg, sound_pos, sound_seg, num_search_segs, wall_is_doorway_mask::fly_rendpast);
209 if ( path_distance > -1 ) {
210 const int volume = max_volume - fixdiv(path_distance,max_distance);
211 if (volume > 0)
212 {
213 angle_from_ear = vm_vec_delta_ang_norm(listener.rvec,vector_to_sound,listener.uvec);
214 auto cosang = fix_cos(angle_from_ear);
215 if (CGameCfg.ReverseStereo)
216 cosang *= -1;
217 return {volume, sound_pan{(cosang + F1_0) / 2}};
218 }
219 }
220 }
221 return {};
222 }
223
224 static void digi_update_sound_loc(const vms_matrix &listener, const vms_vector &listener_pos, const vcsegptridx_t listener_seg, const vms_vector &sound_pos, const vcsegptridx_t sound_seg, sound_object &so)
225 {
226 auto &&[volume, pan] = digi_get_sound_loc(listener, listener_pos, listener_seg, sound_pos, sound_seg, so.max_volume, so.max_distance);
227 so.volume = volume;
228 so.pan = pan;
229 }
230
231 }
232
233 void digi_play_sample_once(sound_effect soundno, fix max_volume)
234 {
235 if ( Newdemo_state == ND_STATE_RECORDING )
236 newdemo_record_sound( soundno );
237
238 soundno = digi_xlat_sound(soundno);
239
240 // start the sample playing
241 digi_start_sound(soundno, max_volume, sound_pan{0x7fff}, 0, -1, -1, sound_object_none);
242 }
243
244 void digi_play_sample(sound_effect soundno, fix max_volume)
245 {
246 if ( Newdemo_state == ND_STATE_RECORDING )
247 newdemo_record_sound( soundno );
248
249 soundno = digi_xlat_sound(soundno);
250
251 // start the sample playing
252 digi_start_sound(soundno, max_volume, sound_pan{0x7fff}, 0, -1, -1, sound_object_none);
253 }
254
255 void digi_play_sample_3d(sound_effect soundno, const sound_pan angle, int volume)
256 {
257 if ( Newdemo_state == ND_STATE_RECORDING ) {
258 newdemo_record_sound_3d_once( soundno, angle, volume );
259 }
260
261 soundno = digi_xlat_sound(soundno);
262
263 if (volume < 10 ) return;
264
265 // start the sample playing
266 digi_start_sound( soundno, volume, angle, 0, -1, -1, sound_object_none);
267 }
268
269 namespace {
270
271 static void SoundQ_init();
272 static void SoundQ_process();
273 static void SoundQ_pause();
274
275 }
276
277 void digi_init_sounds()
278 {
279 SoundQ_init();
280
281 digi_stop_all_channels();
282
283 digi_stop_looping_sound();
284 range_for (auto &i, SoundObjects)
285 {
286 i.channel = sound_channel::None;
287 i.flags = 0; // Mark as dead, so some other sound can use this sound
288 }
289 N_active_sound_objects = 0;
290 }
291
292 namespace {
293
294 // plays a sample that loops forever.
295 // Call digi_stop_channe(channel) to stop it.
296 // Call digi_set_channel_volume(channel, volume) to change volume.
297 // if loop_start is -1, entire sample loops
298 // Returns the channel that sound is playing on, or -1 if can't play.
299 // This could happen because of no sound drivers loaded or not enough channels.
300 static sound_effect digi_looping_sound{sound_effect::None};
301 static int digi_looping_volume;
302 static int digi_looping_start = -1;
303 static int digi_looping_end = -1;
304 static sound_channel digi_looping_channel = sound_channel::None;
305
306 static void digi_play_sample_looping_sub()
307 {
308 if (const auto s{digi_looping_sound}; s != sound_effect::None)
309 digi_looping_channel = digi_start_sound(s, digi_looping_volume, sound_pan{0x7fff}, 1, digi_looping_start, digi_looping_end, sound_object_none);
310 }
311
312 }
313
314 void digi_play_sample_looping(sound_effect soundno, fix max_volume,int loop_start, int loop_end)
315 {
316 soundno = digi_xlat_sound(soundno);
317
318 if (soundno == sound_effect::None)
319 return;
320
321 if (digi_looping_channel != sound_channel::None)
322 digi_stop_sound( digi_looping_channel );
323
324 digi_looping_sound = soundno;
325 digi_looping_volume = max_volume;
326 digi_looping_start = loop_start;
327 digi_looping_end = loop_end;
328 digi_play_sample_looping_sub();
329 }
330
331 void digi_change_looping_volume( fix volume )
332 {
333 digi_looping_volume = volume;
334 if (digi_looping_channel != sound_channel::None)
335 digi_set_channel_volume( digi_looping_channel, volume );
336 }
337
338 namespace {
339
340 static void digi_pause_looping_sound()
341 {
342 const auto c{digi_looping_channel};
343 if (c != sound_channel::None)
344 {
345 digi_looping_channel = sound_channel::None;
346 digi_stop_sound(c);
347 }
348 }
349
350 }
351
352 void digi_stop_looping_sound()
353 {
354 digi_looping_sound = sound_effect::None;
355 digi_pause_looping_sound();
356 }
357
358 namespace {
359
360 static void digi_unpause_looping_sound()
361 {
362 digi_play_sample_looping_sub();
363 }
364
365 }
366
367 //hack to not start object when loading level
368 int Dont_start_sound_objects{0};
369
370 namespace {
371
372 static void digi_start_sound_object(sound_object &s)
373 {
374 // start sample structures
375 s.channel = sound_channel::None;
376
377 if ( s.volume <= 0 )
378 return;
379
380 if ( Dont_start_sound_objects )
381 return;
382
383 // only use up to half the sound channels for "permanent" sounts
384 if ((s.flags & SOF_PERMANENT) && (N_active_sound_objects >= max(1, digi_max_channels / 4)))
385 return;
386
387 // start the sample playing
388
389 s.channel = digi_start_sound( s.soundnum,
390 s.volume,
391 s.pan,
392 s.flags & SOF_PLAY_FOREVER,
393 s.loop_start,
394 s.loop_end, &s);
395
396 if (s.channel != sound_channel::None)
397 N_active_sound_objects++;
398 }
399
400 static void digi_link_sound_common(const object_base &viewer, sound_object &so, const vms_vector &pos, const int forever, const fix max_volume, const vm_distance max_distance, const sound_effect soundnum, const vcsegptridx_t segnum)
401 {
402 so.signature=next_signature++;
403 if ( forever )
404 so.flags |= SOF_PLAY_FOREVER;
405 so.soundnum = soundnum;
406 so.max_volume = max_volume;
407 so.max_distance = max_distance;
408 so.volume = 0;
409 so.pan = {};
410 if (Dont_start_sound_objects) { //started at level start
411 so.flags |= SOF_PERMANENT;
412 so.channel = sound_channel::None;
413 }
414 else
415 {
416 digi_update_sound_loc(viewer.orient, viewer.pos, segnum.absolute_sibling(viewer.segnum), pos, segnum, so);
417 digi_start_sound_object(so);
418 // If it's a one-shot sound effect, and it can't start right away, then
419 // just cancel it and be done with it.
420 if (so.channel == sound_channel::None && !(so.flags & SOF_PLAY_FOREVER))
421 {
422 so.flags = 0;
423 return;
424 }
425 }
426 }
427
428 }
429
430 void digi_link_sound_to_object3(const sound_effect org_soundnum, const vcobjptridx_t objnum, const uint8_t forever, const fix max_volume, const sound_stack once, const vm_distance max_distance, const int loop_start, const int loop_end)
431 {
432 auto &viewer = *Viewer;
433 const auto soundnum{digi_xlat_sound(org_soundnum)};
434
435 if (max_volume < 0)
436 return;
437 // if ( max_volume > F1_0 ) max_volume = F1_0;
438
439 if (soundnum == sound_effect::None)
440 return;
441 if (!GameSounds[soundnum].data)
442 {
443 Int3();
444 return;
445 }
446
447 if ( Newdemo_state == ND_STATE_RECORDING ) {
448 if ( !forever ) { // forever flag is not recorded, use original limited sound objects hack for demo recording
449 auto segnum = vcsegptridx(objnum->segnum);
450 const auto &&[volume, pan] = digi_get_sound_loc(viewer.orient, viewer.pos, segnum.absolute_sibling(viewer.segnum),
451 objnum->pos, segnum, max_volume,
452 max_distance);
453 newdemo_record_sound_3d_once( org_soundnum, pan, volume );
454 } else
455 newdemo_record_link_sound_to_object3( org_soundnum, objnum, max_volume, max_distance, loop_start, loop_end );
456 }
457
458 const auto &&f = find_sound_object(SoundObjects, soundnum, objnum, once);
459 if (f.first == f.second)
460 return;
461 auto &so = *f.first;
462 so.flags = SOF_USED | SOF_LINK_TO_OBJ;
463 so.link_type.obj.objnum = objnum;
464 so.link_type.obj.objsignature = objnum->signature;
465 so.loop_start = loop_start;
466 so.loop_end = loop_end;
467 digi_link_sound_common(viewer, so, objnum->pos, forever, max_volume, max_distance, soundnum, vcsegptridx(objnum->segnum));
468 }
469
470 void digi_link_sound_to_object2(const sound_effect soundnum, const vcobjptridx_t objnum, const uint8_t forever, const fix max_volume, const sound_stack once, const vm_distance max_distance)
471 {
472 digi_link_sound_to_object3(soundnum, objnum, forever, max_volume, once, max_distance, -1, -1);
473 }
474
475 void digi_link_sound_to_object(const sound_effect soundnum, const vcobjptridx_t objnum, const uint8_t forever, const fix max_volume, const sound_stack once)
476 {
477 digi_link_sound_to_object2(soundnum, objnum, forever, max_volume, once, vm_distance{256*F1_0});
478 }
479
480 namespace {
481
482 static void digi_link_sound_to_pos2(const sound_effect org_soundnum, const vcsegptridx_t segnum, const sidenum_t sidenum, const vms_vector &pos, int forever, fix max_volume, const vm_distance max_distance)
483 {
484 auto &viewer = *Viewer;
485 const auto soundnum{digi_xlat_sound(org_soundnum)};
486
487 if (max_volume < 0)
488 return;
489 // if ( max_volume > F1_0 ) max_volume = F1_0;
490
491 if (soundnum == sound_effect::None)
492 return;
493 if (!GameSounds[soundnum].data)
494 {
495 Int3();
496 return;
497 }
498 if (!forever)
499 {
500 // Hack to keep sounds from building up...
501 const auto &&[volume, pan] = digi_get_sound_loc(viewer.orient, viewer.pos, segnum.absolute_sibling(viewer.segnum), pos, segnum, max_volume, max_distance);
502 digi_play_sample_3d(org_soundnum, pan, volume);
503 return;
504 }
505
506 auto f = find_sound_object_flags0(SoundObjects);
507 if (f.first == f.second)
508 return;
509 auto &so = *f.first;
510 so.flags = SOF_USED | SOF_LINK_TO_POS;
511 so.link_type.pos.segnum = segnum;
512 so.link_type.pos.sidenum = sidenum;
513 so.link_type.pos.position = pos;
514 so.loop_start = so.loop_end = -1;
515 digi_link_sound_common(viewer, so, pos, forever, max_volume, max_distance, soundnum, segnum);
516 }
517
518 }
519
520 void digi_link_sound_to_pos(const sound_effect soundnum, const vcsegptridx_t segnum, const sidenum_t sidenum, const vms_vector &pos, const int forever, const fix max_volume)
521 {
522 digi_link_sound_to_pos2(soundnum, segnum, sidenum, pos, forever, max_volume, vm_distance{F1_0 * 256});
523 }
524
525 //if soundnum==-1, kill any sound
526 void digi_kill_sound_linked_to_segment(const vmsegidx_t segnum, const sidenum_t sidenum, sound_effect soundnum)
527 {
528 if (soundnum != sound_effect::None)
529 soundnum = digi_xlat_sound(soundnum);
530 range_for (auto &i, SoundObjects)
531 {
532 if ( (i.flags & SOF_USED) && (i.flags & SOF_LINK_TO_POS) ) {
533 if ((i.link_type.pos.segnum == segnum) && (i.link_type.pos.sidenum==sidenum) && (soundnum == sound_effect::None || i.soundnum==soundnum))
534 {
535 digi_kill_sound(i);
536 }
537 }
538 }
539 }
540
541 void digi_kill_sound_linked_to_object(const vcobjptridx_t objnum)
542 {
543 if ( Newdemo_state == ND_STATE_RECORDING ) {
544 newdemo_record_kill_sound_linked_to_object( objnum );
545 }
546 range_for (auto &i, SoundObjects)
547 {
548 if ( (i.flags & SOF_USED) && (i.flags & SOF_LINK_TO_OBJ ) ) {
549 if (i.link_type.obj.objnum == objnum) {
550 digi_kill_sound(i);
551 }
552 }
553 }
554 }
555
556 namespace {
557
558 // John's new function, 2/22/96.
559 static void digi_record_sound_objects()
560 {
561 range_for (auto &s, SoundObjects)
562 {
563 if ((s.flags & SOF_USED) && (s.flags & SOF_LINK_TO_OBJ) && (s.flags & SOF_PLAY_FOREVER))
564 {
565 newdemo_record_link_sound_to_object3( digi_unxlat_sound(s.soundnum), s.link_type.obj.objnum,
566 s.max_volume, s.max_distance, s.loop_start, s.loop_end );
567 }
568 }
569 }
570
571 static int was_recording = 0;
572
573 }
574
575 void digi_sync_sounds()
576 {
577 int oldvolume;
578
579 if ( Newdemo_state == ND_STATE_RECORDING) {
580 if ( !was_recording ) {
581 digi_record_sound_objects();
582 }
583 was_recording = 1;
584 } else {
585 was_recording = 0;
586 }
587
588 SoundQ_process();
589 if (!Viewer)
590 return;
591 auto &Objects = LevelUniqueObjectState.Objects;
592 auto &vcobjptr = Objects.vcptr;
593 const auto viewer{Viewer};
594 range_for (auto &s, SoundObjects)
595 {
596 if (s.flags & SOF_USED)
597 {
598 oldvolume = s.volume;
599 const auto oldpan{s.pan};
600
601 if ( !(s.flags & SOF_PLAY_FOREVER) ) {
602 // Check if its done.
603 if (s.channel != sound_channel::None)
604 {
605 if ( !digi_is_channel_playing(s.channel) ) {
606 digi_end_sound( s.channel );
607 s.flags = 0; // Mark as dead, so some other sound can use this sound
608 N_active_sound_objects--;
609 continue; // Go on to next sound...
610 }
611 }
612 }
613
614 if ( s.flags & SOF_LINK_TO_POS ) {
615 digi_update_sound_loc(viewer->orient, viewer->pos, vcsegptridx(viewer->segnum), s.link_type.pos.position, vcsegptridx(s.link_type.pos.segnum), s);
616 } else if ( s.flags & SOF_LINK_TO_OBJ ) {
617 auto &objp{[&vcobjptr, &s]() -> const object & {
618 if (Newdemo_state != ND_STATE_PLAYBACK)
619 return vcobjptr(s.link_type.obj.objnum);
620 auto objnum = newdemo_find_object(s.link_type.obj.objsignature);
621 if (objnum != object_none)
622 return vcobjptr_t{objnum};
623 return vcobjptr(object_first);
624 }()};
625
626 if ((objp.type==OBJ_NONE) || (objp.signature!=s.link_type.obj.objsignature)) {
627 // The object that this is linked to is dead, so just end this sound if it is looping.
628 if (s.channel != sound_channel::None)
629 {
630 if (s.flags & SOF_PLAY_FOREVER)
631 digi_stop_sound( s.channel );
632 else
633 digi_end_sound( s.channel );
634 N_active_sound_objects--;
635 }
636 s.flags = 0; // Mark as dead, so some other sound can use this sound
637 continue; // Go on to next sound...
638 } else {
639 digi_update_sound_loc(viewer->orient, viewer->pos, vcsegptridx(viewer->segnum), objp.pos, vcsegptridx(objp.segnum), s);
640 }
641 }
642
643 if (oldvolume != s.volume) {
644 if ( s.volume < 1 ) {
645 // Sound is too far away, so stop it from playing.
646
647 const auto c{s.channel};
648 if (c != sound_channel::None)
649 {
650 s.channel = sound_channel::None;
651 if (s.flags & SOF_PLAY_FOREVER)
652 digi_stop_sound(c);
653 else
654 digi_end_sound(c);
655 N_active_sound_objects--;
656 }
657
658 if (! (s.flags & SOF_PLAY_FOREVER)) {
659 s.flags = 0; // Mark as dead, so some other sound can use this sound
660 continue;
661 }
662
663 } else {
664 if (s.channel == sound_channel::None)
665 {
666 digi_start_sound_object(s);
667 } else {
668 digi_set_channel_volume( s.channel, s.volume );
669 }
670 }
671 }
672
673 if (oldpan != s.pan) {
674 if (s.channel != sound_channel::None)
675 digi_set_channel_pan( s.channel, s.pan );
676 }
677
678 }
679 }
680 }
681
682 void digi_pause_digi_sounds()
683 {
684 digi_pause_looping_sound();
685 range_for (auto &s, SoundObjects)
686 {
687 if (!(s.flags & SOF_USED))
688 continue;
689 const auto c{s.channel};
690 if (c != sound_channel::None)
691 {
692 s.channel = sound_channel::None;
693 if (! (s.flags & SOF_PLAY_FOREVER))
694 s.flags = 0; // Mark as dead, so some other sound can use this sound
695 N_active_sound_objects--;
696 digi_stop_sound(c);
697 }
698 }
699
700 digi_stop_all_channels();
701 SoundQ_pause();
702 }
703
704 void digi_resume_digi_sounds()
705 {
706 digi_sync_sounds(); //don't think we really need to do this, but can't hurt
707 digi_unpause_looping_sound();
708 }
709
710 // Called by the code in digi.c when another sound takes this sound object's
711 // slot because the sound was done playing.
712 void digi_end_soundobj(sound_object &s)
713 {
714 Assert(s.flags & SOF_USED);
715 assert(s.channel != sound_channel::None);
716
717 N_active_sound_objects--;
718 s.channel = sound_channel::None;
719 }
720
721 void digi_stop_digi_sounds()
722 {
723 digi_stop_looping_sound();
724 range_for (auto &s, SoundObjects)
725 {
726 if (s.flags & SOF_USED)
727 {
728 if (s.channel != sound_channel::None)
729 {
730 digi_stop_sound( s.channel );
731 N_active_sound_objects--;
732 }
733 s.flags = 0; // Mark as dead, so some other sound can use this sound
734 }
735 }
736
737 digi_stop_all_channels();
738 SoundQ_init();
739 }
740
741 #ifndef NDEBUG
742 void verify_sound_channel_free(const sound_channel channel)
743 {
744 const auto predicate = [channel](const sound_object &s) {
745 return (s.flags & SOF_USED) && s.channel == channel;
746 };
747 if (std::any_of(SoundObjects.begin(), SoundObjects.end(), predicate))
748 throw std::runtime_error("sound busy");
749 }
750 #endif
751
752 namespace {
753
754 struct sound_q
755 {
756 fix64 time_added;
757 sound_effect soundnum;
758 };
759
760 #define MAX_LIFE F1_0*30 // After being queued for 30 seconds, don't play it
761 static int SoundQ_head, SoundQ_tail, SoundQ_num;
762 static std::array<sound_q, 32> SoundQ;
763
764 void SoundQ_init()
765 {
766 SoundQ_head = SoundQ_tail = 0;
767 SoundQ_num = 0;
768 SoundQ_channel = sound_channel::None;
769 }
770
771 void SoundQ_pause()
772 {
773 SoundQ_channel = sound_channel::None;
774 }
775
776 }
777
778 void SoundQ_end()
779 {
780 // Current playing sound is stopped, so take it off the Queue
781 SoundQ_head = (SoundQ_head+1);
782 if (SoundQ_head >= SoundQ.size())
783 SoundQ_head = 0;
784 SoundQ_num--;
785 SoundQ_channel = sound_channel::None;
786 }
787
788 namespace {
789
790 void SoundQ_process()
791 {
792 if (const auto channel{SoundQ_channel}; channel != sound_channel::None)
793 {
794 if (digi_is_channel_playing(channel))
795 return;
796 SoundQ_end();
797 }
798
799 while ( SoundQ_head != SoundQ_tail ) {
800 sound_q * q = &SoundQ[SoundQ_head];
801
802 if ( q->time_added+MAX_LIFE > timer_query() ) {
803 SoundQ_channel = digi_start_sound(q->soundnum, F1_0+1, sound_pan{0x7fff}, 0, -1, -1, sound_object_none);
804 return;
805 } else {
806 // expired; remove from Queue
807 SoundQ_end();
808 }
809 }
810 }
811 }
812
813 void digi_start_sound_queued(sound_effect soundnum, fix volume)
814 {
815 int i;
816
817 soundnum = digi_xlat_sound(soundnum);
818
819 if (soundnum == sound_effect::None)
820 return;
821
822 i = SoundQ_tail+1;
823 if (i >= SoundQ.size())
824 i = 0;
825
826 // Make sure its loud so it doesn't get cancelled!
827 if ( volume < F1_0+1 )
828 volume = F1_0 + 1;
829
830 if ( i != SoundQ_head ) {
831 SoundQ[SoundQ_tail].time_added = timer_query();
832 SoundQ[SoundQ_tail].soundnum = soundnum;
833 SoundQ_num++;
834 SoundQ_tail = i;
835 }
836
837 // Try to start it!
838 SoundQ_process();
839 }
840
841 }