From 0ed3491e3fccc8914bc919bfc36c825088422a8f Mon Sep 17 00:00:00 2001
From: Persephone Bubblegum-Holiday <kawaiizenbo@outlook.com>
Date: Mon, 26 May 2025 08:59:59 -0700
Subject: [PATCH] add option to compress audio to mp3 when converting rshw

---
 PC2Converter/PC2Converter/MainForm.cs | 52 +++++++++++++++++++++------
 1 file changed, 41 insertions(+), 11 deletions(-)

diff --git a/PC2Converter/PC2Converter/MainForm.cs b/PC2Converter/PC2Converter/MainForm.cs
index 171d3f3..296aa21 100644
--- a/PC2Converter/PC2Converter/MainForm.cs
+++ b/PC2Converter/PC2Converter/MainForm.cs
@@ -1,5 +1,6 @@
 using System;
 using System.Collections;
+using System.Diagnostics;
 using System.IO;
 
 using Eto.Forms;
@@ -15,12 +16,16 @@ namespace PC2Converter
 		TextBox FilePathTextBox = new TextBox();
 		Button BrowseFileButton = new Button { Text = "Browse", Width = 100 };
 		Button ConvertButton = new Button { Text = "Convert", Width = 100 };
+		CheckBox CompressAudioCheckBox = new CheckBox { Text = "Compress Audio", Width = 100 };
 
-		string[] InputTypes = { "RSHW File (RR Engine)", "CSHW File (RR Engine)", "SSHW File (RR Engine)" };
+		string[] InputTypes = {
+			"RSHW File (RR Engine)", "CSHW File (RR Engine)", "SSHW File (RR Engine)"/*,
+			"3ST File (APS)", "ROA/R12/C&R File (APS)"*/
+		};
 
-		string[] RSHWOutputTypes = { "UST 32 Bits (Single Bot RAE/3st)", "UST 8 Bits (Single Bot Cybers Rosetta)", "UST 256 Bits (Full RAE/3st)" , "UST 256 Bits (Full Cybers Rosetta)" };
-		string[] CSHWOutputTypes = { "UST 8 Bits (Single Bot Cybers)", "UST 256 Bits (Full Cybers)" };
-		string[] SSHWOutputTypes = { "UST 32 Bits (Single Bot Studio C)", "UST 256 Bits (Full Studio C)" };
+		string[] RAE3stOutputTypes = { "UST 32 Bits (Single Bot RAE/3st)", "UST 8 Bits (Single Bot Cybers Rosetta)", "UST 256 Bits (Full RAE/3st)" , "UST 256 Bits (Full Cybers Rosetta)" };
+		string[] CyberOutputTypes = { "UST 8 Bits (Single Bot Cybers)", "UST 256 Bits (Full Cybers)" };
+		string[] StudioCOutputTypes = { "UST 32 Bits (Single Bot Studio C)", "UST 256 Bits (Full Studio C)" };
 
 		string[] RAE3stCharacters = { "Rolfe/Chuck E.", "Mitzi/Helen", "Fatz/Munch", "Beach Bear/Jasper", "Dook/Pasqually", "Billy Bob", "Looney Bird/PizzaCam"};
 		string[] CyberCharacters = { "Chuck E.", "Helen/Guest Star", "Munch", "Jasper", "Pasqually" };
@@ -44,8 +49,6 @@ namespace PC2Converter
 			/* pasqually */ new int[]{ 30, 25, 21, 28, 29, 26, 31, 35 }
 		};
 
-		int[] CyberRosettaInvertedBits = { 2, 8, 181, 169, 41, 59, 58, 151, 158, 26, 35, 31 };
-
 		int[][] SC_RAE3stBitMap =
 		{
 			/*    rolfe/chuck    */ new int[]{ 1, 9, 10, 2, 3, 4, 5, 6, 7, 8, 11, 12, 13, 17, 18, 19, 14, 15, 16, 20, 36, 37 },
@@ -62,6 +65,9 @@ namespace PC2Converter
 			/* chuck e. */ new int[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32 }
 		};
 
