File size: 13,670 Bytes
400cebf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
987f35a
 
400cebf
 
 
 
 
987f35a
 
400cebf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6dde92c
 
 
 
400cebf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import re
import json
import numpy as np
from typing import Dict, List, Optional
from dataclasses import dataclass
from loguru import logger


# 常量
SAMPLE_RATE = 24000
DEFAULT_SPEED = 1.0
DEFAULT_FADE_OUT = 0.05
DEFAULT_PAUSE = 0.05

ALIASES = {
    'en-us': 'a', 'en-gb': 'b', 'es': 'e', 'fr-fr': 'f',
    'hi': 'h', 'it': 'i', 'pt-br': 'p', 'ja': 'j', 'zh': 'z',
}

LANG_CODES = {
    'a': 'American English', 'b': 'British English', 'e': 'es',
    'f': 'fr-fr', 'h': 'hi', 'i': 'it', 'p': 'pt-br',
    'j': 'Japanese', 'z': 'Mandarin Chinese',
}


@dataclass
class G2PContext:
    """G2P"""
    g2p: any
    g2p_type: str
    vocab: Dict[str, int]


def clean_text(text: str) -> str:
    text = re.sub(r'\s+', ' ', text)
    text = text.strip()
    text = ''.join(char for char in text if ord(char) >= 32 or char in '\n\r\t')
    return text


def split_sentences(text: str, lang_code: str = 'a') -> List[str]:
    if lang_code in ['z', 'j']:
        sentences = re.split(r'([。!?;,、:""''()【】《》…\n])', text)
    else:
        sentences = re.split(r'([.!?;,:\n])', text)
    
    result = []
    #一句话带一个标点
    for i in range(0, len(sentences)-1, 2):
        if i+1 < len(sentences):
            sentence = sentences[i] + sentences[i+1]
        else:
            sentence = sentences[i]
        sentence = sentence.strip()
        if sentence:
            result.append(sentence)
    
    # 处理最后没有标点的文本片段,添加默认结束标点
    if len(sentences) % 2 == 1 and sentences[-1].strip():
        last_text = sentences[-1].strip()
        end_punctuation = '。' if lang_code in ['z', 'j'] else '.'
        result.append(last_text + end_punctuation)
    
    return result if result else [text]


def apply_fade_out(audio: np.ndarray, fade_samples: int) -> np.ndarray:
    """末尾淡出音频"""
    if len(audio) <= fade_samples or fade_samples <= 0:
        return audio
    fade_out = np.linspace(1.0, 0.0, fade_samples).astype(np.float32)
    audio_faded = audio.copy()
    audio_faded[-fade_samples:] *= fade_out
    return audio_faded


def audio_numpy_concat(segment_data_list: List[np.ndarray], sr: int = SAMPLE_RATE, 
                       speed: float = DEFAULT_SPEED, pause_duration: float = DEFAULT_PAUSE) -> np.ndarray:
    """拼接音频片段"""
    if not segment_data_list:
        return np.array([], dtype=np.float32)
    
    audio_segments = []
    pause_samples = int((sr * pause_duration) / speed)
    
    for i, segment_data in enumerate(segment_data_list):
        audio_segments.append(segment_data.reshape(-1))
        if i < len(segment_data_list) - 1 and pause_samples > 0:
            audio_segments.append(np.zeros(pause_samples, dtype=np.float32))
    
    return np.concatenate(audio_segments).astype(np.float32)


def load_vocab_from_config(config_path: str) -> Dict[str, int]:
    with open(config_path, 'r', encoding='utf-8') as f:
        config = json.load(f)
    return config.get('vocab', {})


def init_g2p(lang_code: str, trf: bool = False, en_callable=None):
    lang_code = lang_code.lower()
    lang_code = ALIASES.get(lang_code, lang_code)
    
    if lang_code not in LANG_CODES:
        raise ValueError(f"不支持的语言代码: {lang_code}")
    
    if lang_code in 'ab':
        from misaki import en, espeak
        try:
            fallback = espeak.EspeakFallback(british=lang_code=='b')
        except:
            logger.warning("EspeakFallback 未启用")
            fallback = None
        g2p = en.G2P(trf=trf, british=lang_code=='b', fallback=fallback, unk='')
        return g2p, 'en'
    elif lang_code == 'j':
        from misaki import ja
        return ja.JAG2P(), 'ja'
    elif lang_code == 'z':
        from misaki import zh
        return zh.ZHG2P(version=None, en_callable=en_callable), 'zh'
    else:
        from misaki import espeak
        language = LANG_CODES[lang_code]
        return espeak.EspeakG2P(language=language), 'espeak'


