Skip to content

Commit

Permalink
LostArtefacts#304 Cross-game Preparations
Browse files Browse the repository at this point in the history
Adds SFX export support for TR1.
  • Loading branch information
lahm86 committed May 9, 2022
1 parent 73356bb commit aa0bc91
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
48 changes: 47 additions & 1 deletion SFXExport/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.IO;
using TRLevelReader;
using TRLevelReader.Model;

namespace SFXExport
{
Expand All @@ -21,6 +23,22 @@ static void Main(string[] args)
}
Directory.CreateDirectory(exportDir);

switch (Path.GetExtension(file).ToUpper())
{
case ".SFX":
ExtractFromSFX(file, exportDir);
break;
case ".PHD":
ExtractFromPHD(file, exportDir);
break;
default:
Console.WriteLine("Unsupported file.");
break;
}
}

private static void ExtractFromSFX(string file, string exportDir)
{
int sample = 0;
using (BinaryReader reader = new BinaryReader(File.Open(file, FileMode.Open)))
{
Expand Down Expand Up @@ -49,10 +67,32 @@ static void Main(string[] args)
}
}

private static void ExtractFromPHD(string file, string exportDir)
{
TRLevel level = new TR1LevelReader().ReadLevel(file);
for (int i = 0; i < level.NumSampleIndices; i++)
{
uint sampleStart = level.SampleIndices[i];
uint sampleEnd = i < level.NumSampleIndices - 1 ? level.SampleIndices[i + 1] : (uint)level.Samples.Length;
if (sampleEnd > level.Samples.Length)
{
sampleEnd = (uint)level.Samples.Length;
}

using (BinaryWriter writer = new BinaryWriter(File.Create(Path.Combine(exportDir, i + ".wav"))))
{
for (uint j = sampleStart; j < sampleEnd; j++)
{
writer.Write(level.Samples[j]);
}
}
}
}

private static void Usage()
{
Console.WriteLine();
Console.WriteLine("Usage: SFXExport [*.SFX] [EXPORT_DIR]");
Console.WriteLine("Usage: SFXExport [*.SFX|*.PHD] [EXPORT_DIR]");
Console.WriteLine();

Console.WriteLine("Example");
Expand All @@ -61,6 +101,12 @@ private static void Usage()
Console.ResetColor();
Console.WriteLine("\t\tExtract each sound effect from the SFX file and create a sample file for each in the TR2 directory.");
Console.WriteLine();

Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("\tSFXExport LEVEL1.PHD TR1");
Console.ResetColor();
Console.WriteLine("\t\tExtract each sound effect from Caves and create a sample file for each in the TR1 directory.");
Console.WriteLine();
}
}
}
3 changes: 3 additions & 0 deletions SFXExport/SFXExport.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@
<Copyright>Copyright © Tombrunners 2021</Copyright>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\TRLevelReader\TRLevelReader.csproj" />
</ItemGroup>
</Project>

0 comments on commit aa0bc91

Please sign in to comment.