-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResourcePool.cs
143 lines (125 loc) · 4.78 KB
/
ResourcePool.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using SpeechGenerator.Handller;
using SpeechGenerator.Models;
using System;
using System.Threading;
using System.Threading.Tasks;
using Timer = System.Timers.Timer;
namespace SpeechGenerator
{
/// <summary>
/// 程序运行资源池,所有共享的配置、资源和线程,都放在了此处
/// </summary>
public class ResourcePool
{
/// <summary>
/// 用来转换语音的线程
/// </summary>
private static CancellationTokenSource cts;
private static int retryTimenow = 0;
/// <summary>
/// 计时器
/// </summary>
private static Timer RetryTimer = new Timer(1000);
private static int Backwards;
/// <summary>
/// 加载预置的支持的语言格式
/// </summary>
public static SpeechResource SpeechResource = null;
/// <summary>
/// 程序配置
/// </summary>
public static Config Config = null;
/// <summary>
/// 文本资源
/// </summary>
public static TextResource TextResource = new TextResource();
static ResourcePool()
{
SpeechResource = SpeechResource.LoadSpeechResources();
Config = Config.LoadConfig();
RetryTimer.AutoReset = true;
RetryTimer.Elapsed += (sender, e) => { TitleChange?.Invoke($"连接受限,{--Backwards}秒后尝试第({retryTimenow}/{Config.RetryTime})次重试", 0); };
Backwards = Config.RetryInterval / 1000;
}
/// <summary>
/// 终止转换
/// </summary>
public static void AbordTask()
{
cts.Cancel();
TitleChange?.Invoke("OpenTTS", 0);
TimerReset();
}
/// <summary>
/// 转换单个资源,用于试听按钮
/// </summary>
/// <param name="item"></param>
public static async Task<Result> AuditionAsync(TextItem item)
{
return await SpeechConverter.CreateAudioFromTextAsync(item);
}
public static async Task<Result> StartConvertingAsync()
{
var result = new Result();
cts = new CancellationTokenSource();
await Task.Run(() => { result = ConvertTextToSpeachAsync(cts); });
return result;
}
private static Result ConvertTextToSpeachAsync(CancellationTokenSource cts)
{
try
{
foreach (var item in TextResource)
{
retryTimenow = 0;
cts.Token.ThrowIfCancellationRequested();
TitleChange?.Invoke($"({TextResource.IndexOf(item)}/{TextResource.Count})转换中...", TextResource.IndexOf(item));
if (item.IsProcessed)
continue;
var covRes = SpeechConverter.CreateAudioFileFromText(TextResource.DicName, item);
if (!covRes.Success)
{
//失败,进入重试
for (int i = 0; i < Config.RetryTime; i++)
{
retryTimenow++;
cts.Token.ThrowIfCancellationRequested();
RetryTimer.Start();
Thread.Sleep(Config.RetryInterval);
TimerReset();
covRes = SpeechConverter.CreateAudioFileFromText(TextResource.DicName, item);
}
if (!covRes.Success)
return new Result { Success = false, Message = $"重试{Config.RetryTime}次依然无法生成该条语音,\r\n建议检查格式,\r\n并重启程序再试" };
}
TextRowChanged?.Invoke(item, TextResource.IndexOf(item));
item.IsProcessed = true;
}
}
catch (OperationCanceledException)
{
return Result.Fail("任务已停止");
}
finally
{
TitleChange?.Invoke("OpenTTS", TextResource.Count);
}
return Result.Sucess("转换完成");
}
private static void TimerReset()
{
RetryTimer.Stop();
Backwards = Config.RetryInterval / 1000;
}
public delegate void PollDelegate(object sender, int index);
public delegate void PollFailDelegate(object sender, string message);
/// <summary>
/// 每个文本资源处理完成后触发
/// </summary>
public static event PollDelegate TextRowChanged;
/// <summary>
/// 通过该事件修改窗口的标题
/// </summary>
public static event PollDelegate TitleChange;
}
}