Skip to content

Commit

Permalink
Optimizations, the encoder, now trims the samples after the loop end …
Browse files Browse the repository at this point in the history
…point.
  • Loading branch information
jmarti856 committed Mar 20, 2022
1 parent 08be630 commit d35db41
Show file tree
Hide file tree
Showing 3 changed files with 199 additions and 196 deletions.
84 changes: 41 additions & 43 deletions PS2VagTool/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;

namespace PS2VagTool
{
Expand All @@ -14,65 +15,62 @@ private static void Main(string[] args)
if (args.Length > 0)
{
//Show help if required
if (args.Length == 1)
if (args[0].Equals("help", StringComparison.OrdinalIgnoreCase) || args[0].Equals("?"))
{
if (args[0].Equals("help", StringComparison.OrdinalIgnoreCase) || args[0].Equals("?"))
{
Console.WriteLine("PlayStation 2 Vag Tool. By jmarti856");
Console.WriteLine("Info: Supports WAV and AIFF files, also reads the loop points inside the file format.");
Console.WriteLine("");
Console.WriteLine("------------------------------------For Encoding------------------------------------");
Console.WriteLine("Usage: <InputFile> <OutputFile>");
Console.WriteLine("Optioms:");
Console.WriteLine("-1 : force non-looping");
Console.WriteLine("-L : force looping");
Console.WriteLine("");
Console.WriteLine("------------------------------------For Decoding------------------------------------");
Console.WriteLine("Usage: Decode <InputFile> <OutputFile>");
}
Console.WriteLine("PlayStation 2 Vag Tool. By jmarti856");
Console.WriteLine("Info: Supports WAV and AIFF files, also reads the loop points inside the file format.");
Console.WriteLine("");
Console.WriteLine("------------------------------------For Encoding------------------------------------");
Console.WriteLine("Usage: <InputFile>");
Console.WriteLine("Optioms:");
Console.WriteLine("-1 : force non-looping");
Console.WriteLine("-L : force looping");
Console.WriteLine("");
Console.WriteLine("------------------------------------For Decoding------------------------------------");
Console.WriteLine("Usage: Decode <InputFile> <OutputFile>");

}
else
{
//Get Parameters
bool forceLooping = false;
bool forceNoLooping = false;
if (args.Length > 1)
string inputFile = args[0];
//Check if we have to execute the decoder
if (inputFile.Equals("Decode", StringComparison.OrdinalIgnoreCase))
{
string inputFile = args[0];
//Check if we have to execute the decoder
if (inputFile.Equals("Decode", StringComparison.OrdinalIgnoreCase))
if (args.Length > 2)
{
if (args.Length > 2)
inputFile = args[1];
if (ProgramFunctions.CheckFileExists(inputFile))
{
inputFile = args[1];
if (ProgramFunctions.CheckFileExists(inputFile))
{
ProgramFunctions.ExecuteDecoder(inputFile, args[2].Trim());
}
ProgramFunctions.ExecuteDecoder(inputFile, args[2].Trim());
}
}
else
}
else
{
//Execute encoder
if (ProgramFunctions.CheckFileExists(inputFile))
{
//Execute encoder
if (ProgramFunctions.CheckFileExists(inputFile))
//Get Parameters
bool forceLooping = false;
bool forceNoLooping = false;

//Check if we have some options setted
if (args.Length > 1)
{
//Check if we have some options setted
if (args.Length > 2)
string options = args[1];
if (char.Parse(options.TrimStart('-')) == '1')
{
forceNoLooping = true;
}
else if (char.Parse(options.TrimStart('-')) == 'L')
{
string options = args[2];
if (char.Parse(options.TrimStart('-')) == '1')
{
forceNoLooping = true;
}
else if (char.Parse(options.TrimStart('-')) == 'L')
{
forceLooping = true;
}
forceLooping = true;
}
ProgramFunctions.ExecuteEncoder(inputFile, args[1].Trim(), forceNoLooping, forceLooping);
}
ProgramFunctions.ExecuteEncoder(inputFile, Path.ChangeExtension(inputFile, ".vag"), forceNoLooping, forceLooping);
}
}

}
}
}
Expand Down
67 changes: 29 additions & 38 deletions PS2VagTool/ProgramFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,26 @@ internal static class ProgramFunctions
//-------------------------------------------------------------------------------------------------------------------------------
internal static void ExecuteEncoder(string inputFile, string outputFile, bool forceNoLooping, bool forceLooping)
{
//Variables to store audio info
short[] pcmData = null;
int frequency = 0, channels = 0;
uint loopStartValue = 0, loopEndValue = 0;
bool isLooped = false;

//Inspect input file and get data
string fileExtension = Path.GetExtension(inputFile);
if (fileExtension.Equals(".aif", StringComparison.OrdinalIgnoreCase) || fileExtension.Equals(".aiff", StringComparison.OrdinalIgnoreCase))
{
//Get markers
List<MarkerChunkData> markers = new List<MarkerChunkData>();
AiffFileChunksReader.ReadAiffHeader(File.OpenRead(inputFile), markers);
if (markers.Count > 1)
{
loopStartValue = SonyVag.GetLoopOffsetForVag(markers[0].position) - 1;
loopEndValue = SonyVag.GetLoopOffsetForVag(markers[1].position) - 2;
}

//Read file data
short[] pcmData;
byte[] vagData;
int frequency, channels;
using (AiffFileReader reader = new AiffFileReader(inputFile))
{
//Get basic info
Expand All @@ -33,39 +46,11 @@ internal static void ExecuteEncoder(string inputFile, string outputFile, bool fo
pcmData = WavFunctions.ConvertByteArrayToShortArray(pcmByteData);
}

//Get markers
List<MarkerChunkData> markers = new List<MarkerChunkData>();
AiffFileChunksReader.ReadAiffHeader(File.OpenRead(inputFile), markers);
uint loopOffsetValue = 0;
if (markers.Count > 0)
{
loopOffsetValue = SonyVag.GetLoopOffsetForVag(markers[0].position) - 1;
}

//Start Encode!!
if (forceNoLooping)
{
vagData = SonyVag.Encode(pcmData, 0, false);
}
else if (forceLooping)
{
vagData = SonyVag.Encode(pcmData, 0, true);
}
else
{
vagData = SonyVag.Encode(pcmData, loopOffsetValue, markers.Count > 0);
}

//Write File
SonyVag.WriteVagFile(vagData, outputFile, channels, frequency);
isLooped = markers.Count > 0;
}
else if (fileExtension.Equals(".wav", StringComparison.OrdinalIgnoreCase))
{
short[] pcmData;
byte[] vagData;
int[] loopData;
int frequency, channels;
//Get File Data
using (WaveFileReader reader = new WaveFileReader(inputFile))
{
//Get basic info
Expand All @@ -82,24 +67,30 @@ internal static void ExecuteEncoder(string inputFile, string outputFile, bool fo
}

//Check loop Data
uint loopOffsetValue = 0;
if (loopData.Length > 0)
{
loopOffsetValue = SonyVag.GetLoopOffsetForVag((uint)loopData[1]);
loopStartValue = SonyVag.GetLoopOffsetForVag((uint)loopData[1]) -1;
loopEndValue = SonyVag.GetLoopOffsetForVag((uint)loopData[2])-2;
}

//Start Encode!!
isLooped = loopData[0] == 1;
}

//Start Encode!!
if (pcmData != null)
{
byte[] vagData;
if (forceNoLooping)
{
vagData = SonyVag.Encode(pcmData, 0, false);
vagData = SonyVag.Encode(pcmData, 0, loopEndValue, false);
}
else if (forceLooping)
{
vagData = SonyVag.Encode(pcmData, 0, true);
vagData = SonyVag.Encode(pcmData, 0, loopEndValue, true);
}
else
{
vagData = SonyVag.Encode(pcmData, loopOffsetValue, loopData[0] == 1);
vagData = SonyVag.Encode(pcmData, loopStartValue, loopEndValue, isLooped);
}

//Write File
Expand Down
Loading

0 comments on commit d35db41

Please sign in to comment.