optimize StreamingSoundQoa.Create

pull/49/head
cosmonaut 2023-05-22 19:24:26 -07:00
parent adeba633e5
commit 00f4bfdeae
1 changed files with 30 additions and 9 deletions

View File

@ -16,17 +16,42 @@ namespace MoonWorks.Audio
public override bool Loaded => QoaHandle != IntPtr.Zero; public override bool Loaded => QoaHandle != IntPtr.Zero;
private string FilePath; private string FilePath;
public unsafe static StreamingSoundQoa Create(AudioDevice device, string filePath) private const uint QOA_MAGIC = 0x716f6166; /* 'qoaf' */
private static unsafe UInt64 ReverseEndianness(UInt64 value)
{ {
var handle = FAudio.qoa_open_from_filename(filePath); byte* bytes = (byte*) &value;
if (handle == IntPtr.Zero)
{ return
throw new AudioLoadException("Error opening QOA file!"); ((UInt64)(bytes[0]) << 56) | ((UInt64)(bytes[1]) << 48) |
((UInt64)(bytes[2]) << 40) | ((UInt64)(bytes[3]) << 32) |
((UInt64)(bytes[4]) << 24) | ((UInt64)(bytes[5]) << 16) |
((UInt64)(bytes[6]) << 8) | ((UInt64)(bytes[7]) << 0);
} }
FAudio.qoa_attributes(handle, out var channels, out var samplerate, out var samplesPerChannelPerFrame, out var totalSamplesPerChannel); public unsafe static StreamingSoundQoa Create(AudioDevice device, string filePath)
{
using var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
using var reader = new BinaryReader(stream);
var streamingSound = new StreamingSoundQoa( UInt64 fileHeader = ReverseEndianness(reader.ReadUInt64());
if ((fileHeader >> 32) != QOA_MAGIC)
{
throw new AudioLoadException("Specified file is not a QOA file.");
}
uint totalSamplesPerChannel = (uint) (fileHeader & (0xFFFFFFFF));
if (totalSamplesPerChannel == 0)
{
throw new AudioLoadException("Specified file is not a valid QOA file.");
}
UInt64 frameHeader = ReverseEndianness(reader.ReadUInt64());
uint channels = (uint) ((frameHeader >> 56) & 0x0000FF);
uint samplerate = (uint) ((frameHeader >> 32) & 0xFFFFFF);
uint samplesPerChannelPerFrame = (uint) ((frameHeader >> 16) & 0x00FFFF);
return new StreamingSoundQoa(
device, device,
filePath, filePath,
channels, channels,
@ -34,10 +59,6 @@ namespace MoonWorks.Audio
samplesPerChannelPerFrame, samplesPerChannelPerFrame,
totalSamplesPerChannel totalSamplesPerChannel
); );
FAudio.qoa_close(handle);
return streamingSound;
} }
internal unsafe StreamingSoundQoa( internal unsafe StreamingSoundQoa(