Skip to content

Commit

Permalink
Implemented Voicer Window
Browse files Browse the repository at this point in the history
TODO appvayor test
  • Loading branch information
EX3exp committed Sep 25, 2024
1 parent ae381aa commit c918e52
Show file tree
Hide file tree
Showing 13 changed files with 319 additions and 46 deletions.
2 changes: 2 additions & 0 deletions Mirivoice/App.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
<StreamGeometry x:Key="play_circle_regular">M12,2 C17.5228475,2 22,6.4771525 22,12 C22,17.5228475 17.5228475,22 12,22 C6.4771525,22 2,17.5228475 2,12 C2,6.4771525 6.4771525,2 12,2 Z M12,3.5 C7.30557963,3.5 3.5,7.30557963 3.5,12 C3.5,16.6944204 7.30557963,20.5 12,20.5 C16.6944204,20.5 20.5,16.6944204 20.5,12 C20.5,7.30557963 16.6944204,3.5 12,3.5 Z M9.0527864,8.5854102 C9.1625594,8.3658642 9.41205933,8.26236922 9.63961698,8.328815 L9.7236068,8.3618034 L16.1055728,11.5527864 C16.2023365,11.6011683 16.2807978,11.6796295 16.3291796,11.7763932 C16.4389526,11.9959392 16.3720486,12.2576361 16.1823574,12.3998148 L16.1055728,12.4472136 L9.7236068,15.6381966 C9.65417908,15.6729105 9.57762255,15.690983 9.5,15.690983 C9.25454011,15.690983 9.05039163,15.5141078 9.00805567,15.2808586 L9,15.190983 L9,8.80901699 C9,8.73139445 9.01807255,8.65483791 9.0527864,8.5854102 Z</StreamGeometry>
<StreamGeometry x:Key="person_voice_regular">M14.7541747,14.999921 C15.9961948,14.999921 17.0030511,16.0067773 17.0030511,17.2487975 L17.0030511,17.8242119 C17.0030511,18.7184933 16.6834576,19.583334 16.1019339,20.2627207 C14.5325642,22.0961953 12.1453816,23.0010712 9,23.0010712 C5.85413751,23.0010712 3.46812444,22.0958945 1.90182147,20.2616629 C1.32205983,19.5827471 1.00354153,18.7192572 1.00354153,17.8264756 L1.00354153,17.2487975 C1.00354153,16.0067773 2.0103978,14.999921 3.25241795,14.999921 L14.7541747,14.999921 Z M14.7541747,16.499921 L3.25241795,16.499921 C2.83882492,16.499921 2.50354153,16.8352044 2.50354153,17.2487975 L2.50354153,17.8264756 C2.50354153,18.3621428 2.69465071,18.8802318 3.0425094,19.2875833 C4.29581764,20.7552799 6.2616965,21.5010712 9,21.5010712 C11.7383108,21.5010712 13.7058994,20.7552449 14.9623798,19.287315 C15.3112944,18.8796824 15.5030511,18.3607764 15.5030511,17.8242084 L15.5030511,17.2487975 C15.5030511,16.8352044 15.1677677,16.499921 14.7541747,16.499921 Z M19.0539673,1.40356572 C19.4136007,1.19804993 19.8717446,1.32298689 20.0772604,1.68262022 C21.1678751,3.59109352 21.75,5.75404419 21.75,8 C21.75,10.253531 21.1639339,12.4234401 20.0663341,14.3364615 C19.8601976,14.6957394 19.4018387,14.8198852 19.0425608,14.6137487 C18.6832828,14.4076123 18.5591371,13.9492534 18.7652735,13.5899754 C19.733372,11.9026637 20.25,9.98984903 20.25,8 C20.25,6.0168344 19.7368452,4.11014998 18.7749128,2.42685873 C18.5693971,2.0672254 18.694334,1.60908151 19.0539673,1.40356572 Z M9,3.0046246 C11.7614237,3.0046246 14,5.24320085 14,8.0046246 C14,10.7660483 11.7614237,13.0046246 9,13.0046246 C6.23857625,13.0046246 4,10.7660483 4,8.0046246 C4,5.24320085 6.23857625,3.0046246 9,3.0046246 Z M15.5885024,3.39942904 C15.9485763,3.19468615 16.4064508,3.32060681 16.6111937,3.68068072 C17.3537737,4.98662919 17.75,6.46536363 17.75,8 C17.75,9.53814598 17.351956,11.020107 16.6061524,12.3281732 C16.4009899,12.6880082 15.9429689,12.813395 15.5831339,12.6082324 C15.2232989,12.4030699 15.0979122,11.9450489 15.3030747,11.5852139 C15.9206607,10.5020287 16.25,9.27586289 16.25,8 C16.25,6.72704469 15.922164,5.50354601 15.3072507,4.42212034 C15.1025078,4.06204643 15.2284285,3.60417194 15.5885024,3.39942904 Z M9,4.5046246 C7.06700338,4.5046246 5.5,6.07162798 5.5,8.0046246 C5.5,9.93762123 7.06700338,11.5046246 9,11.5046246 C10.9329966,11.5046246 12.5,9.93762123 12.5,8.0046246 C12.5,6.07162798 10.9329966,4.5046246 9,4.5046246 Z</StreamGeometry>
<StreamGeometry x:Key="stop_regular">M19.25 4.5C19.3881 4.5 19.5 4.61193 19.5 4.75V19.25C19.5 19.3881 19.3881 19.5 19.25 19.5H4.75C4.61193 19.5 4.5 19.3881 4.5 19.25V4.75C4.5 4.61193 4.61193 4.5 4.75 4.5H19.25ZM4.75 3C3.7835 3 3 3.7835 3 4.75V19.25C3 20.2165 3.7835 21 4.75 21H19.25C20.2165 21 21 20.2165 21 19.25V4.75C21 3.7835 20.2165 3 19.25 3H4.75Z</StreamGeometry>
<StreamGeometry x:Key="speaker_regular">M25.081 6.41848C26.2076 5.4071 28 6.2067 28 7.72074V40.2793C28 41.7933 26.2077 42.5929 25.081 41.5815L16.875 34.2153C16.0034 33.4328 14.8734 33 13.702 33H9C6.10051 33 3.75 30.6495 3.75 27.75V20.25C3.75 17.3505 6.10051 15 9 15H13.702C14.8734 15 16.0034 14.5672 16.875 13.7847L25.081 6.41848ZM25.5 9.40186L18.5451 15.6451C17.2146 16.8394 15.4898 17.5 13.702 17.5H9C7.48122 17.5 6.25 18.7312 6.25 20.25V27.75C6.25 29.2688 7.48122 30.5 9 30.5H13.702C15.4898 30.5 17.2146 31.1606 18.5451 32.3549L25.5 38.5982V9.40186Z M36.3376 8.39563C36.8095 7.8917 37.6005 7.86571 38.1044 8.33757L38.1053 8.3384C38.1053 8.3384 37.7675 8 38.1151 8.34762L38.1336 8.36537C38.1485 8.37976 38.1685 8.39927 38.1933 8.42389C38.2428 8.47311 38.3113 8.54277 38.396 8.63271C38.5655 8.81254 38.8004 9.07377 39.0785 9.4151C39.6344 10.0974 40.3656 11.1025 41.094 12.4203C42.5525 15.0588 44.0001 18.9526 44.0001 24C44.0001 29.0474 42.5525 32.9413 41.094 35.5797C40.3656 36.8975 39.6344 37.9027 39.0785 38.5849C38.8004 38.9262 38.5655 39.1875 38.396 39.3673C38.3714 39.3935 38.3481 39.4179 38.3262 39.4407C38.273 39.4961 38.2284 39.5412 38.1933 39.5761C38.1685 39.6007 38.1485 39.6202 38.1336 39.6346L38.1151 39.6524L38.1088 39.6584L38.1063 39.6607L38.1053 39.6616C38.1053 39.6616 38.2392 39.5277 38.3262 39.4407C38.4326 39.3343 38.4688 39.298 38.1044 39.6624C37.6005 40.1343 36.8095 40.1083 36.3376 39.6044C35.8666 39.1013 35.8917 38.3122 36.3931 37.84L36.3949 37.8383L36.3981 37.8352C36.404 37.8296 36.4148 37.819 36.4304 37.8035C36.4614 37.7726 36.5111 37.7223 36.5767 37.6526C36.708 37.5133 36.9029 37.2972 37.1404 37.0057C37.6157 36.4224 38.2595 35.54 38.9061 34.3703C40.1976 32.0338 41.5001 28.5526 41.5001 24C41.5001 19.4474 40.1976 15.9663 38.9061 13.6297C38.2595 12.46 37.6157 11.5776 37.1404 10.9943C36.9029 10.7028 36.708 10.4867 36.5767 10.3474C36.5111 10.2777 36.4614 10.2274 36.4304 10.1965C36.4148 10.181 36.404 10.1705 36.3981 10.1648L36.3949 10.1617L36.3931 10.16C35.8917 9.68786 35.8666 8.89869 36.3376 8.39563Z M32.3341 14.3994C32.8038 13.8936 33.5947 13.8643 34.1006 14.334C34.1008 14.3342 34.1014 14.3348 34.1014 14.3348L34.1023 14.3356L34.1043 14.3374L34.1092 14.3421L34.1229 14.355C34.1336 14.3653 34.1477 14.3788 34.1647 14.3955C34.1988 14.429 34.2451 14.4753 34.3018 14.5344C34.4152 14.6524 34.5707 14.8215 34.7541 15.0398C35.1202 15.4757 35.6009 16.1117 36.0798 16.9327C37.0374 18.5743 38.0001 20.9776 38.0001 24C38.0001 27.0224 37.0374 29.4257 36.0798 31.0673C35.6009 31.8883 35.1202 32.5243 34.7541 32.9602C34.5707 33.1785 34.4152 33.3476 34.3018 33.4656C34.2451 33.5247 34.1988 33.5711 34.1647 33.6045L34.1452 33.6235L34.1229 33.645L34.1092 33.6579L34.1043 33.6626L34.1015 33.6652L34.1006 33.666C33.5947 34.1357 32.8038 34.1065 32.3341 33.6006C31.8653 33.0958 31.8935 32.3072 32.3962 31.8371L32.3979 31.8355L32.414 31.8198C32.4312 31.803 32.4601 31.7741 32.4991 31.7336C32.5771 31.6524 32.695 31.5246 32.8398 31.3523C33.1299 31.0069 33.5242 30.4867 33.9203 29.8077C34.7127 28.4493 35.5001 26.4776 35.5001 24C35.5001 21.5224 34.7127 19.5507 33.9203 18.1923C33.5242 17.5133 33.1299 16.9931 32.8398 16.6477C32.695 16.4754 32.5771 16.3476 32.4991 16.2664C32.4601 16.2259 32.4312 16.197 32.414 16.1802L32.3973 16.164L32.3981 16.1647C31.8929 15.6949 31.8645 14.9051 32.3341 14.3994Z</StreamGeometry>
<StreamGeometry x:Key="info_regular">M14,2 C20.6274,2 26,7.37258 26,14 C26,20.6274 20.6274,26 14,26 C7.37258,26 2,20.6274 2,14 C2,7.37258 7.37258,2 14,2 Z M14,3.5 C8.20101,3.5 3.5,8.20101 3.5,14 C3.5,19.799 8.20101,24.5 14,24.5 C19.799,24.5 24.5,19.799 24.5,14 C24.5,8.20101 19.799,3.5 14,3.5 Z M14,11 C14.3796833,11 14.6934889,11.2821653 14.7431531,11.6482323 L14.75,11.75 L14.75,19.25 C14.75,19.6642 14.4142,20 14,20 C13.6203167,20 13.3065111,19.7178347 13.2568469,19.3517677 L13.25,19.25 L13.25,11.75 C13.25,11.3358 13.5858,11 14,11 Z M14,7 C14.5523,7 15,7.44772 15,8 C15,8.55228 14.5523,9 14,9 C13.4477,9 13,8.55228 13,8 C13,7.44772 13.4477,7 14,7 Z</StreamGeometry>
</Application.Resources>
<Application.Styles>
<FluentTheme />
Expand Down
5 changes: 2 additions & 3 deletions Mirivoice/Assets/Lang/strings-en-US.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,8 @@
<system:String x:Key="voicers.title">Voicers</system:String>
<system:String x:Key="voicers.voicer">Voicer</system:String>
<system:String x:Key="voicers.readme">Read Me: </system:String>

