fix certain types of sounds firing a frame late

This commit is contained in:
minenice55 2024-01-24 00:17:35 -05:00
parent 23d9edf5fe
commit b94a70fbc6
5 changed files with 74 additions and 52 deletions

View file

@ -189,6 +189,7 @@ namespace HeavenStudio
RiqBeatmap chart = GameManager.instance.Beatmap;
double offset = chart.data.offset;
double dspTime = AudioSettings.dspTime;
dspStart = dspTime;
startPos = GetSongPosFromBeat(beat);
firstBeatOffset = offset;
@ -213,10 +214,6 @@ namespace HeavenStudio
musicSource.pitch = timelinePitch;
Debug.Log($"playback scheduled for dsptime {dspStart}");
}
if (musicSource.clip == null)
{
dspStart = dspTime;
}
songPosBeat = beat;
startBeat = songPosBeat;
@ -404,7 +401,7 @@ namespace HeavenStudio
}
}
double MapTimeToPitchChanges(double time)
public double MapTimeToPitchChanges(double time)
{
double counter = 0;
double lastChangeTime = 0;

View file

@ -21,7 +21,7 @@ namespace HeavenStudio
[Header("Lists")]
[NonSerialized] public RiqBeatmap Beatmap = new();
private List<GameObject> preloadedGames = new();
private Dictionary<string, GameObject> cachedGamePrefabs = new();
[NonSerialized] public ObjectPool<Sound> SoundObjects;
[Header("Components")]
@ -1092,9 +1092,11 @@ namespace HeavenStudio
{
ResetCamera(); // resetting camera before setting new minigame so minigames can set camera values in their awake call - Rasmus
Destroy(currentGameO);
GameObject prefab = GetGame(game);
if (prefab == null) return;
currentGameO = Instantiate(GetGame(game));
Destroy(currentGameO);
currentGameO = Instantiate(prefab);
if (currentGameO.TryGetComponent<Minigame>(out var minigame))
{
_currentMinigame = minigame;
@ -1112,6 +1114,7 @@ namespace HeavenStudio
public void DestroyGame()
{
cachedGamePrefabs.Clear();
SetGame("noGame");
}
@ -1152,19 +1155,26 @@ namespace HeavenStudio
.Select(x => GetGameInfo(x))
.Where(x => x != null)
.Where(x => !x.fxOnly)
.Select(x => x.LoadableName);
name = gameInfos.FirstOrDefault() ?? "noGame";
.Where(x => x.LoadableName is not "noGame" or "" or null);
if (gameInfos.Count() > 0)
{
gameInfo = gameInfos.FirstOrDefault();
if (gameInfo == null) return Resources.Load<GameObject>($"Games/noGame");
}
else
{
return Resources.Load<GameObject>($"Games/noGame");
}
}
if (gameInfo.usesAssetBundle)
{
//game is packed in an assetbundle, load from that instead
if (gameInfo.AssetsLoaded && gameInfo.LoadedPrefab != null) return gameInfo.LoadedPrefab;
// couldn't load cached prefab, try loading from assetbundle
try
{
return gameInfo.GetCommonAssetBundle().LoadAsset<GameObject>(name);
Debug.LogWarning($"Game prefab wasn't cached, loading from assetbundle for game {name}");
return gameInfo.LoadGamePrefab();
}
catch (Exception e)
{
@ -1172,10 +1182,14 @@ namespace HeavenStudio
return Resources.Load<GameObject>($"Games/{name}");
}
}
}
}
return Resources.Load<GameObject>($"Games/{name}");
}
else
{
Debug.LogWarning($"Game {name} not found, using noGame");
return Resources.Load<GameObject>($"Games/noGame");
}
}
public Minigames.Minigame GetGameInfo(string name)
{

View file

@ -479,9 +479,7 @@ namespace HeavenStudio
{
if (AssetsLoaded || !usesAssetBundle) return;
await UniTask.WhenAll(LoadCommonAssetBundleAsync(), LoadLocalizedAssetBundleAsync());
await UniTask.WhenAll(LoadGamePrefabAsync());
await UniTask.WhenAll(LoadCommonAudioClips());
await UniTask.WhenAll(LoadLocalizedAudioClips());
await UniTask.WhenAll(LoadGamePrefabAsync(), LoadCommonAudioClips(), LoadLocalizedAudioClips());
}
public async UniTask LoadCommonAssetBundleAsync()
@ -547,6 +545,14 @@ namespace HeavenStudio
}
}
public GameObject LoadGamePrefab()
{
if (!usesAssetBundle) return null;
loadedPrefab = GetCommonAssetBundle().LoadAsset<GameObject>(name);
return loadedPrefab;
}
public async UniTask LoadCommonAudioClips()
{
if (!commonLoaded) return;
@ -556,14 +562,6 @@ namespace HeavenStudio
var assets = bundleCommon.LoadAllAssetsAsync();
await assets;
// await UniTask.SwitchToThreadPool();
// foreach (var asset in assets.allAssets)
// {
// AudioClip clip = asset as AudioClip;
// commonAudioClips.Add(clip.name, clip);
// }
// await UniTask.SwitchToMainThread();
}
public async UniTask LoadLocalizedAudioClips()
@ -575,14 +573,6 @@ namespace HeavenStudio
var assets = bundleLocalized.LoadAllAssetsAsync();
await assets;
// await UniTask.SwitchToThreadPool();
// foreach (var asset in assets.allAssets)
// {
// AudioClip clip = asset as AudioClip;
// localeAudioClips.Add(clip.name, clip);
// }
// await UniTask.SwitchToMainThread();
}
public async UniTask UnloadAllAssets()