+		int[] CyberRosettaInvertedBits = { 2, 8, 181, 169, 41, 59, 58, 151, 158, 26, 35, 31 };
+		int[] RAE3stAPSDescrambleTable = { 10, 11, 0, 1, 2, 3, 4, 12, 13, 5, 6, 7, 8, 9, 14, 15};
+
 		bool FullStage = false;
 		const int USTVersion = 1;
 
@@ -130,6 +136,7 @@ namespace PC2Converter
 			layout.BeginVertical();
 			layout.BeginHorizontal();
 			layout.Add(null, true);
+			layout.Add(CompressAudioCheckBox);
 			layout.Add(ConvertButton);
 			layout.EndHorizontal();
 			layout.EndVertical();
@@ -184,7 +191,7 @@ namespace PC2Converter
 					fullStage = true;
 					break;
 			}
-			if (fullStage) headerOut += "N/A,";
+			if (fullStage) headerOut += "All,";
 			else headerOut += character + ";";
 			return headerOut;
 		}
@@ -196,7 +203,7 @@ namespace PC2Converter
 				case 0:
 					OutputTypeSelector.Items.Clear();
 					CharacterSelector.Items.Clear();
-					foreach (string s in RSHWOutputTypes)
+					foreach (string s in RAE3stOutputTypes)
 					{
 						OutputTypeSelector.Items.Add(s);
 					}
@@ -204,7 +211,7 @@ namespace PC2Converter
 				case 1:
 					OutputTypeSelector.Items.Clear();
 					CharacterSelector.Items.Clear();
-					foreach (string s in CSHWOutputTypes)
+					foreach (string s in CyberOutputTypes)
 					{
 						OutputTypeSelector.Items.Add(s);
 					}
@@ -212,7 +219,7 @@ namespace PC2Converter
 				case 2:
 					OutputTypeSelector.Items.Clear();
 					CharacterSelector.Items.Clear();
-					foreach (string s in SSHWOutputTypes)
+					foreach (string s in StudioCOutputTypes)
 					{
 						OutputTypeSelector.Items.Add(s);
 					}
@@ -440,9 +447,32 @@ namespace PC2Converter
 					}
 					writeOut.Add(frameByte.ToString($"X{(bitsCount/4).ToString()}"));
 				}
-				File.WriteAllText(outFilePath, MakeHeader(Path.GetFileNameWithoutExtension(outFilePath), outputType, CharacterSelector.SelectedKey)+String.Join(",", writeOut.ToArray())+";"+Convert.ToBase64String(file.audioData));
+
+				byte[] audioOut = file.audioData;
+				if ((bool)CompressAudioCheckBox.Checked)
+				{
+					if (File.Exists("tmp.mp3")) File.Delete("tmp.mp3");
+					File.WriteAllBytes("tmp.wav", audioOut);
+					Process ffmpegProcess = new Process();
+					ffmpegProcess.StartInfo.FileName = "ffmpeg";
+					ffmpegProcess.StartInfo.Arguments = "-i tmp.wav -c:a mp3 -b:a 128 tmp.mp3";
+					ffmpegProcess.Start();
+					ffmpegProcess.WaitForExit();
+					audioOut = File.ReadAllBytes("tmp.mp3");
+					File.Delete("tmp.wav");
+					File.Delete("tmp.mp3");
+				}
+
+				File.WriteAllText(outFilePath, MakeHeader(Path.GetFileNameWithoutExtension(outFilePath), outputType, CharacterSelector.SelectedKey)+String.Join(",", writeOut.ToArray())+";"+Convert.ToBase64String(audioOut));
 			}
 			MessageBox.Show("Conversion complete!");
 		}
+
+		int RRtoAPSBit(int rrBit) // aps lower drawer starts at 129 instead of 151
+		{
+			int outAPSBit = rrBit;
+			if (rrBit > 150) outAPSBit = rrBit - 22;
+			return outAPSBit;
+		}
 	}
 }