def text_to_phonemes(text: str, g2p, g2p_type: str) -> str:
    if g2p_type == 'en':
        _, tokens = g2p(text)
        phonemes = ''.join(t.phonemes + (' ' if t.whitespace else '') for t in tokens).strip()
        return phonemes
    else:
        phonemes, _ = g2p(text)
        return phonemes


def phonemes_to_input_ids(phonemes: str, vocab: Dict[str, int], debug: bool = False) -> np.ndarray:
    input_ids = []
    skipped_phonemes = []
    skipped_positions = []
    
    for i, p in enumerate(phonemes):
        if p in vocab:
            input_ids.append(vocab[p])
        else:
            skipped_phonemes.append(p)
            skipped_positions.append(i)
            if debug:
                start = max(0, i-5)
                end = min(len(phonemes), i+6)
                context = phonemes[start:end]
                logger.warning(f"未知音素 '{p}' (ord={ord(p)}) 位置={i}, 上下文: ...{context}...")
    
    if skipped_phonemes:
        logger.error(f"总共跳过 {len(skipped_phonemes)} 个音素在位置 {skipped_positions}: {skipped_phonemes}")
    
    return np.array(input_ids, dtype=np.int64)


def load_voice_embedding(voice_path: str, phoneme_len: Optional[int] = None) -> np.ndarray:
    if "checkpoints/voices_npy" in voice_path:
        pass
    else:
        voice_path = voice_path.replace('checkpoints/voices', 'checkpoints/voices_npy').replace('.pt', '.npy')
    pack = np.load(voice_path).reshape(510,1,256)
    if phoneme_len is not None:
        ref_s = pack[phoneme_len:phoneme_len+1]
    else:
        idx = pack.shape[0] // 2
        ref_s = pack[idx:idx+1]
    return ref_s[0]


def split_input_ids_semantic(
    input_ids: np.ndarray,
    fixed_seq_len: int,
    phonemes: str = None,
    vocab: Dict[str, int] = None
) -> List[Dict]:
    """input_ids分割"""
    content = input_ids[0, 1:-1]
    chunk_with_special = np.concatenate([[0], content, [0]])
    
    # 填充到固定长度
    padding_len = fixed_seq_len - len(chunk_with_special)
    if padding_len > 0:
        chunk_padded = np.concatenate([chunk_with_special, np.zeros(padding_len, dtype=input_ids.dtype)])
    else:
        chunk_padded = chunk_with_special
    
    return [{
        'input_ids': chunk_padded.reshape(1, -1),
        'actual_len': len(chunk_with_special),
        # 'is_last': True,
        # 'is_first': True,
        'trim_end_chars': 0
    }]


def generate_input_ids_from_text(text: str, lang_code: str = None, config_path: str = None, 
                                 g2p=None, g2p_type: str = None, vocab: Dict[str, int] = None,
                                 g2p_context: G2PContext = None):
    """从文本生成input_ids"""
    if g2p_context is not None:
        g2p = g2p_context.g2p
        g2p_type = g2p_context.g2p_type
        vocab = g2p_context.vocab
    else:
        if vocab is None:
            vocab = load_vocab_from_config(config_path)
        if g2p is None:
            g2p, g2p_type = init_g2p(lang_code)
    
    phonemes = text_to_phonemes(text, g2p, g2p_type)
    content_ids = phonemes_to_input_ids(phonemes, vocab, debug=False)
    input_ids = np.concatenate([[0], content_ids, [0]]).reshape(1, -1)
    return input_ids, phonemes


def split_long_sentence(sentence, lang_code, g2p, g2p_type, vocab, max_merge_len=78, depth=0):
    try:
        input_ids, phonemes = generate_input_ids_from_text(
            sentence, g2p=g2p, g2p_type=g2p_type, vocab=vocab
        )
        content_len = input_ids.shape[1]
        
        if content_len <= max_merge_len:
            return [{
                'sentence': sentence,
                'input_ids': input_ids,
                'phonemes': phonemes,
                'content_len': content_len
            }]
        else:
            # 中文/日文按字符长度一半分割
            if lang_code in ['z', 'j']:
                mid = len(sentence) // 2
                first_half = sentence[:mid]
                second_half = sentence[mid:]
            else:
                # 英文按单词个数一半分割
                words = sentence.split()
                mid_word = len(words) // 2
                first_half = ' '.join(words[:mid_word])
                second_half = ' '.join(words[mid_word:])
            
            result_first = split_long_sentence(first_half, lang_code, g2p, g2p_type, vocab, 
                                              max_merge_len, depth + 1)
            result_second = split_long_sentence(second_half, lang_code, g2p, g2p_type, vocab,
                                               max_merge_len, depth + 1)
            
            return result_first + result_second
    except Exception:
        return []