<system:String x:Key="voicers.description">Character</system:String>
<system:String x:Key="voicers.styles">Styles</system:String>

<system:String x:Key="voicers.styles">Styles: </system:String>
<system:String x:Key="voicers.illust">Illustrator. </system:String>

<system:String x:Key="voicers.engineer">Engineer. </system:String>
Expand Down
6 changes: 3 additions & 3 deletions Mirivoice/Engines/TTS/EngineVITS2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ public override void Inference(string ipaText, string cacheFilePath, int spkid)

// scales array
Tensor<float> scalesTensor = new DenseTensor<float>(3);
scalesTensor[0] = 0.667f; // scale
scalesTensor[1] = 1.0f; // pitch
scalesTensor[2] = 0.8f; // energy
scalesTensor[0] = 0.667f; // noise_scale
scalesTensor[1] = 1.0f; // length_scale. If increased, the speed of the speech will be faster
scalesTensor[2] = 0.8f; // noise_scale_w


var inputs = new List<NamedOnnxValue>
Expand Down
7 changes: 4 additions & 3 deletions Mirivoice/Mirivoice.Core/Format/Voicer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ public void Inference(string ipaText, string cacheFilePath, LineBoxView l)
//Log.Debug("[Infer Started] --- ipaText");
int spkid = CurrentVoicerMeta.SpeakerId;
Engine.Inference(ipaText, cacheFilePath, spkid);
l.IsCacheIsVaild = true;


