Refactor audio conversion and enhance playback experience

Updated `AudioConverter` to streamline null checks and return empty arrays. Added OGG to WAV conversion in `AssetAPI` for better audio handling. Modified `AudioTab` to preload audio automatically, improving user experience.

Related: #1877
This commit is contained in:
ds5678 2025-08-04 12:17:28 -07:00
parent 206f0ec99b
commit 645ed5a336
3 changed files with 13 additions and 7 deletions

View File

@ -8,14 +8,11 @@ public static class AudioConverter
{
public static byte[] OggToWav(byte[] oggData)
{
if (oggData == null)
{
throw new ArgumentNullException(nameof(oggData));
}
ArgumentNullException.ThrowIfNull(oggData);
if (oggData.Length == 0)
{
return Array.Empty<byte>();
return [];
}
try
@ -28,7 +25,7 @@ public static class AudioConverter
catch (Exception ex)
{
Logger.Error(LogCategory.Export, "Failed to convert audio from OGG to WAV", ex);
return Array.Empty<byte>();
return [];
}
}
}

View File

@ -181,6 +181,15 @@ internal static class AssetAPI
}
else if (AudioClipDecoder.TryDecode(clip, out byte[]? decodedAudioData, out string? extension, out _))
{
if (extension is "ogg")
{
byte[] wavData = AudioConverter.OggToWav(decodedAudioData);
if (wavData.Length > 0)
{
decodedAudioData = wavData;
extension = "wav";
}
}
return Results.Bytes(decodedAudioData, $"audio/{extension}").ExecuteAsync(context);
}
else

View File

@ -28,7 +28,7 @@ internal sealed class AudioTab : AssetHtmlTab
{
using (new Td(writer).WithAlign("center").WithCustomAttribute("valign", "middle").End())
{
new Audio(writer).WithControls("").WithClass("mt-4").WithSrc(Source).Close();
new Audio(writer).WithControls("").WithPreload("auto").WithClass("mt-4").WithSrc(Source).Close();
}
}
}