def concat_audios(sub_audios: List[Dict], sr: int = SAMPLE_RATE) -> np.ndarray:
    """拼接音频"""
    if not sub_audios:
        return np.array([], dtype=np.float32)
    
    if len(sub_audios) == 1:
        return sub_audios[0]['audio']
    
    audio_segments = [sub['audio'] for sub in sub_audios]
    return np.concatenate(audio_segments).astype(np.float32)


def process_and_merge_sentences(text: str, lang_code: str, g2p, g2p_type: str, 
                                vocab: Dict[str, int], max_merge_len: int = 96) -> List[Dict]:
    """
    处理文本:清理(待处理)、分句、生成input_ids、长句分割、短句合并
    
    """
    cleaned_text = clean_text(text)
    sentences = split_sentences(cleaned_text, lang_code=lang_code)
    
    # 为每个句子生成 input_ids
    sentence_data = []
    for sentence in sentences:
        try:
            input_ids, phonemes = generate_input_ids_from_text(
                sentence, g2p=g2p, g2p_type=g2p_type, vocab=vocab
            )
            content_len = input_ids.shape[1]
            
            if content_len <= max_merge_len:
                sentence_data.append({
                    'sentence': sentence,
                    'input_ids': input_ids,
                    'phonemes': phonemes,
                    'content_len': content_len,
                    'is_long': False
                })
            else:
                sub_results = split_long_sentence(sentence, lang_code, g2p, g2p_type, vocab, max_merge_len)
                sentence_data.append({
                    'sentence': sentence,
                    'sub_results': sub_results,
                    'is_long': True
                })
        except Exception as e:
            logger.error(f"错误处理句子 '{sentence}': {e}")
    
    if not sentence_data:
        raise ValueError("没有生成任何 input_ids")
    
    # 长句保持分割,短句合并
    merged_groups = []
    i = 0
    
    while i < len(sentence_data):
        if sentence_data[i]['is_long']:
            sub_results = sentence_data[i]['sub_results']
            merged_groups.append({'is_long_split': True, 'sub_results': sub_results})
            i += 1
        else:
            merged_sentences = []
            total_len = 0
            j = i
            
            while j < len(sentence_data) and not sentence_data[j]['is_long']:
                next_len = sentence_data[j]['content_len']
                
                if total_len + next_len < max_merge_len:
                    merged_sentences.append(sentence_data[j]['sentence'])
                    total_len += next_len
                    j += 1
                else:
                    break
            
            if j == i:
                merged_sentences.append(sentence_data[i]['sentence'])
                j = i + 1
            
            # 重新生成合并后的 input_ids
            merged_text = ' '.join(merged_sentences)
            merged_input_ids, merged_phonemes = generate_input_ids_from_text(
                merged_text, g2p=g2p, g2p_type=g2p_type, vocab=vocab
            )
            
            merged_groups.append({'input_ids': merged_input_ids, 'phonemes': merged_phonemes})
            i = j
    
    return merged_groups


def run_batch_inference(engine, merged_groups: List[Dict], voice_path: str, 
                       vocab: Dict[str, int], speed: float = DEFAULT_SPEED, 
                       fade_out_duration: float = DEFAULT_FADE_OUT,
                       sr: int = SAMPLE_RATE) -> List[np.ndarray]:
    """推理"""
    audio_list = []
    
    for group in merged_groups:
        try:
            if group.get('is_long_split', False):
                # 长句分割:对每个子片段推理后拼接
                sub_audios = []
                for sub in group['sub_results']:
                    phoneme_len = sub['input_ids'].shape[1] - 2
                    ref_s = load_voice_embedding(voice_path, phoneme_len=phoneme_len)
                    audio = engine.inference(sub['input_ids'], ref_s, sub['phonemes'], vocab, 
                                           speed=speed, fade_out_duration=0)
                    sub_audios.append({'audio': audio})
                
                combined_audio = concat_audios(sub_audios, sr=sr)
                audio_list.append(combined_audio)
            else:
                # 短句或合并句:直接推理
                phoneme_len = group['input_ids'].shape[1] - 2
                ref_s = load_voice_embedding(voice_path, phoneme_len=phoneme_len)
                audio = engine.inference(group['input_ids'], ref_s, group['phonemes'], vocab,
                                       speed=speed, fade_out_duration=fade_out_duration)
                audio_list.append(audio)
        except Exception as e:
            logger.error(f"推理错误: {e}")
    
    if not audio_list:
        raise ValueError("没有生成任何音频")
    
    return audio_list