File Re-Organization/Compression

This commit is contained in:
Michael 2021-02-06 23:41:04 -06:00
parent 5dc87bc555
commit 7f8cb8655b
26 changed files with 1393 additions and 1852 deletions

494
BNS.cs
View file

@ -690,4 +690,498 @@ namespace libWiiSharp
#endregion
#endregion
}
internal class BNS_Data
{
private readonly byte[] magic = new byte[4]
{
68,
65,
84,
65
};
private uint size = 315392;
private byte[] data;
public uint Size
{
get => size;
set => size = value;
}
public byte[] Data
{
get => data;
set => data = value;
}
public void Write(Stream outStream)
{
byte[] bytes = BitConverter.GetBytes(Shared.Swap(size));
outStream.Write(magic, 0, magic.Length);
outStream.Write(bytes, 0, bytes.Length);
outStream.Write(data, 0, data.Length);
}
public void Read(Stream input)
{
BinaryReader binaryReader = new BinaryReader(input);
size = Shared.CompareByteArrays(magic, binaryReader.ReadBytes(4)) ? Shared.Swap(binaryReader.ReadUInt32()) : throw new Exception("This is not a valid BNS audfo file!");
data = binaryReader.ReadBytes((int)size - 8);
}
}
internal class BNS_Header
{
private readonly byte[] magic = new byte[4]
{
66,
78,
83,
32
};
private uint flags = 4278124800;
private uint fileSize = 315584;
private ushort size = 32;
private ushort chunkCount = 2;
private uint infoOffset = 32;
private uint infoLength = 160;
private uint dataOffset = 192;
private uint dataLength = 315392;
public uint DataOffset
{
get => dataOffset;
set => dataOffset = value;
}
public uint InfoLength
{
get => infoLength;
set => infoLength = value;
}
public ushort Size
{
get => size;
set => size = value;
}
public uint DataLength
{
get => dataLength;
set => dataLength = value;
}
public uint FileSize
{
get => fileSize;
set => fileSize = value;
}
public void Write(Stream outStream)
{
outStream.Write(magic, 0, magic.Length);
byte[] bytes1 = BitConverter.GetBytes(flags);
Array.Reverse(bytes1);
outStream.Write(bytes1, 0, bytes1.Length);
byte[] bytes2 = BitConverter.GetBytes(fileSize);
Array.Reverse(bytes2);
outStream.Write(bytes2, 0, bytes2.Length);
byte[] bytes3 = BitConverter.GetBytes(size);
Array.Reverse(bytes3);
outStream.Write(bytes3, 0, bytes3.Length);
byte[] bytes4 = BitConverter.GetBytes(chunkCount);
Array.Reverse(bytes4);
outStream.Write(bytes4, 0, bytes4.Length);
byte[] bytes5 = BitConverter.GetBytes(infoOffset);
Array.Reverse(bytes5);
outStream.Write(bytes5, 0, bytes5.Length);
byte[] bytes6 = BitConverter.GetBytes(infoLength);
Array.Reverse(bytes6);
outStream.Write(bytes6, 0, bytes6.Length);
byte[] bytes7 = BitConverter.GetBytes(dataOffset);
Array.Reverse(bytes7);
outStream.Write(bytes7, 0, bytes7.Length);
byte[] bytes8 = BitConverter.GetBytes(dataLength);
Array.Reverse(bytes8);
outStream.Write(bytes8, 0, bytes8.Length);
}
public void Read(Stream input)
{
BinaryReader binaryReader = new BinaryReader(input);
if (!Shared.CompareByteArrays(magic, binaryReader.ReadBytes(4)))
{
binaryReader.BaseStream.Seek(28L, SeekOrigin.Current);
if (!Shared.CompareByteArrays(magic, binaryReader.ReadBytes(4)))
{
throw new Exception("This is not a valid BNS audio file!");
}
}
flags = Shared.Swap(binaryReader.ReadUInt32());
fileSize = Shared.Swap(binaryReader.ReadUInt32());
size = Shared.Swap(binaryReader.ReadUInt16());
chunkCount = Shared.Swap(binaryReader.ReadUInt16());
infoOffset = Shared.Swap(binaryReader.ReadUInt32());
infoLength = Shared.Swap(binaryReader.ReadUInt32());
dataOffset = Shared.Swap(binaryReader.ReadUInt32());
dataLength = Shared.Swap(binaryReader.ReadUInt32());
}
}
internal class BNS_Info
{
//Private Variables
private readonly byte[] magic = new byte[4]
{
73,
78,
70,
79
};
private uint size = 160;
private byte codec;
private byte hasLoop;
private byte channelCount = 2;
private byte zero;
private ushort sampleRate = 44100;
private ushort pad0;
private uint loopStart;
private uint loopEnd; //Or total sample count
private uint offsetToChannelStart = 24;
private uint pad1;
private uint channel1StartOffset = 32;
private uint channel2StartOffset = 44;
private uint channel1Start;
private uint coefficients1Offset = 56;
private uint pad2;
private uint channel2Start;
private uint coefficients2Offset = 104;
private uint pad3;
private int[] coefficients1 = new int[16];
private ushort channel1Gain;
private ushort channel1PredictiveScale;
private ushort channel1PreviousValue;
private ushort channel1NextPreviousValue;
private ushort channel1LoopPredictiveScale;
private ushort channel1LoopPreviousValue;
private ushort channel1LoopNextPreviousValue;
private ushort channel1LoopPadding;
private int[] coefficients2 = new int[16];
private ushort channel2Gain;
private ushort channel2PredictiveScale;
private ushort channel2PreviousValue;
private ushort channel2NextPreviousValue;
private ushort channel2LoopPredictiveScale;
private ushort channel2LoopPreviousValue;
private ushort channel2LoopNextPreviousValue;
private ushort channel2LoopPadding;
//Public Variables
public byte HasLoop
{
get => hasLoop;
set => hasLoop = value;
}
public uint Coefficients1Offset
{
get => coefficients1Offset;
set => coefficients1Offset = value;
}
public uint Channel1StartOffset
{
get => channel1StartOffset;
set => channel1StartOffset = value;
}
public uint Channel2StartOffset
{
get => channel2StartOffset;
set => channel2StartOffset = value;
}
public uint Size
{
get => size;
set => size = value;
}
public ushort SampleRate
{
get => sampleRate;
set => sampleRate = value;
}
public byte ChannelCount
{
get => channelCount;
set => channelCount = value;
}
public uint Channel1Start
{
get => channel1Start;
set => channel1Start = value;
}
public uint Channel2Start
{
get => channel2Start;
set => channel2Start = value;
}
public uint LoopStart
{
get => loopStart;
set => loopStart = value;
}
public uint LoopEnd
{
get => loopEnd;
set => loopEnd = value;
}
public int[] Coefficients1
{
get => coefficients1;
set => coefficients1 = value;
}
public int[] Coefficients2
{
get => coefficients2;
set => coefficients2 = value;
}
public void Write(Stream outStream)
{
outStream.Write(magic, 0, magic.Length);
byte[] bytes1 = BitConverter.GetBytes(size);
Array.Reverse(bytes1);
outStream.Write(bytes1, 0, bytes1.Length);
outStream.WriteByte(codec);
outStream.WriteByte(hasLoop);
outStream.WriteByte(channelCount);
outStream.WriteByte(zero);
byte[] bytes2 = BitConverter.GetBytes(sampleRate);
Array.Reverse(bytes2);
outStream.Write(bytes2, 0, bytes2.Length);
byte[] bytes3 = BitConverter.GetBytes(pad0);
Array.Reverse(bytes3);
outStream.Write(bytes3, 0, bytes3.Length);
byte[] bytes4 = BitConverter.GetBytes(loopStart);
Array.Reverse(bytes4);
outStream.Write(bytes4, 0, bytes4.Length);
byte[] bytes5 = BitConverter.GetBytes(loopEnd);
Array.Reverse(bytes5);
outStream.Write(bytes5, 0, bytes5.Length);
byte[] bytes6 = BitConverter.GetBytes(offsetToChannelStart);
Array.Reverse(bytes6);
outStream.Write(bytes6, 0, bytes6.Length);
byte[] bytes7 = BitConverter.GetBytes(pad1);
Array.Reverse(bytes7);
outStream.Write(bytes7, 0, bytes7.Length);
byte[] bytes8 = BitConverter.GetBytes(channel1StartOffset);
Array.Reverse(bytes8);
outStream.Write(bytes8, 0, bytes8.Length);
byte[] bytes9 = BitConverter.GetBytes(channel2StartOffset);
Array.Reverse(bytes9);
outStream.Write(bytes9, 0, bytes9.Length);
byte[] bytes10 = BitConverter.GetBytes(channel1Start);
Array.Reverse(bytes10);
outStream.Write(bytes10, 0, bytes10.Length);
byte[] bytes11 = BitConverter.GetBytes(coefficients1Offset);
Array.Reverse(bytes11);
outStream.Write(bytes11, 0, bytes11.Length);
if (channelCount == 2)
{
byte[] bytes12 = BitConverter.GetBytes(pad2);
Array.Reverse(bytes12);
outStream.Write(bytes12, 0, bytes12.Length);
byte[] bytes13 = BitConverter.GetBytes(channel2Start);
Array.Reverse(bytes13);
outStream.Write(bytes13, 0, bytes13.Length);
byte[] bytes14 = BitConverter.GetBytes(coefficients2Offset);
Array.Reverse(bytes14);
outStream.Write(bytes14, 0, bytes14.Length);
byte[] bytes15 = BitConverter.GetBytes(pad3);
Array.Reverse(bytes15);
outStream.Write(bytes15, 0, bytes15.Length);
foreach (int num in coefficients1)
{
byte[] bytes16 = BitConverter.GetBytes(num);
Array.Reverse(bytes16);
outStream.Write(bytes16, 2, bytes16.Length - 2);
}
byte[] bytes17 = BitConverter.GetBytes(channel1Gain);
Array.Reverse(bytes17);
outStream.Write(bytes17, 0, bytes17.Length);
byte[] bytes18 = BitConverter.GetBytes(channel1PredictiveScale);
Array.Reverse(bytes18);
outStream.Write(bytes18, 0, bytes18.Length);
byte[] bytes19 = BitConverter.GetBytes(channel1PreviousValue);
Array.Reverse(bytes19);
outStream.Write(bytes19, 0, bytes19.Length);
byte[] bytes20 = BitConverter.GetBytes(channel1NextPreviousValue);
Array.Reverse(bytes20);
outStream.Write(bytes20, 0, bytes20.Length);
byte[] bytes21 = BitConverter.GetBytes(channel1LoopPredictiveScale);
Array.Reverse(bytes21);
outStream.Write(bytes21, 0, bytes21.Length);
byte[] bytes22 = BitConverter.GetBytes(channel1LoopPreviousValue);
Array.Reverse(bytes22);
outStream.Write(bytes22, 0, bytes22.Length);
byte[] bytes23 = BitConverter.GetBytes(channel1LoopNextPreviousValue);
Array.Reverse(bytes23);
outStream.Write(bytes23, 0, bytes23.Length);
byte[] bytes24 = BitConverter.GetBytes(channel1LoopPadding);
Array.Reverse(bytes24);
outStream.Write(bytes24, 0, bytes24.Length);
foreach (int num in coefficients2)
{
byte[] bytes16 = BitConverter.GetBytes(num);
Array.Reverse(bytes16);
outStream.Write(bytes16, 2, bytes16.Length - 2);
}
byte[] bytes25 = BitConverter.GetBytes(channel2Gain);
Array.Reverse(bytes25);
outStream.Write(bytes25, 0, bytes25.Length);
byte[] bytes26 = BitConverter.GetBytes(channel2PredictiveScale);
Array.Reverse(bytes26);
outStream.Write(bytes26, 0, bytes26.Length);
byte[] bytes27 = BitConverter.GetBytes(channel2PreviousValue);
Array.Reverse(bytes27);
outStream.Write(bytes27, 0, bytes27.Length);
byte[] bytes28 = BitConverter.GetBytes(channel2NextPreviousValue);
Array.Reverse(bytes28);
outStream.Write(bytes28, 0, bytes28.Length);
byte[] bytes29 = BitConverter.GetBytes(channel2LoopPredictiveScale);
Array.Reverse(bytes29);
outStream.Write(bytes29, 0, bytes29.Length);
byte[] bytes30 = BitConverter.GetBytes(channel2LoopPreviousValue);
Array.Reverse(bytes30);
outStream.Write(bytes30, 0, bytes30.Length);
byte[] bytes31 = BitConverter.GetBytes(channel2LoopNextPreviousValue);
Array.Reverse(bytes31);
outStream.Write(bytes31, 0, bytes31.Length);
byte[] bytes32 = BitConverter.GetBytes(channel2LoopPadding);
Array.Reverse(bytes32);
outStream.Write(bytes32, 0, bytes32.Length);
}
else
{
if (channelCount != 1)
{
return;
}
foreach (int num in coefficients1)
{
byte[] bytes12 = BitConverter.GetBytes(num);
Array.Reverse(bytes12);
outStream.Write(bytes12, 2, bytes12.Length - 2);
}
byte[] bytes13 = BitConverter.GetBytes(channel1Gain);
Array.Reverse(bytes13);
outStream.Write(bytes13, 0, bytes13.Length);
byte[] bytes14 = BitConverter.GetBytes(channel1PredictiveScale);
Array.Reverse(bytes14);
outStream.Write(bytes14, 0, bytes14.Length);
byte[] bytes15 = BitConverter.GetBytes(channel1PreviousValue);
Array.Reverse(bytes15);
outStream.Write(bytes15, 0, bytes15.Length);
byte[] bytes16 = BitConverter.GetBytes(channel1NextPreviousValue);
Array.Reverse(bytes16);
outStream.Write(bytes16, 0, bytes16.Length);
byte[] bytes17 = BitConverter.GetBytes(channel1LoopPredictiveScale);
Array.Reverse(bytes17);
outStream.Write(bytes17, 0, bytes17.Length);
byte[] bytes18 = BitConverter.GetBytes(channel1LoopPreviousValue);
Array.Reverse(bytes18);
outStream.Write(bytes18, 0, bytes18.Length);
byte[] bytes19 = BitConverter.GetBytes(channel1LoopNextPreviousValue);
Array.Reverse(bytes19);
outStream.Write(bytes19, 0, bytes19.Length);
byte[] bytes20 = BitConverter.GetBytes(channel1LoopPadding);
Array.Reverse(bytes20);
outStream.Write(bytes20, 0, bytes20.Length);
}
}
public void Read(Stream input)
{
BinaryReader binaryReader = new BinaryReader(input);
size = Shared.CompareByteArrays(magic, binaryReader.ReadBytes(4)) ? Shared.Swap(binaryReader.ReadUInt32()) : throw new Exception("This is not a valid BNS audfo file!");
codec = binaryReader.ReadByte();
hasLoop = binaryReader.ReadByte();
channelCount = binaryReader.ReadByte();
zero = binaryReader.ReadByte();
sampleRate = Shared.Swap(binaryReader.ReadUInt16());
pad0 = Shared.Swap(binaryReader.ReadUInt16());
loopStart = Shared.Swap(binaryReader.ReadUInt32());
loopEnd = Shared.Swap(binaryReader.ReadUInt32());
offsetToChannelStart = Shared.Swap(binaryReader.ReadUInt32());
pad1 = Shared.Swap(binaryReader.ReadUInt32());
channel1StartOffset = Shared.Swap(binaryReader.ReadUInt32());
channel2StartOffset = Shared.Swap(binaryReader.ReadUInt32());
channel1Start = Shared.Swap(binaryReader.ReadUInt32());
coefficients1Offset = Shared.Swap(binaryReader.ReadUInt32());
if (channelCount == 2)
{
pad2 = Shared.Swap(binaryReader.ReadUInt32());
channel2Start = Shared.Swap(binaryReader.ReadUInt32());
coefficients2Offset = Shared.Swap(binaryReader.ReadUInt32());
pad3 = Shared.Swap(binaryReader.ReadUInt32());
for (int index = 0; index < 16; ++index)
{
coefficients1[index] = (short)Shared.Swap(binaryReader.ReadUInt16());
}
channel1Gain = Shared.Swap(binaryReader.ReadUInt16());
channel1PredictiveScale = Shared.Swap(binaryReader.ReadUInt16());
channel1PreviousValue = Shared.Swap(binaryReader.ReadUInt16());
channel1NextPreviousValue = Shared.Swap(binaryReader.ReadUInt16());
channel1LoopPredictiveScale = Shared.Swap(binaryReader.ReadUInt16());
channel1LoopPreviousValue = Shared.Swap(binaryReader.ReadUInt16());
channel1LoopNextPreviousValue = Shared.Swap(binaryReader.ReadUInt16());
channel1LoopPadding = Shared.Swap(binaryReader.ReadUInt16());
for (int index = 0; index < 16; ++index)
{
coefficients2[index] = (short)Shared.Swap(binaryReader.ReadUInt16());
}
channel2Gain = Shared.Swap(binaryReader.ReadUInt16());
channel2PredictiveScale = Shared.Swap(binaryReader.ReadUInt16());
channel2PreviousValue = Shared.Swap(binaryReader.ReadUInt16());
channel2NextPreviousValue = Shared.Swap(binaryReader.ReadUInt16());
channel2LoopPredictiveScale = Shared.Swap(binaryReader.ReadUInt16());
channel2LoopPreviousValue = Shared.Swap(binaryReader.ReadUInt16());
channel2LoopNextPreviousValue = Shared.Swap(binaryReader.ReadUInt16());
channel2LoopPadding = Shared.Swap(binaryReader.ReadUInt16());
}
else
{
if (channelCount != 1)
{
return;
}
for (int index = 0; index < 16; ++index)
{
coefficients1[index] = (short)Shared.Swap(binaryReader.ReadUInt16());
}
channel1Gain = Shared.Swap(binaryReader.ReadUInt16());
channel1PredictiveScale = Shared.Swap(binaryReader.ReadUInt16());
channel1PreviousValue = Shared.Swap(binaryReader.ReadUInt16());
channel1NextPreviousValue = Shared.Swap(binaryReader.ReadUInt16());
channel1LoopPredictiveScale = Shared.Swap(binaryReader.ReadUInt16());
channel1LoopPreviousValue = Shared.Swap(binaryReader.ReadUInt16());
channel1LoopNextPreviousValue = Shared.Swap(binaryReader.ReadUInt16());
channel1LoopPadding = Shared.Swap(binaryReader.ReadUInt16());
}
}
}
}