if (l != null)
{
l.IsCacheIsVaild = true;
}
}


Expand Down
37 changes: 34 additions & 3 deletions Mirivoice/Mirivoice.Core/Managers/AudioManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ public class AudioManager
private int _currentFileIndex;
private LineBoxView _;
private readonly MainViewModel v;
private bool MainViewModelPlaying;
public AudioManager(MainViewModel v)
{
this.v = v;
_waveOut = new WaveOutEvent();
_waveOut.PlaybackStopped += OnPlaybackStopped; // called when playback is stopped
_audioReaders = new List<AudioFileReader>();
_currentFileIndex = 0;
MainViewModelPlaying = false;
}

public string SaveToCache(byte[] audioData)
Expand Down Expand Up @@ -78,6 +80,7 @@ List<string> GetAllCacheFiles()
/// <param name="DirPath"></param>
public async void PlayAllCacheFiles(int startIndex, bool exportOnlyAndDoNotPlay=false, bool exportPerTrack=true, string fileName="", string DirPath="")
{
MainViewModelPlaying = true;
if ( _waveOut != null && _waveOut.PlaybackState == PlaybackState.Paused)
{
_waveOut.Play();
Expand Down Expand Up @@ -225,7 +228,29 @@ private void PlayNextFile()
}
}


