2023-08-02 21:53:35 +00:00
|
|
|
import argparse
|
|
|
|
import random
|
|
|
|
import string
|
|
|
|
import torch
|
|
|
|
|
|
|
|
from functools import cache
|
|
|
|
from pathlib import Path
|
|
|
|
from phonemizer import phonemize
|
|
|
|
from phonemizer.backend import BACKENDS
|
|
|
|
|
|
|
|
from tqdm import tqdm
|
|
|
|
|
2024-07-22 04:21:37 +00:00
|
|
|
try:
|
|
|
|
import pykakasi
|
|
|
|
except Exception as e:
|
|
|
|
pass
|
|
|
|
|
2023-08-02 21:53:35 +00:00
|
|
|
@cache
|
|
|
|
def _get_graphs(path):
|
|
|
|
with open(path, "r") as f:
|
|
|
|
graphs = f.read()
|
|
|
|
return graphs
|
|
|
|
|
2024-07-22 04:21:37 +00:00
|
|
|
def romanize( runes, sep="" ):
|
|
|
|
kks = pykakasi.kakasi()
|
|
|
|
result = kks.convert( runes )
|
|
|
|
return sep.join([ res['hira'] for res in result ])
|
|
|
|
|
2024-09-13 17:53:36 +00:00
|
|
|
# to-do: fill out this table
|
|
|
|
# although desu the only thing that might be needed are en-uk/en-gb, es-la, pt-br, and pt-pt
|
|
|
|
def coerce_language( lang ):
|
|
|
|
if lang == "en":
|
|
|
|
lang = "en-us"
|
|
|
|
if lang == "fr":
|
|
|
|
return "fr-fr"
|
|
|
|
return lang
|
|
|
|
|
2023-08-02 21:53:35 +00:00
|
|
|
cached_backends = {}
|
2024-07-22 05:30:40 +00:00
|
|
|
def _get_backend( language="en-us", backend="espeak", punctuation=True, stress=True, strip=True ):
|
2023-08-02 21:53:35 +00:00
|
|
|
key = f'{language}_{backend}'
|
|
|
|
if key in cached_backends:
|
|
|
|
return cached_backends[key]
|
|
|
|
|
|
|
|
if backend == 'espeak':
|
2024-07-22 05:30:40 +00:00
|
|
|
phonemizer = BACKENDS[backend]( language, preserve_punctuation=punctuation, with_stress=stress)
|
2023-08-02 21:53:35 +00:00
|
|
|
elif backend == 'espeak-mbrola':
|
|
|
|
phonemizer = BACKENDS[backend]( language )
|
|
|
|
else:
|
2024-07-22 05:30:40 +00:00
|
|
|
phonemizer = BACKENDS[backend]( language, preserve_punctuation=punctuation )
|
2023-08-02 21:53:35 +00:00
|
|
|
|
|
|
|
cached_backends[key] = phonemizer
|
|
|
|
return phonemizer
|
|
|
|
|
|
|
|
|
2024-07-22 05:30:40 +00:00
|
|
|
def encode(text: str, language="en-us", backend="auto", punctuation=True, stress=True, strip=True) -> list[str]:
|
2024-09-13 17:53:36 +00:00
|
|
|
language = coerce_language( language )
|
2023-08-02 21:53:35 +00:00
|
|
|
|
2024-07-22 04:21:37 +00:00
|
|
|
# Convert to kana because espeak does not like kanji...
|
|
|
|
if language[:2] == "ja" and backend == "auto":
|
|
|
|
text = romanize( text )
|
|
|
|
|
2023-08-27 00:53:23 +00:00
|
|
|
if not backend or backend == "auto":
|
|
|
|
backend = "espeak" # if language[:2] != "en" else "festival"
|
|
|
|
|
2024-07-22 05:30:40 +00:00
|
|
|
backend = _get_backend(language=language, backend=backend, stress=stress, strip=strip, punctuation=punctuation)
|
2023-08-02 21:53:35 +00:00
|
|
|
if backend is not None:
|
2024-08-07 01:23:33 +00:00
|
|
|
tokens = backend.phonemize( [ text ], strip=strip )
|
2023-08-02 21:53:35 +00:00
|
|
|
else:
|
2024-08-07 01:23:33 +00:00
|
|
|
tokens = phonemize( [ text ], language=language, strip=strip, preserve_punctuation=punctuation, with_stress=stress )
|
2023-08-02 21:53:35 +00:00
|
|
|
|
2024-07-22 05:30:40 +00:00
|
|
|
if not len(tokens):
|
2024-08-07 01:23:33 +00:00
|
|
|
raise Exception(f"Failed to phonemize, received empty string: {text}")
|
|
|
|
|
|
|
|
return tokens[0]
|
|
|
|
|
|
|
|
# Helper function to debug phonemizer
|
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
|
|
|
|
parser.add_argument("string", type=str)
|
|
|
|
parser.add_argument("--language", type=str, default="en-us")
|
|
|
|
parser.add_argument("--backend", type=str, default="auto")
|
|
|
|
parser.add_argument("--no-punctuation", action="store_true")
|
|
|
|
parser.add_argument("--no-stress", action="store_true")
|
|
|
|
parser.add_argument("--no-strip", action="store_true")
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
2023-08-02 21:53:35 +00:00
|
|
|
|
2024-08-07 01:23:33 +00:00
|
|
|
phonemes = encode( args.string, language=args.language, backend=args.backend, punctuation=not args.no_punctuation, stress=not args.no_stress, strip=not args.no_strip )
|
|
|
|
print( phonemes )
|