Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for [[ipa-phonemes]] in text #401

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 48 additions & 15 deletions src/python_run/piper/voice.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,53 @@ def load(

def phonemize(self, text: str) -> List[List[str]]:
"""Text to phonemes grouped by sentence."""
phonemes = []
next_phoneme_input = False
merge_phonemes = False
if self.config.phoneme_type == PhonemeType.ESPEAK:
if self.config.espeak_voice == "ar":
# Arabic diacritization
# https://github.com/mush42/libtashkeel/
text = tashkeel_run(text)

return phonemize_espeak(text, self.config.espeak_voice)

if self.config.phoneme_type == PhonemeType.TEXT:
return phonemize_codepoints(text)

raise ValueError(f"Unexpected phoneme type: {self.config.phoneme_type}")
import re
regex = re.compile(r"(\[\[|\]\])")
split_text = re.split(regex, text)
for text_part in split_text:
if text_part == "]]":
merge_phonemes = True
next_phoneme_input = False
if text_part == "[[":
next_phoneme_input = True
if text_part == "[[" or text_part == "]]":
# add pause
phonemes[-1].append(" ")
continue
if next_phoneme_input:
# add raw phoneme input, always merged to last segment
sentences = re.split("\n|\. ", text_part)
phonemes[-1].extend(sentences[0])
phonemes[-1].extend(" ")
for p in sentences[1:]:
phonemes.append(list(p))
else:
if self.config.espeak_voice == "ar":
# Arabic diacritization
# https://github.com/mush42/libtashkeel/
text = tashkeel_run(text_part)
sentences = re.split("\n|\. ", text_part)
for p in sentences:
if (p.strip() == ''): continue
ps = phonemize_espeak(p, self.config.espeak_voice)
if merge_phonemes:
# merge first list of token
phonemes[-1].extend(ps[0])
#add remaining list (more sentences)
for p in ps[1:]:
phonemes.append(p)
merge_phonemes = False;
else:
phonemes.extend(ps)
elif self.config.phoneme_type == PhonemeType.TEXT:
phonemes.extend(phonemize_codepoints(text_part))
else:
raise ValueError(f"Unexpected phoneme type: {self.config.phoneme_type}")
return phonemes

def phonemes_to_ids(self, phonemes: List[str]) -> List[int]:
"""Phonemes to ids."""
Expand Down Expand Up @@ -100,7 +135,6 @@ def synthesize(
wav_file.setframerate(self.config.sample_rate)
wav_file.setsampwidth(2) # 16-bit
wav_file.setnchannels(1) # mono

for audio_bytes in self.synthesize_stream_raw(
text,
speaker_id=speaker_id,
Expand All @@ -122,20 +156,19 @@ def synthesize_stream_raw(
) -> Iterable[bytes]:
"""Synthesize raw audio per sentence from text."""
sentence_phonemes = self.phonemize(text)

# 16-bit mono
num_silence_samples = int(sentence_silence * self.config.sample_rate)
silence_bytes = bytes(num_silence_samples * 2)

for phonemes in sentence_phonemes:
for i, phonemes in enumerate(sentence_phonemes):
phoneme_ids = self.phonemes_to_ids(phonemes)
yield self.synthesize_ids_to_raw(
phoneme_ids,
speaker_id=speaker_id,
length_scale=length_scale,
noise_scale=noise_scale,
noise_w=noise_w,
) + silence_bytes
) + (silence_bytes if (i+1)!=len(sentence_phonemes) else bytes())

def synthesize_ids_to_raw(
self,
Expand Down