Skip to content

Commit

Permalink
合成任务回调转发主线程执行
Browse files Browse the repository at this point in the history
  • Loading branch information
LiuYunPlayer committed Jun 12, 2024
1 parent a55b43f commit b6f17c5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 17 deletions.
2 changes: 1 addition & 1 deletion TuneLab/Data/ISynthesisPiece.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace TuneLab.Data;

internal interface ISynthesisPiece : ISynthesisData, IAudioSource
{
event Action? Complete;
event Action? Finished;
event Action? Progress;
new IEnumerable<INote> Notes { get; }
double SynthesisProgress { get; }
Expand Down
58 changes: 42 additions & 16 deletions TuneLab/Data/MidiPart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using TuneLab.Base.Utils;
using TuneLab.Base.Science;
using TuneLab.Utils;
using System.Threading;

namespace TuneLab.Data;

Expand Down Expand Up @@ -347,7 +348,7 @@ void ReSegmentImpl()
{
var newPiece = new SynthesisPiece(this, segment);
newPiece.SynthesisStatusChanged += () => { mSynthesisStatusChanged.Invoke(newPiece); };
newPiece.Complete += () => { if (string.IsNullOrEmpty(newPiece.LastError)) return; Log.Debug(string.Format("Synthesis error: {0} {1}", Voice.Name, newPiece.LastError)); };
newPiece.Finished += () => { if (string.IsNullOrEmpty(newPiece.LastError)) return; Log.Debug(string.Format("Synthesis error: {0} {1}", Voice.Name, newPiece.LastError)); };
newPieces.Add(newPiece);
}
}
Expand Down Expand Up @@ -679,7 +680,7 @@ protected override bool IsInOrder(INote prev, INote next)

class SynthesisPiece : ISynthesisPiece, IDisposable
{
public event Action? Complete;
public event Action? Finished;
public event Action? Progress;
public event Action? SynthesisStatusChanged;
public double SynthesisProgress => mSynthesisProgress;
Expand Down Expand Up @@ -726,24 +727,42 @@ public SynthesisPiece(MidiPart part, SynthesisSegment<INote> segment)
mTask = mPart.Voice.CreateSynthesisTask(this);
mTask.Complete += (result) => // FIXME: 将信号转发到主线程执行
{
mSynthesisResult = result;
mWaveform = new(mSynthesisResult.AudioData);
foreach (var note in Notes)
context.Post(_ =>
{
if (mSynthesisResult.SynthesizedPhonemes.TryGetValue(note, out var phonemes))
mSynthesisResult = result;
mWaveform = new(mSynthesisResult.AudioData);
foreach (var note in Notes)
{
note.SynthesizedPhonemes = phonemes;
if (mSynthesisResult.SynthesizedPhonemes.TryGetValue(note, out var phonemes))
{
note.SynthesizedPhonemes = phonemes;
}
else
{
note.SynthesizedPhonemes = null;
}
}
else
{
note.SynthesizedPhonemes = null;
}
}
SynthesisStatus = SynthesisStatus.SynthesisSucceeded;
Complete?.Invoke();
SynthesisStatus = SynthesisStatus.SynthesisSucceeded;
Finished?.Invoke();
}, null);
};
mTask.Error += (error) =>
{
context.Post(_ =>
{
mLastError = error;
SynthesisStatus = SynthesisStatus.SynthesisFailed;
Finished?.Invoke();
}, null);
};
mTask.Progress += (progress) =>
{
context.Post(_ =>
{
mSynthesisProgress = progress;
Progress?.Invoke();
}, null);
};
mTask.Error += (error) => { mLastError = error; SynthesisStatus = SynthesisStatus.SynthesisFailed; Complete?.Invoke(); };
mTask.Progress += (progress) => { mSynthesisProgress = progress; Progress?.Invoke(); };
part.Properties.Modified.Subscribe(SetDirtyAndResegment, s);
foreach (var note in Notes)
{
Expand Down Expand Up @@ -822,6 +841,13 @@ bool ISynthesisData.GetAutomation(string automationID, [MaybeNullWhen(false)][No
return result;
}

static SynthesisPiece()
{
context = SynchronizationContext.Current!;
}

static SynchronizationContext context;

MidiPart mPart;
INote[] mNotes;
SynthesisStatus mSynthesisStatus = SynthesisStatus.NotSynthesized;
Expand Down

0 comments on commit b6f17c5

Please sign in to comment.