View file

@ -1,63 +0,0 @@
/* This file is part of libWiiSharp
* Copyright (C) 2009 Leathl
* Copyright (C) 2020 - 2021 Github Contributors
*
* libWiiSharp is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libWiiSharp is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.IO;
namespace libWiiSharp
{
internal class BNS_Data
{
private readonly byte[] magic = new byte[4]
{
68,
65,
84,
65
};
private uint size = 315392;
private byte[] data;
public uint Size
{
get => size;
set => size = value;
}
public byte[] Data
{
get => data;
set => data = value;
}
public void Write(Stream outStream)
{
byte[] bytes = BitConverter.GetBytes(Shared.Swap(size));
outStream.Write(magic, 0, magic.Length);
outStream.Write(bytes, 0, bytes.Length);
outStream.Write(data, 0, data.Length);
}
public void Read(Stream input)
{
BinaryReader binaryReader = new BinaryReader(input);
size = Shared.CompareByteArrays(magic, binaryReader.ReadBytes(4)) ? Shared.Swap(binaryReader.ReadUInt32()) : throw new Exception("This is not a valid BNS audfo file!");
data = binaryReader.ReadBytes((int)size - 8);
}
}
}

View file

@ -1,122 +0,0 @@
/* This file is part of libWiiSharp
* Copyright (C) 2009 Leathl
* Copyright (C) 2020 - 2021 Github Contributors
*
* libWiiSharp is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libWiiSharp is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.IO;
namespace libWiiSharp
{
internal class BNS_Header
{
private readonly byte[] magic = new byte[4]
{
66,
78,
83,
32
};
private uint flags = 4278124800;
private uint fileSize = 315584;
private ushort size = 32;
private ushort chunkCount = 2;
private uint infoOffset = 32;
private uint infoLength = 160;
private uint dataOffset = 192;
private uint dataLength = 315392;
public uint DataOffset
{
get => dataOffset;
set => dataOffset = value;
}
public uint InfoLength
{
get => infoLength;
set => infoLength = value;
}
public ushort Size
{
get => size;
set => size = value;
}
public uint DataLength
{
get => dataLength;
set => dataLength = value;
}
public uint FileSize
{
get => fileSize;
set => fileSize = value;
}
public void Write(Stream outStream)
{
outStream.Write(magic, 0, magic.Length);
byte[] bytes1 = BitConverter.GetBytes(flags);
Array.Reverse(bytes1);
outStream.Write(bytes1, 0, bytes1.Length);
byte[] bytes2 = BitConverter.GetBytes(fileSize);
Array.Reverse(bytes2);
outStream.Write(bytes2, 0, bytes2.Length);
byte[] bytes3 = BitConverter.GetBytes(size);
Array.Reverse(bytes3);
outStream.Write(bytes3, 0, bytes3.Length);
byte[] bytes4 = BitConverter.GetBytes(chunkCount);
Array.Reverse(bytes4);
outStream.Write(bytes4, 0, bytes4.Length);
byte[] bytes5 = BitConverter.GetBytes(infoOffset);
Array.Reverse(bytes5);
outStream.Write(bytes5, 0, bytes5.Length);
byte[] bytes6 = BitConverter.GetBytes(infoLength);
Array.Reverse(bytes6);
outStream.Write(bytes6, 0, bytes6.Length);
byte[] bytes7 = BitConverter.GetBytes(dataOffset);
Array.Reverse(bytes7);
outStream.Write(bytes7, 0, bytes7.Length);
byte[] bytes8 = BitConverter.GetBytes(dataLength);
Array.Reverse(bytes8);
outStream.Write(bytes8, 0, bytes8.Length);
}
public void Read(Stream input)
{
BinaryReader binaryReader = new BinaryReader(input);
if (!Shared.CompareByteArrays(magic, binaryReader.ReadBytes(4)))
{
binaryReader.BaseStream.Seek(28L, SeekOrigin.Current);
if (!Shared.CompareByteArrays(magic, binaryReader.ReadBytes(4)))
{
throw new Exception("This is not a valid BNS audio file!");
}
}
flags = Shared.Swap(binaryReader.ReadUInt32());
fileSize = Shared.Swap(binaryReader.ReadUInt32());
size = Shared.Swap(binaryReader.ReadUInt16());
chunkCount = Shared.Swap(binaryReader.ReadUInt16());
infoOffset = Shared.Swap(binaryReader.ReadUInt32());
infoLength = Shared.Swap(binaryReader.ReadUInt32());
dataOffset = Shared.Swap(binaryReader.ReadUInt32());
dataLength = Shared.Swap(binaryReader.ReadUInt32());
}
}
}

View file

@ -1,380 +0,0 @@
/* This file is part of libWiiSharp
* Copyright (C) 2009 Leathl
* Copyright (C) 2020 - 2021 Github Contributors
*
* libWiiSharp is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libWiiSharp is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.IO;
namespace libWiiSharp
{
internal class BNS_Info
{
//Private Variables
private readonly byte[] magic = new byte[4]
{
73,
78,
70,
79
};
private uint size = 160;
private byte codec;
private byte hasLoop;
private byte channelCount = 2;
private byte zero;
private ushort sampleRate = 44100;
private ushort pad0;
private uint loopStart;
private uint loopEnd; //Or total sample count
private uint offsetToChannelStart = 24;
private uint pad1;
private uint channel1StartOffset = 32;
private uint channel2StartOffset = 44;
private uint channel1Start;
private uint coefficients1Offset = 56;
private uint pad2;
private uint channel2Start;
private uint coefficients2Offset = 104;
private uint pad3;
private int[] coefficients1 = new int[16];
private ushort channel1Gain;
private ushort channel1PredictiveScale;
private ushort channel1PreviousValue;
private ushort channel1NextPreviousValue;
private ushort channel1LoopPredictiveScale;
private ushort channel1LoopPreviousValue;
private ushort channel1LoopNextPreviousValue;
private ushort channel1LoopPadding;
private int[] coefficients2 = new int[16];
private ushort channel2Gain;
private ushort channel2PredictiveScale;
private ushort channel2PreviousValue;
private ushort channel2NextPreviousValue;
private ushort channel2LoopPredictiveScale;
private ushort channel2LoopPreviousValue;
private ushort channel2LoopNextPreviousValue;
private ushort channel2LoopPadding;
//Public Variables
public byte HasLoop
{
get => hasLoop;
set => hasLoop = value;
}
public uint Coefficients1Offset
{
get => coefficients1Offset;
set => coefficients1Offset = value;
}
public uint Channel1StartOffset
{
get => channel1StartOffset;
set => channel1StartOffset = value;
}
public uint Channel2StartOffset
{
get => channel2StartOffset;
set => channel2StartOffset = value;
}
public uint Size
{
get => size;
set => size = value;
}
public ushort SampleRate
{
get => sampleRate;
set => sampleRate = value;
}
public byte ChannelCount
{
get => channelCount;
set => channelCount = value;
}
public uint Channel1Start
{
get => channel1Start;
set => channel1Start = value;
}
public uint Channel2Start
{
get => channel2Start;
set => channel2Start = value;
}
public uint LoopStart
{
get => loopStart;
set => loopStart = value;
}
public uint LoopEnd
{
get => loopEnd;
set => loopEnd = value;
}
public int[] Coefficients1
{
get => coefficients1;
set => coefficients1 = value;
}
public int[] Coefficients2
{
get => coefficients2;
set => coefficients2 = value;
}
public void Write(Stream outStream)
{
outStream.Write(magic, 0, magic.Length);
byte[] bytes1 = BitConverter.GetBytes(size);
Array.Reverse(bytes1);
outStream.Write(bytes1, 0, bytes1.Length);
outStream.WriteByte(codec);
outStream.WriteByte(hasLoop);
outStream.WriteByte(channelCount);
outStream.WriteByte(zero);
byte[] bytes2 = BitConverter.GetBytes(sampleRate);
Array.Reverse(bytes2);
outStream.Write(bytes2, 0, bytes2.Length);
byte[] bytes3 = BitConverter.GetBytes(pad0);
Array.Reverse(bytes3);
outStream.Write(bytes3, 0, bytes3.Length);
byte[] bytes4 = BitConverter.GetBytes(loopStart);
Array.Reverse(bytes4);
outStream.Write(bytes4, 0, bytes4.Length);
byte[] bytes5 = BitConverter.GetBytes(loopEnd);
Array.Reverse(bytes5);
outStream.Write(bytes5, 0, bytes5.Length);
byte[] bytes6 = BitConverter.GetBytes(offsetToChannelStart);
Array.Reverse(bytes6);
outStream.Write(bytes6, 0, bytes6.Length);
byte[] bytes7 = BitConverter.GetBytes(pad1);
Array.Reverse(bytes7);
outStream.Write(bytes7, 0, bytes7.Length);
byte[] bytes8 = BitConverter.GetBytes(channel1StartOffset);
Array.Reverse(bytes8);
outStream.Write(bytes8, 0, bytes8.Length);
byte[] bytes9 = BitConverter.GetBytes(channel2StartOffset);
Array.Reverse(bytes9);
outStream.Write(bytes9, 0, bytes9.Length);
byte[] bytes10 = BitConverter.GetBytes(channel1Start);
Array.Reverse(bytes10);
outStream.Write(bytes10, 0, bytes10.Length);
byte[] bytes11 = BitConverter.GetBytes(coefficients1Offset);
Array.Reverse(bytes11);
outStream.Write(bytes11, 0, bytes11.Length);
if (channelCount == 2)
{
byte[] bytes12 = BitConverter.GetBytes(pad2);
Array.Reverse(bytes12);
outStream.Write(bytes12, 0, bytes12.Length);
byte[] bytes13 = BitConverter.GetBytes(channel2Start);
Array.Reverse(bytes13);
outStream.Write(bytes13, 0, bytes13.Length);
byte[] bytes14 = BitConverter.GetBytes(coefficients2Offset);
Array.Reverse(bytes14);
outStream.Write(bytes14, 0, bytes14.Length);
byte[] bytes15 = BitConverter.GetBytes(pad3);
Array.Reverse(bytes15);
outStream.Write(bytes15, 0, bytes15.Length);
foreach (int num in coefficients1)
{
byte[] bytes16 = BitConverter.GetBytes(num);
Array.Reverse(bytes16);
outStream.Write(bytes16, 2, bytes16.Length - 2);
}
byte[] bytes17 = BitConverter.GetBytes(channel1Gain);
Array.Reverse(bytes17);
outStream.Write(bytes17, 0, bytes17.Length);
byte[] bytes18 = BitConverter.GetBytes(channel1PredictiveScale);
Array.Reverse(bytes18);
outStream.Write(bytes18, 0, bytes18.Length);
byte[] bytes19 = BitConverter.GetBytes(channel1PreviousValue);
Array.Reverse(bytes19);
outStream.Write(bytes19, 0, bytes19.Length);
byte[] bytes20 = BitConverter.GetBytes(channel1NextPreviousValue);
Array.Reverse(bytes20);
outStream.Write(bytes20, 0, bytes20.Length);
byte[] bytes21 = BitConverter.GetBytes(channel1LoopPredictiveScale);
Array.Reverse(bytes21);
outStream.Write(bytes21, 0, bytes21.Length);
byte[] bytes22 = BitConverter.GetBytes(channel1LoopPreviousValue);
Array.Reverse(bytes22);
outStream.Write(bytes22, 0, bytes22.Length);
byte[] bytes23 = BitConverter.GetBytes(channel1LoopNextPreviousValue);
Array.Reverse(bytes23);
outStream.Write(bytes23, 0, bytes23.Length);
byte[] bytes24 = BitConverter.GetBytes(channel1LoopPadding);
Array.Reverse(bytes24);
outStream.Write(bytes24, 0, bytes24.Length);
foreach (int num in coefficients2)
{
byte[] bytes16 = BitConverter.GetBytes(num);
Array.Reverse(bytes16);
outStream.Write(bytes16, 2, bytes16.Length - 2);
}
byte[] bytes25 = BitConverter.GetBytes(channel2Gain);
Array.Reverse(bytes25);
outStream.Write(bytes25, 0, bytes25.Length);
byte[] bytes26 = BitConverter.GetBytes(channel2PredictiveScale);
Array.Reverse(bytes26);
outStream.Write(bytes26, 0, bytes26.Length);
byte[] bytes27 = BitConverter.GetBytes(channel2PreviousValue);
Array.Reverse(bytes27);
outStream.Write(bytes27, 0, bytes27.Length);
byte[] bytes28 = BitConverter.GetBytes(channel2NextPreviousValue);
Array.Reverse(bytes28);
outStream.Write(bytes28, 0, bytes28.Length);
byte[] bytes29 = BitConverter.GetBytes(channel2LoopPredictiveScale);
Array.Reverse(bytes29);
outStream.Write(bytes29, 0, bytes29.Length);
byte[] bytes30 = BitConverter.GetBytes(channel2LoopPreviousValue);
Array.Reverse(bytes30);
outStream.Write(bytes30, 0, bytes30.Length);
byte[] bytes31 = BitConverter.GetBytes(channel2LoopNextPreviousValue);
Array.Reverse(bytes31);
outStream.Write(bytes31, 0, bytes31.Length);
byte[] bytes32 = BitConverter.GetBytes(channel2LoopPadding);
Array.Reverse(bytes32);
outStream.Write(bytes32, 0, bytes32.Length);
}
else
{
if (channelCount != 1)
{
return;
}
foreach (int num in coefficients1)
{
byte[] bytes12 = BitConverter.GetBytes(num);
Array.Reverse(bytes12);
outStream.Write(bytes12, 2, bytes12.Length - 2);
}
byte[] bytes13 = BitConverter.GetBytes(channel1Gain);
Array.Reverse(bytes13);
outStream.Write(bytes13, 0, bytes13.Length);
byte[] bytes14 = BitConverter.GetBytes(channel1PredictiveScale);
Array.Reverse(bytes14);
outStream.Write(bytes14, 0, bytes14.Length);
byte[] bytes15 = BitConverter.GetBytes(channel1PreviousValue);
Array.Reverse(bytes15);
outStream.Write(bytes15, 0, bytes15.Length);
byte[] bytes16 = BitConverter.GetBytes(channel1NextPreviousValue);
Array.Reverse(bytes16);
outStream.Write(bytes16, 0, bytes16.Length);
byte[] bytes17 = BitConverter.GetBytes(channel1LoopPredictiveScale);
Array.Reverse(bytes17);
outStream.Write(bytes17, 0, bytes17.Length);
byte[] bytes18 = BitConverter.GetBytes(channel1LoopPreviousValue);
Array.Reverse(bytes18);
outStream.Write(bytes18, 0, bytes18.Length);
byte[] bytes19 = BitConverter.GetBytes(channel1LoopNextPreviousValue);
Array.Reverse(bytes19);
outStream.Write(bytes19, 0, bytes19.Length);
byte[] bytes20 = BitConverter.GetBytes(channel1LoopPadding);
Array.Reverse(bytes20);
outStream.Write(bytes20, 0, bytes20.Length);
}
}
public void Read(Stream input)
{
BinaryReader binaryReader = new BinaryReader(input);
size = Shared.CompareByteArrays(magic, binaryReader.ReadBytes(4)) ? Shared.Swap(binaryReader.ReadUInt32()) : throw new Exception("This is not a valid BNS audfo file!");
codec = binaryReader.ReadByte();
hasLoop = binaryReader.ReadByte();
channelCount = binaryReader.ReadByte();
zero = binaryReader.ReadByte();
sampleRate = Shared.Swap(binaryReader.ReadUInt16());
pad0 = Shared.Swap(binaryReader.ReadUInt16());
loopStart = Shared.Swap(binaryReader.ReadUInt32());
loopEnd = Shared.Swap(binaryReader.ReadUInt32());
offsetToChannelStart = Shared.Swap(binaryReader.ReadUInt32());
pad1 = Shared.Swap(binaryReader.ReadUInt32());
channel1StartOffset = Shared.Swap(binaryReader.ReadUInt32());
channel2StartOffset = Shared.Swap(binaryReader.ReadUInt32());
channel1Start = Shared.Swap(binaryReader.ReadUInt32());
coefficients1Offset = Shared.Swap(binaryReader.ReadUInt32());
if (channelCount == 2)
{
pad2 = Shared.Swap(binaryReader.ReadUInt32());
channel2Start = Shared.Swap(binaryReader.ReadUInt32());
coefficients2Offset = Shared.Swap(binaryReader.ReadUInt32());
pad3 = Shared.Swap(binaryReader.ReadUInt32());
for (int index = 0; index < 16; ++index)
{
coefficients1[index] = (short)Shared.Swap(binaryReader.ReadUInt16());
}
channel1Gain = Shared.Swap(binaryReader.ReadUInt16());
channel1PredictiveScale = Shared.Swap(binaryReader.ReadUInt16());
channel1PreviousValue = Shared.Swap(binaryReader.ReadUInt16());
channel1NextPreviousValue = Shared.Swap(binaryReader.ReadUInt16());
channel1LoopPredictiveScale = Shared.Swap(binaryReader.ReadUInt16());
channel1LoopPreviousValue = Shared.Swap(binaryReader.ReadUInt16());
channel1LoopNextPreviousValue = Shared.Swap(binaryReader.ReadUInt16());
channel1LoopPadding = Shared.Swap(binaryReader.ReadUInt16());
for (int index = 0; index < 16; ++index)
{
coefficients2[index] = (short)Shared.Swap(binaryReader.ReadUInt16());
}
channel2Gain = Shared.Swap(binaryReader.ReadUInt16());
channel2PredictiveScale = Shared.Swap(binaryReader.ReadUInt16());
channel2PreviousValue = Shared.Swap(binaryReader.ReadUInt16());
channel2NextPreviousValue = Shared.Swap(binaryReader.ReadUInt16());
channel2LoopPredictiveScale = Shared.Swap(binaryReader.ReadUInt16());
channel2LoopPreviousValue = Shared.Swap(binaryReader.ReadUInt16());
channel2LoopNextPreviousValue = Shared.Swap(binaryReader.ReadUInt16());
channel2LoopPadding = Shared.Swap(binaryReader.ReadUInt16());
}
else
{
if (channelCount != 1)
{
return;
}
for (int index = 0; index < 16; ++index)
{
coefficients1[index] = (short)Shared.Swap(binaryReader.ReadUInt16());
}
channel1Gain = Shared.Swap(binaryReader.ReadUInt16());
channel1PredictiveScale = Shared.Swap(binaryReader.ReadUInt16());
channel1PreviousValue = Shared.Swap(binaryReader.ReadUInt16());
channel1NextPreviousValue = Shared.Swap(binaryReader.ReadUInt16());
channel1LoopPredictiveScale = Shared.Swap(binaryReader.ReadUInt16());
channel1LoopPreviousValue = Shared.Swap(binaryReader.ReadUInt16());
channel1LoopNextPreviousValue = Shared.Swap(binaryReader.ReadUInt16());
channel1LoopPadding = Shared.Swap(binaryReader.ReadUInt16());
}
}
}
}

View file

@ -1,314 +0,0 @@
/* This file is part of libWiiSharp
* Copyright (C) 2009 Leathl
* Copyright (C) 2020 - 2021 Github Contributors
*
* libWiiSharp is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libWiiSharp is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
namespace libWiiSharp
{
internal class ColorIndexConverter
{
private uint[] rgbaPalette;
private byte[] tplPalette;
private readonly uint[] rgbaData;
private byte[] tplData;
private readonly TPL_TextureFormat tplFormat;
private readonly TPL_PaletteFormat paletteFormat;
private readonly int width;
private readonly int height;
public byte[] Palette => tplPalette;
public byte[] Data => tplData;
public ColorIndexConverter(
uint[] rgbaData,
int width,
int height,
TPL_TextureFormat tplFormat,
TPL_PaletteFormat paletteFormat)
{
if (tplFormat != TPL_TextureFormat.CI4 && tplFormat != TPL_TextureFormat.CI8)
{
throw new Exception("Texture format must be either CI4 or CI8");
}
if (paletteFormat != TPL_PaletteFormat.IA8 && paletteFormat != TPL_PaletteFormat.RGB565 && paletteFormat != TPL_PaletteFormat.RGB5A3)
{
throw new Exception("Palette format must be either IA8, RGB565 or RGB5A3!");
}
this.rgbaData = rgbaData;
this.width = width;
this.height = height;
this.tplFormat = tplFormat;
this.paletteFormat = paletteFormat;
BuildPalette();
if (tplFormat != TPL_TextureFormat.CI4)
{
if (tplFormat == TPL_TextureFormat.CI8)
{
ToCI8();
}
else
{
ToCI14X2();
}
}
else
{
ToCI4();
}
}
private void ToCI4()
{
byte[] numArray = new byte[Shared.AddPadding(width, 8) * Shared.AddPadding(height, 8) / 2];
int num = 0;
for (int index1 = 0; index1 < height; index1 += 8)
{
for (int index2 = 0; index2 < width; index2 += 8)
{
for (int index3 = index1; index3 < index1 + 8; ++index3)
{
for (int index4 = index2; index4 < index2 + 8; index4 += 2)
{
uint colorIndex1 = GetColorIndex(index3 >= height || index4 >= width ? 0U : rgbaData[index3 * width + index4]);
uint colorIndex2 = GetColorIndex(index3 >= height || index4 >= width ? 0U : (index3 * width + index4 + 1 < rgbaData.Length ? rgbaData[index3 * width + index4 + 1] : 0U));
numArray[num++] = (byte)((uint)(byte)colorIndex1 << 4 | (byte)colorIndex2);
}
}
}
}
tplData = numArray;
}
private void ToCI8()
{
byte[] numArray = new byte[Shared.AddPadding(width, 8) * Shared.AddPadding(height, 4)];
int num1 = 0;
for (int index1 = 0; index1 < height; index1 += 4)
{
for (int index2 = 0; index2 < width; index2 += 8)
{
for (int index3 = index1; index3 < index1 + 4; ++index3)
{
for (int index4 = index2; index4 < index2 + 8; ++index4)
{
uint num2 = index3 >= height || index4 >= width ? 0U : rgbaData[index3 * width + index4];
numArray[num1++] = (byte)GetColorIndex(num2);
}
}
}
}
tplData = numArray;
}
private void ToCI14X2()
{
byte[] numArray1 = new byte[Shared.AddPadding(width, 4) * Shared.AddPadding(height, 4) * 2];
int num1 = 0;
for (int index1 = 0; index1 < height; index1 += 4)
{
for (int index2 = 0; index2 < width; index2 += 4)
{
for (int index3 = index1; index3 < index1 + 4; ++index3)
{
for (int index4 = index2; index4 < index2 + 4; ++index4)
{
byte[] bytes = BitConverter.GetBytes((ushort)GetColorIndex(index3 >= height || index4 >= width ? 0U : rgbaData[index3 * width + index4]));
byte[] numArray2 = numArray1;
int index5 = num1;
int num2 = index5 + 1;
int num3 = bytes[1];
numArray2[index5] = (byte)num3;
byte[] numArray3 = numArray1;
int index6 = num2;
num1 = index6 + 1;
int num4 = bytes[0];
numArray3[index6] = (byte)num4;
}
}
}
}
tplData = numArray1;
}
private void BuildPalette()
{
int num1 = 256;
if (tplFormat == TPL_TextureFormat.CI4)
{
num1 = 16;
}
else if (tplFormat == TPL_TextureFormat.CI14X2)
{
num1 = 16384;
}
List<uint> uintList = new List<uint>();
List<ushort> ushortList = new List<ushort>();
uintList.Add(0U);
ushortList.Add(0);
for (int index = 1; index < rgbaData.Length && uintList.Count != num1; ++index)
{
if ((rgbaData[index] >> 24 & byte.MaxValue) >= (tplFormat == TPL_TextureFormat.CI14X2 ? 1L : 25L))
{
ushort num2 = Shared.Swap(ConvertToPaletteValue((int)rgbaData[index]));
if (!uintList.Contains(rgbaData[index]) && !ushortList.Contains(num2))
{
uintList.Add(rgbaData[index]);
ushortList.Add(num2);
}
}
}
while (uintList.Count % 16 != 0)
{
uintList.Add(uint.MaxValue);
ushortList.Add(ushort.MaxValue);
}
tplPalette = Shared.UShortArrayToByteArray(ushortList.ToArray());
rgbaPalette = uintList.ToArray();
}
private ushort ConvertToPaletteValue(int rgba)
{
int num1 = 0;
int num2;
if (paletteFormat == TPL_PaletteFormat.IA8)
{
int num3 = ((rgba & byte.MaxValue) + (rgba >> 8 & byte.MaxValue) + (rgba >> 16 & byte.MaxValue)) / 3 & byte.MaxValue;
num2 = (ushort)((rgba >> 24 & byte.MaxValue) << 8 | num3);
}
else if (paletteFormat == TPL_PaletteFormat.RGB565)
{
num2 = (ushort)((rgba >> 16 & byte.MaxValue) >> 3 << 11 | (rgba >> 8 & byte.MaxValue) >> 2 << 5 | (rgba & byte.MaxValue) >> 3);
}
else
{
int num3 = rgba >> 16 & byte.MaxValue;
int num4 = rgba >> 8 & byte.MaxValue;
int num5 = rgba & byte.MaxValue;
int num6 = rgba >> 24 & byte.MaxValue;
if (num6 <= 218)
{
int num7 = num1 & -32769;
int num8 = num3 * 15 / byte.MaxValue & 15;
int num9 = num4 * 15 / byte.MaxValue & 15;
int num10 = num5 * 15 / byte.MaxValue & 15;
int num11 = num6 * 7 / byte.MaxValue & 7;
num2 = num7 | num11 << 12 | num10 | num9 << 4 | num8 << 8;
}
else
{
int num7 = num1 | 32768;
int num8 = num3 * 31 / byte.MaxValue & 31;
int num9 = num4 * 31 / byte.MaxValue & 31;
int num10 = num5 * 31 / byte.MaxValue & 31;
num2 = num7 | num10 | num9 << 5 | num8 << 10;
}
}
return (ushort)num2;
}
private uint GetColorIndex(uint value)
{
uint num1 = int.MaxValue;
uint num2 = 0;
if ((value >> 24 & byte.MaxValue) < (tplFormat == TPL_TextureFormat.CI14X2 ? 1L : 25L))
{
return 0;
}
ushort paletteValue1 = ConvertToPaletteValue((int)value);
for (int index = 0; index < rgbaPalette.Length; ++index)
{
ushort paletteValue2 = ConvertToPaletteValue((int)rgbaPalette[index]);
if (paletteValue1 == paletteValue2)
{
return (uint)index;
}
uint distance = GetDistance(paletteValue1, paletteValue2);
if (distance < num1)
{
num1 = distance;
num2 = (uint)index;
}
}
return num2;
}
private uint GetDistance(ushort color, ushort paletteColor)
{
int rgbaValue1 = (int)ConvertToRgbaValue(color);
uint rgbaValue2 = ConvertToRgbaValue(paletteColor);
uint val1_1 = (uint)rgbaValue1 >> 24 & byte.MaxValue;
uint val1_2 = (uint)rgbaValue1 >> 16 & byte.MaxValue;
uint val1_3 = (uint)rgbaValue1 >> 8 & byte.MaxValue;
uint val1_4 = (uint)(rgbaValue1 & byte.MaxValue);
uint val2_1 = rgbaValue2 >> 24 & byte.MaxValue;
uint val2_2 = rgbaValue2 >> 16 & byte.MaxValue;
uint val2_3 = rgbaValue2 >> 8 & byte.MaxValue;
uint val2_4 = rgbaValue2 & byte.MaxValue;
int num1 = (int)Math.Max(val1_1, val2_1) - (int)Math.Min(val1_1, val2_1);
uint num2 = Math.Max(val1_2, val2_2) - Math.Min(val1_2, val2_2);
uint num3 = Math.Max(val1_3, val2_3) - Math.Min(val1_3, val2_3);
uint num4 = Math.Max(val1_4, val2_4) - Math.Min(val1_4, val2_4);
int num5 = (int)num2;
return (uint)(num1 + num5) + num3 + num4;
}
private uint ConvertToRgbaValue(ushort pixel)
{
if (paletteFormat == TPL_PaletteFormat.IA8)
{
int num1 = pixel >> 8;
int num2 = pixel & byte.MaxValue;
return (uint)(num1 | num1 << 8 | num1 << 16 | num2 << 24);
}
if (paletteFormat == TPL_PaletteFormat.RGB565)
{
int num1 = (pixel >> 11 & 31) << 3 & byte.MaxValue;
int num2 = (pixel >> 5 & 63) << 2 & byte.MaxValue;
int num3 = (pixel & 31) << 3 & byte.MaxValue;
int maxValue = byte.MaxValue;
return (uint)(num3 | num2 << 8 | num1 << 16 | maxValue << 24);
}
int num4;
int num5;
int num6;
int num7;
if ((pixel & 32768) != 0)
{
num4 = (pixel >> 10 & 31) * byte.MaxValue / 31;
num5 = (pixel >> 5 & 31) * byte.MaxValue / 31;
num6 = (pixel & 31) * byte.MaxValue / 31;
num7 = byte.MaxValue;
}
else
{
num7 = (pixel >> 12 & 7) * byte.MaxValue / 7;
num4 = (pixel >> 8 & 15) * byte.MaxValue / 15;
num5 = (pixel >> 4 & 15) * byte.MaxValue / 15;
num6 = (pixel & 15) * byte.MaxValue / 15;
}
return (uint)(num6 | num5 << 8 | num4 << 16 | num7 << 24);
}
}
}

View file

@ -1,28 +0,0 @@
/* This file is part of libWiiSharp
* Copyright (C) 2009 Leathl
* Copyright (C) 2020 - 2021 Github Contributors
*
* libWiiSharp is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libWiiSharp is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace libWiiSharp
{
public enum Region : ushort
{
Japan,
USA,
Europe,
Free,
}
}

49
TMD.cs
View file

@ -23,6 +23,15 @@ using System.Security.Cryptography;
namespace libWiiSharp
{
public enum Region : ushort
{
Japan,
USA,
Europe,
Free,
}
public enum ContentType : ushort
{
Normal = 1,
@ -598,4 +607,44 @@ namespace libWiiSharp
debug(new object(), new MessageEventArgs(string.Format(debugMessage, args)));
}
}
public class TMD_Content
{
private uint contentId;
private ushort index;
private ushort type;
private ulong size;
private byte[] hash = new byte[20];
public uint ContentID
{
get => contentId;
set => contentId = value;
}
public ushort Index
{
get => index;
set => index = value;
}
public ContentType Type
{
get => (ContentType)type;
set => type = (ushort)value;
}
public ulong Size
{
get => size;
set => size = value;
}
public byte[] Hash
{
get => hash;
set => hash = value;
}
}
}

View file

@ -1,59 +0,0 @@
/* This file is part of libWiiSharp
* Copyright (C) 2009 Leathl
* Copyright (C) 2020 - 2021 Github Contributors
*
* libWiiSharp is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libWiiSharp is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace libWiiSharp
{
public class TMD_Content
{
private uint contentId;
private ushort index;
private ushort type;
private ulong size;
private byte[] hash = new byte[20];
public uint ContentID
{
get => contentId;
set => contentId = value;
}
public ushort Index
{
get => index;
set => index = value;
}
public ContentType Type
{
get => (ContentType)type;
set => type = (ushort)value;
}
public ulong Size
{
get => size;
set => size = value;
}
public byte[] Hash
{
get => hash;
set => hash = value;
}
}
}

526
TPL.cs
View file

@ -16,6 +16,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//TPL conversion based on Wii.py by Xuzz, SquidMan, megazig, Matt_P, Omega and The Lemon Man.
//Zetsubou by SquidMan was also a reference.
//Thanks to the authors!
using System;
using System.Collections.Generic;
using System.Drawing;
@ -25,6 +29,30 @@ using System.Runtime.InteropServices;
namespace libWiiSharp
{
public enum TPL_TextureFormat
{
I4 = 0,
I8 = 1,
IA4 = 2,
IA8 = 3,
RGB565 = 4,
RGB5A3 = 5,
RGBA8 = 6,
CI4 = 8,
CI8 = 9,
CI14X2 = 10, // 0x0000000A
CMP = 14, // 0x0000000E
}
public enum TPL_PaletteFormat
{
IA8 = 0,
RGB565 = 1,
RGB5A3 = 2,
None = 255, // 0x000000FF
}
public class TPL : IDisposable
{
private TPL_Header tplHeader = new TPL_Header();
@ -1432,4 +1460,502 @@ namespace libWiiSharp
debug(new object(), new MessageEventArgs(string.Format(debugMessage, args)));
}
}
public class TPL_Header
{
private readonly uint tplMagic = 2142000;
private uint numOfTextures;
private readonly uint headerSize = 12;
public uint TplMagic => tplMagic;
public uint NumOfTextures
{
get => numOfTextures;
set => numOfTextures = value;
}
public uint HeaderSize => headerSize;
public void Write(Stream writeStream)
{
writeStream.Write(BitConverter.GetBytes(Shared.Swap(tplMagic)), 0, 4);
writeStream.Write(BitConverter.GetBytes(Shared.Swap(numOfTextures)), 0, 4);
writeStream.Write(BitConverter.GetBytes(Shared.Swap(headerSize)), 0, 4);
}
}
public class TPL_TextureEntry
{
private uint textureHeaderOffset;
private uint paletteHeaderOffset;
public uint TextureHeaderOffset
{
get => textureHeaderOffset;
set => textureHeaderOffset = value;
}
public uint PaletteHeaderOffset
{
get => paletteHeaderOffset;
set => paletteHeaderOffset = value;
}
public void Write(Stream writeStream)
{
writeStream.Write(BitConverter.GetBytes(Shared.Swap(textureHeaderOffset)), 0, 4);
writeStream.Write(BitConverter.GetBytes(Shared.Swap(paletteHeaderOffset)), 0, 4);
}
}
public class TPL_TextureHeader
{
private ushort textureHeight;
private ushort textureWidth;
private uint textureFormat;
private uint textureDataOffset;
private uint wrapS;
private uint wrapT;
private uint minFilter = 1;
private uint magFilter = 1;
private uint lodBias;
private byte edgeLod;
private byte minLod;
private byte maxLod;
private byte unpacked;
public ushort TextureHeight
{
get => textureHeight;
set => textureHeight = value;
}
public ushort TextureWidth
{
get => textureWidth;
set => textureWidth = value;
}
public uint TextureFormat
{
get => textureFormat;
set => textureFormat = value;
}
public uint TextureDataOffset
{
get => textureDataOffset;
set => textureDataOffset = value;
}
public uint WrapS
{
get => wrapS;
set => wrapS = value;
}
public uint WrapT
{
get => wrapT;
set => wrapT = value;
}
public uint MinFilter
{
get => minFilter;
set => minFilter = value;
}
public uint MagFilter
{
get => magFilter;
set => magFilter = value;
}
public uint LodBias
{
get => lodBias;
set => lodBias = value;
}
public byte EdgeLod
{
get => edgeLod;
set => edgeLod = value;
}
public byte MinLod
{
get => minLod;
set => minLod = value;
}
public byte MaxLod
{
get => maxLod;
set => maxLod = value;
}
public byte Unpacked
{
get => unpacked;
set => unpacked = value;
}
public void Write(Stream writeStream)
{
writeStream.Write(BitConverter.GetBytes(Shared.Swap(textureHeight)), 0, 2);
writeStream.Write(BitConverter.GetBytes(Shared.Swap(textureWidth)), 0, 2);
writeStream.Write(BitConverter.GetBytes(Shared.Swap(textureFormat)), 0, 4);
writeStream.Write(BitConverter.GetBytes(Shared.Swap(textureDataOffset)), 0, 4);
writeStream.Write(BitConverter.GetBytes(Shared.Swap(wrapS)), 0, 4);
writeStream.Write(BitConverter.GetBytes(Shared.Swap(wrapT)), 0, 4);
writeStream.Write(BitConverter.GetBytes(Shared.Swap(minFilter)), 0, 4);
writeStream.Write(BitConverter.GetBytes(Shared.Swap(magFilter)), 0, 4);
writeStream.Write(BitConverter.GetBytes(Shared.Swap(lodBias)), 0, 4);
writeStream.WriteByte(edgeLod);
writeStream.WriteByte(minLod);
writeStream.WriteByte(maxLod);
writeStream.WriteByte(unpacked);
}
}
public class TPL_PaletteHeader
{
private ushort numberOfItems;
private byte unpacked;
private byte pad;
private uint paletteFormat = byte.MaxValue;
private uint paletteDataOffset;
public ushort NumberOfItems
{
get => numberOfItems;
set => numberOfItems = value;
}
public byte Unpacked
{
get => unpacked;
set => unpacked = value;
}
public byte Pad
{
get => pad;
set => pad = value;
}
public uint PaletteFormat
{
get => paletteFormat;
set => paletteFormat = value;
}
public uint PaletteDataOffset
{
get => paletteDataOffset;
set => paletteDataOffset = value;
}
public void Write(Stream writeStream)
{
writeStream.Write(BitConverter.GetBytes(Shared.Swap(numberOfItems)), 0, 2);
writeStream.WriteByte(unpacked);
writeStream.WriteByte(pad);
writeStream.Write(BitConverter.GetBytes(Shared.Swap(paletteFormat)), 0, 4);
writeStream.Write(BitConverter.GetBytes(Shared.Swap(paletteDataOffset)), 0, 4);
}
}
internal class ColorIndexConverter
{
private uint[] rgbaPalette;
private byte[] tplPalette;
private readonly uint[] rgbaData;
private byte[] tplData;
private readonly TPL_TextureFormat tplFormat;
private readonly TPL_PaletteFormat paletteFormat;
private readonly int width;
private readonly int height;
public byte[] Palette => tplPalette;
public byte[] Data => tplData;
public ColorIndexConverter(
uint[] rgbaData,
int width,
int height,
TPL_TextureFormat tplFormat,
TPL_PaletteFormat paletteFormat)
{
if (tplFormat != TPL_TextureFormat.CI4 && tplFormat != TPL_TextureFormat.CI8)
{
throw new Exception("Texture format must be either CI4 or CI8");
}
if (paletteFormat != TPL_PaletteFormat.IA8 && paletteFormat != TPL_PaletteFormat.RGB565 && paletteFormat != TPL_PaletteFormat.RGB5A3)
{
throw new Exception("Palette format must be either IA8, RGB565 or RGB5A3!");
}
this.rgbaData = rgbaData;
this.width = width;
this.height = height;
this.tplFormat = tplFormat;
this.paletteFormat = paletteFormat;
BuildPalette();
if (tplFormat != TPL_TextureFormat.CI4)
{
if (tplFormat == TPL_TextureFormat.CI8)
{
ToCI8();
}
else
{
ToCI14X2();
}
}
else
{
ToCI4();
}
}
private void ToCI4()
{
byte[] numArray = new byte[Shared.AddPadding(width, 8) * Shared.AddPadding(height, 8) / 2];
int num = 0;
for (int index1 = 0; index1 < height; index1 += 8)
{
for (int index2 = 0; index2 < width; index2 += 8)
{
for (int index3 = index1; index3 < index1 + 8; ++index3)
{
for (int index4 = index2; index4 < index2 + 8; index4 += 2)
{
uint colorIndex1 = GetColorIndex(index3 >= height || index4 >= width ? 0U : rgbaData[index3 * width + index4]);
uint colorIndex2 = GetColorIndex(index3 >= height || index4 >= width ? 0U : (index3 * width + index4 + 1 < rgbaData.Length ? rgbaData[index3 * width + index4 + 1] : 0U));
numArray[num++] = (byte)((uint)(byte)colorIndex1 << 4 | (byte)colorIndex2);
}
}
}
}
tplData = numArray;
}
private void ToCI8()
{
byte[] numArray = new byte[Shared.AddPadding(width, 8) * Shared.AddPadding(height, 4)];
int num1 = 0;
for (int index1 = 0; index1 < height; index1 += 4)
{
for (int index2 = 0; index2 < width; index2 += 8)
{
for (int index3 = index1; index3 < index1 + 4; ++index3)
{
for (int index4 = index2; index4 < index2 + 8; ++index4)
{
uint num2 = index3 >= height || index4 >= width ? 0U : rgbaData[index3 * width + index4];
numArray[num1++] = (byte)GetColorIndex(num2);
}
}
}
}
tplData = numArray;
}
private void ToCI14X2()
{
byte[] numArray1 = new byte[Shared.AddPadding(width, 4) * Shared.AddPadding(height, 4) * 2];
int num1 = 0;
for (int index1 = 0; index1 < height; index1 += 4)
{
for (int index2 = 0; index2 < width; index2 += 4)
{
for (int index3 = index1; index3 < index1 + 4; ++index3)
{
for (int index4 = index2; index4 < index2 + 4; ++index4)
{
byte[] bytes = BitConverter.GetBytes((ushort)GetColorIndex(index3 >= height || index4 >= width ? 0U : rgbaData[index3 * width + index4]));
byte[] numArray2 = numArray1;
int index5 = num1;
int num2 = index5 + 1;
int num3 = bytes[1];
numArray2[index5] = (byte)num3;
byte[] numArray3 = numArray1;
int index6 = num2;
num1 = index6 + 1;
int num4 = bytes[0];
numArray3[index6] = (byte)num4;
}
}
}
}
tplData = numArray1;
}
private void BuildPalette()
{
int num1 = 256;
if (tplFormat == TPL_TextureFormat.CI4)
{
num1 = 16;
}
else if (tplFormat == TPL_TextureFormat.CI14X2)
{
num1 = 16384;
}
List<uint> uintList = new List<uint>();
List<ushort> ushortList = new List<ushort>();
uintList.Add(0U);
ushortList.Add(0);
for (int index = 1; index < rgbaData.Length && uintList.Count != num1; ++index)
{
if ((rgbaData[index] >> 24 & byte.MaxValue) >= (tplFormat == TPL_TextureFormat.CI14X2 ? 1L : 25L))
{
ushort num2 = Shared.Swap(ConvertToPaletteValue((int)rgbaData[index]));
if (!uintList.Contains(rgbaData[index]) && !ushortList.Contains(num2))
{
uintList.Add(rgbaData[index]);
ushortList.Add(num2);
}
}
}
while (uintList.Count % 16 != 0)
{
uintList.Add(uint.MaxValue);
ushortList.Add(ushort.MaxValue);
}
tplPalette = Shared.UShortArrayToByteArray(ushortList.ToArray());
rgbaPalette = uintList.ToArray();
}
private ushort ConvertToPaletteValue(int rgba)
{
int num1 = 0;
int num2;
if (paletteFormat == TPL_PaletteFormat.IA8)
{
int num3 = ((rgba & byte.MaxValue) + (rgba >> 8 & byte.MaxValue) + (rgba >> 16 & byte.MaxValue)) / 3 & byte.MaxValue;
num2 = (ushort)((rgba >> 24 & byte.MaxValue) << 8 | num3);
}
else if (paletteFormat == TPL_PaletteFormat.RGB565)
{
num2 = (ushort)((rgba >> 16 & byte.MaxValue) >> 3 << 11 | (rgba >> 8 & byte.MaxValue) >> 2 << 5 | (rgba & byte.MaxValue) >> 3);
}
else
{
int num3 = rgba >> 16 & byte.MaxValue;
int num4 = rgba >> 8 & byte.MaxValue;
int num5 = rgba & byte.MaxValue;
int num6 = rgba >> 24 & byte.MaxValue;
if (num6 <= 218)
{
int num7 = num1 & -32769;
int num8 = num3 * 15 / byte.MaxValue & 15;
int num9 = num4 * 15 / byte.MaxValue & 15;
int num10 = num5 * 15 / byte.MaxValue & 15;
int num11 = num6 * 7 / byte.MaxValue & 7;
num2 = num7 | num11 << 12 | num10 | num9 << 4 | num8 << 8;
}
else
{
int num7 = num1 | 32768;
int num8 = num3 * 31 / byte.MaxValue & 31;
int num9 = num4 * 31 / byte.MaxValue & 31;
int num10 = num5 * 31 / byte.MaxValue & 31;
num2 = num7 | num10 | num9 << 5 | num8 << 10;
}
}
return (ushort)num2;
}
private uint GetColorIndex(uint value)
{
uint num1 = int.MaxValue;
uint num2 = 0;
if ((value >> 24 & byte.MaxValue) < (tplFormat == TPL_TextureFormat.CI14X2 ? 1L : 25L))
{
return 0;
}
ushort paletteValue1 = ConvertToPaletteValue((int)value);
for (int index = 0; index < rgbaPalette.Length; ++index)
{
ushort paletteValue2 = ConvertToPaletteValue((int)rgbaPalette[index]);
if (paletteValue1 == paletteValue2)
{
return (uint)index;
}
uint distance = GetDistance(paletteValue1, paletteValue2);
if (distance < num1)
{
num1 = distance;
num2 = (uint)index;
}
}
return num2;
}
private uint GetDistance(ushort color, ushort paletteColor)
{
int rgbaValue1 = (int)ConvertToRgbaValue(color);
uint rgbaValue2 = ConvertToRgbaValue(paletteColor);
uint val1_1 = (uint)rgbaValue1 >> 24 & byte.MaxValue;
uint val1_2 = (uint)rgbaValue1 >> 16 & byte.MaxValue;
uint val1_3 = (uint)rgbaValue1 >> 8 & byte.MaxValue;
uint val1_4 = (uint)(rgbaValue1 & byte.MaxValue);
uint val2_1 = rgbaValue2 >> 24 & byte.MaxValue;
uint val2_2 = rgbaValue2 >> 16 & byte.MaxValue;
uint val2_3 = rgbaValue2 >> 8 & byte.MaxValue;
uint val2_4 = rgbaValue2 & byte.MaxValue;
int num1 = (int)Math.Max(val1_1, val2_1) - (int)Math.Min(val1_1, val2_1);
uint num2 = Math.Max(val1_2, val2_2) - Math.Min(val1_2, val2_2);
uint num3 = Math.Max(val1_3, val2_3) - Math.Min(val1_3, val2_3);
uint num4 = Math.Max(val1_4, val2_4) - Math.Min(val1_4, val2_4);
int num5 = (int)num2;
return (uint)(num1 + num5) + num3 + num4;
}
private uint ConvertToRgbaValue(ushort pixel)
{
if (paletteFormat == TPL_PaletteFormat.IA8)
{
int num1 = pixel >> 8;
int num2 = pixel & byte.MaxValue;
return (uint)(num1 | num1 << 8 | num1 << 16 | num2 << 24);
}
if (paletteFormat == TPL_PaletteFormat.RGB565)
{
int num1 = (pixel >> 11 & 31) << 3 & byte.MaxValue;
int num2 = (pixel >> 5 & 63) << 2 & byte.MaxValue;
int num3 = (pixel & 31) << 3 & byte.MaxValue;
int maxValue = byte.MaxValue;
return (uint)(num3 | num2 << 8 | num1 << 16 | maxValue << 24);
}
int num4;
int num5;
int num6;
int num7;
if ((pixel & 32768) != 0)
{
num4 = (pixel >> 10 & 31) * byte.MaxValue / 31;
num5 = (pixel >> 5 & 31) * byte.MaxValue / 31;
num6 = (pixel & 31) * byte.MaxValue / 31;
num7 = byte.MaxValue;
}
else
{
num7 = (pixel >> 12 & 7) * byte.MaxValue / 7;
num4 = (pixel >> 8 & 15) * byte.MaxValue / 15;
num5 = (pixel >> 4 & 15) * byte.MaxValue / 15;
num6 = (pixel & 15) * byte.MaxValue / 15;
}
return (uint)(num6 | num5 << 8 | num4 << 16 | num7 << 24);
}
}
}

View file

@ -1,47 +0,0 @@
/* This file is part of libWiiSharp
*Copyright(C) 2009 Leathl
* Copyright(C) 2020 - 2021 Github Contributors
*
* libWiiSharp is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libWiiSharp is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.IO;
namespace libWiiSharp
{
public class TPL_Header
{
private readonly uint tplMagic = 2142000;
private uint numOfTextures;
private readonly uint headerSize = 12;
public uint TplMagic => tplMagic;
public uint NumOfTextures
{
get => numOfTextures;
set => numOfTextures = value;
}
public uint HeaderSize => headerSize;
public void Write(Stream writeStream)
{
writeStream.Write(BitConverter.GetBytes(Shared.Swap(tplMagic)), 0, 4);
writeStream.Write(BitConverter.GetBytes(Shared.Swap(numOfTextures)), 0, 4);
writeStream.Write(BitConverter.GetBytes(Shared.Swap(headerSize)), 0, 4);
}
}
}

View file

@ -1,28 +0,0 @@
/* This file is part of libWiiSharp
* Copyright (C) 2009 Leathl
* Copyright (C) 2020 - 2021 Github Contributors
*
* libWiiSharp is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libWiiSharp is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace libWiiSharp
{
public enum TPL_PaletteFormat
{
IA8 = 0,
RGB565 = 1,
RGB5A3 = 2,
None = 255, // 0x000000FF
}
}

View file

@ -1,71 +0,0 @@
/* This file is part of libWiiSharp
* Copyright (C) 2009 Leathl
* Copyright (C) 2020 - 2021 Github Contributors
*
* libWiiSharp is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libWiiSharp is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.IO;
namespace libWiiSharp
{
public class TPL_PaletteHeader
{
private ushort numberOfItems;
private byte unpacked;
private byte pad;
private uint paletteFormat = byte.MaxValue;
private uint paletteDataOffset;
public ushort NumberOfItems
{
get => numberOfItems;
set => numberOfItems = value;
}
public byte Unpacked
{
get => unpacked;
set => unpacked = value;
}
public byte Pad
{
get => pad;
set => pad = value;
}
public uint PaletteFormat
{
get => paletteFormat;
set => paletteFormat = value;
}
public uint PaletteDataOffset
{
get => paletteDataOffset;
set => paletteDataOffset = value;
}
public void Write(Stream writeStream)
{
writeStream.Write(BitConverter.GetBytes(Shared.Swap(numberOfItems)), 0, 2);
writeStream.WriteByte(unpacked);
writeStream.WriteByte(pad);
writeStream.Write(BitConverter.GetBytes(Shared.Swap(paletteFormat)), 0, 4);
writeStream.Write(BitConverter.GetBytes(Shared.Swap(paletteDataOffset)), 0, 4);
}
}
}

View file

@ -1,47 +0,0 @@
/* This file is part of libWiiSharp
* Copyright (C) 2009 Leathl
* Copyright (C) 2020 - 2021 Github Contributors
*
* libWiiSharp is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libWiiSharp is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.IO;
namespace libWiiSharp
{
public class TPL_TextureEntry
{
private uint textureHeaderOffset;
private uint paletteHeaderOffset;
public uint TextureHeaderOffset
{
get => textureHeaderOffset;
set => textureHeaderOffset = value;
}
public uint PaletteHeaderOffset
{
get => paletteHeaderOffset;
set => paletteHeaderOffset = value;
}
public void Write(Stream writeStream)
{
writeStream.Write(BitConverter.GetBytes(Shared.Swap(textureHeaderOffset)), 0, 4);
writeStream.Write(BitConverter.GetBytes(Shared.Swap(paletteHeaderOffset)), 0, 4);
}
}
}

View file

@ -1,35 +0,0 @@
/* This file is part of libWiiSharp
* Copyright (C) 2009 Leathl
* Copyright (C) 2020 - 2021 Github Contributors
*
* libWiiSharp is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libWiiSharp is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace libWiiSharp
{
public enum TPL_TextureFormat
{
I4 = 0,
I8 = 1,
IA4 = 2,
IA8 = 3,
RGB565 = 4,
RGB5A3 = 5,
RGBA8 = 6,
CI4 = 8,
CI8 = 9,
CI14X2 = 10, // 0x0000000A
CMP = 14, // 0x0000000E
}
}

View file

@ -1,135 +0,0 @@
/* This file is part of libWiiSharp
* Copyright (C) 2009 Leathl
* Copyright (C) 2020 - 2021 Github Contributors
*
* libWiiSharp is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libWiiSharp is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.IO;
namespace libWiiSharp
{
public class TPL_TextureHeader
{
private ushort textureHeight;
private ushort textureWidth;
private uint textureFormat;
private uint textureDataOffset;
private uint wrapS;
private uint wrapT;
private uint minFilter = 1;
private uint magFilter = 1;
private uint lodBias;
private byte edgeLod;
private byte minLod;
private byte maxLod;
private byte unpacked;
public ushort TextureHeight
{
get => textureHeight;
set => textureHeight = value;
}
public ushort TextureWidth
{
get => textureWidth;
set => textureWidth = value;
}
public uint TextureFormat
{
get => textureFormat;
set => textureFormat = value;
}
public uint TextureDataOffset
{
get => textureDataOffset;
set => textureDataOffset = value;
}
public uint WrapS
{
get => wrapS;
set => wrapS = value;
}
public uint WrapT
{
get => wrapT;
set => wrapT = value;
}
public uint MinFilter
{
get => minFilter;
set => minFilter = value;
}
public uint MagFilter
{
get => magFilter;
set => magFilter = value;
}
public uint LodBias
{
get => lodBias;
set => lodBias = value;
}
public byte EdgeLod
{
get => edgeLod;
set => edgeLod = value;
}
public byte MinLod
{
get => minLod;
set => minLod = value;
}
public byte MaxLod
{
get => maxLod;
set => maxLod = value;
}
public byte Unpacked
{
get => unpacked;
set => unpacked = value;
}
public void Write(Stream writeStream)
{
writeStream.Write(BitConverter.GetBytes(Shared.Swap(textureHeight)), 0, 2);
writeStream.Write(BitConverter.GetBytes(Shared.Swap(textureWidth)), 0, 2);
writeStream.Write(BitConverter.GetBytes(Shared.Swap(textureFormat)), 0, 4);
writeStream.Write(BitConverter.GetBytes(Shared.Swap(textureDataOffset)), 0, 4);
writeStream.Write(BitConverter.GetBytes(Shared.Swap(wrapS)), 0, 4);
writeStream.Write(BitConverter.GetBytes(Shared.Swap(wrapT)), 0, 4);
writeStream.Write(BitConverter.GetBytes(Shared.Swap(minFilter)), 0, 4);
writeStream.Write(BitConverter.GetBytes(Shared.Swap(magFilter)), 0, 4);
writeStream.Write(BitConverter.GetBytes(Shared.Swap(lodBias)), 0, 4);
writeStream.WriteByte(edgeLod);
writeStream.WriteByte(minLod);
writeStream.WriteByte(maxLod);
writeStream.WriteByte(unpacked);
}
}
}

83
U8.cs
View file

@ -25,6 +25,12 @@ using System.Text;
namespace libWiiSharp
{
public enum U8_NodeType : ushort
{
File = 0,
Directory = 256, // 0x0100
}
public class U8 : IDisposable
{
//private const int dataPadding = 32;
@ -962,4 +968,81 @@ namespace libWiiSharp
progress(new object(), new ProgressChangedEventArgs(progressPercentage, string.Empty));
}
}
public class U8_Header
{
private readonly uint u8Magic = 1437218861;
private readonly uint offsetToRootNode = 32;
private uint headerSize;
private uint offsetToData;
private readonly byte[] padding = new byte[16];
public uint U8Magic => u8Magic;
public uint OffsetToRootNode => offsetToRootNode;
public uint HeaderSize
{
get => headerSize;
set => headerSize = value;
}
public uint OffsetToData
{
get => offsetToData;
set => offsetToData = value;
}
public byte[] Padding => padding;
public void Write(Stream writeStream)
{
writeStream.Write(BitConverter.GetBytes(Shared.Swap(u8Magic)), 0, 4);
writeStream.Write(BitConverter.GetBytes(Shared.Swap(offsetToRootNode)), 0, 4);
writeStream.Write(BitConverter.GetBytes(Shared.Swap(headerSize)), 0, 4);
writeStream.Write(BitConverter.GetBytes(Shared.Swap(offsetToData)), 0, 4);
writeStream.Write(padding, 0, 16);
}
}
public class U8_Node
{
private ushort type;
private ushort offsetToName;
private uint offsetToData;
private uint sizeOfData;
public U8_NodeType Type
{
get => (U8_NodeType)type;
set => type = (ushort)value;
}
public ushort OffsetToName
{
get => offsetToName;
set => offsetToName = value;
}
public uint OffsetToData
{
get => offsetToData;
set => offsetToData = value;
}
public uint SizeOfData
{
get => sizeOfData;
set => sizeOfData = value;
}
public void Write(Stream writeStream)
{
writeStream.Write(BitConverter.GetBytes(Shared.Swap(type)), 0, 2);
writeStream.Write(BitConverter.GetBytes(Shared.Swap(offsetToName)), 0, 2);
writeStream.Write(BitConverter.GetBytes(Shared.Swap(offsetToData)), 0, 4);
writeStream.Write(BitConverter.GetBytes(Shared.Swap(sizeOfData)), 0, 4);
}
}
}

View file

@ -1,59 +0,0 @@
/* This file is part of libWiiSharp
* Copyright (C) 2009 Leathl
* Copyright (C) 2020 - 2021 Github Contributors
*
* libWiiSharp is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libWiiSharp is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.IO;
namespace libWiiSharp
{
public class U8_Header
{
private readonly uint u8Magic = 1437218861;
private readonly uint offsetToRootNode = 32;
private uint headerSize;
private uint offsetToData;
private readonly byte[] padding = new byte[16];
public uint U8Magic => u8Magic;
public uint OffsetToRootNode => offsetToRootNode;
public uint HeaderSize
{
get => headerSize;
set => headerSize = value;
}
public uint OffsetToData
{
get => offsetToData;
set => offsetToData = value;
}
public byte[] Padding => padding;
public void Write(Stream writeStream)
{
writeStream.Write(BitConverter.GetBytes(Shared.Swap(u8Magic)), 0, 4);
writeStream.Write(BitConverter.GetBytes(Shared.Swap(offsetToRootNode)), 0, 4);
writeStream.Write(BitConverter.GetBytes(Shared.Swap(headerSize)), 0, 4);
writeStream.Write(BitConverter.GetBytes(Shared.Swap(offsetToData)), 0, 4);
writeStream.Write(padding, 0, 16);
}
}
}

View file

@ -1,63 +0,0 @@
/* This file is part of libWiiSharp
* Copyright (C) 2009 Leathl
* Copyright (C) 2020 - 2021 Github Contributors
*
* libWiiSharp is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libWiiSharp is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.IO;
namespace libWiiSharp
{
public class U8_Node
{
private ushort type;
private ushort offsetToName;
private uint offsetToData;
private uint sizeOfData;
public U8_NodeType Type
{
get => (U8_NodeType)type;
set => type = (ushort)value;
}
public ushort OffsetToName
{
get => offsetToName;
set => offsetToName = value;
}
public uint OffsetToData
{
get => offsetToData;
set => offsetToData = value;
}
public uint SizeOfData
{
get => sizeOfData;
set => sizeOfData = value;
}
public void Write(Stream writeStream)
{
writeStream.Write(BitConverter.GetBytes(Shared.Swap(type)), 0, 2);
writeStream.Write(BitConverter.GetBytes(Shared.Swap(offsetToName)), 0, 2);
writeStream.Write(BitConverter.GetBytes(Shared.Swap(offsetToData)), 0, 4);
writeStream.Write(BitConverter.GetBytes(Shared.Swap(sizeOfData)), 0, 4);
}
}
}

View file

@ -1,26 +0,0 @@
/* This file is part of libWiiSharp
* Copyright (C) 2009 Leathl
* Copyright (C) 2020 - 2021 Github Contributors
*
* libWiiSharp is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libWiiSharp is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace libWiiSharp
{
public enum U8_NodeType : ushort
{
File = 0,
Directory = 256, // 0x0100
}
}

241
Wave.cs
View file

@ -17,6 +17,7 @@
*/
using System;
using System.Collections.Generic;
using System.IO;
namespace libWiiSharp
@ -230,4 +231,244 @@ namespace libWiiSharp
}
}
}
internal class WaveHeader
{
private readonly uint headerId = 1380533830;
private uint fileSize = 12;
private readonly uint format = 1463899717;
public uint FileSize
{
get => fileSize;
set => fileSize = value;
}
public void Write(BinaryWriter writer)
{
writer.Write(Shared.Swap(headerId));
writer.Write(fileSize);
writer.Write(Shared.Swap(format));
}
public void Read(BinaryReader reader)
{
fileSize = (int)Shared.Swap(reader.ReadUInt32()) == (int)headerId ? reader.ReadUInt32() : throw new Exception("Not a valid RIFF Wave file!");
if ((int)Shared.Swap(reader.ReadUInt32()) != (int)format)
{
throw new Exception("Not a valid RIFF Wave file!");
}
}
}
internal class WaveFmtChunk
{
private readonly uint fmtId = 1718449184;
private uint fmtSize = 16;
private ushort audioFormat = 1;
private ushort numChannels = 2;
private uint sampleRate = 44100;
private uint byteRate;
private ushort blockAlign;
private ushort bitsPerSample = 16;
public uint FmtSize => fmtSize;
public ushort NumChannels
{
get => numChannels;
set => numChannels = value;
}
public uint SampleRate
{
get => sampleRate;
set => sampleRate = value;
}
public ushort BitsPerSample
{
get => bitsPerSample;
set => bitsPerSample = value;
}
public uint AudioFormat => audioFormat;
public void Write(BinaryWriter writer)
{
byteRate = sampleRate * numChannels * bitsPerSample / 8U;
blockAlign = (ushort)(numChannels * bitsPerSample / 8);
writer.Write(Shared.Swap(fmtId));
writer.Write(fmtSize);
writer.Write(audioFormat);
writer.Write(numChannels);
writer.Write(sampleRate);
writer.Write(byteRate);
writer.Write(blockAlign);
writer.Write(bitsPerSample);
}
public void Read(BinaryReader reader)
{
fmtSize = (int)Shared.Swap(reader.ReadUInt32()) == (int)fmtId ? reader.ReadUInt32() : throw new Exception("Wrong chunk ID!");
audioFormat = reader.ReadUInt16();
numChannels = reader.ReadUInt16();
sampleRate = reader.ReadUInt32();
byteRate = reader.ReadUInt32();
blockAlign = reader.ReadUInt16();
bitsPerSample = reader.ReadUInt16();
}
}
internal class WaveDataChunk
{
private readonly uint dataId = 1684108385;
private uint dataSize = 8;
private byte[] data;
public uint DataSize => dataSize;
public byte[] Data
{
get => data;
set
{
data = value;
dataSize = (uint)data.Length;
}
}
public void Write(BinaryWriter writer)
{
writer.Write(Shared.Swap(dataId));
writer.Write(dataSize);
writer.Write(data, 0, data.Length);
}
public void Read(BinaryReader reader)
{
dataSize = (int)Shared.Swap(reader.ReadUInt32()) == (int)dataId ? reader.ReadUInt32() : throw new Exception("Wrong chunk ID!");
data = reader.ReadBytes((int)dataSize);
}
}
internal class WaveSmplChunk
{
private readonly uint smplId = 1936552044;
private uint smplSize = 36;
private uint manufacturer;
private uint product;
private uint samplePeriod;
private uint unityNote = 60;
private uint pitchFraction;
private uint smpteFormat;
private uint smpteOffset;
private uint numLoops;
private uint samplerData;
private readonly List<WaveSmplLoop> smplLoops = new List<WaveSmplLoop>();
public uint SmplSize => smplSize;
public uint NumLoops => numLoops;
public WaveSmplLoop[] Loops => smplLoops.ToArray();
public void AddLoop(int loopStartSample, int loopEndSample)
{
RemoveAllLoops();
++numLoops;
smplLoops.Add(new WaveSmplLoop()
{
LoopStart = (uint)loopStartSample,
LoopEnd = (uint)loopEndSample
});
}
public void RemoveAllLoops()
{
smplLoops.Clear();
numLoops = 0U;
}
public void Write(BinaryWriter writer)
{
writer.Write(Shared.Swap(smplId));
writer.Write(smplSize);
writer.Write(manufacturer);
writer.Write(product);
writer.Write(samplePeriod);
writer.Write(unityNote);
writer.Write(pitchFraction);
writer.Write(smpteFormat);
writer.Write(smpteOffset);
writer.Write(numLoops);
writer.Write(samplerData);
for (int index = 0; index < numLoops; ++index)
{
smplLoops[index].Write(writer);
}
}
public void Read(BinaryReader reader)
{
smplSize = (int)Shared.Swap(reader.ReadUInt32()) == (int)smplId ? reader.ReadUInt32() : throw new Exception("Wrong chunk ID!");
manufacturer = reader.ReadUInt32();
product = reader.ReadUInt32();
samplePeriod = reader.ReadUInt32();
unityNote = reader.ReadUInt32();
pitchFraction = reader.ReadUInt32();
smpteFormat = reader.ReadUInt32();
smpteOffset = reader.ReadUInt32();
numLoops = reader.ReadUInt32();
samplerData = reader.ReadUInt32();
for (int index = 0; index < numLoops; ++index)
{
WaveSmplLoop waveSmplLoop = new WaveSmplLoop();
waveSmplLoop.Read(reader);
smplLoops.Add(waveSmplLoop);
}
}
}
internal class WaveSmplLoop
{
private uint cuePointId;
private uint type;
private uint start;
private uint end;
private uint fraction;
private uint playCount;
public uint LoopStart
{
get => start;
set => start = value;
}
public uint LoopEnd
{
get => end;
set => end = value;
}
public void Write(BinaryWriter writer)
{
writer.Write(cuePointId);
writer.Write(type);
writer.Write(start);
writer.Write(end);
writer.Write(fraction);
writer.Write(playCount);
}
public void Read(BinaryReader reader)
{
cuePointId = reader.ReadUInt32();
type = reader.ReadUInt32();
start = reader.ReadUInt32();
end = reader.ReadUInt32();
fraction = reader.ReadUInt32();
playCount = reader.ReadUInt32();
}
}
}

