Skip to content

Commit 8832d1e

Browse files
committed
Implemented IMA-ADPCM encoding (NDS)
1 parent ca6e226 commit 8832d1e

File tree

3 files changed

+81
-5
lines changed

3 files changed

+81
-5
lines changed

NDS/NDS.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
</Reference>
5353
</ItemGroup>
5454
<ItemGroup>
55+
<Compile Include="NDSPlugin.cs" />
5556
<Compile Include="NDSProject.cs" />
5657
<Compile Include="NitroSystem\SND\SDAT.cs" />
5758
<Compile Include="Nitro\ARM9.cs" />

NDS/NDSPlugin.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using LibEveryFileExplorer;
6+
using LibEveryFileExplorer.Script;
7+
using System.IO;
8+
using CommonFiles;
9+
using LibEveryFileExplorer.IO;
10+
11+
namespace NDS
12+
{
13+
public class NDSPlugin : EFEPlugin
14+
{
15+
public override void OnLoad()
16+
{
17+
18+
}
19+
}
20+
}

NDS/SND/ADPCM.cs

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,69 @@ public Int16[] GetWaveData(byte[] Data, int Offset, int Length)
7070
return DataOut.ToArray();
7171
}
7272

73-
/*public static byte[] Encode(Int16[] WaveData)
73+
public static byte[] Encode(Int16[] WaveData)
7474
{
75-
Int16[] Diff = new short[WaveData.Length];
75+
int Last = WaveData[0];
76+
int Index = GetBestTableIndex((WaveData[1] - WaveData[0]) * 8);
77+
int HeaderIndex = Index;
78+
byte[] Nibbles = new byte[WaveData.Length - 1];//nibbles, lets merge it afterwards
7679
for (int i = 1; i < WaveData.Length; i++)
7780
{
78-
Diff[i] = (short)(WaveData[i] - WaveData[i - 1]);
81+
int val = GetBestConfig(Index, WaveData[i] - Last);
82+
Nibbles[i - 1] = (byte)val;
83+
84+
int diff =
85+
StepTable[Index] / 8 +
86+
StepTable[Index] / 4 * ((val >> 0) & 1) +
87+
StepTable[Index] / 2 * ((val >> 1) & 1) +
88+
StepTable[Index] * ((val >> 2) & 1);
89+
90+
int samp = Last + diff * ((((val >> 3) & 1) == 1) ? -1 : 1);
91+
Last = (short)MathUtil.Clamp(samp, short.MinValue, short.MaxValue);
92+
Index = (short)MathUtil.Clamp(Index + IndexTable[val & 7], 0, 88);
93+
}
94+
byte[] Result = new byte[WaveData.Length / 2 + 4];
95+
IOUtil.WriteS16LE(Result, 0, WaveData[0]);
96+
IOUtil.WriteS16LE(Result, 2, (short)HeaderIndex);
97+
for (int i = 0; i < Nibbles.Length; i += 2)
98+
{
99+
Result[i / 2 + 4] = (byte)(Nibbles[i] | (Nibbles[i + 1] << 4));
79100
}
80-
return null;
81-
}*/
101+
return Result;
102+
}
103+
104+
private static int GetBestTableIndex(int Diff)
105+
{
106+
int LowestDiff = int.MaxValue;
107+
int LowestIdx = -1;
108+
for (int i = 0; i < StepTable.Length; i++)
109+
{
110+
int diff2 = Math.Abs(Math.Abs(Diff) - StepTable[i]);
111+
if (diff2 < LowestDiff)
112+
{
113+
LowestDiff = diff2;
114+
LowestIdx = i;
115+
}
116+
}
117+
return LowestIdx;
118+
}
119+
120+
private static int GetBestConfig(int Index, int Diff)
121+
{
122+
int Result = 0;
123+
if (Diff < 0) Result |= 1 << 3;
124+
Diff = Math.Abs(Diff);
125+
int DiffNew = StepTable[Index] / 8;
126+
if (Math.Abs(DiffNew - Diff) < StepTable[Index] / 4) return Result;
127+
Result |= 1;
128+
DiffNew += StepTable[Index] / 4;
129+
if (Math.Abs(DiffNew - Diff) < StepTable[Index] / 2) return Result;
130+
Result |= 1 << 1;
131+
DiffNew += StepTable[Index] / 2;
132+
if (Math.Abs(DiffNew - Diff) < StepTable[Index]) return Result;
133+
Result |= 1 << 2;
134+
DiffNew += StepTable[Index];
135+
return Result;
136+
}
82137
}
83138
}

0 commit comments

Comments
 (0)