Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved stability of rendering #952

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions OpenUtau.Core/Classic/WorldlineRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using OpenUtau.Core.Render;
using OpenUtau.Core.SignalChain;
using OpenUtau.Core.Ustx;
using Serilog;

namespace OpenUtau.Classic {
public class WorldlineRenderer : IRenderer {
Expand Down Expand Up @@ -58,12 +57,8 @@ public Task<RenderResult> Render(RenderPhrase phrase, Progress progress, int tra
string progressInfo = $"Track {trackNo + 1}: {this} {string.Join(" ", phrase.phones.Select(p => p.phoneme))}";
progress.Complete(0, progressInfo);
if (File.Exists(wavPath)) {
try {
using (var waveStream = Wave.OpenFile(wavPath)) {
result.samples = Wave.GetSamples(waveStream.ToSampleProvider().ToMono(1, 0));
}
} catch (Exception e) {
Log.Error(e, "Failed to render.");
using (var waveStream = Wave.OpenFile(wavPath)) {
result.samples = Wave.GetSamples(waveStream.ToSampleProvider().ToMono(1, 0));
}
}
if (result.samples == null) {
Expand Down
1 change: 0 additions & 1 deletion OpenUtau.Core/Classic/WorldlineResampler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using OpenUtau.Core;
using OpenUtau.Core.Render;
using OpenUtau.Core.SignalChain;
using OpenUtau.Core.Ustx;
using Serilog;

namespace OpenUtau.Classic {
Expand Down
24 changes: 13 additions & 11 deletions OpenUtau.Core/PlaybackManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,18 @@ private void StartPlayback(double startMs, MasterAdapter masterAdapter) {

private void Render(UProject project, int tick) {
Task.Run(() => {
RenderEngine engine = new RenderEngine(project, tick);
var result = engine.RenderProject(tick, DocManager.Inst.MainScheduler, ref renderCancellation);
faders = result.Item2;
StartingToPlay = false;
StartPlayback(project.timeAxis.TickPosToMsPos(tick), result.Item1);
}).ContinueWith((task) => {
if (task.IsFaulted) {
Log.Error(task.Exception, "Failed to render.");
DocManager.Inst.ExecuteCmd(new ErrorMessageNotification("Failed to render.", task.Exception));
throw task.Exception;
try {
RenderEngine engine = new RenderEngine(project, tick);
var result = engine.RenderProject(tick, DocManager.Inst.MainScheduler, ref renderCancellation);
faders = result.Item2;
StartingToPlay = false;
StartPlayback(project.timeAxis.TickPosToMsPos(tick), result.Item1);
} catch (Exception e) {
Log.Error(e, "Failed to render.");
StopPlayback();
DocManager.Inst.ExecuteCmd(new ErrorMessageNotification("Failed to render.", e));
}
}, CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, DocManager.Inst.MainScheduler);
});
}

public void UpdatePlayPos() {
Expand All @@ -145,6 +145,7 @@ public static float DecibelToVolume(double db) {
return (db <= -24) ? 0 : (float)MusicMath.DecibelToLinear((db < -16) ? db * 2 + 16 : db);
}

// Exporting mixdown
public async Task RenderMixdown(UProject project, string exportPath) {
await Task.Run(() => {
try {
Expand All @@ -165,6 +166,7 @@ await Task.Run(() => {
});
}

// Exporting each tracks
public async Task RenderToFiles(UProject project, string exportPath) {
await Task.Run(() => {
string file = "";
Expand Down
17 changes: 10 additions & 7 deletions OpenUtau.Core/Render/RenderEngine.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
Expand Down Expand Up @@ -49,6 +49,7 @@ public RenderEngine(UProject project, int startTick = 0) {
this.startTick = startTick;
}

// for playback or export
public Tuple<WaveMix, List<Fader>> RenderMixdown(int startTick, TaskScheduler uiScheduler, ref CancellationTokenSource cancellation, bool wait = false) {
var newCancellation = new CancellationTokenSource();
var oldCancellation = Interlocked.Exchange(ref cancellation, newCancellation);
Expand Down Expand Up @@ -94,10 +95,10 @@ public Tuple<WaveMix, List<Fader>> RenderMixdown(int startTick, TaskScheduler ui
RenderRequests(requests, newCancellation, playing: !wait);
});
task.ContinueWith(task => {
if (task.IsFaulted) {
Log.Error(task.Exception, "Failed to render.");
if (task.IsFaulted && !wait) {
Log.Error(task.Exception.Flatten(), "Failed to render.");
PlaybackManager.Inst.StopPlayback();
DocManager.Inst.ExecuteCmd(new ErrorMessageNotification("Failed to render.", task.Exception));
throw task.Exception;
}
}, CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, uiScheduler);
if (wait) {
Expand All @@ -106,14 +107,16 @@ public Tuple<WaveMix, List<Fader>> RenderMixdown(int startTick, TaskScheduler ui
return Tuple.Create(new WaveMix(faders), faders);
}

// for playback
public Tuple<MasterAdapter, List<Fader>> RenderProject(int startTick, TaskScheduler uiScheduler, ref CancellationTokenSource cancellation) {
double startMs = project.timeAxis.TickPosToMsPos(startTick);
var renderMixdownResult = RenderMixdown(startTick, uiScheduler, ref cancellation,wait:false);
var renderMixdownResult = RenderMixdown(startTick, uiScheduler, ref cancellation, wait: false);
var master = new MasterAdapter(renderMixdownResult.Item1);
master.SetPosition((int)(startMs * 44100 / 1000) * 2);
return Tuple.Create(master, renderMixdownResult.Item2);
}

// for export
public List<WaveMix> RenderTracks(TaskScheduler uiScheduler, ref CancellationTokenSource cancellation) {
var newCancellation = new CancellationTokenSource();
var oldCancellation = Interlocked.Exchange(ref cancellation, newCancellation);
Expand Down Expand Up @@ -141,6 +144,7 @@ public List<WaveMix> RenderTracks(TaskScheduler uiScheduler, ref CancellationTok
return trackMixes;
}

// for pre render
public void PreRenderProject(ref CancellationTokenSource cancellation) {
var newCancellation = new CancellationTokenSource();
var oldCancellation = Interlocked.Exchange(ref cancellation, newCancellation);
Expand Down Expand Up @@ -221,8 +225,7 @@ private void RenderRequests(
source.SetSamples(task.Result.samples);
if (request.sources.All(s => s.HasSamples)) {
request.part.SetMix(request.mix);
new Task(() => DocManager.Inst.ExecuteCmd(new PartRenderedNotification(request.part)))
.Start(DocManager.Inst.MainScheduler);
DocManager.Inst.ExecuteCmd(new PartRenderedNotification(request.part));
}
}
progress.Clear();
Expand Down
32 changes: 14 additions & 18 deletions OpenUtau/Views/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -803,23 +803,7 @@ void OnPlayOrPause(object sender, RoutedEventArgs args) {
}

void PlayOrPause() {
try {
viewModel.PlaybackViewModel.PlayOrPause();
} catch (Core.Render.NoResamplerException) {
MessageBox.Show(
this,
ThemeManager.GetString("dialogs.noresampler.message"),
ThemeManager.GetString("dialogs.noresampler.caption"),
MessageBox.MessageBoxButtons.Ok);
} catch (Core.Render.NoWavtoolException) {
MessageBox.Show(
this,
ThemeManager.GetString("dialogs.noresampler.message"),
ThemeManager.GetString("dialogs.noresampler.caption"),
MessageBox.MessageBoxButtons.Ok);
} catch (Exception e) {
MessageBox.ShowError(this, e);
}
viewModel.PlaybackViewModel.PlayOrPause();
}

public void HScrollPointerWheelChanged(object sender, PointerWheelEventArgs args) {
Expand Down Expand Up @@ -1226,7 +1210,19 @@ private async Task<bool> AskIfSaveAndContinue() {

public void OnNext(UCommand cmd, bool isUndo) {
if (cmd is ErrorMessageNotification notif) {
MessageBox.ShowError(this, notif.message, notif.e);
switch (notif.e) {
case Core.Render.NoResamplerException:
case Core.Render.NoWavtoolException:
MessageBox.Show(
this,
ThemeManager.GetString("dialogs.noresampler.message"),
ThemeManager.GetString("dialogs.noresampler.caption"),
MessageBox.MessageBoxButtons.Ok);
break;
default:
MessageBox.ShowError(this, notif.message, notif.e);
break;
}
} else if (cmd is LoadingNotification loadingNotif && loadingNotif.window == typeof(MainWindow)) {
if (loadingNotif.startLoading) {
MessageBox.ShowLoading(this);
Expand Down
6 changes: 1 addition & 5 deletions OpenUtau/Views/PianoRollWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -930,11 +930,7 @@ bool OnKeyExtendedHandler(KeyEventArgs args) {
#region document keys
case Key.Space:
if (isNone) {
try {
playVm.PlayOrPause();
} catch (Exception e) {
MessageBox.ShowError(this, e);
}
playVm.PlayOrPause();
return true;
}
break;
Expand Down