Skip to content

Commit c4ee72c

Browse files
committed
Support pausing music for music function enabled
1 parent c4c5406 commit c4ee72c

File tree

2 files changed

+92
-15
lines changed

2 files changed

+92
-15
lines changed

KeyAsio.Gui/Realtime/RealtimeModeManager.cs

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public class RealtimeModeManager : ViewModelBase
4141
private int _score;
4242
private Beatmap _beatmap;
4343
private bool _isStarted;
44+
private bool _previousSelectSongStatus = true;
45+
private int _pauseCount = 0;
4446

4547
private readonly object _isStartedLock = new();
4648
private readonly HitsoundFileCache _hitsoundFileCache = new();
@@ -645,6 +647,13 @@ private async void OnStatusChanged(OsuListenerManager.OsuStatus pre, OsuListener
645647
_result = true;
646648
_singleSynchronousTrack.PlayMods = ModsInfo.Mods.None;
647649
}
650+
else if (pre != OsuListenerManager.OsuStatus.NoFoundProcess && cur == OsuListenerManager.OsuStatus.NoFoundProcess)
651+
{
652+
if (AppSettings.RealtimeOptions.EnableMusicFunctions)
653+
{
654+
_selectSongTrack.StopCurrentMusic(2000);
655+
}
656+
}
648657
else
649658
{
650659
_selectSongTrack.StartLowPass(200, 16000);
@@ -655,7 +664,8 @@ private async void OnStatusChanged(OsuListenerManager.OsuStatus pre, OsuListener
655664

656665
private void OnBeatmapChanged(Beatmap? beatmap)
657666
{
658-
if (OsuStatus == OsuListenerManager.OsuStatus.SelectSong && beatmap != null)
667+
if (OsuStatus is OsuListenerManager.OsuStatus.SelectSong or
668+
OsuListenerManager.OsuStatus.Idle && beatmap != null)
659669
{
660670
var coosu = OsuFile.ReadFromFile(beatmap.FilenameFull, k =>
661671
{
@@ -674,17 +684,47 @@ private void OnBeatmapChanged(Beatmap? beatmap)
674684
_audioFilePath = audioFilePath;
675685
_selectSongTrack.StopCurrentMusic(200);
676686
_selectSongTrack.PlaySingleAudio(coosu, audioFilePath, coosu.General.PreviewTime);
687+
_previousSelectSongStatus = true;
688+
_pauseCount = 0;
677689
}
678690
}
679691

680692
private void OnPlayModsChanged(ModsInfo.Mods oldMods, ModsInfo.Mods newMods)
681693
{
682694
}
683695

684-
private void OnPlayTimeChanged(int oldMs, int newMs)
696+
private void OnFetchedPlayTimeChanged(int oldMs, int newMs, bool paused = false)
685697
{
698+
const int selectSongPauseThreshold = 20;
699+
const int playingPauseThreshold = 5;
700+
if (paused && _previousSelectSongStatus)
701+
{
702+
_pauseCount++;
703+
}
704+
else if (!paused)
705+
{
706+
_pauseCount = 0;
707+
}
708+
709+
var enableMusicFunctions = AppSettings.RealtimeOptions.EnableMusicFunctions;
710+
if (enableMusicFunctions && OsuStatus is OsuListenerManager.OsuStatus.SelectSong or
711+
OsuListenerManager.OsuStatus.Idle)
712+
{
713+
if (_pauseCount >= selectSongPauseThreshold && _previousSelectSongStatus)
714+
{
715+
_selectSongTrack.PauseCurrentMusic();
716+
_previousSelectSongStatus = false;
717+
}
718+
else if (_pauseCount < selectSongPauseThreshold && !_previousSelectSongStatus)
719+
{
720+
_selectSongTrack.RecoverCurrentMusic();
721+
_previousSelectSongStatus = true;
722+
}
723+
}
724+
686725
if (IsStarted && oldMs > newMs) // Retry
687726
{
727+
_pauseCount = 0;
688728
_selectSongTrack.StopCurrentMusic();
689729
_selectSongTrack.StartLowPass(200, 16000);
690730
_firstStartInitialized = true;
@@ -697,25 +737,32 @@ private void OnPlayTimeChanged(int oldMs, int newMs)
697737
return;
698738
}
699739

700-
if (IsStarted && AppSettings.RealtimeOptions.EnableMusicFunctions)
740+
if (enableMusicFunctions && IsStarted)
701741
{
702742
if (_firstStartInitialized && OsuFile != null && AudioFilename != null && _folder != null && SharedViewModel.Instance.AudioEngine != null)
703743
{
704-
var musicPath = Path.Combine(_folder, AudioFilename);
705-
if (CachedSoundFactory.ContainsCache(musicPath))
744+
if (_pauseCount >= playingPauseThreshold)
745+
{
746+
_singleSynchronousTrack.ClearAudio();
747+
}
748+
else
706749
{
707-
//todo: online offset && local offset
708-
const int codeLatency = -1;
709-
const int osuForceLatency = 15;
710-
var oldMapForceOffset = OsuFile.Version < 5 ? 24 : 0;
711-
_singleSynchronousTrack.Offset = osuForceLatency + codeLatency + oldMapForceOffset;
712-
_singleSynchronousTrack.LeadInMilliseconds = OsuFile.General.AudioLeadIn;
713-
if (!_result)
750+
var musicPath = Path.Combine(_folder, AudioFilename);
751+
if (CachedSoundFactory.ContainsCache(musicPath))
714752
{
715-
_singleSynchronousTrack.PlayMods = PlayMods;
753+
//todo: online offset && local offset
754+
const int codeLatency = -1;
755+
const int osuForceLatency = 15;
756+
var oldMapForceOffset = OsuFile.Version < 5 ? 24 : 0;
757+
_singleSynchronousTrack.Offset = osuForceLatency + codeLatency + oldMapForceOffset;
758+
_singleSynchronousTrack.LeadInMilliseconds = OsuFile.General.AudioLeadIn;
759+
if (!_result)
760+
{
761+
_singleSynchronousTrack.PlayMods = PlayMods;
762+
}
763+
764+
_singleSynchronousTrack.SyncAudio(CachedSoundFactory.GetCacheSound(musicPath), newMs);
716765
}
717-
718-
_singleSynchronousTrack.SyncAudio(CachedSoundFactory.GetCacheSound(musicPath), newMs);
719766
}
720767
}
721768
}

KeyAsio.Gui/Realtime/Tracks/SelectSongTrack.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,36 @@ public void StartLowPass(int fadeMilliseconds, int targetVol)
126126
});
127127
}
128128

129+
public async void PauseCurrentMusic()
130+
{
131+
SmartWaveReader? smartWaveReader;
132+
FadeInOutSampleProvider? fadeInOutSampleProvider;
133+
lock (_instanceLock)
134+
{
135+
smartWaveReader = _smartWaveReader;
136+
fadeInOutSampleProvider = _fadeInOutSampleProvider;
137+
if (smartWaveReader is null || fadeInOutSampleProvider is null)
138+
return;
139+
}
140+
141+
Mixer?.RemoveMixerInput(fadeInOutSampleProvider);
142+
}
143+
144+
public async void RecoverCurrentMusic()
145+
{
146+
SmartWaveReader? smartWaveReader;
147+
FadeInOutSampleProvider? fadeInOutSampleProvider;
148+
lock (_instanceLock)
149+
{
150+
smartWaveReader = _smartWaveReader;
151+
fadeInOutSampleProvider = _fadeInOutSampleProvider;
152+
if (smartWaveReader is null || fadeInOutSampleProvider is null)
153+
return;
154+
}
155+
156+
Mixer?.AddMixerInput(fadeInOutSampleProvider);
157+
}
158+
129159
private static async ValueTask RepositionAndFadeIn(WaveStream waveStream, int playTime,
130160
FadeInOutSampleProvider fadeInOutSampleProvider, int fadeInMilliseconds)
131161
{

0 commit comments

Comments
 (0)