1
0
mirror of synced 2024-11-23 22:00:56 +01:00

Added 2 second padding if offset is > - 1f

This commit is contained in:
Fluto 2022-02-11 18:38:29 +11:00
parent d45e33f966
commit b27df30815
2 changed files with 68 additions and 25 deletions

View File

@ -22,6 +22,9 @@ namespace TJAConvert
{
public static class Program
{
public const int PaddedSongTime = 2 * 1000; // in ms
public const float TjaOffsetForPaddingSong = -1.0f; // in ms
public static async Task Main(string[] args)
{
if (args.Length != 1)
@ -100,7 +103,7 @@ namespace TJAConvert
Directory.CreateDirectory(tempOutDirectory);
var originalTjaData = File.ReadAllBytes(tjaPath);
var tjaHash = (int)(MurmurHash2.Hash(originalTjaData) & 0xFFFF_FFF);
var tjaHash = (int) (MurmurHash2.Hash(originalTjaData) & 0xFFFF_FFF);
var passed = await TJAToFumens(metadata, tjaPath, tjaHash, tempOutDirectory);
if (passed >= 0) passed = CreateMusicFile(metadata, tjaHash, tempOutDirectory) ? 0 : -1;
@ -108,14 +111,16 @@ namespace TJAConvert
var copyFilePath = Path.Combine(newDirectory, Path.GetFileName(originalAudioPath));
File.Copy(originalAudioPath, copyFilePath);
int millisecondsAddedSilence = metadata.Offset > TjaOffsetForPaddingSong ? PaddedSongTime : 0;
var audioExtension = Path.GetExtension(copyFilePath).TrimStart('.');
switch (audioExtension.ToLowerInvariant())
{
case "wav":
if (passed >= 0) passed = WavToACB(copyFilePath, tempOutDirectory, tjaHash) ? 0 : -1;
if (passed >= 0) passed = WavToACB(copyFilePath, tempOutDirectory, tjaHash, millisecondsAddedSilence: millisecondsAddedSilence) ? 0 : -1;
break;
case "ogg":
if (passed >= 0) passed = OGGToACB(copyFilePath, tempOutDirectory, tjaHash) ? 0 : -1;
if (passed >= 0) passed = OGGToACB(copyFilePath, tempOutDirectory, tjaHash, millisecondsAddedSilence) ? 0 : -1;
break;
default:
Console.WriteLine($"Do not support {audioExtension} audio files");
@ -202,6 +207,7 @@ namespace TJAConvert
{
try
{
var addedTime = metadata.Offset > TjaOffsetForPaddingSong ? PaddedSongTime : 0;
var musicInfo = new CustomSong
{
id = tjaHash.ToString(),
@ -212,8 +218,8 @@ namespace TJAConvert
branchHard = false,
branchMania = false,
branchUra = false,
previewPos = (int) (metadata.PreviewTime * 1000),
fumenOffsetPos = (int) (metadata.Offset * 10),
previewPos = (int) (metadata.PreviewTime * 1000) + addedTime,
fumenOffsetPos = (int) (metadata.Offset * 10) + (addedTime),
tjaFileHash = tjaHash,
songName = new TextEntry()
{
@ -882,8 +888,9 @@ namespace TJAConvert
return false;
number2 = test;
currentLines[i] = $"#BRANCHSTART p,{(int)Math.Ceiling(number1)},{(int)Math.Ceiling(number2)}";
currentLines[i] = $"#BRANCHSTART p,{(int) Math.Ceiling(number1)},{(int) Math.Ceiling(number2)}";
}
File.WriteAllLines(newPath, currentLines);
return true;
}
@ -1059,7 +1066,7 @@ namespace TJAConvert
}
}
private static bool OGGToACB(string oggPath, string outDirectory, int tjaHash)
private static bool OGGToACB(string oggPath, string outDirectory, int tjaHash, int millisecondsAddedSilence = 0)
{
try
{
@ -1072,7 +1079,7 @@ namespace TJAConvert
using (FileStream compressedFileStream = File.Create($"{acbPath}.acb"))
decompressor.CopyTo(compressedFileStream);
var hca = OggToHca(oggPath);
var hca = OggToHca(oggPath, millisecondsAddedSilence);
if (hca == null)
return false;
@ -1092,7 +1099,7 @@ namespace TJAConvert
}
}
private static bool WavToACB(string wavPath, string outDirectory, int tjaHash, bool deleteWav = false)
private static bool WavToACB(string wavPath, string outDirectory, int tjaHash, bool deleteWav = false, int millisecondsAddedSilence = 0)
{
try
{
@ -1105,7 +1112,7 @@ namespace TJAConvert
using (FileStream compressedFileStream = File.Create($"{acbPath}.acb"))
decompressor.CopyTo(compressedFileStream);
var hca = WavToHca(wavPath);
var hca = WavToHca(wavPath, millisecondsAddedSilence);
File.WriteAllBytes($"{acbPath}/00000.hca", hca);
Pack(acbPath);
if (File.Exists($"{outDirectory}/song_{tjaHash}.bin"))
@ -1125,22 +1132,53 @@ namespace TJAConvert
}
}
private static byte[] WavToHca(string path)
private static byte[] WavToHca(string path, int millisecondSilence = 0)
{
var wavReader = new WaveReader();
var hcaWriter = new HcaWriter();
var waveReader = new WaveReader();
var audioData = waveReader.Read(File.ReadAllBytes(path));
return hcaWriter.GetFile(audioData);
if (millisecondSilence > 0)
{
WaveFileReader reader = new WaveFileReader(path);
var memoryStream = new MemoryStream();
var trimmed = new OffsetSampleProvider(reader.ToSampleProvider())
{
DelayBy = TimeSpan.FromMilliseconds(millisecondSilence)
};
WaveFileWriter.WriteWavFileToStream(memoryStream, trimmed.ToWaveProvider16());
var audioData = wavReader.Read(memoryStream.ToArray());
return hcaWriter.GetFile(audioData);
}
else
{
var audioData = wavReader.Read(File.ReadAllBytes(path));
return hcaWriter.GetFile(audioData);
}
}
private static byte[] OggToHca(string inPath)
private static byte[] OggToHca(string inPath, int millisecondSilence = 0)
{
try
{
using FileStream fileIn = new FileStream(inPath, FileMode.Open);
var vorbis = new VorbisWaveReader(fileIn);
var wavProvider = new SampleToWaveProvider16(vorbis);
var memoryStream = new MemoryStream();
WaveFileWriter.WriteWavFileToStream(memoryStream, new SampleToWaveProvider16(vorbis));
if (millisecondSilence > 0)
{
var trimmed = new OffsetSampleProvider(wavProvider.ToSampleProvider())
{
DelayBy = TimeSpan.FromMilliseconds(millisecondSilence)
};
WaveFileWriter.WriteWavFileToStream(memoryStream, trimmed.ToWaveProvider16());
}
else
{
WaveFileWriter.WriteWavFileToStream(memoryStream, wavProvider);
}
var hcaWriter = new HcaWriter();
var waveReader = new WaveReader();

View File

@ -192,7 +192,7 @@ public class MusicPatch
{
if (IsTjaConverted(musicDirectory, out var conversionStatus) && conversionStatus != null)
{
foreach (var item in conversionStatus.Items.Where(item => item.Successful))
foreach (var item in conversionStatus.Items.Where(item => item.Successful && item.Version == ConversionStatus.ConversionItem.CurrentVersion))
SubmitDirectory(Path.Combine(musicDirectory, item.FolderName), true);
return;
}
@ -239,14 +239,14 @@ public class MusicPatch
if (!match.Success)
continue;
var resultInt = int.Parse(match.Groups["ID"].Value);
var resultCode = int.Parse(match.Groups["ID"].Value);
var folderPath = match.Groups["PATH"].Value;
folderPath = Path.GetFullPath(folderPath).Replace(Path.GetFullPath(musicDirectory), ".");
var existingEntry = conversionStatus.Items.FirstOrDefault(x => x.FolderName == folderPath);
var asciiFolderPath = Regex.Replace(folderPath, @"[^\u0000-\u007F]+", string.Empty);
if (resultInt >= 0)
if (resultCode >= 0)
Log.LogInfo($"Converted {asciiFolderPath} successfully");
else
Log.LogError($"Could not convert {asciiFolderPath}");
@ -257,13 +257,17 @@ public class MusicPatch
{
Attempts = 1,
FolderName = folderPath,
Successful = resultInt >= 0,
Successful = resultCode >= 0,
ResultCode = resultCode,
Version = ConversionStatus.ConversionItem.CurrentVersion,
});
}
else
{
existingEntry.Attempts++;
existingEntry.Successful = resultInt >= 0;
existingEntry.Successful = resultCode >= 0;
existingEntry.ResultCode = resultCode;
existingEntry.Version = ConversionStatus.ConversionItem.CurrentVersion;
}
}
@ -422,7 +426,7 @@ public class MusicPatch
if (conversionStatus == null)
return false;
return conversionStatus.Items.Count != 0 && conversionStatus.Items.All(x => x.Successful);
return conversionStatus.Items.Count != 0 && conversionStatus.Items.All(x => x.Successful && x.Version == ConversionStatus.ConversionItem.CurrentVersion);
}
catch
{
@ -1881,13 +1885,14 @@ public class MusicPatch
public class ConversionItem
{
[JsonIgnore] public const int CurrentVersion = 1;
[JsonIgnore] public const int CurrentVersion = 2;
[JsonIgnore] public const int MaxAttempts = 3;
[JsonProperty("f")] public string FolderName;
[JsonProperty("a")] public int Attempts;
[JsonProperty("s")] public bool Successful;
[JsonProperty("v")] public int Version = CurrentVersion;
[JsonProperty("v")] public int Version;
[JsonProperty("e")] public int ResultCode;
public override string ToString()
{
@ -1902,4 +1907,4 @@ public class MusicPatch
public string SongName;
public int UniqueId;
}
}
}