View file

@ -70,6 +70,7 @@ namespace HeavenStudio.Util
audioSource = GetComponent<AudioSource>();
cond = Conductor.instance;
double dspTime = AudioSettings.dspTime;
available = false;
audioSource.clip = clip;
@ -85,8 +86,8 @@ namespace HeavenStudio.Util
{
playInstant = true;
played = true;
startTime = AudioSettings.dspTime;
audioSource.PlayScheduled(startTime);
startTime = dspTime;
audioSource.Play();
}
else
{
@ -94,15 +95,17 @@ namespace HeavenStudio.Util
if (cond != null)
{
scheduledPitch = cond.SongPitch;
startTime = (AudioSettings.dspTime + (cond.GetSongPosFromBeat(beat) - cond.songPositionAsDouble) / (double)scheduledPitch) - offset;
startTime = (dspTime + (cond.GetSongPosFromBeat(beat) - cond.songPositionAsDouble) / (double)scheduledPitch) - offset;
}
if (scheduledPitch != 0 && AudioSettings.dspTime >= startTime)
if (scheduledPitch != 0 && dspTime >= startTime)
{
audioSource.PlayScheduled(startTime);
played = true;
queued = true;
}
}
Update();
}
private void Update()
@ -157,7 +160,7 @@ namespace HeavenStudio.Util
audioSource.UnPause();
}
scheduledPitch = cond.SongPitch;
startTime = (dspTime + (cond.GetSongPosFromBeat(beat) - cond.songPositionAsDouble) / (double)scheduledPitch);
startTime = (dspTime + (cond.GetSongPosFromBeat(beat) - cond.songPositionAsDouble) / (double)scheduledPitch) - offset;
if (queued)
audioSource.SetScheduledStartTime(startTime);
}
@ -203,7 +206,7 @@ namespace HeavenStudio.Util
{
if (looping && loopEndBeat != -1) // Looping sounds play forever unless params are set.
{
if (cond.songPositionInBeatsAsDouble > loopEndBeat)
if (cond.songPositionInBeatsAsDouble >= loopEndBeat)
{
KillLoop(fadeTime);
loopDone = true;
@ -257,12 +260,22 @@ namespace HeavenStudio.Util
{
if (!gameObject.activeSelf) return;
this.bendedPitch = bendedPitch;
if (bendTime == 0)
{
audioSource.pitch = bendedPitch;
return;
}
StartCoroutine(BendUpLoop(bendTime));
}
public void BendDown(float bendTime)
{
if (!gameObject.activeSelf) return;
if (bendTime == 0)
{
audioSource.pitch = pitch;
return;
}
StartCoroutine(BendDownLoop(bendTime));
}
@ -301,6 +314,11 @@ namespace HeavenStudio.Util
public void KillLoop(double fadeTime)
{
if (!gameObject.activeSelf) return;
if (fadeTime == 0)
{
GameManager.instance.SoundObjects.Release(this);
return;
}
StartCoroutine(FadeLoop(fadeTime));
}

View file

@ -48,5 +48,8 @@ MonoBehaviour:
- Assets/Scripts/LevelEditor/Timeline/SpecialTmeline/SectionDialog.cs
- Assets/Scripts/LevelEditor/Timeline/SpecialTmeline/TimelineObjs/SectionTimelineObj.cs
- Assets/Scripts/Games/DJSchool/Student.cs
- Assets/Scripts/UI/PauseMenu.cs
- Assets/Scripts/Util/Sound.cs
- Assets/Scripts/Conductor.cs
PathsToSkipImportEvent: []
PathsToIgnoreOverwriteSettingOnAttribute: []