From fd4051b163a0f7b57e1a656e47030ea680f07e01 Mon Sep 17 00:00:00 2001 From: LongerWarrior Date: Sun, 22 Mar 2026 18:37:00 +0200 Subject: [PATCH] Convert audio via temp file --- FModel/ViewModels/AudioPlayerViewModel.cs | 61 +++++++++++------------ 1 file changed, 30 insertions(+), 31 deletions(-) diff --git a/FModel/ViewModels/AudioPlayerViewModel.cs b/FModel/ViewModels/AudioPlayerViewModel.cs index eff4cbb7..802e87ab 100644 --- a/FModel/ViewModels/AudioPlayerViewModel.cs +++ b/FModel/ViewModels/AudioPlayerViewModel.cs @@ -340,12 +340,8 @@ public class AudioPlayerViewModel : ViewModel, ISource, IDisposable Directory.CreateDirectory(path.SubstringBeforeLast('/')); } - using (var stream = new FileStream(path, FileMode.Create, FileAccess.Write)) - using (var writer = new BinaryWriter(stream)) - { - writer.Write(fileToSave.Data); - writer.Flush(); - } + using var stream = new FileStream(path, FileMode.Create, FileAccess.Write); + stream.Write(fileToSave.Data); if (File.Exists(path)) { @@ -684,25 +680,11 @@ public class AudioPlayerViewModel : ViewModel, ISource, IDisposable } } - Directory.CreateDirectory(inputFilePath.SubstringBeforeLast("/")); - File.WriteAllBytes(inputFilePath, inputFileData); + var success = TryConvertToWAV(inputFilePath, inputFileData, vgmFilePath, true, out var tempWavFilePath); - wavFilePath = Path.ChangeExtension(inputFilePath, ".wav"); - var vgmProcess = Process.Start(new ProcessStartInfo - { - FileName = vgmFilePath, - Arguments = $"-o \"{wavFilePath}\" \"{inputFilePath}\"", - UseShellExecute = false, - CreateNoWindow = true - }); - vgmProcess?.WaitForExit(5000); - - File.Delete(inputFilePath); - - var success = vgmProcess?.ExitCode == 0 && File.Exists(wavFilePath); if (!success) { - Log.Error("Failed to convert {InputFilePath} to .wav format", inputFilePath); + Log.Error("Failed to convert {InputFilePath} to .wav format", Path.GetFileName(inputFilePath)); if (updateUi) { FLogger.Append(ELog.Error, () => @@ -731,20 +713,37 @@ public class AudioPlayerViewModel : ViewModel, ISource, IDisposable return false; } - Directory.CreateDirectory(SelectedAudioFile.FilePath.SubstringBeforeLast("/")); - File.WriteAllBytes(SelectedAudioFile.FilePath, SelectedAudioFile.Data); + return TryConvertToWAV(SelectedAudioFile.FilePath, SelectedAudioFile.Data, decoderPath, false, out rawFilePath); + } - rawFilePath = Path.ChangeExtension(SelectedAudioFile.FilePath, ".wav"); - var decoderProcess = Process.Start(new ProcessStartInfo + private static bool TryConvertToWAV(string inputFilePath, byte[] inputFileData, string converterPath, bool usevgmstream, out string wavFilePath) + { + wavFilePath = Path.ChangeExtension(inputFilePath, ".wav"); + var directory = Path.GetDirectoryName(inputFilePath); + Directory.CreateDirectory(directory); + + var tempfile = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + Path.GetExtension(inputFilePath)); + File.WriteAllBytes(tempfile, inputFileData); + + var tempWavFilePath = Path.ChangeExtension(tempfile, ".wav"); + + var process = Process.Start(new ProcessStartInfo { - FileName = decoderPath, - Arguments = $"-i \"{SelectedAudioFile.FilePath}\" -o \"{rawFilePath}\"", + FileName = converterPath, + Arguments = usevgmstream ? $"-o \"{tempWavFilePath}\" \"{tempfile}\"" : $"-i \"{tempfile}\" -o \"{tempWavFilePath}\"", UseShellExecute = false, CreateNoWindow = true }); - decoderProcess?.WaitForExit(5000); + process?.WaitForExit(5000); - File.Delete(SelectedAudioFile.FilePath); - return decoderProcess?.ExitCode == 0 && File.Exists(rawFilePath); + File.Delete(tempfile); + + var success = process?.ExitCode == 0 && File.Exists(tempWavFilePath); + if (success) + { + File.Move(tempWavFilePath, wavFilePath, true); + } + + return success; } }