View file

@ -1,55 +0,0 @@
/* This file is part of libWiiSharp
* Copyright (C) 2009 Leathl
* Copyright (C) 2020 - 2021 Github Contributors
*
* libWiiSharp is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libWiiSharp is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.IO;
namespace libWiiSharp
{
internal class WaveDataChunk
{
private readonly uint dataId = 1684108385;
private uint dataSize = 8;
private byte[] data;
public uint DataSize => dataSize;
public byte[] Data
{
get => data;
set
{
data = value;
dataSize = (uint)data.Length;
}
}
public void Write(BinaryWriter writer)
{
writer.Write(Shared.Swap(dataId));
writer.Write(dataSize);
writer.Write(data, 0, data.Length);
}
public void Read(BinaryReader reader)
{
dataSize = (int)Shared.Swap(reader.ReadUInt32()) == (int)dataId ? reader.ReadUInt32() : throw new Exception("Wrong chunk ID!");
data = reader.ReadBytes((int)dataSize);
}
}
}

View file

@ -1,82 +0,0 @@
/* This file is part of libWiiSharp
* Copyright (C) 2009 Leathl
* Copyright (C) 2020 - 2021 Github Contributors
*
* libWiiSharp is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libWiiSharp is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.IO;
namespace libWiiSharp
{
internal class WaveFmtChunk
{
private readonly uint fmtId = 1718449184;
private uint fmtSize = 16;
private ushort audioFormat = 1;
private ushort numChannels = 2;
private uint sampleRate = 44100;
private uint byteRate;
private ushort blockAlign;
private ushort bitsPerSample = 16;
public uint FmtSize => fmtSize;
public ushort NumChannels
{
get => numChannels;
set => numChannels = value;
}
public uint SampleRate
{
get => sampleRate;
set => sampleRate = value;
}
public ushort BitsPerSample
{
get => bitsPerSample;
set => bitsPerSample = value;
}
public uint AudioFormat => audioFormat;
public void Write(BinaryWriter writer)
{
byteRate = sampleRate * numChannels * bitsPerSample / 8U;
blockAlign = (ushort)(numChannels * bitsPerSample / 8);
writer.Write(Shared.Swap(fmtId));
writer.Write(fmtSize);
writer.Write(audioFormat);
writer.Write(numChannels);
writer.Write(sampleRate);
writer.Write(byteRate);
writer.Write(blockAlign);
writer.Write(bitsPerSample);
}
public void Read(BinaryReader reader)
{
fmtSize = (int)Shared.Swap(reader.ReadUInt32()) == (int)fmtId ? reader.ReadUInt32() : throw new Exception("Wrong chunk ID!");
audioFormat = reader.ReadUInt16();
numChannels = reader.ReadUInt16();
sampleRate = reader.ReadUInt32();
byteRate = reader.ReadUInt32();
blockAlign = reader.ReadUInt16();
bitsPerSample = reader.ReadUInt16();
}
}
}

View file

@ -1,52 +0,0 @@
/* This file is part of libWiiSharp
* Copyright (C) 2009 Leathl
* Copyright (C) 2020 - 2021 Github Contributors
*
* libWiiSharp is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libWiiSharp is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.IO;
namespace libWiiSharp
{
internal class WaveHeader
{
private readonly uint headerId = 1380533830;
private uint fileSize = 12;
private readonly uint format = 1463899717;
public uint FileSize
{
get => fileSize;
set => fileSize = value;
}
public void Write(BinaryWriter writer)
{
writer.Write(Shared.Swap(headerId));
writer.Write(fileSize);
writer.Write(Shared.Swap(format));
}
public void Read(BinaryReader reader)
{
fileSize = (int)Shared.Swap(reader.ReadUInt32()) == (int)headerId ? reader.ReadUInt32() : throw new Exception("Not a valid RIFF Wave file!");
if ((int)Shared.Swap(reader.ReadUInt32()) != (int)format)
{
throw new Exception("Not a valid RIFF Wave file!");
}
}
}
}

View file

@ -1,102 +0,0 @@
/* This file is part of libWiiSharp
* Copyright (C) 2009 Leathl
* Copyright (C) 2020 - 2021 Github Contributors
*
* libWiiSharp is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libWiiSharp is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.IO;
namespace libWiiSharp
{
internal class WaveSmplChunk
{
private readonly uint smplId = 1936552044;
private uint smplSize = 36;
private uint manufacturer;
private uint product;
private uint samplePeriod;
private uint unityNote = 60;
private uint pitchFraction;
private uint smpteFormat;
private uint smpteOffset;
private uint numLoops;
private uint samplerData;
private readonly List<WaveSmplLoop> smplLoops = new List<WaveSmplLoop>();
public uint SmplSize => smplSize;
public uint NumLoops => numLoops;
public WaveSmplLoop[] Loops => smplLoops.ToArray();
public void AddLoop(int loopStartSample, int loopEndSample)
{
RemoveAllLoops();
++numLoops;
smplLoops.Add(new WaveSmplLoop()
{
LoopStart = (uint)loopStartSample,
LoopEnd = (uint)loopEndSample
});
}
public void RemoveAllLoops()
{
smplLoops.Clear();
numLoops = 0U;
}
public void Write(BinaryWriter writer)
{
writer.Write(Shared.Swap(smplId));
writer.Write(smplSize);
writer.Write(manufacturer);
writer.Write(product);
writer.Write(samplePeriod);
writer.Write(unityNote);
writer.Write(pitchFraction);
writer.Write(smpteFormat);
writer.Write(smpteOffset);
writer.Write(numLoops);
writer.Write(samplerData);
for (int index = 0; index < numLoops; ++index)
{
smplLoops[index].Write(writer);
}
}
public void Read(BinaryReader reader)
{
smplSize = (int)Shared.Swap(reader.ReadUInt32()) == (int)smplId ? reader.ReadUInt32() : throw new Exception("Wrong chunk ID!");
manufacturer = reader.ReadUInt32();
product = reader.ReadUInt32();
samplePeriod = reader.ReadUInt32();
unityNote = reader.ReadUInt32();
pitchFraction = reader.ReadUInt32();
smpteFormat = reader.ReadUInt32();
smpteOffset = reader.ReadUInt32();
numLoops = reader.ReadUInt32();
samplerData = reader.ReadUInt32();
for (int index = 0; index < numLoops; ++index)
{
WaveSmplLoop waveSmplLoop = new WaveSmplLoop();
waveSmplLoop.Read(reader);
smplLoops.Add(waveSmplLoop);
}
}
}
}

View file

@ -1,64 +0,0 @@
/* This file is part of libWiiSharp
* Copyright (C) 2009 Leathl
* Copyright (C) 2020 - 2021 Github Contributors
*
* libWiiSharp is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libWiiSharp is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System.IO;
namespace libWiiSharp
{
internal class WaveSmplLoop
{
private uint cuePointId;
private uint type;
private uint start;
private uint end;
private uint fraction;
private uint playCount;
public uint LoopStart
{
get => start;
set => start = value;
}
public uint LoopEnd
{
get => end;
set => end = value;
}
public void Write(BinaryWriter writer)
{
writer.Write(cuePointId);
writer.Write(type);
writer.Write(start);
writer.Write(end);
writer.Write(fraction);
writer.Write(playCount);
}
public void Read(BinaryReader reader)
{
cuePointId = reader.ReadUInt32();
type = reader.ReadUInt32();
start = reader.ReadUInt32();
end = reader.ReadUInt32();
fraction = reader.ReadUInt32();
playCount = reader.ReadUInt32();
}
}
}

View file

@ -47,13 +47,9 @@
</ItemGroup>
<ItemGroup>
<Compile Include="BNS.cs" />
<Compile Include="BNS_Data.cs" />
<Compile Include="BNS_Header.cs" />
<Compile Include="BNS_Info.cs" />
<Compile Include="Brlan.cs" />
<Compile Include="Brlyt.cs" />
<Compile Include="CertificateChain.cs" />
<Compile Include="ColorIndexConverter.cs" />
<Compile Include="CommonKey.cs" />
<Compile Include="ContentIndices.cs" />
<Compile Include="HbcTransmitter.cs" />
@ -63,30 +59,14 @@
<Compile Include="Lz77.cs" />
<Compile Include="MessageEventArgs.cs" />
<Compile Include="NusClient.cs" />
<Compile Include="Region.cs" />
<Compile Include="Shared.cs" />
<Compile Include="StoreType.cs" />
<Compile Include="Ticket.cs" />
<Compile Include="TMD.cs" />
<Compile Include="TMD_Content.cs" />
<Compile Include="TPL.cs" />
<Compile Include="TPL_Header.cs" />
<Compile Include="TPL_PaletteFormat.cs" />
<Compile Include="TPL_PaletteHeader.cs" />
<Compile Include="TPL_TextureEntry.cs" />
<Compile Include="TPL_TextureFormat.cs" />
<Compile Include="TPL_TextureHeader.cs" />
<Compile Include="U8.cs" />
<Compile Include="U8_Header.cs" />
<Compile Include="U8_Node.cs" />
<Compile Include="U8_NodeType.cs" />
<Compile Include="WAD.cs" />
<Compile Include="Wave.cs" />
<Compile Include="WaveDataChunk.cs" />
<Compile Include="WaveFmtChunk.cs" />
<Compile Include="WaveHeader.cs" />
<Compile Include="WaveSmplChunk.cs" />
<Compile Include="WaveSmplLoop.cs" />
<Compile Include="AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>