public void PlayAudio(string cacheFilePath)
{
if (MainViewModelPlaying)
{
return;
}
MainViewModelPlaying = false;
if (File.Exists(cacheFilePath))
{
var reader = new AudioFileReader(cacheFilePath);
if (_waveOut.PlaybackState == PlaybackState.Playing)
{
_waveOut.Stop();
}
_waveOut.Init(reader);
_waveOut.Play();
}
else
{
Log.Error($"Cache file not found: {cacheFilePath}");
}
}

private void OnPlaybackStopped(object sender, StoppedEventArgs e)
{
if (_audioReaders.Count == 0) // when stopped
Expand All @@ -235,6 +260,10 @@ private void OnPlaybackStopped(object sender, StoppedEventArgs e)
{
_waveOut.Dispose();
}
if (!MainViewModelPlaying)
{
return;
}
v.LineBoxCollection[currentLine].viewModel.IsSelected = false;
v.LineBoxCollection[SelectedBtnIndexBeforePlay].viewModel.IsSelected = true;
v.LinesViewerOffset = new Avalonia.Vector(0, OffsetBeforePlay);
Expand All @@ -244,6 +273,7 @@ private void OnPlaybackStopped(object sender, StoppedEventArgs e)
return;
}
_audioReaders[_currentFileIndex].Dispose();

