Skip to content

Commit

Permalink
Use blank phoneme, Implemented voicer install
Browse files Browse the repository at this point in the history
  • Loading branch information
EX3exp committed Sep 24, 2024
1 parent 376072d commit a0bedc0
Show file tree
Hide file tree
Showing 9 changed files with 249 additions and 27 deletions.
12 changes: 12 additions & 0 deletions Mirivoice/Assets/Lang/strings-en-US.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@
<system:String x:Key="menu.tools.title">Tools</system:String>
<system:String x:Key="menu.tools.settings">Global Settings</system:String>
<system:String x:Key="menu.tools.voicers">Voicers</system:String>
<system:String x:Key="menu.tools.voicerinstall">Install Voicer...</system:String>
<system:String x:Key="menu.tools.voicerinstall.desc">Select .zip file to install Voicer</system:String>
<system:String x:Key="menu.tools.voicerinstall.process">Installing Voicer...</system:String>
<system:String x:Key="menu.tools.voicerinstall.error">Failed to install Voicer.
There's no voicer.yaml inside the archive.</system:String>
<system:String x:Key="menu.tools.voicerinstall.success">Voicer installed successfully!</system:String>
<system:String x:Key="menu.tools.voicerinstall.failed">
Failed to install Voicer.
Please Check Log file for more informations.
</system:String>

<system:String x:Key="menu.tools.voicerMakingSupport">Voicer Making Support...</system:String>
<system:String x:Key="menu.tools.voicerMakingSupport.preprocessor">Transcription IPA Converter</system:String>
<system:String x:Key="menu.tools.voicerMakingSupport.preprocessor.langCode">LangCode</system:String>
Expand All @@ -67,6 +78,7 @@
<system:String x:Key="menu.tools.voicerMakingSupport.preprocessor.convert">Convert</system:String>
<system:String x:Key="menu.tools.voicerMakingSupport.preprocessor.noFile">No file selected</system:String>


<!--help...-->
<system:String x:Key="menu.help.title">Help</system:String>
<system:String x:Key="menu.help.update">Check Updates</system:String>
Expand Down
23 changes: 13 additions & 10 deletions Mirivoice/Engines/ConfigVITS2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,16 @@ public string _pad {
}
private set { }
}


public string _blank
{
get
{
return "<blank>";
}
private set { }
}

public string _punctuation {
get
{
Expand Down Expand Up @@ -114,6 +123,8 @@ public List<string> symbols
get {
// Export all symbols
StringBuilder sb = new StringBuilder();
sb.Append(_blank);
sb.Append("\t");
sb.Append(_pad);


Expand All @@ -122,7 +133,7 @@ public List<string> symbols
sb.Append("\t");
sb.Append(_punctuation);
sb.Append("\t");
sb.Append("<bos>\t<eos>\t<space>");
sb.Append("<bos>\t<eos>\t<space>\t<blank>");
return sb.ToString().Split("\t").ToList();
}
private set
Expand All @@ -132,14 +143,6 @@ private set
}


// Special symbol ids
public int SPACE_ID {
get {
return symbols.IndexOf("<space>");
}
private set {
}
}

}

Expand Down
34 changes: 31 additions & 3 deletions Mirivoice/Mirivoice.Core/Editor/VoicerSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Serilog;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;

namespace Mirivoice.Mirivoice.Core.Editor
{
Expand All @@ -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));

}

}
Expand All @@ -58,18 +61,43 @@ public VoicerSelector(VoicerSelectingViewModelBase v, ref int _currentDefaultVoi
SetDefVoicerCommand = new MementoCommand<int>(setDefVoicerOriginator);
}


/// <summary>
/// can be used when new voicer is ADDED
/// (do not use when voicer is removed)
/// </summary>
public void UpdateVoicerCollection()
{
ObservableCollection<Voicer> _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);
Expand Down
109 changes: 109 additions & 0 deletions Mirivoice/Mirivoice.Core/Utils/VoicerInstaller.cs
Original file line number Diff line number Diff line change
@@ -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<bool> 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<string>();
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;
}


}
}
1 change: 1 addition & 0 deletions Mirivoice/Mirivoice.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<PackageReference Include="Serilog" Version="4.0.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
<PackageReference Include="SharpCompress" Version="0.38.0" />
<PackageReference Include="VYaml" Version="0.27.1" />
</ItemGroup>

Expand Down
58 changes: 51 additions & 7 deletions Mirivoice/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -372,7 +378,7 @@ public void OnPlayButtonClick()
{
return;
}
currentLineBoxIndex = 0;
currentLineBoxIndex = 1;
}
else
{
Expand Down Expand Up @@ -487,12 +493,6 @@ public void OnStopButtonClick()
StopButtonEnabled = false;
}


public void PlayAudio()
{
// TODO
// play cached audios
}
public void OnAddBoxesButtonClick()
{
addLineBoxesReceiver.SetScript(mTextBoxEditor.CurrentScript);
Expand Down Expand Up @@ -656,6 +656,23 @@ public async Task<bool> ShowConfirmWindow(string resourceId)
}
}

public async Task<bool> ShowTaskWindow(string resourceIdText, string resourceIdTitle, Task<bool> 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<bool> ShowErrorWindow(string errormsg)
{
Expand Down Expand Up @@ -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<string> p_ = new List<string>();
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) { }

Expand Down
1 change: 1 addition & 0 deletions Mirivoice/Views/MainView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
<MenuItem Header="{DynamicResource menu.tools.settings}" Command="{Binding OnGlobalSettingButtonClick}"></MenuItem>
<Separator/>
<MenuItem Header="{DynamicResource menu.tools.voicers}" Command="{Binding OnVoicersButtonClick}"></MenuItem>
<MenuItem Header="{DynamicResource menu.tools.voicerinstall}" Command="{Binding OnVoicerInstallButtonClick}"></MenuItem>
<MenuItem Header="{DynamicResource menu.tools.voicerMakingSupport}">
<MenuItem Header="{DynamicResource menu.tools.voicerMakingSupport.preprocessor}" Command="{Binding OnDataPreprocessButtonClick}"></MenuItem>
</MenuItem>
Expand Down
2 changes: 1 addition & 1 deletion Mirivoice/Views/MessageWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
</Style>
</StackPanel.Styles>
</StackPanel>

<StackPanel Grid.Row="1" HorizontalAlignment="Center" Orientation="Horizontal" x:Name="Buttons">
<StackPanel.Styles>
<Style Selector="Button">
Expand Down
Loading

0 comments on commit a0bedc0

Please sign in to comment.