Skip to content

Commit

Permalink
Fixed audiomanager's _player becomes null, Added Export selected line…
Browse files Browse the repository at this point in the history
… only
  • Loading branch information
EX3exp committed Oct 9, 2024
1 parent 65539b5 commit 17b2f31
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 7 deletions.
3 changes: 3 additions & 0 deletions Mirivoice/Assets/Lang/en-US.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
An Error occured during export.
Please Check Log file for more informations.</system:String>
<system:String x:Key="menu.files.export.perLine.desc">Select Directory to export</system:String>
<system:String x:Key="menu.files.export.selectedLine">Export selected line's audio (.wav)</system:String>
<system:String x:Key="menu.files.export.selectedLine.desc">Set Filename to export</system:String>
<system:String x:Key="menu.files.export.selectedLine.noSelected">There's no selected line...</system:String>
<system:String x:Key="menu.files.export.merged">Export merged audio (.wav)</system:String>
<system:String x:Key="menu.files.export.merged.desc">Set Filename to export</system:String>

Expand Down
4 changes: 3 additions & 1 deletion Mirivoice/Assets/Lang/ko-KR.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@
<system:String x:Key="menu.files.export.perLine.desc">내보낼 폴더 선택</system:String>
<system:String x:Key="menu.files.export.merged">[내보내기] 모든 대사를 '합쳐진 오디오'로 (.wav)</system:String>
<system:String x:Key="menu.files.export.merged.desc">내보낼 파일 이름 지정</system:String>

<system:String x:Key="menu.files.export.selectedLine">[내보내기] 선택된 대사를 오디오로 (.wav)</system:String>
<system:String x:Key="menu.files.export.selectedLine.noSelected">선택된 대사가 없어요.</system:String>
<system:String x:Key="menu.files.export.selectedLine.desc">내보낼 파일 이름 지정</system:String>
<system:String x:Key="menu.files.export.srt">[내보내기] '합쳐진 오디오' 용 자막 파일 (.srt)</system:String>
<system:String x:Key="menu.files.export.srt.desc">내보낼 파일 이름 지정</system:String>

Expand Down
64 changes: 60 additions & 4 deletions Mirivoice/Mirivoice.Core/Managers/AudioManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ List<string> GetAllCacheFiles()
/// <param name="exportPerTrack"></param>
/// <param name="fileName"></param>
/// <param name="DirPath"></param>
public async void PlayAllCacheFiles(int startIndex, bool exportOnlyAndDoNotPlay = false, bool exportPerTrack = true, string fileName = "", string DirPath = "", bool exportSrtInsteadOfAudio = false)
public async void PlayAllCacheFiles(int startIndex, bool exportOnlyAndDoNotPlay = false, bool exportPerTrack = true, string fileName = "", string DirPath = "", bool exportSrtInsteadOfAudio = false, bool SelectedOnly=false)
{
MainViewModelPlaying = true;
if (_player != null && _player.Paused)
Expand All @@ -110,6 +110,7 @@ public async void PlayAllCacheFiles(int startIndex, bool exportOnlyAndDoNotPlay
List<string> lines = new List<string>();
List<string> voicerNames = new List<string>();


int index = 0;
v.SingleTextBoxEditorEnabled = false;
v.CurrentEdit.IsEnabled = false;
Expand All @@ -125,19 +126,29 @@ public async void PlayAllCacheFiles(int startIndex, bool exportOnlyAndDoNotPlay
voicerNames.Add(v.LineBoxCollection[i].viewModel.voicerSelector.CurrentVoicer.Info.Name);
}
v.MainWindowGetInput = false;
bool skipInference = false;
for (int i = 0; i < v.LineBoxCollection.Count; ++i)
{
LineBoxView l = v.LineBoxCollection[i];

if (i < startIndex - 1)
if (i < startIndex - 1 || skipInference)
{
//Log.Debug($"[Skipping Cache Generation] Line {l.viewModel.LineNo} is before the start index.");
continue;
}

if (i == startIndex - 1)
{
l.v.StartProgress(0, v.LineBoxCollection.Count - startIndex + 1, "Inference");
if (SelectedOnly)
{
l.v.StartProgress(0, 1, "Inference");
skipInference = true;
}
else
{
l.v.StartProgress(0, v.LineBoxCollection.Count - startIndex + 1, "Inference");
}

}

//Log.Debug($"[Generating Cache]");
Expand All @@ -148,14 +159,18 @@ public async void PlayAllCacheFiles(int startIndex, bool exportOnlyAndDoNotPlay
await l.StartInference();

l.v.ProgressProgressbar(1);
if (SelectedOnly)
{
l.v.EndProgress();
}
}));
}

// Await all tasks to complete
await Task.WhenAll(tasks);

