This commit is contained in:
in0finite 2021-09-06 22:39:30 +02:00
parent 5a84e5fd61
commit 01d80f7bad
2 changed files with 55 additions and 1 deletions

View file

@ -63,6 +63,50 @@ namespace SanAndreasUnity.Audio
});
}
public struct AudioClipCreationData
{
public int Channels, SampleRate, TotalSamples;
}
public static AudioClipCreationData ReadStreamIntoBuffer(
Stream stream,
bool closeStreamOnDispose,
Func<int, float[]> 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;
}
/// <summary>
/// Dispose
/// </summary>

View file

@ -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<AudioClip> onFinish)
{
}
private static void CreateAudioClipFromStreamInBackgroundThread(
string streamFileName,
int bankIndex)
{
Profiler.BeginSample("CreateAudioClipFromStream()");