File size: 3,015 Bytes
40fac91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

import matplotlib.pyplot as plt

import os
import json
import math

import scipy
import torch
from torch import nn
from torch.nn import functional as F
from torch.utils.data import DataLoader

import commons
import utils
#from data_utils import TextAudioLoader, TextAudioCollate, TextAudioSpeakerLoader, TextAudioSpeakerCollate
from models import SynthesizerTrn
from text.symbols import symbols
from text.symbols1 import symbols1
from text import text_to_sequence
from text import text_to_sequence1

from scipy.io.wavfile import write
import io
"""
import argparse
parser = argparse.ArgumentParser(description='查看传参')
parser.add_argument("--text",type=str,default="你好。")
parser.add_argument("--character",type=int,default=0)
args = parser.parse_args()
"""


def get_text(text, hps):
    text_norm = text_to_sequence(text, hps.data.text_cleaners)
    if hps.data.add_blank:
        text_norm = commons.intersperse(text_norm, 0)
    text_norm = torch.LongTensor(text_norm)
    return text_norm
    
def get_text1(text, hps):
    text_norm = text_to_sequence1(text, hps.data.text_cleaners)
    if hps.data.add_blank:
        text_norm = commons.intersperse(text_norm, 0)
    text_norm = torch.LongTensor(text_norm)
    return text_norm
    
    
hps = utils.get_hparams_from_file("./vits/configs/ys.json")
hps1= utils.get_hparams_from_file("./vits/configs/bh3.json")


net_g = SynthesizerTrn(
    len(symbols),
    hps.data.filter_length // 2 + 1,
    hps.train.segment_size // hps.data.hop_length,
    n_speakers=hps.data.n_speakers,#
    **hps.model).cuda()
_ = net_g.eval()

net_g1 = SynthesizerTrn(
    len(symbols1),
    hps1.data.filter_length // 2 + 1,
    hps1.train.segment_size // hps1.data.hop_length,
    n_speakers=hps1.data.n_speakers,#
    **hps1.model).cuda()
_ = net_g1.eval()


_ = utils.load_checkpoint("./vits/models/ys.pth", net_g, None)
_ = utils.load_checkpoint("./vits/models/bh3.pth", net_g1, None)

def ys(text,character):
#text=args.text
  audio_bytes = io.BytesIO()
  stn_tst = get_text(text, hps)
  with torch.no_grad():
    x_tst = stn_tst.cuda().unsqueeze(0)
    x_tst_lengths = torch.LongTensor([stn_tst.size(0)]).cuda()
    #character=args.character
    sid=torch.LongTensor([character]).cuda()
    audio = net_g.infer(x_tst, x_tst_lengths, noise_scale=.667, sid = sid, noise_scale_w=0.8, length_scale=1.2)[0][0,0].data.cpu().float().numpy()
    scipy.io.wavfile.write(audio_bytes, hps.data.sampling_rate, audio)
    return audio_bytes

def bh3(text,character):
  audio_bytes = io.BytesIO()
  stn_tst = get_text1(text, hps1)
  with torch.no_grad():
    x_tst = stn_tst.cuda().unsqueeze(0)
    x_tst_lengths = torch.LongTensor([stn_tst.size(0)]).cuda()
    #character=args.character
    sid=torch.LongTensor([character]).cuda()
    audio = net_g1.infer(x_tst, x_tst_lengths, noise_scale=.667, sid = sid, noise_scale_w=0.8, length_scale=1.2)[0][0,0].data.cpu().float().numpy()
    scipy.io.wavfile.write(audio_bytes, hps1.data.sampling_rate, audio)
    return audio_bytes