Skip to content

Commit

Permalink
SpeechEnhancer -> SPXConverter
Browse files Browse the repository at this point in the history
  • Loading branch information
LazyDuchess committed Jun 30, 2024
1 parent 98f1dbe commit 8c80e79
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,21 @@

namespace OpenTS2
{
public class SpeechEnhancer
public class SPXConverter
{
[MenuItem("OpenTS2/Other/Improve Speech Using Tasks")]
private static void EnhanceSpeechTasks()
[MenuItem("OpenTS2/Experiments/Convert all SPX to WAV")]
private static void ConvertSPX()
{
var baseGameOnly = false;
var compressed = false;
if (!EditorUtility.DisplayDialog("SPX to WAV", "This operation will convert ALL SPX resources to WAV. This will take a while and use a lot of resources. Proceed?", "Yes", "No"))
return;
baseGameOnly = EditorUtility.DisplayDialog("SPX to WAV", "Which products do you want to convert?", "Base-Game only", "All products");
compressed = EditorUtility.DisplayDialog("SPX to WAV", "Do you want to compress the resulting packages?", "Yes", "No");
Core.CoreInitialized = false;
Core.InitializeCore();
EPManager.Instance.InstalledProducts = (int)ProductFlags.BaseGame;
if (baseGameOnly)
EPManager.Instance.InstalledProducts = (int)ProductFlags.BaseGame;
var epManager = EPManager.Instance;
var products = epManager.GetInstalledProducts();
var contentManager = ContentManager.Instance;
Expand Down Expand Up @@ -72,7 +79,7 @@ private static void EnhanceSpeechTasks()
{
Debug.Log($"Starting work on {package}");
var newPackage = new DBPFFile();
newPackage.FilePath = Path.Combine("Enhanced Speech", package);
newPackage.FilePath = Path.Combine("SPX to WAV", package);
var entriesToDoForThisPackage = new List<DBPFEntry>();
foreach (var spx in speechResources)
{
Expand Down Expand Up @@ -102,7 +109,7 @@ private static void EnhanceSpeechTasks()
{
var spxFile = new SPXFile(entry.GetBytes());
if (spxFile != null && spxFile.DecompressedData != null && spxFile.DecompressedData.Length > 0)
newPackage.Changes.Set(spxFile.DecompressedData, entry.TGI, false);
newPackage.Changes.Set(spxFile.DecompressedData, entry.TGI, compressed);
}
catch (Exception e)
{
Expand All @@ -125,109 +132,7 @@ await Task.WhenAll(tasks).ContinueWith((task) =>
}
}, TaskScheduler.Default);
}
}).Start();
}


[MenuItem("OpenTS2/Other/Improve Speech")]
private static void EnhanceSpeech()
{
Core.CoreInitialized = false;
Core.InitializeCore();
EPManager.Instance.InstalledProducts = (int)ProductFlags.BaseGame;
var epManager = EPManager.Instance;
var products = epManager.GetInstalledProducts();
var contentManager = ContentManager.Instance;

foreach(var product in products)
{
var packages = Filesystem.GetPackagesInDirectory(Path.Combine(Filesystem.GetDataPathForProduct(product), "Res/Sound"));
contentManager.AddPackages(packages);
}

var audioResources = contentManager.ResourceMap.Where(x => x.Key.TypeID == TypeIDs.AUDIO);
var speechResources = new List<DBPFEntry>();
var speechPackages = new HashSet<string>();
Debug.Log($"Found {audioResources.Count()} Audio Resources.");
Debug.Log($"Looking for speech resources...");

new Thread(() =>
{
foreach (var audioResource in audioResources)
{
try
{
var audioData = audioResource.Value.GetBytes();
var magic = Encoding.UTF8.GetString(audioData, 0, 2);
if (magic == "SP")
{
speechResources.Add(audioResource.Value);
var packageFileName = Path.GetFileName(audioResource.Value.Package.FilePath);
speechPackages.Add(packageFileName);
}
}
catch (Exception) { }
}
Debug.Log($"Found {speechResources.Count} SPX audio resources, in {speechPackages.Count} packages.");
var packagesStr = "Packages:";
foreach(var package in speechPackages)
{
packagesStr += $"\n{package}";
}
Debug.Log(packagesStr);
GC.Collect();
foreach(var package in speechPackages)
{
Debug.Log($"Starting work on {package}");
var newPackage = new DBPFFile();
newPackage.FilePath = Path.Combine("Enhanced Speech", package);
var entriesToDoForThisPackage = new List<DBPFEntry>();
foreach (var spx in speechResources)
{
try
{
if (spx.Package == null) continue;
if (Path.GetFileName(spx.Package.FilePath) != package) continue;
entriesToDoForThisPackage.Add(spx);
//var asset = spx.GetAsset<WAVAudioAsset>();
//newPackage.Changes.Set(asset.GetWAVData(), asset.TGI, true);
}
catch (Exception e)
{
Debug.LogError(e);
}
}
Debug.Log($"Will convert {entriesToDoForThisPackage.Count} Entries");
var centry = 0;
foreach(var entry in entriesToDoForThisPackage)
{
centry += 1;
if (centry % 500 == 0)
Debug.Log($"Progress: {centry}/{entriesToDoForThisPackage.Count}");
try
{
var spxFile = new SPXFile(entry.GetBytes());
if (spxFile != null && spxFile.DecompressedData != null && spxFile.DecompressedData.Length > 0)
newPackage.Changes.Set(spxFile.DecompressedData, entry.TGI, false);
}
catch (Exception e) {
Debug.LogError(e);
}
}
try
{
Debug.Log("Writing to disk...");
newPackage.WriteToFile();
Debug.Log($"Completed {package}!");
GC.Collect();
}
catch (Exception e)
{
Debug.LogError(e);
}
}
Debug.Log("All SPX has been converted to WAV! Resulting packages have been written to the SPX to WAV folder.");
}).Start();
}
}
Expand Down
File renamed without changes.

0 comments on commit 8c80e79

Please sign in to comment.