diff --git a/PS2VagTool/PS2VagTool.csproj b/PS2VagTool/PS2VagTool.csproj index 8b09055..4c3f665 100644 --- a/PS2VagTool/PS2VagTool.csproj +++ b/PS2VagTool/PS2VagTool.csproj @@ -8,7 +8,7 @@ {61A9CCAB-37F2-419C-96DA-30F61568B8B8} Exe PS2VagTool - PS2VagTool + AIFF2VAG v4.0 512 true diff --git a/PS2VagTool/Program.cs b/PS2VagTool/Program.cs index ffe9d8d..70517f2 100644 --- a/PS2VagTool/Program.cs +++ b/PS2VagTool/Program.cs @@ -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: "); - Console.WriteLine("Optioms:"); - Console.WriteLine("-1 : force non-looping"); - Console.WriteLine("-L : force looping"); - Console.WriteLine(""); - Console.WriteLine("------------------------------------For Decoding------------------------------------"); - Console.WriteLine("Usage: "); + 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: "); + Console.WriteLine("Optioms:"); + Console.WriteLine("-1 : force non-looping"); + Console.WriteLine("-L : force looping"); + Console.WriteLine(""); + Console.WriteLine("------------------------------------For Decoding------------------------------------"); + Console.WriteLine("Usage: "); + } } else { @@ -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()); } } } @@ -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); } } } diff --git a/PS2VagTool/ProgramFunctions.cs b/PS2VagTool/ProgramFunctions.cs index 00028bd..764dc2a 100644 --- a/PS2VagTool/ProgramFunctions.cs +++ b/PS2VagTool/ProgramFunctions.cs @@ -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 @@ -66,6 +68,10 @@ 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); @@ -73,8 +79,6 @@ internal static void ExecuteEncoder(string inputFile, string outputFile, bool fo 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 @@ -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); + } } } @@ -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) { diff --git a/PS2VagTool/Vag Functions/SonyVagWritter.cs b/PS2VagTool/Vag Functions/SonyVagWritter.cs index 0ed8998..06c3c66 100644 --- a/PS2VagTool/Vag Functions/SonyVagWritter.cs +++ b/PS2VagTool/Vag Functions/SonyVagWritter.cs @@ -1,4 +1,5 @@ -using System.IO; +using System; +using System.IO; using System.Text; namespace PS2VagTool.Vag_Functions @@ -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); } } }