v.LineBoxCollection[currentLine].viewModel.IsSelected = false;
v.LinesViewerOffset = new Avalonia.Vector(0, v.LinesViewerOffset.Y + 104);

Expand All @@ -265,6 +295,7 @@ private void OnPlaybackStopped(object sender, StoppedEventArgs e)
v.LinesViewerOffset = new Avalonia.Vector(0, OffsetBeforePlay);
v.MainWindowGetInput = true;
v.StopButtonEnabled = false;
MainViewModelPlaying = false;
}
}

Expand All @@ -286,8 +317,8 @@ public void StopAudio()
_waveOut.Pause();
_currentFileIndex = 0;
_audioReaders.Clear();
_waveOut.Stop();

_waveOut.Stop();
_waveOut.Dispose();
}

}
Expand Down
36 changes: 33 additions & 3 deletions Mirivoice/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,32 @@ public void Redo()

public void ClearCache()
{
MainManager.Instance.AudioM.ClearCache();
string cachePath = MainManager.Instance.PathM.CachePath;
if (Directory.Exists(cachePath))
{
foreach (string file in Directory.GetFiles(cachePath))
{
try
{
File.Delete(file);
}
catch (Exception e)
{
Log.Error($"Failed to delete cache file: {file} {e.Message}");
}
}
}
}

public void PlayAudio(string path)
{
MainManager.Instance.AudioM.PlayAudio(path);
}

