/
BSVPlugin.cs
713 lines (591 loc) · 16.3 KB
/
BSVPlugin.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
using BilibiliDM_PluginFramework;
using Force.Crc32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace BulletScreenVoice
{
public class BSVPlugin: DMPlugin
{
class TTSTask
{
internal enum State
{
Queuing,
Translating,
Translated,
Playing,
Fin
}
internal State state;
internal string text;
internal string filePath;
}
public static BSVPlugin instance;
string baseDir;
string pluginDir;
string dllDir;
public string voicesDir;
string configFilePath;
Config config;
bool needInitAudio = true;
bool needInitTTS = true;
string roomId;
List<TTSTask> taskQueue = new List<TTSTask>();
public BSVPlugin()
{
instance = this;
this.PluginName = "弹幕语音播报";
this.PluginVer = "v0.0.2";
this.PluginDesc = "读出观众发送的弹幕文字";
this.PluginAuth = "毘耶离";
this.PluginCont = "dolaham@qq.com";
this.Connected += BSVPlugin_Connected;
this.Disconnected += BSVPlugin_Disconnected;
this.ReceivedDanmaku += BSVPlugin_ReceivedDanmaku;
this.ReceivedRoomCount += BSVPlugin_ReceivedRoomCount;
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
baseDir = Path.GetDirectoryName(path) + "\\";
pluginDir = baseDir + "bsv\\";
dllDir = pluginDir + "dll\\";
loadDlls();
voicesDir = pluginDir + "voices\\";
configFilePath = pluginDir + "config.json";
config = Config.load(configFilePath);
if (config == null)
{
config = new Config();
}
if (!AudioService.findAudioDevice(config.audioDeviceId) && AudioService.allDevices.Length > 0)
{
config.audioDeviceId = AudioService.allDevices[0];
}
UserService.init();
}
bool loadDlls()
{
string[] dllFilenames = {
"Crc32.NET.dll"
, "NAudio.dll"
, "Pathoschild.Http.Client.dll"
, "System.Net.Http.Formatting.dll"
, "System.Text.Encodings.Web.dll"
, "System.Web.Http.dll"
, "System.Web.Http.WebHost.dll"
, "TencentCloud.dll"
};
string curLoadingDllFilename = "";
string curLoadingDllFullFilePath = "";
try
{
for(int i = 0; i < dllFilenames.Length; ++i)
{
curLoadingDllFilename = dllFilenames[i];
curLoadingDllFullFilePath = dllDir + curLoadingDllFilename;
Assembly.LoadFrom(curLoadingDllFullFilePath);
}
}
catch (Exception e)
{
Log(e.Message);
Log(string.Format("加载 {0} 时发生错误,请确定以下路径的文件存在:{1}", curLoadingDllFilename, curLoadingDllFullFilePath));
return false;
}
return true;
}
// 连接至直播间
private void BSVPlugin_Connected(object sender, BilibiliDM_PluginFramework.ConnectedEvtArgs e)
{
roomId = e.roomid.ToString();
if (config.readConnect)
{
string str = config.templateConnect.Replace("{roomId}", roomId);
addTTSTask(str);
}
}
// 与直播间断开连接
private void BSVPlugin_Disconnected(object sender, BilibiliDM_PluginFramework.DisconnectEvtArgs e)
{
if (config.readDisconnect)
{
string errMsg = e?.Error?.Message;
string str = config.templateDisconnect.Replace("{err}", errMsg);
addTTSTask(str);
}
}
// 插件启用
public override void Start()
{
base.Start();
//請勿使用任何阻塞方法
needInitAudio = true;
needInitTTS = true;
}
// 插件停用
public override void Stop()
{
base.Stop();
//請勿使用任何阻塞方法
AudioService.playStoppedEvent -= onPlayFinished;
AudioService.uninit();
TTSService.uninit();
}
// 打开插件设置界面
public override void Admin()
{
base.Admin();
AdminForm form = new AdminForm();
form.displayConfig(config);
DialogResult result = form.ShowDialog();
Log("Admin: " + result);
if (result == DialogResult.OK)
{
if (!Directory.Exists(pluginDir))
{
Directory.CreateDirectory(pluginDir);
}
string oldAudioDeviceGuid = config.audioDeviceId;
form.grabConfig(config);
Config.save(config, configFilePath);
if (oldAudioDeviceGuid != config.audioDeviceId)
{
needInitAudio = true;
}
if (config.useCustomSecret)
{
needInitTTS = true;
}
}
}
// 收到直播间人气数据
private void BSVPlugin_ReceivedRoomCount(object sender, BilibiliDM_PluginFramework.ReceivedRoomCountArgs e)
{
}
// 上次弹幕用户id
long lastSayUserId;
// 上次弹幕时间
DateTime lastSayTime;
// 上次礼物文本模板
string lastGiftTemplate;
// 上次礼物的用户id
long lastGiftUserId;
string lastGiftUserName;
// 上次礼物名称
string lastGiftName;
// 上次礼物数量
int lastGiftCount;
// 收到弹幕、礼物等消息
private void BSVPlugin_ReceivedDanmaku(object sender, BilibiliDM_PluginFramework.ReceivedDanmakuArgs e)
{
DanmakuModel dm = e.Danmaku;
long userId = dm.UserID_long;
switch(dm.MsgType)
{
case MsgTypeEnum.LiveStart: // 直播开始
if (config.readLiveBegin)
{
string str = makeStringFromTemplate(dm, config.templateLiveBegin);
addTTSTask(str);
}
break;
case MsgTypeEnum.LiveEnd: // 直播结束
if (config.readLiveEnd)
{
string str = makeStringFromTemplate(dm, config.templateLiveEnd);
addTTSTask(str);
}
break;
case MsgTypeEnum.Welcome: // 有观众进入直播间
{
UserService.UserInfo userInfo = UserService.getOrCreateUserInfo(userId);
userInfo.isMilord = dm.isVIP;
int userCfgIndex = (int)getUserConfigIndex(userInfo);
Config.UserConfig userCfg = config.userConfigs[userCfgIndex];
if (userCfg.readWelcome)
{
string str = makeStringFromTemplate(dm, userCfg.templateWelcome);
addTTSTask(str);
}
}
break;
case MsgTypeEnum.WelcomeGuard: // 有普通观众或船员进入直播间
{
UserService.UserInfo userInfo = UserService.getOrCreateUserInfo(userId);
userInfo.level = (UserLevel)dm.UserGuardLevel;
int userCfgIndex = (int)getUserConfigIndex(userInfo);
Config.UserConfig userCfg = config.userConfigs[userCfgIndex];
if (userCfg.readWelcome)
{
string str = makeStringFromTemplate(dm, userCfg.templateWelcome);
addTTSTask(str);
}
}
break;
case MsgTypeEnum.Comment: // 弹幕文本
{
int userCfgIndex = dm.UserGuardLevel;
if(userCfgIndex == (int)UserLevel.Common)
{
UserService.UserInfo userInfo = UserService.getUserInfo(userId);
userCfgIndex = (int)getUserConfigIndex(userInfo);
}
Config.UserConfig userCfg = config.userConfigs[userCfgIndex];
if(userCfg.readText)
{
string str = null;
if(dm.UserID_long == lastSayUserId)
{
// 本次弹幕的用户与上一个弹幕的用户是同一个
TimeSpan ts = DateTime.Now - lastSayTime;
if(ts.TotalSeconds < config.nameInterval)
{
// 弹幕间隔小于指定值,不添加“用户说”前缀
str = dm.CommentText;
}
}
if(string.IsNullOrEmpty(str))
{
str = makeStringFromTemplate(dm, userCfg.templateText);
}
addTTSTask(str);
lastSayUserId = dm.UserID_long;
lastSayTime = DateTime.Now;
}
}
break;
case MsgTypeEnum.GiftSend: // 收到礼物
{
UserService.UserInfo userInfo = UserService.getUserInfo(userId);
int userCfgIndex = (int)getUserConfigIndex(userInfo);
Config.UserConfig userCfg = config.userConfigs[userCfgIndex];
if(userCfg.readGift)
{
if(dm.UserID_long == lastGiftUserId && dm.GiftName == lastGiftName)
{
// 礼物用户与上一次礼物用户是同一个、且礼物相同,则合并礼物数量
lastGiftCount += dm.GiftCount;
// 延迟一段时间再一次性读出礼物信息
makeDelayGiftVoiceTimer(1);
}
else
{
// 礼物用户与上次用户不同、或者礼物不同
if(lastGiftUserId != 0)
{
// 还有待读的礼物信息,则现在读出
clearDelayGiftVoiceTimer();
string str = makeStringFromTemplate(lastGiftTemplate, lastGiftUserName, "", lastGiftName, lastGiftCount, "");
addTTSTask(str);
}
// 缓存礼物信息
lastGiftTemplate = userCfg.templateGift;
lastGiftUserId = dm.UserID_long;
lastGiftUserName = dm.UserName;
lastGiftName = dm.GiftName;
lastGiftCount = dm.GiftCount;
// 延迟一段时间后再读出
makeDelayGiftVoiceTimer(1);
}
}
}
break;
case MsgTypeEnum.GuardBuy: // 购买船票
{
int userCfgIndex = dm.UserGuardLevel;
if (userCfgIndex == (int)UserLevel.Common)
{
UserService.UserInfo userInfo = UserService.getUserInfo(userId);
userCfgIndex = (int)getUserConfigIndex(userInfo);
}
Config.UserConfig userCfg = config.userConfigs[userCfgIndex];
if(userCfg.readTicket)
{
string str = makeStringFromTemplate(dm, userCfg.templateTicket);
addTTSTask(str);
}
}
break;
}
// 互动消息
switch(dm.InteractType)
{
case InteractTypeEnum.Follow:
{
// 关注
if(config.bReadFollow)
{
string str = makeStringFromTemplate(dm, config.templateFollow);
addTTSTask(str);
}
}
break;
}
}
System.Timers.Timer timerDelayGiftVoice;
void clearDelayGiftVoiceTimer()
{
if(timerDelayGiftVoice != null)
{
timerDelayGiftVoice.Stop();
timerDelayGiftVoice.Close();
timerDelayGiftVoice = null;
}
}
void makeDelayGiftVoiceTimer(float delay)
{
clearDelayGiftVoiceTimer();
timerDelayGiftVoice = new System.Timers.Timer();
timerDelayGiftVoice.AutoReset = false;
timerDelayGiftVoice.Interval = (int)(delay * 1000);
timerDelayGiftVoice.Elapsed += DelayGiftVoiceFunc;
timerDelayGiftVoice.Start();
}
void DelayGiftVoiceFunc(object sender, EventArgs e)
{
if(lastGiftUserId != 0)
{
string str = makeStringFromTemplate(lastGiftTemplate, lastGiftUserName, "", lastGiftName, lastGiftCount, "");
addTTSTask(str);
lastGiftUserId = 0;
lastGiftUserName = "";
lastGiftName = "";
lastGiftCount = 0;
}
clearDelayGiftVoiceTimer();
}
static UserConfigIndex getUserConfigIndex(int id)
{
UserService.UserInfo userInfo = UserService.getUserInfo(id);
return getUserConfigIndex(userInfo);
}
static UserConfigIndex getUserConfigIndex(UserService.UserInfo userInfo)
{
if(userInfo == null)
{
return UserConfigIndex.Common;
}
if(userInfo.level != UserLevel.Common)
{
return (UserConfigIndex)userInfo.level;
}
if(userInfo.isAdmin)
{
return UserConfigIndex.Admin;
}
else if(userInfo.isMilord)
{
return UserConfigIndex.Milord;
}
return UserConfigIndex.Common;
}
// 从模板构造字符串
string makeStringFromTemplate(DanmakuModel dm, string template)
{
string userName = dm.UserName;
if(string.IsNullOrEmpty(userName))
{
UserService.UserInfo userInfo = UserService.getUserInfo(dm.UserID_long);
if(userInfo != null)
{
userName = userInfo.name;
}
}
string roomId = dm.roomID;
if(string.IsNullOrEmpty(roomId))
{
roomId = this.roomId;
}
return makeStringFromTemplate(template, userName, dm.CommentText, dm.GiftName, dm.GiftCount, roomId);
}
string makeStringFromTemplate(string template, string userName, string commentText, string giftName, int giftCount, string roomId)
{
string str = template.Replace("{user}", userName);
str = str.Replace("{text}", commentText);
str = str.Replace("{gift}", giftName);
str = str.Replace("{count}", giftCount.ToString());
str = str.Replace("{roomId}", roomId);
return str;
}
// 添加一个文本转语音任务
void addTTSTask(string text)
{
if(string.IsNullOrEmpty(text) || string.IsNullOrWhiteSpace(text))
{
return;
}
TTSTask task = new TTSTask() { state = TTSTask.State.Queuing, text = text };
byte[] bytes = Encoding.UTF8.GetBytes(text);
uint crc32 = Crc32Algorithm.Compute(bytes);
task.filePath = string.Format("{0}{1}_{2}_{3}_{4}.mp3", voicesDir, crc32, config.volume, config.speed, config.voiceType);
taskQueue.Add(task);
updateTaskQueue();
}
// 开始文本转语音
void translateTTSTask(TTSTask task)
{
task.state = TTSTask.State.Translating;
if(needInitTTS)
{
needInitTTS = false;
TTSService.uninit();
string secretId = config.useCustomSecret ? config.secretId : TencentSecret.secretId;
string secretKey = config.useCustomSecret ? config.secretKey : TencentSecret.secretKey;
TTSService.init(secretId, secretKey);
}
TTSService.translate(task.text, config.volume, (int)config.speed, (int)config.voiceType, (text, audioData, err) =>
{
onTranslateFinished(err, task, audioData);
});
}
// 文本转语音完毕
void onTranslateFinished(string err, TTSTask task, byte[] audioData)
{
if(string.IsNullOrEmpty(err))
{
// 没有错误,转换成功
try
{
if (File.Exists(task.filePath))
{
File.Delete(task.filePath);
}
if(!Directory.Exists(pluginDir))
{
Directory.CreateDirectory(pluginDir);
}
if(!Directory.Exists(voicesDir))
{
Directory.CreateDirectory(voicesDir);
}
// 声音数据写入文件
File.WriteAllBytes(task.filePath, audioData);
foreach(TTSTask itTask in taskQueue)
{
if(itTask.filePath == task.filePath)
{
itTask.state = TTSTask.State.Translated;
}
}
}
catch (Exception e)
{
Log(e.Message);
task.state = TTSTask.State.Fin;
}
}
else
{
// 有错误,转换失败
Log(err);
// 出错的任务直接设为 fin
task.state = TTSTask.State.Fin;
}
translatingTasks.Remove(task.filePath);
updateTaskQueue();
}
// 播放文本语音
void playTTSTask(TTSTask task)
{
task.state = TTSTask.State.Playing;
if (needInitAudio)
{
needInitAudio = false;
AudioService.playStoppedEvent -= onPlayFinished;
AudioService.uninit();
AudioService.init(config);
AudioService.playStoppedEvent += onPlayFinished;
}
AudioService.playAudio(task.filePath);
}
// 播放完毕
void onPlayFinished()
{
foreach(TTSTask task in taskQueue)
{
if(task.state == TTSTask.State.Playing)
{
task.state = TTSTask.State.Fin;
}
}
updateTaskQueue();
}
HashSet<string> translatingTasks = new HashSet<string>();
// 更新文本转语音任务队列
void updateTaskQueue()
{
for(int i = 0; i < taskQueue.Count; )
{
TTSTask task = taskQueue[i];
bool removed = false;
switch(task.state)
{
case TTSTask.State.Queuing:
{
if(File.Exists(task.filePath))
{
task.state = TTSTask.State.Translated;
if (i == 0)
{
playTTSTask(task);
}
}
else
{
if(!translatingTasks.Contains(task.filePath))
{
translatingTasks.Add(task.filePath);
translateTTSTask(task);
}
}
}
break;
case TTSTask.State.Translating:
break;
case TTSTask.State.Translated:
if(i == 0)
{
playTTSTask(task);
}
break;
case TTSTask.State.Playing:
break;
case TTSTask.State.Fin:
taskQueue.RemoveAt(i);
removed = true;
break;
}
if(!removed)
{
++i;
}
}
}
public void deleteVoices()
{
try
{
if (Directory.Exists(voicesDir))
{
string[] files = Directory.GetFiles(voicesDir, "*.*");
foreach (string file in files)
{
File.Delete(file);
}
Directory.Delete(voicesDir);
}
}
catch (Exception e)
{
Log(e.Message);
}
}
}
}