Skip to content

Commit 80d4e51

Browse files
Merge pull request #220 from UBTL/main
Ogg File Importer
2 parents 34111cc + 98013ad commit 80d4e51

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

Prowl.Editor/Assets/Importers/AudioClipImporter.cs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
using Prowl.Runtime.Audio;
99
using Prowl.Runtime.GUI;
1010
using Prowl.Runtime.Utils;
11+
using NVorbis;
1112

1213
namespace Prowl.Editor.Assets.Importers;
1314

14-
[Importer("FileIcon.png", typeof(AudioClip), ".wav")]
15+
[Importer("FileIcon.png", typeof(AudioClip), ".wav", ".wave", ".ogg")]
1516
public class AudioClipImporter : ScriptedImporter
1617
{
1718
public override void Import(SerializedAsset ctx, FileInfo assetPath)
@@ -20,10 +21,55 @@ public override void Import(SerializedAsset ctx, FileInfo assetPath)
2021
{
2122
".wav" => LoadWav(assetPath),
2223
".wave" => LoadWav(assetPath),
24+
".ogg" => LoadOgg(assetPath),
25+
".oga" => LoadOgg(assetPath),
2326
_ => throw new InvalidOperationException("Unsupported audio format: " + assetPath.Extension.ToLower()),
2427
});
2528
}
2629

30+
private static AudioClip LoadOgg(FileInfo file)
31+
{
32+
// Decode Ogg Vorbis to interleaved 16-bit PCM (little-endian) using NVorbis
33+
using var fs = file.OpenRead();
34+
using var vorbis = new VorbisReader(fs, false);
35+
36+
int channels = vorbis.Channels;
37+
int sampleRate = vorbis.SampleRate;
38+
const short bitsPerSample = 16; // output s16 PCM
39+
40+
// Converting NVorbis outputs floats of [-1..1] interleaved across channels
41+
// to s16 to avoid large allocations.
42+
var floatBuf = new float[4096]; // count is "samples"
43+
using var ms = new MemoryStream();
44+
45+
int samplesRead;
46+
Span<byte> le = stackalloc byte[2]; // scratch for one s16
47+
48+
while ((samplesRead = vorbis.ReadSamples(floatBuf, 0, floatBuf.Length)) > 0)
49+
{
50+
for (int i = 0; i < samplesRead; i++)
51+
{
52+
// clamp and convert float -> 16-bit signed
53+
float f = floatBuf[i];
54+
if (f > 1f) f = 1f;
55+
else if (f < -1f) f = -1f;
56+
57+
short s = (short)MathF.Round(f * 32767f);
58+
59+
// write little-endian
60+
System.Buffers.Binary.BinaryPrimitives.WriteInt16LittleEndian(le, s);
61+
ms.Write(le);
62+
}
63+
}
64+
65+
byte[] audioData = ms.ToArray();
66+
67+
// Creating the clip just like the WAV path does:
68+
AudioClip audioClip = AudioClip.Create(file.Name, audioData, (short)channels, bitsPerSample, sampleRate);
69+
return audioClip;
70+
}
71+
72+
2773
#region Wave Format
2874

2975
private static AudioClip LoadWav(FileInfo file)

Prowl.Editor/Prowl.Editor.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
<PackageReference Include="AssimpNet" Version="5.0.0-beta1" />
4343
<PackageReference Include="CommandLineParser" Version="2.9.1" />
4444
<PackageReference Include="Glslang.NET" Version="1.2.0" />
45+
<PackageReference Include="NVorbis" Version="0.10.5" />
4546
<PackageReference Include="SixLabors.ImageSharp" Version="2.1.9" />
4647
<PackageReference Include="SixLabors.ImageSharp.Textures" Version="0.0.0-alpha.0.140" />
4748
<PackageReference Include="SPIRV-Cross.NET" Version="1.0.1" />

Prowl.Players/Prowl.Desktop/Prowl.Desktop.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
<ItemGroup>
2121
<!-- Janky fix to include native SDL2 binaries in the built player -->
22+
<PackageReference Include="NVorbis" Version="0.10.5" />
2223
<PackageReference Include="Ultz.Native.SDL" Version="2.30.1" />
2324
</ItemGroup>
2425
</Project>

Prowl.Runtime/Prowl.Runtime.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
<ItemGroup>
6363
<!-- <PackageReference Include="Jitter2" Version="2.2.0" /> -->
6464
<PackageReference Include="Jitter2.Double" Version="2.4.8-alpha" />
65+
<PackageReference Include="NVorbis" Version="0.10.5" />
6566
<PackageReference Include="Prowl.Echo" Version="1.7.0" />
6667
<PackageReference Include="Silk.NET.OpenAL" Version="2.22.0" />
6768
<PackageReference Include="Silk.NET.OpenAL.Soft.Native" Version="1.23.1" />

0 commit comments

Comments
 (0)