File size: 1,742 Bytes
8d24b90
 
 
 
9df835b
8d24b90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9df835b
 
8d24b90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import json
import warnings
from typing import List

import pyopenjtalk


def preprocess_input(src_str, seg_syb=" "):
    src_str = src_str.replace("\n", seg_syb)
    src_str = src_str.replace(" ", seg_syb)
    return src_str


def pyopenjtalk_g2p(text) -> List[str]:
    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter("always")
        # phones is a str object separated by space
        phones = pyopenjtalk.g2p(text, kana=False)
        if len(w) > 0:
            for warning in w:
                if "No phoneme" in str(warning.message):
                    return False
                    
    phones = phones.split(" ")
    return phones


def split_pinyin(pinyin: str, zh_plan: dict) -> tuple[str]:
    # load pinyin dict from local/pinyin.dict
    pinyin = pinyin.lower()
    if pinyin in zh_plan["dict"]:
        return zh_plan["dict"][pinyin]
    elif pinyin in zh_plan["syllable_alias"]:
        return zh_plan["dict"][zh_plan["syllable_alias"][pinyin]]
    else:
        return False


def get_tokenizer(lang):
    if lang == "zh":
        with open(os.path.join("resource/all_plans.json"), "r") as f:
            all_plan_dict = json.load(f)
        for plan in all_plan_dict["plans"]:
            if plan["language"] == "zh":
                zh_plan = plan
        return lambda text: split_pinyin(text, zh_plan)
    elif lang == "jp":
        return pyopenjtalk_g2p

def load_pitch_dict(file_path = "resource/midi-note.scp"):
    pitch_dict = {}
    with open(file_path, "r", encoding="utf-8") as f:
        for line in f:
            items = line.strip().split()
            pitch_dict[items[0]] = int(items[1])
            pitch_dict[items[1]] = int(items[1])
    return pitch_dict