Skip to content

Commit

Permalink
Change: Make the wav parser handle some extensible WAV files
Browse files Browse the repository at this point in the history
  • Loading branch information
leezer3 committed Mar 29, 2023
1 parent c8d7aec commit b629720
Showing 1 changed file with 43 additions and 16 deletions.
59 changes: 43 additions & 16 deletions source/Plugins/Sound.RiffWave/Plugin.Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,30 @@ public partial class Plugin
// --- structures and enumerations ---
private abstract class WaveFormatEx
{
internal ushort wFormatTag;
internal readonly ushort wFormatTag;
internal ushort nChannels;
internal uint nSamplesPerSec;
internal uint nAvgBytesPerSec;
internal ushort nBlockAlign;
internal ushort wBitsPerSample;
internal ushort cbSize;

protected WaveFormatEx(ushort tag)
{
wFormatTag = tag;
}
}

private class WaveFormatPcm : WaveFormatEx
{
public WaveFormatPcm(ushort tag) : base(tag)
{
}
}

private class WaveFormatAdPcm : WaveFormatEx
{

internal struct CoefSet
{
internal short iCoef1;
Expand Down Expand Up @@ -61,10 +70,24 @@ internal BlockData(int channels)
230, 230, 230, 230, 307, 409, 512, 614,
768, 614, 512, 409, 307, 230, 230, 230
};

public WaveFormatAdPcm(ushort tag) : base(tag)
{
}
}

private class WaveFormatMp3 : WaveFormatEx
{
public WaveFormatMp3(ushort tag) : base(tag)
{
}
}

private class WaveFormatExtensible : WaveFormatEx
{
public WaveFormatExtensible(ushort tag) : base(tag)
{
}
}


Expand Down Expand Up @@ -194,22 +217,25 @@ private static Sound WaveLoadFromStream(BinaryReader reader, BinaryReaderExtensi

ushort wFormatTag = reader.ReadUInt16(endianness);

if (wFormatTag == 0x0001 || wFormatTag == 0x0003)
{
format = new WaveFormatPcm { wFormatTag = wFormatTag };
}
else if (wFormatTag == 0x0002)
{
format = new WaveFormatAdPcm { wFormatTag = wFormatTag };
}
else if (wFormatTag == 0x0050 || wFormatTag == 0x0055)
{
format = new WaveFormatMp3 { wFormatTag = wFormatTag };
}
else
switch (wFormatTag)
{
// unsupported format
throw new InvalidDataException("Unsupported wFormatTag");
case 0x0001:
case 0x0003:
format = new WaveFormatPcm(wFormatTag);
break;
case 0x0002:
format = new WaveFormatAdPcm(wFormatTag);
break;
case 0x0050:
case 0x0055:
format = new WaveFormatMp3(wFormatTag);
break;
case 0xFFFE:
format = new WaveFormatExtensible(wFormatTag);
break;
default:
// unsupported format
throw new InvalidDataException("Unsupported wFormatTag");
}

format.nChannels = reader.ReadUInt16(endianness);
Expand Down Expand Up @@ -491,6 +517,7 @@ private static Sound ChangeBitDepth(ushort formatTag, int samplesPerSec, int byt
switch (formatTag)
{
case 0x0001:
case 0xFFFE:
switch (bytesPerSample)
{
case 3:
Expand Down

0 comments on commit b629720

Please sign in to comment.