From 280558cd9788a111112703149077cfcdd331abae Mon Sep 17 00:00:00 2001 From: Sascha Nitsch Date: Sat, 17 Feb 2024 02:17:06 +0100 Subject: [PATCH 1/4] =?UTF-8?q?added=20support=20for=20[[ipa-phonemes]]=20?= =?UTF-8?q?in=20text:=20echo=20-n=20"Dies=20ist=20ein=20[[f=CB=88i=CB=90t?= =?UTF-8?q?=CA=83=C9=99]]=20des=20pull=20requests."=20|=20piper=20...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/python_run/piper/voice.py | 50 +++++++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 11 deletions(-) diff --git a/src/python_run/piper/voice.py b/src/python_run/piper/voice.py index 0360c273e..ff20a1fd5 100644 --- a/src/python_run/piper/voice.py +++ b/src/python_run/piper/voice.py @@ -56,18 +56,46 @@ 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 isPhoneme: + # add raw phoneme input, always merged to last segment + phonemes[-1].extend(text_part) + else: + if self.config.espeak_voice == "ar": + # Arabic diacritization + # https://github.com/mush42/libtashkeel/ + text = tashkeel_run(text_part) + ps = phonemize_espeak(text_part, self.config.espeak_voice) + if join: + # merge first list of token + phonemes[-1].extend(ps[0]) + #add remaining list (more sentences) + for p in ps[1:]: + phonemes.append(p) + join = 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.""" From bcc1d5e6a2b1203049448964e3279f08765630ba Mon Sep 17 00:00:00 2001 From: Sascha Nitsch Date: Sat, 17 Feb 2024 17:39:06 +0100 Subject: [PATCH 2/4] fixed incomplete variable renaming --- src/python_run/piper/voice.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/python_run/piper/voice.py b/src/python_run/piper/voice.py index ff20a1fd5..294840d97 100644 --- a/src/python_run/piper/voice.py +++ b/src/python_run/piper/voice.py @@ -73,7 +73,7 @@ def phonemize(self, text: str) -> List[List[str]]: # add pause phonemes[-1].append(" ") continue - if isPhoneme: + if next_phoneme_input: # add raw phoneme input, always merged to last segment phonemes[-1].extend(text_part) else: @@ -82,13 +82,13 @@ def phonemize(self, text: str) -> List[List[str]]: # https://github.com/mush42/libtashkeel/ text = tashkeel_run(text_part) ps = phonemize_espeak(text_part, self.config.espeak_voice) - if join: + 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) - join = False; + merge_phonemes = False; else: phonemes.extend(ps) elif self.config.phoneme_type == PhonemeType.TEXT: From 06c6c623b78c7db4b71b6174707c69faeaa948e3 Mon Sep 17 00:00:00 2001 From: Sascha Nitsch Date: Sat, 17 Feb 2024 18:09:57 +0100 Subject: [PATCH 3/4] support for \n in [[ ]] to split sentences. --- src/python_run/piper/voice.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/python_run/piper/voice.py b/src/python_run/piper/voice.py index 294840d97..91def80e3 100644 --- a/src/python_run/piper/voice.py +++ b/src/python_run/piper/voice.py @@ -75,7 +75,10 @@ def phonemize(self, text: str) -> List[List[str]]: continue if next_phoneme_input: # add raw phoneme input, always merged to last segment - phonemes[-1].extend(text_part) + sentences = text_part.split("\n") + phonemes[-1].extend(sentences[0]) + for p in sentences[1:]: + phonemes.append(list(p)) else: if self.config.espeak_voice == "ar": # Arabic diacritization From 8d06836730a74b0379520c36fcfab10c956aaf57 Mon Sep 17 00:00:00 2001 From: Sascha Nitsch Date: Thu, 11 Jul 2024 20:07:08 +0200 Subject: [PATCH 4/4] fixed incomplete splitting of sentences, do not add silence on last sentence --- src/python_run/piper/voice.py | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/python_run/piper/voice.py b/src/python_run/piper/voice.py index 91def80e3..20c84ce45 100644 --- a/src/python_run/piper/voice.py +++ b/src/python_run/piper/voice.py @@ -75,8 +75,9 @@ def phonemize(self, text: str) -> List[List[str]]: continue if next_phoneme_input: # add raw phoneme input, always merged to last segment - sentences = text_part.split("\n") + sentences = re.split("\n|\. ", text_part) phonemes[-1].extend(sentences[0]) + phonemes[-1].extend(" ") for p in sentences[1:]: phonemes.append(list(p)) else: @@ -84,16 +85,19 @@ def phonemize(self, text: str) -> List[List[str]]: # Arabic diacritization # https://github.com/mush42/libtashkeel/ text = tashkeel_run(text_part) - ps = phonemize_espeak(text_part, 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) + 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: @@ -131,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, @@ -153,12 +156,11 @@ 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, @@ -166,7 +168,7 @@ def synthesize_stream_raw( 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,