Skip to content

Commit

Permalink
Add test tool to convert all speech to wav
Browse files Browse the repository at this point in the history
  • Loading branch information
LazyDuchess committed Jun 30, 2024
1 parent e4a72d3 commit 98f1dbe
Show file tree
Hide file tree
Showing 3 changed files with 252 additions and 0 deletions.
234 changes: 234 additions & 0 deletions Assets/Editor/OpenTS2/SpeechEnhancer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
using OpenTS2.Common;
using OpenTS2.Content;
using OpenTS2.Content.DBPF;
using OpenTS2.Engine;
using OpenTS2.Files;
using OpenTS2.Files.Formats.DBPF;
using OpenTS2.Files.Formats.SPX;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using UnityEditor;
using UnityEngine;

namespace OpenTS2
{
public class SpeechEnhancer
{
[MenuItem("OpenTS2/Other/Improve Speech Using Tasks")]
private static void EnhanceSpeechTasks()
{
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(async () =>
{
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);
}
catch (Exception e)
{
Debug.LogError(e);
}
}
Debug.Log($"Will convert {entriesToDoForThisPackage.Count} Entries");
var tasks = new List<Task>();
var centry = 0;
foreach (var entry in entriesToDoForThisPackage)
{
centry += 1;
var capturedEntry = centry;
tasks.Add(Task.Run(() =>
{
if (capturedEntry % 500 == 0)
Debug.Log($"Progress: {capturedEntry}/{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);
}
}));
}
await Task.WhenAll(tasks).ContinueWith((task) =>
{
try
{
Debug.Log("Writing to disk...");
newPackage.WriteToFile();
Debug.Log($"Completed {package}!");
GC.Collect();
}
catch (Exception e)
{
Debug.LogError(e);
}
}, 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);
}
}
}).Start();
}
}
}
11 changes: 11 additions & 0 deletions Assets/Editor/OpenTS2/SpeechEnhancer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Assets/Scripts/OpenTS2/Content/DBPF/WAVAudioAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,12 @@ public override void FreeUnmanagedResources()
return;
_clip.Free();
}

#if UNITY_EDITOR
public byte[] GetWAVData()
{
return Data;
}
#endif
}
}

0 comments on commit 98f1dbe

Please sign in to comment.