Skip to content

Commit

Permalink
async content loader, load music async
Browse files Browse the repository at this point in the history
  • Loading branch information
LazyDuchess committed Jun 28, 2024
1 parent f749586 commit 5396d7e
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 7 deletions.
5 changes: 2 additions & 3 deletions Assets/Scripts/OpenTS2/Audio/MusicController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,10 @@ private void OnLotLoaded()

private void PlayNextSong()
{
StopAllCoroutines();
var contentManager = ContentManager.Instance;
var currentSong = _currentMusicCategory.PopNextSong();
var songAsset = contentManager.GetAsset<AudioAsset>(currentSong.Key);
_tsAudioSource.Audio = songAsset;
_tsAudioSource.Play();
_tsAudioSource.PlayAsync(currentSong.Key);
}

private void OnSongEnd()
Expand Down
21 changes: 21 additions & 0 deletions Assets/Scripts/OpenTS2/Audio/TSAudioSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using NAudio.Wave;
using NAudio.Wave.SampleProviders;
using OpenTS2.Audio;
using OpenTS2.Common;
using OpenTS2.Content;
using OpenTS2.Content.DBPF;
using System;
Expand Down Expand Up @@ -38,6 +39,7 @@ public AudioAsset Audio
private float _timeAudioSourcePlaying = 0f;
private bool _audioClipPlaying = false;
private bool _paused = false;
private AsyncContentManager _asyncContentLoader;

public void Pause()
{
Expand Down Expand Up @@ -69,6 +71,23 @@ public void Play()
PlayInternal(Audio);
}

public void PlayAsync(ResourceKey audioResourceKey)
{
CleanUp();
StartCoroutine(PlayAsyncInternal(audioResourceKey));
}

private IEnumerator PlayAsyncInternal(ResourceKey key)
{
var audioRequest = _asyncContentLoader.RequestAsset(key);
while (!audioRequest.Finished)
yield return null;
if (audioRequest.Result == AsyncContentManager.Results.Success)
PlayInternal(audioRequest.Asset as AudioAsset);
else
Stop();
}

private void PlayInternal(AudioAsset asset)
{
if (asset is HitListAsset)
Expand All @@ -90,6 +109,7 @@ public void Stop()

private void Awake()
{
_asyncContentLoader = AsyncContentManager.Instance;
_audioSource = GetComponent<AudioSource>();
if (_audioSource == null)
{
Expand All @@ -100,6 +120,7 @@ private void Awake()

private void CleanUp()
{
StopAllCoroutines();
if (_waveOutEv != null)
{
_waveOutEv.PlaybackStopped -= WaveOutPlaybackStopped;
Expand Down
64 changes: 64 additions & 0 deletions Assets/Scripts/OpenTS2/Content/AsyncContentManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using OpenTS2.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace OpenTS2.Content
{
public class AsyncContentManager
{
public class Request
{
public bool Finished = false;
public Results Result;
public ResourceKey Key;
public AbstractAsset Asset;
}
public enum Results
{
Success,
Failed
}
public static AsyncContentManager Instance { get; private set; }
private Thread _thread;
private Queue<Request> _requests = new Queue<Request>();
private ContentManager _contentManager;
public AsyncContentManager()
{
Instance = this;
_contentManager = ContentManager.Instance;
_thread = new Thread(new ThreadStart(ThreadLoop));
_thread.IsBackground = true;
_thread.Name = "AsyncContentLoader";
_thread.Start();
}

public Request RequestAsset(ResourceKey key)
{
var request = new Request();
request.Key = key;
_requests.Enqueue(request);
return request;
}

private void ThreadLoop()
{
while (true)
{
if (_requests.Count > 0)
{
var request = _requests.Peek();
request.Asset = _contentManager.GetAsset(request.Key);
request.Result = request.Asset != null ? Results.Success : Results.Failed;
request.Finished = true;
_requests.Dequeue();
}
else
Thread.Sleep(1);
}
}
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/OpenTS2/Content/AsyncContentManager.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 14 additions & 4 deletions Assets/Scripts/OpenTS2/Content/DBPF/WAVAudioAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,28 @@ namespace OpenTS2.Content.DBPF
{
public class WAVAudioAsset : AudioAsset
{
public AudioClip Clip { get; private set; }
public AudioClip Clip
{
get
{
if (_clip == null)
_clip = WAV.ToAudioClip(_data, GlobalTGI.ToString());
return _clip;
}
}
private AudioClip _clip;
private byte[] _data;

public WAVAudioAsset(byte[] data)
{
Clip = WAV.ToAudioClip(data, GlobalTGI.ToString());
_data = data;
}

public override void FreeUnmanagedResources()
{
if (Clip == null)
if (_clip == null)
return;
Clip.Free();
_clip.Free();
}
}
}
1 change: 1 addition & 0 deletions Assets/Scripts/OpenTS2/Engine/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public static void InitializeCore()
var nhoodManager = new NeighborhoodManager();
var casController = new CASController();
var lotManger = new LotManager();
var asyncContentLoader = new AsyncContentManager();

TerrainManager.Initialize();
MaterialManager.Initialize();
Expand Down

0 comments on commit 5396d7e

Please sign in to comment.