From a7e97fe178d263e7c9dbe01a2c5978a284696a8f Mon Sep 17 00:00:00 2001 From: Masusder <59669685+Masusder@users.noreply.github.com> Date: Thu, 16 Oct 2025 18:39:23 +0200 Subject: [PATCH 1/4] Read number of channels correctly --- .../CodecRebuilders/FmodVorbisRebuilder.cs | 2 +- Fmod5Sharp/FmodTypes/FmodSampleMetadata.cs | 65 ++++++++++--------- 2 files changed, 36 insertions(+), 31 deletions(-) diff --git a/Fmod5Sharp/CodecRebuilders/FmodVorbisRebuilder.cs b/Fmod5Sharp/CodecRebuilders/FmodVorbisRebuilder.cs index 4237c86..fb2477d 100644 --- a/Fmod5Sharp/CodecRebuilders/FmodVorbisRebuilder.cs +++ b/Fmod5Sharp/CodecRebuilders/FmodVorbisRebuilder.cs @@ -176,7 +176,7 @@ private static void ReadSamplePackets(BinaryReader inputReader, out List pa { var packetSize = inputReader.ReadUInt16(); - if (packetSize == 0) + if (packetSize == 0 || packetSize == 0xFFFF) break; //EOS packetLengths.Add(packetSize); diff --git a/Fmod5Sharp/FmodTypes/FmodSampleMetadata.cs b/Fmod5Sharp/FmodTypes/FmodSampleMetadata.cs index fb10120..bc2838b 100644 --- a/Fmod5Sharp/FmodTypes/FmodSampleMetadata.cs +++ b/Fmod5Sharp/FmodTypes/FmodSampleMetadata.cs @@ -1,38 +1,43 @@ -using System.Collections.Generic; +using Fmod5Sharp.Util; +using System.Collections.Generic; using System.IO; -using Fmod5Sharp.Util; namespace Fmod5Sharp.FmodTypes { - public class FmodSampleMetadata : IBinaryReadable - { - internal bool HasAnyChunks; - internal uint FrequencyId; - internal ulong DataOffset; - internal List Chunks = new(); - internal int NumChannels; + public class FmodSampleMetadata : IBinaryReadable + { + internal bool HasAnyChunks; + internal uint FrequencyId; + internal ulong DataOffset; + internal List Chunks = new(); + internal int NumChannels; - public bool IsStereo; - public ulong SampleCount; + public bool IsStereo; + public ulong SampleCount; - public int Frequency => FsbLoader.Frequencies.TryGetValue(FrequencyId, out var actualFrequency) ? actualFrequency : (int)FrequencyId; //If set by FREQUENCY chunk, id is actual frequency - public uint Channels => (uint)NumChannels; + public int Frequency => FsbLoader.Frequencies.TryGetValue(FrequencyId, out var actualFrequency) ? actualFrequency : (int)FrequencyId; // If set by FREQUENCY chunk, id is actual frequency + public uint Channels => (uint)NumChannels; - void IBinaryReadable.Read(BinaryReader reader) - { - var encoded = reader.ReadUInt64(); - - HasAnyChunks = (encoded & 1) == 1; //Bit 0 - FrequencyId = (uint) encoded.Bits( 1, 4); //Bits 1-4 - var pow2 = (int) encoded.Bits(5, 2); //Bits 5-6 - NumChannels = 1 << pow2; - if (NumChannels > 2) - throw new("> 2 channels not supported"); - - IsStereo = NumChannels == 2; - - DataOffset = encoded.Bits(7, 27) * 32; - SampleCount = encoded.Bits(34, 30); - } - } + void IBinaryReadable.Read(BinaryReader reader) + { + var sampleMode = reader.ReadUInt64(); + + HasAnyChunks = (sampleMode & 1) == 1; // Bit 0 + FrequencyId = (uint)sampleMode.Bits(1, 4); // Bits 1-4 + + int channelBits = (int)sampleMode.Bits(5, 2); // Bits 5-6 + NumChannels = channelBits switch + { + 0 => 1, + 1 => 2, + 2 => 6, + 3 => 8, + _ => throw new("Number of channels not supported"), + }; + IsStereo = NumChannels == 2; + + DataOffset = sampleMode.Bits(7, 27) * 32; + SampleCount = sampleMode.Bits(34, 30); + } + } } \ No newline at end of file From e6498912bade4a64967431ced99f643fea534ae3 Mon Sep 17 00:00:00 2001 From: Masusder <59669685+Masusder@users.noreply.github.com> Date: Fri, 17 Oct 2025 08:34:33 +0200 Subject: [PATCH 2/4] Revert formatting --- Fmod5Sharp/FmodTypes/FmodSampleMetadata.cs | 69 +++++++++++----------- 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/Fmod5Sharp/FmodTypes/FmodSampleMetadata.cs b/Fmod5Sharp/FmodTypes/FmodSampleMetadata.cs index bc2838b..b73157e 100644 --- a/Fmod5Sharp/FmodTypes/FmodSampleMetadata.cs +++ b/Fmod5Sharp/FmodTypes/FmodSampleMetadata.cs @@ -1,43 +1,44 @@ -using Fmod5Sharp.Util; -using System.Collections.Generic; +using System.Collections.Generic; using System.IO; +using Fmod5Sharp.Util; namespace Fmod5Sharp.FmodTypes { - public class FmodSampleMetadata : IBinaryReadable - { - internal bool HasAnyChunks; - internal uint FrequencyId; - internal ulong DataOffset; - internal List Chunks = new(); - internal int NumChannels; + public class FmodSampleMetadata : IBinaryReadable + { + internal bool HasAnyChunks; + internal uint FrequencyId; + internal ulong DataOffset; + internal List Chunks = new(); + internal int NumChannels; - public bool IsStereo; - public ulong SampleCount; + public bool IsStereo; + public ulong SampleCount; - public int Frequency => FsbLoader.Frequencies.TryGetValue(FrequencyId, out var actualFrequency) ? actualFrequency : (int)FrequencyId; // If set by FREQUENCY chunk, id is actual frequency - public uint Channels => (uint)NumChannels; + public int Frequency => FsbLoader.Frequencies.TryGetValue(FrequencyId, out var actualFrequency) ? actualFrequency : (int)FrequencyId; //If set by FREQUENCY chunk, id is actual frequency + public uint Channels => (uint)NumChannels; - void IBinaryReadable.Read(BinaryReader reader) - { - var sampleMode = reader.ReadUInt64(); + void IBinaryReadable.Read(BinaryReader reader) + { + var encoded = reader.ReadUInt64(); + + HasAnyChunks = (encoded & 1) == 1; //Bit 0 + FrequencyId = (uint) encoded.Bits( 1, 4); //Bits 1-4 - HasAnyChunks = (sampleMode & 1) == 1; // Bit 0 - FrequencyId = (uint)sampleMode.Bits(1, 4); // Bits 1-4 - - int channelBits = (int)sampleMode.Bits(5, 2); // Bits 5-6 - NumChannels = channelBits switch - { - 0 => 1, - 1 => 2, - 2 => 6, - 3 => 8, - _ => throw new("Number of channels not supported"), - }; - IsStereo = NumChannels == 2; - - DataOffset = sampleMode.Bits(7, 27) * 32; - SampleCount = sampleMode.Bits(34, 30); - } - } + int channelBits = (int)encoded.Bits(5, 2); //Bits 5-6 + NumChannels = channelBits switch + { + 0 => 1, + 1 => 2, + 2 => 6, + 3 => 8, + _ => throw new("Number of channels not supported") + }; + + IsStereo = NumChannels == 2; + + DataOffset = encoded.Bits(7, 27) * 32; + SampleCount = encoded.Bits(34, 30); + } + } } \ No newline at end of file From 905eea821ba5087e8c1f61ccc4f118602619dc70 Mon Sep 17 00:00:00 2001 From: Masusder <59669685+Masusder@users.noreply.github.com> Date: Fri, 17 Oct 2025 12:42:46 +0200 Subject: [PATCH 3/4] Corrected frequencies / and dont throw --- Fmod5Sharp/FmodTypes/FmodSampleMetadata.cs | 2 +- Fmod5Sharp/FsbLoader.cs | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Fmod5Sharp/FmodTypes/FmodSampleMetadata.cs b/Fmod5Sharp/FmodTypes/FmodSampleMetadata.cs index b73157e..660fe98 100644 --- a/Fmod5Sharp/FmodTypes/FmodSampleMetadata.cs +++ b/Fmod5Sharp/FmodTypes/FmodSampleMetadata.cs @@ -32,7 +32,7 @@ void IBinaryReadable.Read(BinaryReader reader) 1 => 2, 2 => 6, 3 => 8, - _ => throw new("Number of channels not supported") + _ => 0 }; IsStereo = NumChannels == 2; diff --git a/Fmod5Sharp/FsbLoader.cs b/Fmod5Sharp/FsbLoader.cs index ac8a149..b7f924c 100644 --- a/Fmod5Sharp/FsbLoader.cs +++ b/Fmod5Sharp/FsbLoader.cs @@ -10,9 +10,10 @@ public static class FsbLoader { internal static readonly Dictionary Frequencies = new() { - { 1, 8000 }, + { 0, 4_000 }, + { 1, 8_000 }, { 2, 11_000 }, - { 3, 11_025 }, + { 3, 12_000 }, { 4, 16_000 }, { 5, 22_050 }, { 6, 24_000 }, From 670ab7c85bd11372b2257f70ce267fba6e6737a0 Mon Sep 17 00:00:00 2001 From: Masusder <59669685+Masusder@users.noreply.github.com> Date: Sun, 19 Oct 2025 12:30:02 +0200 Subject: [PATCH 4/4] Doc SampleMetadata --- Fmod5Sharp/FmodTypes/FmodSampleMetadata.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Fmod5Sharp/FmodTypes/FmodSampleMetadata.cs b/Fmod5Sharp/FmodTypes/FmodSampleMetadata.cs index 660fe98..f2e1ddd 100644 --- a/Fmod5Sharp/FmodTypes/FmodSampleMetadata.cs +++ b/Fmod5Sharp/FmodTypes/FmodSampleMetadata.cs @@ -4,7 +4,8 @@ namespace Fmod5Sharp.FmodTypes { - public class FmodSampleMetadata : IBinaryReadable + //Can be verified against "FMOD::CodecFSB5::decodeSubSoundHeader" in fmod.dll + public class FmodSampleMetadata : IBinaryReadable { internal bool HasAnyChunks; internal uint FrequencyId;