public void StopAudio()
{
MainManager.Instance.AudioM.StopAudio();
}
public void OnAddButtonClick()
{
MainManager.Instance.DefaultVoicerIndex = voicerSelector.CurrentDefaultVoicerIndex;
Expand Down Expand Up @@ -578,8 +600,16 @@ public async void WindowClosing(object? sender, WindowClosingEventArgs e)
{
if (MainManager.Instance.Setting.ClearCacheOnQuit)
{
Log.Information("Clearing cache...");
ClearCache();
MainManager.Instance.AudioM.StopAudio();
try
{
Log.Information("Clearing cache...");
ClearCache();
}
catch (Exception ex)
{
Log.Error($"Failed to clear cache: {ex.Message}");
}
Log.Information("Cache cleared.");
}
return;
Expand Down
71 changes: 71 additions & 0 deletions Mirivoice/ViewModels/VoicersStyleBoxViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ReactiveUI;
namespace Mirivoice.ViewModels
{
public class VoicersStyleBoxViewModel : ViewModelBase
{
private bool _isDescOpen = false;
public bool IsDescOpen
{
get => _isDescOpen;
set
{
this.RaiseAndSetIfChanged(ref _isDescOpen, value);
OnPropertyChanged(nameof(IsDescOpen));
}
}

private bool _isDescSelected = false;
public bool IsDescSelected
{
get => _isDescSelected;
set
{
this.RaiseAndSetIfChanged(ref _isDescSelected, value);
OnPropertyChanged(nameof(IsDescSelected));
}
}

private string _descText;
public string DescText
{
get => _descText;
set
{
this.RaiseAndSetIfChanged(ref _descText, value);
OnPropertyChanged(nameof(DescText));
}
}

private string _styleName;
public string StyleName
{
get => _styleName;
set
{
this.RaiseAndSetIfChanged(ref _styleName, value);
OnPropertyChanged(nameof(StyleName));
}
}
public VoicersStyleBoxViewModel()
{
}


public void OnDescButtonClick()
{
if (!IsDescSelected)
{
IsDescOpen = false;
}
else
{
IsDescOpen = true;
}
}
}
}
18 changes: 15 additions & 3 deletions Mirivoice/ViewModels/VoicersVoicerButtonViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@
using Avalonia.Media;
using Avalonia.Platform;
using Mirivoice.Mirivoice.Core.Editor;

using System.Collections.ObjectModel;
using System.IO;
using Avalonia.Media.Imaging;
using Mirivoice.Engines;

namespace Mirivoice.ViewModels
{
public class VoicersVoicerButtonViewModel: ViewModelBase
{
private readonly VoicersWindowViewModel v;
private readonly MainViewModel mv;
private bool _isSelected = false;
public bool IsSelected
{
Expand Down Expand Up @@ -48,12 +50,12 @@ public ImageBrush Icon
OnPropertyChanged(nameof(Icon));
}
}
public VoicersVoicerButtonViewModel(Voicer voicer, VoicersWindowViewModel v)
public VoicersVoicerButtonViewModel(Voicer voicer, VoicersWindowViewModel v, MainViewModel mv)
{
Voicer = voicer;
this.v = v;
VoicerInfo vInfo = Voicer.Info;

this.mv = mv;
LangCode = vInfo.LangCode.ToUpper();

if (vInfo.Icon != null && vInfo.Icon != string.Empty)
Expand Down Expand Up @@ -96,6 +98,16 @@ public void OnButtonClick()
}
IsSelected = true;

List<VoicersStyleBox> voicersStyleBoxes = new List<VoicersStyleBox>();
int i = 0;
VoicerMeta[] voicerMetas = Voicer.VoicerMetaCollection.ToArray();
foreach (var meta in voicerMetas )
{
voicersStyleBoxes.Add(new VoicersStyleBox(Voicer, i, mv));
++i;
}
v.VoicersStyleBoxes = new ObservableCollection<VoicersStyleBox>(voicersStyleBoxes);
v.OnPropertyChanged(nameof(v.VoicersStyleBoxes));
v.Name = Voicer.Info.Name;
v.Voice = Voicer.Info.Voice;
v.Author = Voicer.Info.Author;
Expand Down
6 changes: 5 additions & 1 deletion Mirivoice/ViewModels/VoicersWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace Mirivoice.ViewModels
public class VoicersWindowViewModel: ViewModelBase
{
public ObservableCollection<VoicersVoicerButton> VoicersVoicerButtons { get; set; }
public ObservableCollection<VoicersStyleBox> VoicersStyleBoxes { get; set; }
private string _name;
public string Name
{
Expand Down Expand Up @@ -142,9 +143,12 @@ public string Readme
public VoicersWindowViewModel(MainViewModel v)
{
List<VoicersVoicerButton> voicersVoicerButtons = new List<VoicersVoicerButton>();

foreach (var voicer in v.voicerSelector.Voicers)
{
voicersVoicerButtons.Add(new VoicersVoicerButton(voicer, this));

voicersVoicerButtons.Add(new VoicersVoicerButton(voicer, this, v));

}

VoicersVoicerButtons = new ObservableCollection<VoicersVoicerButton>(voicersVoicerButtons);
Expand Down
Loading

0 comments on commit c918e52

Please sign in to comment.