// Finalize progress
if (v.LineBoxCollection.Count - 1 >= startIndex - 1)
if (!SelectedOnly && v.LineBoxCollection.Count - 1 >= startIndex - 1)
{
v.LineBoxCollection[v.LineBoxCollection.Count - 1].v.EndProgress();
}
Expand All @@ -164,9 +179,38 @@ public async void PlayAllCacheFiles(int startIndex, bool exportOnlyAndDoNotPlay
{

Log.Information("Exporting cache files.");
if (SelectedOnly)
{
// export selected only
Log.Information("Exporting selected line only.");

int no = Int32.Parse(v.CurrentLineBox.viewModel.LineNo) - 1;

string exportPath = Path.Combine(DirPath, $"{no}_{fileName}.wav");
string cacheName = caches[no];
exportPath = SetSuffixToUnique(exportPath, 1);
Log.Debug($"Exporting {cacheName} to {exportPath}");
// resample to 48000kHz
using (var reader = new AudioFileReader(cacheName))
{
using (var resampler = new MediaFoundationResampler(reader, new WaveFormat(48000, reader.WaveFormat.Channels)))
{
resampler.ResamplerQuality = 60;

WaveFileWriter.CreateWaveFile(exportPath, resampler);
}
}
v.CurrentEdit.IsEnabled = true;
v.SingleTextBoxEditorEnabled = true;
v.MainWindowGetInput = true;
return;
}

if (exportPerTrack)
{
// export per track


int no = 1;
foreach (string cacheName in caches)
{
Expand All @@ -186,6 +230,8 @@ public async void PlayAllCacheFiles(int startIndex, bool exportOnlyAndDoNotPlay
}
no++;
}
v.CurrentEdit.IsEnabled = true;
v.SingleTextBoxEditorEnabled = true;
v.MainWindowGetInput = true;
return;
}
Expand Down Expand Up @@ -224,6 +270,8 @@ public async void PlayAllCacheFiles(int startIndex, bool exportOnlyAndDoNotPlay
}
File.WriteAllText(exportPath, sb1.ToString());
File.WriteAllText(exportPathNamesSrt, sb2.ToString());
v.CurrentEdit.IsEnabled = true;
v.SingleTextBoxEditorEnabled = true;
v.MainWindowGetInput = true;
return;
}
Expand Down Expand Up @@ -254,6 +302,8 @@ public async void PlayAllCacheFiles(int startIndex, bool exportOnlyAndDoNotPlay
}
}
}
v.CurrentEdit.IsEnabled = true;
v.SingleTextBoxEditorEnabled = true;
v.MainWindowGetInput = true;
return;
}
Expand Down Expand Up @@ -286,6 +336,12 @@ private async void PlayNextFile()
//Log.Debug($"SelectedBtnIndexBeforePlay: {SelectedBtnIndexBeforePlay}");
//Log.Debug($"OffsetBeforePlay: {OffsetBeforePlay}");
//Log.Debug($"player: {_player}");
if (_player is null)
{
_player = new Player();
_player.PlaybackFinished += OnPlaybackStopped;
}

await _player.Play(audioPaths[_currentFileIndex]);

v.LineBoxCollection[currentLine].viewModel.IsSelected = true;
Expand Down
40 changes: 38 additions & 2 deletions Mirivoice/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ public async void OnExportAudioPerLineClick()
{
Log.Information($"Exporting audio per line to {path}");
string filename = CurrentProjectPath.Split(System.IO.Path.PathSeparator).Last();
MainManager.Instance.AudioM.PlayAllCacheFiles(1, true, true, System.IO.Path.GetFileNameWithoutExtension(filename) , path);
MainManager.Instance.AudioM.PlayAllCacheFiles(1, true, true, System.IO.Path.GetFileNameWithoutExtension(filename), path);
LastExportPath = directory[0];
}
catch (Exception e)
Expand All @@ -476,8 +476,44 @@ public async void OnExportAudioPerLineClick()
var res = await ShowConfirmWindow("menu.files.export.failed");
}
}
MainWindowGetInput = true;

}


public async void OnExportAudioSelectedClick()
{
if (LineBoxCollection.Count == 0)
{
return;
}
if (CurrentLineBox is null)
{
var res = await ShowConfirmWindow("menu.files.export.selectedLine.noSelected");
return;
}
var topLevel = TopLevel.GetTopLevel(mainWindow);
var file = await topLevel.StorageProvider.SaveFilePickerAsync(new FilePickerSaveOptions
{
Title = (string)mainWindow.FindResource("menu.files.export.selectedLine.desc"),
DefaultExtension = "wav",
ShowOverwritePrompt = true,
FileTypeChoices = new[] { MiriVoiceExportAudio },
SuggestedFileName = System.IO.Path.GetFileNameWithoutExtension(CurrentProjectPath) + ".wav"
});

if (file is not null)
{
string path = file.Path.LocalPath;
try
{
MainManager.Instance.AudioM.PlayAllCacheFiles(1, true, false, System.IO.Path.GetFileNameWithoutExtension(path), System.IO.Path.GetDirectoryName(path), false, true);
}
catch (Exception e)
{
Log.Error($"[Failed to export audio selected]{e.ToString}: {e.Message} \n>> traceback: \n\t{e.StackTrace}");
var res = await ShowConfirmWindow("menu.files.export.failed");
}
}
}

public async void OnExportAudioMergedClick()
Expand Down
1 change: 1 addition & 0 deletions Mirivoice/Views/MainView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<MenuItem Header="{DynamicResource menu.files.export}">
<MenuItem Header="{DynamicResource menu.files.export.perLine}" Command="{Binding OnExportAudioPerLineClick}"></MenuItem>
<MenuItem Header="{DynamicResource menu.files.export.merged}" Command="{Binding OnExportAudioMergedClick}"></MenuItem>
<MenuItem Header="{DynamicResource menu.files.export.selectedLine}" Command="{Binding OnExportAudioSelectedClick}"></MenuItem>
<Separator/>
<MenuItem Header="{DynamicResource menu.files.export.srt}" Command="{Binding OnExportSrtClick}"></MenuItem>
</MenuItem>
Expand Down

0 comments on commit 17b2f31

Please sign in to comment.