VPet.ModMaker/VPet.Plugin.EdgeTTS/EdgeTTS.cs

102 lines
3.2 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VPet_Simulator.Windows.Interface;
using EdgeTTS;
using LinePutScript.Converter;
using LinePutScript;
using System.IO;
using VPet_Simulator.Core;
2023-05-25 17:50:18 +00:00
using System.Windows.Controls;
using System.Windows;
2023-08-08 14:17:05 +00:00
using LinePutScript.Localization.WPF;
namespace VPet.Plugin.VPetTTS
{
2023-05-25 17:50:18 +00:00
public class EdgeTTS : MainPlugin
{
2023-05-25 17:50:18 +00:00
public EdgeTTSClient etts;
public Setting Set;
public EdgeTTS(IMainWindow mainwin) : base(mainwin)
{
2023-05-25 17:50:18 +00:00
etts = new EdgeTTSClient();
}
public override void LoadPlugin()
{
2023-05-25 17:50:18 +00:00
var line = MW.Set.FindLine("EdgeTTS");
if (line == null)
{
Set = new Setting();
}
else
{
Set = LPSConvert.DeserializeObject<Setting>(line);
}
if (!Directory.Exists(GraphCore.CachePath + @"\voice"))
Directory.CreateDirectory(GraphCore.CachePath + @"\voice");
2023-05-25 17:50:18 +00:00
if (Set.Enable)
MW.Main.OnSay += Main_OnSay;
2023-07-03 12:17:02 +00:00
2023-08-08 14:17:05 +00:00
//根据当前语言选择合适的默认发音人
if (string.IsNullOrEmpty(Set.Speaker))
if ("EdgeTTSSpeaker".Translate() == "EdgeTTSSpeaker")
Set.Speaker = "en-US-AnaNeural";
else
Set.Speaker = "EdgeTTSSpeaker".Translate();
MenuItem modset = MW.Main.ToolBar.MenuMODConfig;
2023-07-03 12:17:02 +00:00
modset.Visibility = Visibility.Visible;
var menuItem = new MenuItem()
{
Header = "EdgeTTS",
HorizontalContentAlignment = HorizontalAlignment.Center,
};
menuItem.Click += (s, e) => { Setting(); };
modset.Items.Add(menuItem);
2023-05-25 17:50:18 +00:00
}
2023-07-03 12:17:02 +00:00
//public override void LoadDIY()
//{
// MW.Main.ToolBar.AddMenuButton(VPet_Simulator.Core.ToolBar.MenuType.DIY, "EdgeTTS", Setting);
//}
2023-05-27 10:33:24 +00:00
public void Main_OnSay(string saythings)
{//说话语音
var path = GraphCore.CachePath + $"\\voice\\{Sub.GetHashCode(saythings):X}.mp3";
if (File.Exists(path))
{
2023-05-25 17:50:18 +00:00
MW.Main.PlayVoice(new Uri(path));
}
else
{
var res = etts.SynthesisAsync(saythings, Set.Speaker, Set.PitchStr, Set.RateStr).Result;
if (res.Code == ResultCode.Success)
{
FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
BinaryWriter w = new BinaryWriter(fs);
w.Write(res.Data.ToArray());
fs.Close();
fs.Dispose();
w.Dispose();
2023-05-25 17:50:18 +00:00
MW.Main.PlayVoice(new Uri(path));
}
2023-05-25 17:50:18 +00:00
}
}
2023-05-25 17:50:18 +00:00
public winSetting winSetting;
public override void Setting()
{
if (winSetting == null)
{
winSetting = new winSetting(this);
winSetting.Show();
}
else
{
winSetting.Topmost = true;
}
}
public override string PluginName => "EdgeTTS";
}
}