From 0e504b02471f743dcc6cc8e2c20c2f798adb92aa Mon Sep 17 00:00:00 2001 From: Maiko Date: Sat, 14 Oct 2023 20:49:23 +0900 Subject: [PATCH 1/3] Check wav file writeable when exporting --- OpenUtau.Core/PlaybackManager.cs | 33 ++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/OpenUtau.Core/PlaybackManager.cs b/OpenUtau.Core/PlaybackManager.cs index cac0826bc..7242412dc 100644 --- a/OpenUtau.Core/PlaybackManager.cs +++ b/OpenUtau.Core/PlaybackManager.cs @@ -151,8 +151,13 @@ public void RenderMixdown(UProject project, string exportPath) { RenderEngine engine = new RenderEngine(project); var projectMix = engine.RenderMixdown(0,DocManager.Inst.MainScheduler, ref renderCancellation,wait:true).Item1; DocManager.Inst.ExecuteCmd(new ProgressBarNotification(0, $"Exporting to {exportPath}.")); - WaveFileWriter.CreateWaveFile16(exportPath, new ExportAdapter(projectMix).ToMono(1, 0)); - DocManager.Inst.ExecuteCmd(new ProgressBarNotification(0, $"Exported to {exportPath}.")); + if (isWritableFile(exportPath, out Exception? e)) { + WaveFileWriter.CreateWaveFile16(exportPath, new ExportAdapter(projectMix).ToMono(1, 0)); + DocManager.Inst.ExecuteCmd(new ProgressBarNotification(0, $"Exported to {exportPath}.")); + } else { + DocManager.Inst.ExecuteCmd(new ProgressBarNotification(0, $"Failed to export {exportPath}.")); + Log.Error(e, $"Failed to export {exportPath}."); + } }); try { task.Wait(); @@ -175,8 +180,13 @@ public void RenderToFiles(UProject project, string exportPath) { } var file = PathManager.Inst.GetExportPath(exportPath, project.tracks[i]); DocManager.Inst.ExecuteCmd(new ProgressBarNotification(0, $"Exporting to {file}.")); - WaveFileWriter.CreateWaveFile16(file, new ExportAdapter(trackMixes[i]).ToMono(1, 0)); - DocManager.Inst.ExecuteCmd(new ProgressBarNotification(0, $"Exported to {file}.")); + if (isWritableFile(file, out Exception? e)) { + WaveFileWriter.CreateWaveFile16(file, new ExportAdapter(trackMixes[i]).ToMono(1, 0)); + DocManager.Inst.ExecuteCmd(new ProgressBarNotification(0, $"Exported to {file}.")); + } else { + DocManager.Inst.ExecuteCmd(new ProgressBarNotification(0, $"Failed to export {file}.")); + Log.Error(e, $"Failed to export {file}."); + } } }); try { @@ -189,6 +199,21 @@ public void RenderToFiles(UProject project, string exportPath) { }); } + private bool isWritableFile(string filePath, out Exception? e) { + e = null; + try { + if(!File.Exists(filePath)) { + return true; + } + using (FileStream fp = File.Open(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)) { + return true; + } + } catch (Exception ioe) { + e = ioe; + return false; + } + } + void SchedulePreRender() { Log.Information("SchedulePreRender"); var engine = new RenderEngine(DocManager.Inst.Project); From ac4917df2cc634988718bc8f1974187c7567886c Mon Sep 17 00:00:00 2001 From: Maiko Date: Fri, 20 Oct 2023 18:35:40 +0900 Subject: [PATCH 2/3] Show error when wav file can't be exported --- OpenUtau.Core/PlaybackManager.cs | 87 ++++++++++++++---------------- OpenUtau/Views/MainWindow.axaml.cs | 29 +++++----- 2 files changed, 54 insertions(+), 62 deletions(-) diff --git a/OpenUtau.Core/PlaybackManager.cs b/OpenUtau.Core/PlaybackManager.cs index 7242412dc..8caa3209c 100644 --- a/OpenUtau.Core/PlaybackManager.cs +++ b/OpenUtau.Core/PlaybackManager.cs @@ -145,72 +145,63 @@ public static float DecibelToVolume(double db) { return (db <= -24) ? 0 : (float)MusicMath.DecibelToLinear((db < -16) ? db * 2 + 16 : db); } - public void RenderMixdown(UProject project, string exportPath) { - Task.Run(() => { - var task = Task.Run(() => { + public async Task RenderMixdown(UProject project, string exportPath) { + await Task.Run(() => { + try { RenderEngine engine = new RenderEngine(project); - var projectMix = engine.RenderMixdown(0,DocManager.Inst.MainScheduler, ref renderCancellation,wait:true).Item1; + var projectMix = engine.RenderMixdown(0, DocManager.Inst.MainScheduler, ref renderCancellation, wait: true).Item1; DocManager.Inst.ExecuteCmd(new ProgressBarNotification(0, $"Exporting to {exportPath}.")); - if (isWritableFile(exportPath, out Exception? e)) { - WaveFileWriter.CreateWaveFile16(exportPath, new ExportAdapter(projectMix).ToMono(1, 0)); - DocManager.Inst.ExecuteCmd(new ProgressBarNotification(0, $"Exported to {exportPath}.")); - } else { - DocManager.Inst.ExecuteCmd(new ProgressBarNotification(0, $"Failed to export {exportPath}.")); - Log.Error(e, $"Failed to export {exportPath}."); - } - }); - try { - task.Wait(); - } catch (AggregateException ae) { - foreach (var e in ae.Flatten().InnerExceptions) { - Log.Error(e, "Failed to render."); - } + + CheckFileWritable(exportPath); + WaveFileWriter.CreateWaveFile16(exportPath, new ExportAdapter(projectMix).ToMono(1, 0)); + DocManager.Inst.ExecuteCmd(new ProgressBarNotification(0, $"Exported to {exportPath}.")); + } catch (IOException ioe) { + Log.Error(ioe, $"Failed to export {exportPath}."); + DocManager.Inst.ExecuteCmd(new ErrorMessageNotification(ioe)); + DocManager.Inst.ExecuteCmd(new ProgressBarNotification(0, $"Failed to export {exportPath}.")); + } catch (Exception e) { + Log.Error(e, "Failed to render."); + DocManager.Inst.ExecuteCmd(new ErrorMessageNotification(e)); + DocManager.Inst.ExecuteCmd(new ProgressBarNotification(0, $"Failed to render.")); } }); } - public void RenderToFiles(UProject project, string exportPath) { - Task.Run(() => { - var task = Task.Run(() => { + public async Task RenderToFiles(UProject project, string exportPath) { + await Task.Run(() => { + string file = ""; + try { RenderEngine engine = new RenderEngine(project); var trackMixes = engine.RenderTracks(DocManager.Inst.MainScheduler, ref renderCancellation); for (int i = 0; i < trackMixes.Count; ++i) { if (trackMixes[i] == null || i >= project.tracks.Count || project.tracks[i].Muted) { continue; } - var file = PathManager.Inst.GetExportPath(exportPath, project.tracks[i]); + file = PathManager.Inst.GetExportPath(exportPath, project.tracks[i]); DocManager.Inst.ExecuteCmd(new ProgressBarNotification(0, $"Exporting to {file}.")); - if (isWritableFile(file, out Exception? e)) { - WaveFileWriter.CreateWaveFile16(file, new ExportAdapter(trackMixes[i]).ToMono(1, 0)); - DocManager.Inst.ExecuteCmd(new ProgressBarNotification(0, $"Exported to {file}.")); - } else { - DocManager.Inst.ExecuteCmd(new ProgressBarNotification(0, $"Failed to export {file}.")); - Log.Error(e, $"Failed to export {file}."); - } - } - }); - try { - task.Wait(); - } catch (AggregateException ae) { - foreach (var e in ae.Flatten().InnerExceptions) { - Log.Error(e, "Failed to render."); + + CheckFileWritable(file); + WaveFileWriter.CreateWaveFile16(file, new ExportAdapter(trackMixes[i]).ToMono(1, 0)); + DocManager.Inst.ExecuteCmd(new ProgressBarNotification(0, $"Exported to {file}.")); } + } catch (IOException ioe) { + Log.Error(ioe, $"Failed to export {file}."); + DocManager.Inst.ExecuteCmd(new ErrorMessageNotification(ioe)); + DocManager.Inst.ExecuteCmd(new ProgressBarNotification(0, $"Failed to export {file}.")); + } catch (Exception e) { + Log.Error(e, "Failed to render."); + DocManager.Inst.ExecuteCmd(new ErrorMessageNotification(e)); + DocManager.Inst.ExecuteCmd(new ProgressBarNotification(0, $"Failed to render.")); } }); } - private bool isWritableFile(string filePath, out Exception? e) { - e = null; - try { - if(!File.Exists(filePath)) { - return true; - } - using (FileStream fp = File.Open(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)) { - return true; - } - } catch (Exception ioe) { - e = ioe; - return false; + private void CheckFileWritable(string filePath) { + if (!File.Exists(filePath)) { + return; + } + using (FileStream fp = File.Open(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)) { + return; } } diff --git a/OpenUtau/Views/MainWindow.axaml.cs b/OpenUtau/Views/MainWindow.axaml.cs index 7ec9e5a06..e9c7b5fbd 100644 --- a/OpenUtau/Views/MainWindow.axaml.cs +++ b/OpenUtau/Views/MainWindow.axaml.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.IO; using System.Linq; using System.Reactive; using System.Threading; @@ -217,7 +218,7 @@ void OnMenuOpenProjectLocation(object sender, RoutedEventArgs args) { MessageBox.MessageBoxButtons.Ok); } try { - var dir = System.IO.Path.GetDirectoryName(project.FilePath); + var dir = Path.GetDirectoryName(project.FilePath); if (dir != null) { OS.OpenFolder(dir); } else { @@ -259,9 +260,9 @@ void OnMenuSaveTemplate(object sender, RoutedEventArgs args) { if (string.IsNullOrEmpty(file)) { return; } - file = System.IO.Path.GetFileNameWithoutExtension(file); + file = Path.GetFileNameWithoutExtension(file); file = $"{file}.ustx"; - file = System.IO.Path.Combine(PathManager.Inst.TemplatesPath, file); + file = Path.Combine(PathManager.Inst.TemplatesPath, file); Ustx.Save(file, project.CloneAsTemplate()); }; dialog.ShowDialog(this); @@ -328,17 +329,17 @@ async void OnMenuExportMixdown(object sender, RoutedEventArgs args) { var file = await FilePicker.SaveFile( this, "menu.file.exportmixdown", FilePicker.WAV); if (!string.IsNullOrEmpty(file)) { - PlaybackManager.Inst.RenderMixdown(project, file); + await PlaybackManager.Inst.RenderMixdown(project, file); } } async void OnMenuExportWav(object sender, RoutedEventArgs args) { var project = DocManager.Inst.Project; if (await WarnToSave(project)) { - var name = System.IO.Path.GetFileNameWithoutExtension(project.FilePath); - var path = System.IO.Path.GetDirectoryName(project.FilePath); - path = System.IO.Path.Combine(path!, "Export", $"{name}.wav"); - PlaybackManager.Inst.RenderToFiles(project, path); + var name = Path.GetFileNameWithoutExtension(project.FilePath); + var path = Path.GetDirectoryName(project.FilePath); + path = Path.Combine(path!, "Export", $"{name}.wav"); + await PlaybackManager.Inst.RenderToFiles(project, path); } } @@ -347,16 +348,16 @@ async void OnMenuExportWavTo(object sender, RoutedEventArgs args) { var file = await FilePicker.SaveFile( this, "menu.file.exportwavto", FilePicker.WAV); if (!string.IsNullOrEmpty(file)) { - PlaybackManager.Inst.RenderToFiles(project, file); + await PlaybackManager.Inst.RenderToFiles(project, file); } } async void OnMenuExportUst(object sender, RoutedEventArgs e) { var project = DocManager.Inst.Project; if (await WarnToSave(project)) { - var name = System.IO.Path.GetFileNameWithoutExtension(project.FilePath); - var path = System.IO.Path.GetDirectoryName(project.FilePath); - path = System.IO.Path.Combine(path!, "Export", $"{name}.ust"); + var name = Path.GetFileNameWithoutExtension(project.FilePath); + var path = Path.GetDirectoryName(project.FilePath); + path = Path.Combine(path!, "Export", $"{name}.ust"); for (var i = 0; i < project.parts.Count; i++) { var part = project.parts[i]; if (part is UVoicePart voicePart) { @@ -665,7 +666,7 @@ async void OnDrop(object? sender, DragEventArgs args) { return; } string file = storageItem.Path.LocalPath; - var ext = System.IO.Path.GetExtension(file); + var ext = Path.GetExtension(file); if (ext == ".ustx" || ext == ".ust" || ext == ".vsqx" || ext==".ufdata") { if (!DocManager.Inst.ChangesSaved && !await AskIfSaveAndContinue()) { return; @@ -1111,4 +1112,4 @@ public void OnNext(UCommand cmd, bool isUndo) { } } } -} \ No newline at end of file +} From 7dbb477c975869ec1fbcc3bab5d19a494f8f4afb Mon Sep 17 00:00:00 2001 From: Maiko Date: Fri, 20 Oct 2023 23:10:39 +0900 Subject: [PATCH 3/3] fix error log --- OpenUtau.Core/PlaybackManager.cs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/OpenUtau.Core/PlaybackManager.cs b/OpenUtau.Core/PlaybackManager.cs index 8caa3209c..4b533ada8 100644 --- a/OpenUtau.Core/PlaybackManager.cs +++ b/OpenUtau.Core/PlaybackManager.cs @@ -156,12 +156,10 @@ await Task.Run(() => { WaveFileWriter.CreateWaveFile16(exportPath, new ExportAdapter(projectMix).ToMono(1, 0)); DocManager.Inst.ExecuteCmd(new ProgressBarNotification(0, $"Exported to {exportPath}.")); } catch (IOException ioe) { - Log.Error(ioe, $"Failed to export {exportPath}."); - DocManager.Inst.ExecuteCmd(new ErrorMessageNotification(ioe)); + DocManager.Inst.ExecuteCmd(new ErrorMessageNotification($"Failed to export {exportPath}.", ioe)); DocManager.Inst.ExecuteCmd(new ProgressBarNotification(0, $"Failed to export {exportPath}.")); } catch (Exception e) { - Log.Error(e, "Failed to render."); - DocManager.Inst.ExecuteCmd(new ErrorMessageNotification(e)); + DocManager.Inst.ExecuteCmd(new ErrorMessageNotification("Failed to render.", e)); DocManager.Inst.ExecuteCmd(new ProgressBarNotification(0, $"Failed to render.")); } }); @@ -185,12 +183,10 @@ await Task.Run(() => { DocManager.Inst.ExecuteCmd(new ProgressBarNotification(0, $"Exported to {file}.")); } } catch (IOException ioe) { - Log.Error(ioe, $"Failed to export {file}."); - DocManager.Inst.ExecuteCmd(new ErrorMessageNotification(ioe)); + DocManager.Inst.ExecuteCmd(new ErrorMessageNotification($"Failed to export {file}.", ioe)); DocManager.Inst.ExecuteCmd(new ProgressBarNotification(0, $"Failed to export {file}.")); } catch (Exception e) { - Log.Error(e, "Failed to render."); - DocManager.Inst.ExecuteCmd(new ErrorMessageNotification(e)); + DocManager.Inst.ExecuteCmd(new ErrorMessageNotification("Failed to render.", e)); DocManager.Inst.ExecuteCmd(new ProgressBarNotification(0, $"Failed to render.")); } });