jam134 commited on
Commit
e93e0d6
·
verified ·
1 Parent(s): 54f85f8

Create test.py

Browse files
Files changed (1) hide show
  1. bots/test.py +66 -0
bots/test.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import discord
2
+ import sys
3
+ import soundfile as sf
4
+ import numpy as np
5
+ from tools.i18n.i18n import I18nAuto
6
+ from GPT_SoVITS.test_webui import change_gpt_weights, change_sovits_weights, get_tts_wav
7
+ import os
8
+ from discord.ext import commands
9
+
10
+ client = commands.Bot(command_prefix='!')
11
+
12
+
13
+ i18n = I18nAuto()
14
+
15
+ def remove_silence(audio_data, threshold=0.01):
16
+ audio_data = np.array(audio_data)
17
+ non_silent_indices = np.where(np.abs(audio_data) > threshold)[0]
18
+ if non_silent_indices.size > 0:
19
+ start_index = non_silent_indices[0]
20
+ end_index = non_silent_indices[-1] + 1
21
+ return audio_data[start_index:end_index]
22
+ return audio_data
23
+
24
+ async def synthesize_audio(gpt_model_path, sovits_model_path, ref_audio_path, ref_text, target_text, output_path, language_combobox, language_combobox_02):
25
+ change_gpt_weights(gpt_path=gpt_model_path)
26
+ change_sovits_weights(sovits_path=sovits_model_path)
27
+
28
+ synthesis_result = get_tts_wav(ref_wav_path=ref_audio_path,
29
+ prompt_text=ref_text,
30
+ prompt_language=language_combobox,
31
+ text=target_text,
32
+ text_language=language_combobox_02)
33
+
34
+ result_list = list(synthesis_result)
35
+ if result_list:
36
+ last_sampling_rate, last_audio_data = result_list[-1]
37
+ last_audio_data = remove_silence(last_audio_data)
38
+ output_wav_path = os.path.join(output_path, "output.wav")
39
+ sf.write(output_wav_path, last_audio_data, last_sampling_rate)
40
+ return output_wav_path
41
+ return None
42
+
43
+ @client.command(name="say")
44
+ async def synthesize(ctx, *, target_text: str):
45
+
46
+ ref_text = "幽魂盯上你了,進入射擊位置"
47
+ output_path = 'GPT_SoVITS/output'
48
+ gpt_model_path = 'GPT_SoVITS/pretrained_models/s1bert25hz-2kh-longer-epoch=68e-step=50232.ckpt'
49
+ sovits_model_path = 'GPT_SoVITS/pretrained_models/s2G488k.pth'
50
+ ref_audio_path = 'GPT_SoVITS/pretrained_models/Ana.wav'
51
+ language_combobox = i18n("中文")
52
+ language_combobox_02 = i18n("多语种混合")
53
+
54
+ output_wav_path = await synthesize_audio(gpt_model_path, sovits_model_path, ref_audio_path, ref_text, target_text, output_path, language_combobox, language_combobox_02)
55
+
56
+ if output_wav_path:
57
+ if ctx.author.voice:
58
+ channel = ctx.author.voice.channel
59
+ voice_client = await channel.connect()
60
+
61
+ voice_client.play(discord.FFmpegPCMAudio(output_wav_path), after=lambda e: print('播放完成', e))
62
+ await ctx.send("正在播放生成的音頻...")
63
+ else:
64
+ await ctx.send("請先進入語音頻道!")
65
+ else:
66
+ await ctx.send("合成失敗!")