Skip to content

Commit

Permalink
Eager Keysound Loading (Optimization)
Browse files Browse the repository at this point in the history
  • Loading branch information
Noisysundae committed Jul 23, 2022
1 parent 19512f2 commit 1f816cd
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 20 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Refer to the [Releases](https://github.com/Noisysundae/techmania-ns/releases) se

### Latest Versions

* **Windows:** [v1.1-ns (2022-07-22)](https://github.com/Noisysundae/techmania-ns/releases/tag/1.1-ns_20220722)
* **Android:** [v1.1-ns (2022-07-22)](https://github.com/Noisysundae/techmania-ns/releases/tag/1.1-ns_20220722)
* **Windows:** [v1.1-ns (2022-07-23)](https://github.com/Noisysundae/techmania-ns/releases/tag/1.1-ns_20220723)
* **Android:** [v1.1-ns (2022-07-23)](https://github.com/Noisysundae/techmania-ns/releases/tag/1.1-ns_20220723)
* **iOS:** [v1.0.2-ns (2022-04-03)](https://github.com/Noisysundae/techmania-ns/releases/tag/1.0.2-ns_20220403)

## Changes
Expand Down Expand Up @@ -90,6 +90,10 @@ Refer to the [Releases](https://github.com/Noisysundae/techmania-ns/releases) se

### Optimization

* Eager keysound loading
* This speeds up pattern load time, more apparent on faster storages (e.g. SSDs).
* The problem stemmed from the pre-optimize coroutine only loading one file per frame.
* Drawback: The game will freeze briefly while loading keysound.
* (dirty change) Reduce `GetComponent()` calls in the Pattern Editor's `Update()` loop.
* There are still resource hogs on note object culling (instantiate/destroy).
* A new design is pretty much needed.
Expand Down
70 changes: 53 additions & 17 deletions TECHMANIA/Assets/Scripts/Components/ResourceLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,24 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Networking;

public class ResourceLoader : MonoBehaviour
{
private struct RequestCollection {
public UnityWebRequest req;
public UnityWebRequestAsyncOperation op;
public RequestCollection (
UnityWebRequest r,
UnityWebRequestAsyncOperation o)
{
req = r;
op = o;
}
}
private struct AudioSamplesWithRate
{
public int rate;
Expand Down Expand Up @@ -142,7 +154,10 @@ private IEnumerator InnerCacheAudioResources(
UnityAction<float> progressCallback)
{
Options.TemporarilyDisableVSync();
int numLoaded = 0;
int numFiles = filenameWithFolder.Count;
Dictionary<string, RequestCollection>
requests = new Dictionary<string, RequestCollection>();
List<string> filesLoaded = new List<string>();
foreach (string file in filenameWithFolder)
{
string fileRelativePath = Paths.RelativePath(trackFolder, file);
Expand All @@ -169,26 +184,47 @@ private IEnumerator InnerCacheAudioResources(
UnityWebRequest request =
UnityWebRequestMultimedia.GetAudioClip(
Paths.FullPathToUri(file), AudioType.UNKNOWN);
yield return request.SendWebRequest();
requests.Add(
fileRelativePath,
new RequestCollection(
request,
request.SendWebRequest()));
}
else
{
filesLoaded.Add(fileRelativePath);
}
}

AudioClip clip;
string error;
GetAudioClipFromWebRequest(request,
out clip, out error);
if (clip == null)
Func<object> onRequestsYield = () =>
{
foreach (var req in requests.Where(e => e.Value.op.isDone))
{
string file = req.Key;
if (!filesLoaded.Contains(file))
{
cacheAudioCompleteCallback?.Invoke(error);
yield break;
AudioClip clip;
string error;
GetAudioClipFromWebRequest(req.Value.req,
out clip, out error);
if (clip == null)
{
cacheAudioCompleteCallback?.Invoke(error);
continue;
}
audioClips.Add(file, clip);
AddAudioSamples(file, clip);

filesLoaded.Add(file);
progressCallback?.Invoke(
(float) filesLoaded.Count / numFiles);
Debug.Log("Loaded: " + file);
}
audioClips.Add(fileRelativePath, clip);
AddAudioSamples(fileRelativePath, clip);
}

numLoaded++;
progressCallback?.Invoke((float)numLoaded /
filenameWithFolder.Count);
Debug.Log("Loaded: " + file);
}
return null;
};
while (filesLoaded.Count != numFiles)
yield return onRequestsYield();

yield return null; // Wait 1 more frame just in case
cacheAudioCompleteCallback?.Invoke(null);
Expand Down
2 changes: 1 addition & 1 deletion TECHMANIA/ProjectSettings/ProjectSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ PlayerSettings:
16:10: 1
16:9: 1
Others: 1
bundleVersion: 1.1-ns-20220722
bundleVersion: 1.1-ns-20220723
preloadedAssets: []
metroInputSource: 0
wsaTransparentSwapchain: 0
Expand Down

0 comments on commit 1f816cd

Please sign in to comment.