From 01d80f7bad97214b5acd7bf5b61b0417dea058c7 Mon Sep 17 00:00:00 2001 From: in0finite Date: Mon, 6 Sep 2021 22:39:30 +0200 Subject: [PATCH] wip --- Assets/Scripts/Audio/AudioStream.cs | 44 +++++++++++++++++++ .../Scripts/Behaviours/Audio/AudioManager.cs | 12 ++++- 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/Assets/Scripts/Audio/AudioStream.cs b/Assets/Scripts/Audio/AudioStream.cs index db04db58..d5f0ffab 100644 --- a/Assets/Scripts/Audio/AudioStream.cs +++ b/Assets/Scripts/Audio/AudioStream.cs @@ -63,6 +63,50 @@ namespace SanAndreasUnity.Audio }); } + public struct AudioClipCreationData + { + public int Channels, SampleRate, TotalSamples; + } + + public static AudioClipCreationData ReadStreamIntoBuffer( + Stream stream, + bool closeStreamOnDispose, + Func funcGetBuffer) + { + var reader = new VorbisReader(stream, closeStreamOnDispose); + + var audioClipCreationData = new AudioClipCreationData + { + Channels = reader.Channels, + SampleRate = reader.SampleRate, + TotalSamples = (int) reader.TotalSamples, + }; + + var buffer = funcGetBuffer(audioClipCreationData.Channels * audioClipCreationData.TotalSamples); + + // read entire stream + reader.ReadSamples(buffer, 0, buffer.Length); + + return audioClipCreationData; + } + + public static AudioClip CreateAudioClip( + string audioClipName, + AudioClipCreationData audioClipCreationData, + float[] audioData) + { + var clip = AudioClip.Create( + audioClipName, + audioClipCreationData.TotalSamples, + audioClipCreationData.Channels, + audioClipCreationData.SampleRate, + false); + + clip.SetData(audioData, 0); + + return clip; + } + /// /// Dispose /// diff --git a/Assets/Scripts/Behaviours/Audio/AudioManager.cs b/Assets/Scripts/Behaviours/Audio/AudioManager.cs index ccc7522d..6e805451 100644 --- a/Assets/Scripts/Behaviours/Audio/AudioManager.cs +++ b/Assets/Scripts/Behaviours/Audio/AudioManager.cs @@ -72,7 +72,17 @@ namespace SanAndreasUnity.Behaviours.Audio } - public static AudioClip CreateAudioClipFromStream (string streamFileName, int bankIndex) + public static void CreateAudioClipFromStreamAsync( + string streamFileName, + int bankIndex, + System.Action onFinish) + { + + } + + private static void CreateAudioClipFromStreamInBackgroundThread( + string streamFileName, + int bankIndex) { Profiler.BeginSample("CreateAudioClipFromStream()");