File size: 3,267 Bytes
e93e0d6
9c10885
e93e0d6
 
 
 
 
 
f74609a
e93e0d6
c5fc1de
 
 
 
 
 
e93e0d6
 
 
 
 
 
 
 
 
 
 
 
 
9c10885
 
e93e0d6
e2d6c22
 
 
 
 
e93e0d6
 
 
 
 
e2d6c22
 
 
e93e0d6
 
 
 
e2d6c22
e93e0d6
 
 
 
 
 
 
 
 
 
9c10885
e93e0d6
9c10885
 
 
 
 
 
f74609a
9c10885
 
 
 
e93e0d6
9c10885
 
e2d6c22
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
import discord
import sys
import soundfile as sf
import numpy as np
from tools.i18n.i18n import I18nAuto
from GPT_SoVITS.test_webui import change_gpt_weights, change_sovits_weights, get_tts_wav
import os
from discord.ext import commands
import asyncio

# 創建 intents 實例
intents = discord.Intents.default()
intents.messages = True

# 初始化 Bot,並傳入 intents
client = commands.Bot(command_prefix='!', intents=intents)

i18n = I18nAuto()

def remove_silence(audio_data, threshold=0.01):
    audio_data = np.array(audio_data)
    non_silent_indices = np.where(np.abs(audio_data) > threshold)[0]
    if non_silent_indices.size > 0:
        start_index = non_silent_indices[0]
        end_index = non_silent_indices[-1] + 1
        return audio_data[start_index:end_index]
    return audio_data

async def synthesize_audio(gpt_model_path, sovits_model_path, ref_audio_path, ref_text, target_text, output_path, language_combobox, language_combobox_02):
    await asyncio.to_thread(change_gpt_weights, gpt_path=gpt_model_path)
    await asyncio.to_thread(change_sovits_weights, sovits_path=sovits_model_path)

    synthesis_result = await asyncio.to_thread(get_tts_wav, ref_wav_path=ref_audio_path, 
                                                prompt_text=ref_text, 
                                                prompt_language=language_combobox, 
                                                text=target_text, 
                                                text_language=language_combobox_02)

    result_list = list(synthesis_result)
    if result_list:
        last_sampling_rate, last_audio_data = result_list[-1]
        last_audio_data = remove_silence(last_audio_data)
        
        if not os.path.exists(output_path):
            os.makedirs(output_path)
        output_wav_path = os.path.join(output_path, "output.wav")
        sf.write(output_wav_path, last_audio_data, last_sampling_rate)
        return output_wav_path
    return None

@client.command(name="say")
async def synthesize(ctx, *, target_text: str):
    ref_text = "幽魂盯上你了,進入射擊位置"
    output_path = 'GPT_SoVITS/output'
    gpt_model_path = 'GPT_SoVITS/pretrained_models/s1bert25hz-2kh-longer-epoch=68e-step=50232.ckpt'
    sovits_model_path = 'GPT_SoVITS/pretrained_models/s2G488k.pth'
    ref_audio_path = 'GPT_SoVITS/pretrained_models/Ana.wav'
    language_combobox = i18n("中文")
    language_combobox_02 = i18n("多语种混合")

    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)

    if output_wav_path:
        if ctx.author.voice:
            channel = ctx.author.voice.channel
            voice_client = await channel.connect()
            voice_client.play(discord.FFmpegPCMAudio(output_wav_path), after=lambda e: print('播放完成', e))
            await ctx.send("正在播放生成的音頻...")

            # 等待音頻播放完成後斷開連接
            while voice_client.is_playing():
                await asyncio.sleep(1)
            await voice_client.disconnect()
        else:
            await ctx.send("請先進入語音頻道!")
    else:
        await ctx.send("合成失敗!")