From a0bedc0d3a06cf40e61fc4b329864bf1d28ccf87 Mon Sep 17 00:00:00 2001 From: EX3 Date: Tue, 24 Sep 2024 13:19:56 +0900 Subject: [PATCH] Use blank phoneme, Implemented voicer install --- Mirivoice/Assets/Lang/strings-en-US.axaml | 12 ++ Mirivoice/Engines/ConfigVITS2.cs | 23 ++-- .../Mirivoice.Core/Editor/VoicerSelector.cs | 34 +++++- .../Mirivoice.Core/Utils/VoicerInstaller.cs | 109 ++++++++++++++++++ Mirivoice/Mirivoice.csproj | 1 + Mirivoice/ViewModels/MainViewModel.cs | 58 ++++++++-- Mirivoice/Views/MainView.axaml | 1 + Mirivoice/Views/MessageWindow.axaml | 2 +- Mirivoice/Views/MessageWindow.axaml.cs | 36 +++++- 9 files changed, 249 insertions(+), 27 deletions(-) create mode 100644 Mirivoice/Mirivoice.Core/Utils/VoicerInstaller.cs diff --git a/Mirivoice/Assets/Lang/strings-en-US.axaml b/Mirivoice/Assets/Lang/strings-en-US.axaml index e7e003f..02f07ed 100644 --- a/Mirivoice/Assets/Lang/strings-en-US.axaml +++ b/Mirivoice/Assets/Lang/strings-en-US.axaml @@ -59,6 +59,17 @@ Tools Global Settings Voicers + Install Voicer... + Select .zip file to install Voicer + Installing Voicer... + Failed to install Voicer. + There's no voicer.yaml inside the archive. + Voicer installed successfully! + + Failed to install Voicer. + Please Check Log file for more informations. + + Voicer Making Support... Transcription IPA Converter LangCode @@ -67,6 +78,7 @@ Convert No file selected + Help Check Updates diff --git a/Mirivoice/Engines/ConfigVITS2.cs b/Mirivoice/Engines/ConfigVITS2.cs index 66c8e75..9da24cc 100644 --- a/Mirivoice/Engines/ConfigVITS2.cs +++ b/Mirivoice/Engines/ConfigVITS2.cs @@ -80,7 +80,16 @@ public string _pad { } private set { } } - + + public string _blank + { + get + { + return ""; + } + private set { } + } + public string _punctuation { get { @@ -114,6 +123,8 @@ public List symbols get { // Export all symbols StringBuilder sb = new StringBuilder(); + sb.Append(_blank); + sb.Append("\t"); sb.Append(_pad); @@ -122,7 +133,7 @@ public List symbols sb.Append("\t"); sb.Append(_punctuation); sb.Append("\t"); - sb.Append("\t\t"); + sb.Append("\t\t\t"); return sb.ToString().Split("\t").ToList(); } private set @@ -132,14 +143,6 @@ private set } - // Special symbol ids - public int SPACE_ID { - get { - return symbols.IndexOf(""); - } - private set { - } - } } diff --git a/Mirivoice/Mirivoice.Core/Editor/VoicerSelector.cs b/Mirivoice/Mirivoice.Core/Editor/VoicerSelector.cs index 5f9514e..d14cb7b 100644 --- a/Mirivoice/Mirivoice.Core/Editor/VoicerSelector.cs +++ b/Mirivoice/Mirivoice.Core/Editor/VoicerSelector.cs @@ -6,6 +6,7 @@ using Serilog; using System.Collections.ObjectModel; using System.ComponentModel; +using System.Linq; namespace Mirivoice.Mirivoice.Core.Editor { @@ -32,7 +33,9 @@ public Voicer CurrentVoicer this.RaiseAndSetIfChanged(ref _currentVoicer, value); _currentVoicer.RefreshNickAndStyle(); OnPropertyChanged(nameof(CurrentVoicer)); - + v._currentDefaultVoicerIndex = Voicers.IndexOf(value); + OnPropertyChanged(nameof(CurrentDefaultVoicerIndex)); + } } @@ -58,18 +61,43 @@ public VoicerSelector(VoicerSelectingViewModelBase v, ref int _currentDefaultVoi SetDefVoicerCommand = new MementoCommand(setDefVoicerOriginator); } - + /// + /// can be used when new voicer is ADDED + /// (do not use when voicer is removed) + /// + public void UpdateVoicerCollection() + { + ObservableCollection _voicers = MainManager.Instance.GetVoicersCollectionNew(); + int offset = _voicers.Count - Voicers.Count - 1; // this amount of voicers will be added + if (offset < 0) + { + return; + } + for (int i = offset; i < _voicers.Count; i++) + { + Voicers.Add(_voicers[i]); + } + + } + bool Undobackuped = false; public int CurrentDefaultVoicerIndex { get { + if (_currentDefaultVoicerIndex == -1) + { + this.RaiseAndSetIfChanged(ref _currentDefaultVoicerIndex, 0); + } return _currentDefaultVoicerIndex; } set { - + if (value == -1) + { + return; + } lastDefaultVoicerIndex = _currentDefaultVoicerIndex; //Log.Debug("CurrentDefaultVoicerIndex: {value}", value); diff --git a/Mirivoice/Mirivoice.Core/Utils/VoicerInstaller.cs b/Mirivoice/Mirivoice.Core/Utils/VoicerInstaller.cs new file mode 100644 index 0000000..9123522 --- /dev/null +++ b/Mirivoice/Mirivoice.Core/Utils/VoicerInstaller.cs @@ -0,0 +1,109 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.IO.Compression; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; +using Avalonia.Controls; +using Mirivoice; +using Mirivoice.Mirivoice.Core.Format; +using Mirivoice.ViewModels; +using Mirivoice.Views; +using Serilog; +using SharpCompress.Archives; +using SharpCompress.Common; +using SharpCompress.Readers; + +namespace Mirivoice.Mirivoice.Core.Utils +{ + + public class VoicerInstaller + { + const string kVoicerYaml = "voicer.yaml"; + + private string basePath; + private readonly Encoding archiveEncoding; + private readonly Encoding textEncoding; + private readonly MainViewModel v; + + public VoicerInstaller(MainViewModel v) + { + this.basePath = MainManager.Instance.PathM.VoicerPath; + this.v = v; + // make sure we use UTF-8 for all text files + this.archiveEncoding = Encoding.UTF8; + this.textEncoding = Encoding.UTF8; + } + + public async void InstallVoicers(string[] paths) + { + Log.Information($"Installing {paths.Length} Voicers"); + foreach (string p in paths) + { + Log.Information($"Installing voicer from {p}"); + var result = await v.ShowTaskWindow("menu.tools.voicerinstall", "menu.tools.voicerinstall", Install(p), "menu.tools.voicerinstall.process", "menu.tools.voicerinstall.success", "menu.tools.voicerinstall.failed"); + Log.Information($"Voicer installed."); + } + Log.Information($"Refresh Voicers"); + v.voicerSelector.UpdateVoicerCollection(); + foreach (LineBoxView l in v.LineBoxCollection) + { + l.viewModel.voicerSelector.UpdateVoicerCollection(); + } + Log.Information($"Voicers refreshed."); + } + public async Task Install(string path) + { + + Log.Information($"Installing voicer from {path}"); + var readerOptions = new SharpCompress.Readers.ReaderOptions + { + ArchiveEncoding = new ArchiveEncoding + { + Forced = archiveEncoding, + } + }; + var extractionOptions = new ExtractionOptions + { + Overwrite = true, + }; + using (var archive = ArchiveFactory.Open(path, readerOptions)) + { + var touches = new List(); + int total = archive.Entries.Count(); + int count = 0; + bool hasVoicerYaml = archive.Entries.Any(e => Path.GetFileName(e.Key) == kVoicerYaml); + if (!hasVoicerYaml) + { + await v.ShowConfirmWindow("menu.tools.voicerinstall.error"); + Log.Error("Voicer archive does not contain voicer.yaml."); + return false; + } + + string dirName = Path.GetFileNameWithoutExtension(path); + string extPath = Path.Combine(basePath, dirName); + Directory.CreateDirectory(extPath); + foreach (var entry in archive.Entries) + { + if (entry.Key.Contains("..")) + { + // Prevent zipSlip attack + continue; + } + var filePath = Path.Combine(extPath, entry.Key); + Directory.CreateDirectory(Path.GetDirectoryName(filePath)); + if (!entry.IsDirectory) + { + entry.WriteToFile(Path.Combine(extPath, entry.Key), extractionOptions); + } + } + + } + return true; + } + + + } +} \ No newline at end of file diff --git a/Mirivoice/Mirivoice.csproj b/Mirivoice/Mirivoice.csproj index c07ea19..2ee8563 100644 --- a/Mirivoice/Mirivoice.csproj +++ b/Mirivoice/Mirivoice.csproj @@ -76,6 +76,7 @@ + diff --git a/Mirivoice/ViewModels/MainViewModel.cs b/Mirivoice/ViewModels/MainViewModel.cs index c29b7c0..db4fe94 100644 --- a/Mirivoice/ViewModels/MainViewModel.cs +++ b/Mirivoice/ViewModels/MainViewModel.cs @@ -38,6 +38,12 @@ public class MainViewModel : VoicerSelectingViewModelBase { Patterns = new[] { "*.wav" } }; + + public static FilePickerFileType MiriVoiceVoicer { get; } = new("Zip File") + { + Patterns = new[] { "*.zip" } + }; + Mrp initMrp; Version version = System.Reflection.Assembly.GetEntryAssembly()?.GetName().Version; @@ -372,7 +378,7 @@ public void OnPlayButtonClick() { return; } - currentLineBoxIndex = 0; + currentLineBoxIndex = 1; } else { @@ -487,12 +493,6 @@ public void OnStopButtonClick() StopButtonEnabled = false; } - - public void PlayAudio() - { - // TODO - // play cached audios - } public void OnAddBoxesButtonClick() { addLineBoxesReceiver.SetScript(mTextBoxEditor.CurrentScript); @@ -656,6 +656,23 @@ public async Task ShowConfirmWindow(string resourceId) } } + public async Task ShowTaskWindow(string resourceIdText, string resourceIdTitle, Task task, + string resourceIdProcessing, string resourceIdSuccess, string resourceIdFailed) + { + + var result = await MessageWindow.Show(mainWindow, (string)mainWindow.FindResource(resourceIdText), + (string)mainWindow.FindResource(resourceIdTitle), MessageWindow.MessageBoxButtons.OkWithProgress, task, (string)mainWindow.FindResource(resourceIdProcessing), (string)mainWindow.FindResource(resourceIdSuccess), (string)mainWindow.FindResource(resourceIdFailed) + ); + + switch (result) + { + case MessageWindow.MessageBoxResult.Ok: + return true; + default: + return false; + } + } + /* public async Task ShowErrorWindow(string errormsg) { @@ -758,7 +775,34 @@ public void OnDataPreprocessButtonClick() window.Show(); } + public async void OnVoicerInstallButtonClick() + { + VoicerInstaller voicerInstaller = new VoicerInstaller(this); + var topLevel = TopLevel.GetTopLevel(mainWindow); + + var files = await topLevel.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions + { + Title = (string)mainWindow.FindResource("menu.tools.voicerinstall.desc"), + AllowMultiple = true, + FileTypeFilter = new[] { MiriVoiceVoicer } + }); + + List p_ = new List(); + if (files is not null && files.Count >= 1) + { + + foreach (var f in files) + { + if (File.Exists(f.Path.LocalPath)) + { + p_.Add(f.Path.LocalPath); + } + + } + } + voicerInstaller.InstallVoicers(p_.ToArray()); + } public override void OnVoicerChanged(Voicer value) { } diff --git a/Mirivoice/Views/MainView.axaml b/Mirivoice/Views/MainView.axaml index 9eb225c..84a2deb 100644 --- a/Mirivoice/Views/MainView.axaml +++ b/Mirivoice/Views/MainView.axaml @@ -55,6 +55,7 @@ + diff --git a/Mirivoice/Views/MessageWindow.axaml b/Mirivoice/Views/MessageWindow.axaml index 914d02b..b3f81fc 100644 --- a/Mirivoice/Views/MessageWindow.axaml +++ b/Mirivoice/Views/MessageWindow.axaml @@ -51,7 +51,7 @@ - +