File size: 3,119 Bytes
dfc143a
 
 
 
 
6ff53e1
 
 
dfc143a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f18df21
dfc143a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
914f933
 
 
 
dfc143a
8a869ac
914f933
 
dfc143a
 
 
 
8a869ac
dfc143a
 
 
 
 
 
 
 
 
 
 
 
 
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
import scipy.io.wavfile
import os
import onnxruntime
import numpy as np
from huggingface_hub import snapshot_download
from num2words import num2words
import re
from transliterate import translit

class TTS:
    def __init__(self, model_name: str, save_path: str = "./model", add_time_to_end: float = 0.8) -> None:
        if not os.path.exists(save_path):
            os.mkdir(save_path)
        
        model_dir = os.path.join(save_path, model_name)
        
        if not os.path.exists(model_dir):
            snapshot_download(repo_id=model_name, 
                              allow_patterns=["*.txt", "*.onnx", "*.json"], 
                              local_dir=model_dir,
                              local_dir_use_symlinks=False
                            )
        
        self.model = onnxruntime.InferenceSession(os.path.join(model_dir, "exported/model.onnx"), providers=['CPUExecutionProvider'])
        
        if os.path.exists(os.path.join(model_dir, "exported/dictionary.txt")):
            from tokenizer import TokenizerG2P
            print("Use g2p")
            self.tokenizer = TokenizerG2P(os.path.join(model_dir, "exported"))
            
        else:
            from tokenizer import TokenizerGRUUT
            print("Use gruut")
            self.tokenizer = TokenizerGRUUT(os.path.join(model_dir, "exported"))
        
        self.add_time_to_end = add_time_to_end

    
    def _add_silent(self, audio, silence_duration: float = 1.0, sample_rate: int = 22050):
        num_samples_silence = int(sample_rate * silence_duration)
        silence_array = np.zeros(num_samples_silence, dtype=np.float32)
        audio_with_silence = np.concatenate((audio, silence_array), axis=0)
        return audio_with_silence


    def save_wav(self, audio, path:str):
        '''save audio to wav'''
        scipy.io.wavfile.write(path, 22050, audio)
    
    
    def _intersperse(self, lst, item):
        result = [item] * (len(lst) * 2 + 1)
        result[1::2] = lst
        return result
    
    
    def _get_seq(self, text):
        phoneme_ids = self.tokenizer._get_seq(text)
        phoneme_ids_inter = self._intersperse(phoneme_ids, 0)
        return phoneme_ids_inter
        
    def _num2wordsshor(self, match):
        match = match.group()
        ret = num2words(match, lang ='ru')
        return ret 
    
    def __call__(self, text: str, length_scale=1.2):
        text = translit(text, 'ru')
        text = re.sub(r'\d+',self._num2wordsshor,text)
        phoneme_ids = self._get_seq(text)
        text = np.expand_dims(np.array(phoneme_ids, dtype=np.int64), 0)
        text_lengths = np.array([text.shape[1]], dtype=np.int64)
        scales = np.array(
            [0.667, length_scale, 0.8],
            dtype=np.float32,
        )
        audio = self.model.run(
            None,
            {
                "input": text,
                "input_lengths": text_lengths,
                "scales": scales,
                "sid": None,
            },
        )[0][0,0][0]
        audio = self._add_silent(audio, silence_duration = self.add_time_to_end)
        return audio