Skip to content

Commit

Permalink
Some optimizations, also improved the tool help info.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmarti856 committed Mar 17, 2022
1 parent 699bd94 commit 9f38f89
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 81 deletions.
2 changes: 1 addition & 1 deletion PS2VagTool/PS2VagTool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<ProjectGuid>{61A9CCAB-37F2-419C-96DA-30F61568B8B8}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>PS2VagTool</RootNamespace>
<AssemblyName>PS2VagTool</AssemblyName>
<AssemblyName>AIFF2VAG</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
Expand Down
55 changes: 26 additions & 29 deletions PS2VagTool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,22 @@ private static void Main(string[] args)
if (args.Length > 0)
{
//Show help if required
if (args[0].ToLower().Contains("help") || args[0].Contains("?"))
if (args.Length == 1)
{
Console.WriteLine("PlayStation 2 Vag Tool. By jmarti856");
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: <InputFile> <OutputFile>");
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: <InputFile> <OutputFile>");
}
}
else
{
Expand All @@ -42,11 +47,7 @@ private static void Main(string[] args)
inputFile = args[1];
if (ProgramFunctions.CheckFileExists(inputFile))
{
string outputFile = args[2];
if (ProgramFunctions.CheckDirectoryExists(outputFile))
{
ProgramFunctions.ExecuteDecoder(inputFile, outputFile);
}
ProgramFunctions.ExecuteDecoder(inputFile, args[2].Trim());
}
}
}
Expand All @@ -55,24 +56,20 @@ private static void Main(string[] args)
//Execute encoder
if (ProgramFunctions.CheckFileExists(inputFile))
{
string outputFile = args[1];
if (ProgramFunctions.CheckDirectoryExists(outputFile))
//Check if we have some options setted
if (args.Length > 2)
{
//Check if we have some options setted
if (args.Length > 2)
string options = args[2];
if (char.Parse(options.TrimStart('-')) == '1')
{
forceNoLooping = true;
}
else if (char.Parse(options.TrimStart('-')) == 'L')
{
string options = args[2];
if (Convert.ToChar(options.TrimStart('-')) == '1')
{
forceNoLooping = true;
}
else if (Convert.ToChar(options.TrimStart('-')) == 'L')
{
forceLooping = true;
}
forceLooping = true;
}
ProgramFunctions.ExecuteEncoder(inputFile, outputFile, forceNoLooping, forceLooping);
}
ProgramFunctions.ExecuteEncoder(inputFile, args[1].Trim(), forceNoLooping, forceLooping);
}
}
}
Expand Down
45 changes: 16 additions & 29 deletions PS2VagTool/ProgramFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ internal static void ExecuteEncoder(string inputFile, string outputFile, bool fo
int frequency, channels;
using (AiffFileReader reader = new AiffFileReader(inputFile))
{
//Get basic info
frequency = reader.WaveFormat.SampleRate;
channels = reader.WaveFormat.Channels;

//Get pcm short array
byte[] pcmByteData = new byte[reader.Length];
reader.Read(pcmByteData, 0, pcmByteData.Length);
pcmData = WavFunctions.ConvertByteArrayToShortArray(pcmByteData);
frequency = reader.WaveFormat.SampleRate;
channels = reader.WaveFormat.Channels;
}

//Get markers
Expand Down Expand Up @@ -66,15 +68,17 @@ internal static void ExecuteEncoder(string inputFile, string outputFile, bool fo
//Get File Data
using (WaveFileReader reader = new WaveFileReader(inputFile))
{
//Get basic info
frequency = reader.WaveFormat.SampleRate;
channels = reader.WaveFormat.Channels;

//Get Loop Data
loopData = WavFunctions.ReadSampleChunck(reader);

//Get pcm short array
byte[] pcmByteData = new byte[reader.Length];
reader.Read(pcmByteData, 0, pcmByteData.Length);
pcmData = WavFunctions.ConvertByteArrayToShortArray(pcmByteData);
frequency = reader.WaveFormat.SampleRate;
channels = reader.WaveFormat.Channels;
}

//Check loop Data
Expand Down Expand Up @@ -113,7 +117,14 @@ internal static void ExecuteDecoder(string inputFile, string outputFile)

//Save file
IWaveProvider provider = new RawSourceWaveStream(new MemoryStream(pcmData), new WaveFormat(sampleRate, 16, 1));
WaveFileWriter.CreateWaveFile(outputFile, provider);
try
{
WaveFileWriter.CreateWaveFile(outputFile, provider);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}

Expand All @@ -134,30 +145,6 @@ internal static bool CheckFileExists(string filePath)
return fileExists;
}

//-------------------------------------------------------------------------------------------------------------------------------
internal static bool CheckDirectoryExists(string directoryPathToCheck)
{
bool directoryExists = false;

if (Directory.Exists(Path.GetDirectoryName(directoryPathToCheck)))
{
directoryExists = true;
}
else
{
Console.WriteLine("ERROR: output directory not found: " + directoryPathToCheck);
}

return directoryExists;
}

//-------------------------------------------------------------------------------------------------------------------------------
internal static string Truncate(this string value, int maxLength)
{
if (string.IsNullOrEmpty(value)) return value;
return value.Length <= maxLength ? value : value.Substring(0, maxLength);
}

//-------------------------------------------------------------------------------------------------------------------------------
internal static int FlipInt32(int valueToFlip)
{
Expand Down
56 changes: 34 additions & 22 deletions PS2VagTool/Vag Functions/SonyVagWritter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System;
using System.IO;
using System.Text;

namespace PS2VagTool.Vag_Functions
Expand All @@ -11,28 +12,39 @@ public static partial class SonyVag
//-------------------------------------------------------------------------------------------------------------------------------
internal static void WriteVagFile(byte[] vagData, string outputFilePath, int numOfChannels, int samplingFrequency)
{
using (BinaryWriter BinWriter = new BinaryWriter(File.Open(outputFilePath, FileMode.Create, FileAccess.Write, FileShare.Read), Encoding.ASCII))
try
{
//Magic
BinWriter.Write(Encoding.ASCII.GetBytes("VAGp"));
//Version
BinWriter.Write(ProgramFunctions.FlipInt32(32));
//Reserved area
BinWriter.Write(0);
//Waveform data size (bytes)
BinWriter.Write(ProgramFunctions.FlipInt32(vagData.Length + 16));
//Sampling Frequency (Hz)
BinWriter.Write(ProgramFunctions.FlipInt32(samplingFrequency));
//Reserved area
BinWriter.Write(new byte[10]);
//Number of channels
BinWriter.Write((byte)((numOfChannels > 1) ? 2 : 0));
//Reserved area
BinWriter.Write((byte)0);
//BinWriter.Write(0);
BinWriter.Write(Encoding.ASCII.GetBytes(outputFilePath.Truncate(16)));
BinWriter.Write(new byte[16]);
BinWriter.Write(vagData);
using (BinaryWriter BinWriter = new BinaryWriter(File.Open(outputFilePath, FileMode.Create, FileAccess.Write, FileShare.Read), Encoding.ASCII))
{
//Magic
BinWriter.Write(Encoding.ASCII.GetBytes("VAGp"));
//Version
BinWriter.Write(ProgramFunctions.FlipInt32(32));
//Reserved area
BinWriter.Write(0);
//Waveform data size (bytes)
BinWriter.Write(ProgramFunctions.FlipInt32(vagData.Length + 16));
//Sampling Frequency (Hz)
BinWriter.Write(ProgramFunctions.FlipInt32(samplingFrequency));
//Reserved area
BinWriter.Write(new byte[10]);
//Number of channels
BinWriter.Write((byte)((numOfChannels > 1) ? 2 : 0));
//Reserved area
BinWriter.Write((byte)0);
//Name - fixed to 16 bytes
byte[] reservedData = new byte[16];
byte[] stringBytesData = Encoding.ASCII.GetBytes(Path.GetFullPath(outputFilePath));
Array.Copy(stringBytesData, reservedData, Math.Min(16, stringBytesData.Length));
BinWriter.Write(reservedData);
//Empty line
BinWriter.Write(new byte[16]);
BinWriter.Write(vagData);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
Expand Down

0 comments on commit 9f38f89

Please sign in to comment.