@@ -337,14 +337,12 @@ class OpenALSoundManager: public ISoundManager
337
337
};
338
338
339
339
std::unordered_map<int , FadeState> m_sounds_fading;
340
- float m_fade_delay;
341
340
public:
342
341
OpenALSoundManager (SoundManagerSingleton *smg, OnDemandSoundFetcher *fetcher):
343
342
m_fetcher (fetcher),
344
343
m_device (smg->m_device.get()),
345
344
m_context (smg->m_context.get()),
346
- m_next_id (1 ),
347
- m_fade_delay (0 )
345
+ m_next_id (1 )
348
346
{
349
347
infostream << " Audio: Initialized: OpenAL " << std::endl;
350
348
}
@@ -616,38 +614,45 @@ class OpenALSoundManager: public ISoundManager
616
614
617
615
void fadeSound (int soundid, float step, float gain)
618
616
{
619
- m_sounds_fading[soundid] = FadeState (step, getSoundGain (soundid), gain);
617
+ // Ignore the command if step isn't valid.
618
+ if (step == 0 )
619
+ return ;
620
+ float current_gain = getSoundGain (soundid);
621
+ step = gain - current_gain > 0 ? abs (step) : -abs (step);
622
+ if (m_sounds_fading.find (soundid) != m_sounds_fading.end ()) {
623
+ auto current_fade = m_sounds_fading[soundid];
624
+ // Do not replace the fade if it's equivalent.
625
+ if (current_fade.target_gain == gain && current_fade.step == step)
626
+ return ;
627
+ m_sounds_fading.erase (soundid);
628
+ }
629
+ gain = rangelim (gain, 0 , 1 );
630
+ m_sounds_fading[soundid] = FadeState (step, current_gain, gain);
620
631
}
621
632
622
633
void doFades (float dtime)
623
634
{
624
- m_fade_delay += dtime;
625
-
626
- if (m_fade_delay < 0 . 1f )
627
- return ;
635
+ for ( auto i = m_sounds_fading. begin (); i != m_sounds_fading. end ();) {
636
+ FadeState& fade = i-> second ;
637
+ assert (fade. step != 0 );
638
+ fade. current_gain += (fade. step * dtime) ;
628
639
629
- float chkGain = 0 ;
630
- for (auto i = m_sounds_fading.begin ();
631
- i != m_sounds_fading.end ();) {
632
- if (i->second .step < 0 .f )
633
- chkGain = -(i->second .current_gain );
640
+ if (fade.step < 0 .f )
641
+ fade.current_gain = std::max (fade.current_gain , fade.target_gain );
634
642
else
635
- chkGain = i-> second .current_gain ;
643
+ fade. current_gain = std::min (fade .current_gain , fade. target_gain ) ;
636
644
637
- if (chkGain < i->second .target_gain ) {
638
- i->second .current_gain += (i->second .step * m_fade_delay);
639
- i->second .current_gain = rangelim (i->second .current_gain , 0 , 1 );
640
-
641
- updateSoundGain (i->first , i->second .current_gain );
642
- ++i;
643
- } else {
644
- if (i->second .target_gain <= 0 .f )
645
- stopSound (i->first );
645
+ if (fade.current_gain <= 0 .f )
646
+ stopSound (i->first );
647
+ else
648
+ updateSoundGain (i->first , fade.current_gain );
646
649
650
+ // The increment must happen during the erase call, or else it'll segfault.
651
+ if (fade.current_gain == fade.target_gain )
647
652
m_sounds_fading.erase (i++);
648
- }
653
+ else
654
+ i++;
649
655
}
650
- m_fade_delay = 0 ;
651
656
}
652
657
653
658
bool soundExists (int sound)
0 commit comments