def _gen_beep(self, duration: int, lang: str): i_phonem = "i:" if lang == "de" else "i" # "i" phonem is not the same i german. Damn krauts return PhonemeList( PhonemeList([ Phoneme("b", 103), Phoneme(i_phonem, duration, [(0, 103 * 3), (80, 103 * 3), (100, 103 * 3)]), Phoneme("p", 228) ]))
def process(self, phonems: PhonemeList): w_phonem = Phoneme("w", 103, []) for i, phoneme in enumerate(phonems): if phoneme.name == "R": phoneme.name = "w" if random.randint(0, 1) == 0: for j in range(2): phonems.insert(i, w_phonem) else: phoneme.duration = 206 return phonems
def process(self, phonems: PhonemeList): silence = Phoneme("_", 61) reconstructed = PhonemeList([]) for i, phoneme in enumerate(phonems): if phonems[i].name in FrenchPhonemes.CONSONANTS \ and phonems[i + 1].name in FrenchPhonemes.VOWELS \ and random.randint(1, 3) == 1: reconstructed += [phonems[i], phonems[i + 1]] * 2 elif phoneme.name in FrenchPhonemes.VOWELS and random.randint(1, 3) == 1: reconstructed += [phoneme, silence, phoneme] else: reconstructed.append(phoneme) return reconstructed
async def string_to_phonemes(self, text: str, lang: str, voice_params: 'VoiceParameters') -> PhonemeList: """Renders an input string to a phonemlist object using espeak""" lang, voice, sex, volume = self._get_additional_params(lang, voice_params) phonem_synth_string = 'MALLOC_CHECK_=0 espeak -s %d -p %d --pho -q -v mb/mb-%s%d %s ' \ % (voice_params.speed, voice_params.pitch, lang, sex, text) logger.debug("Running espeak command %s" % phonem_synth_string) process = await create_subprocess_shell(phonem_synth_string, stdout=PIPE, stderr=PIPE) phonems, err = await process.communicate() return PhonemeList.from_pho_str(phonems.decode('utf-8').strip())
def process(self, phonems: PhonemeList): reconstructed = PhonemeList([]) ng_phonem = Phoneme("N", 100) euh_phonem = Phoneme("2", 79) phonems.append(Phoneme("_", 10)) # end-silence-padding, just to be safe for i, phoneme in enumerate(phonems): if phoneme.name in FrenchPhonemes.NASAL_WOVELS: reconstructed += [phoneme, ng_phonem] elif phoneme.name in FrenchPhonemes.CONSONANTS - {"w"} and phonems[i + 1].name not in FrenchPhonemes.VOWELS: reconstructed += [phoneme, euh_phonem] elif phoneme.name == "o": phoneme.name = "O" reconstructed.append(phoneme) else: reconstructed.append(phoneme) return reconstructed
def find(self, phoneme_list: PhonemeList, original_string: str): """Recursively, through the tree, tries to find a good rhyme that is *not* equal to the input word (here passed as an argument in original string""" if not phoneme_list: return self.find_random() current_pho = phoneme_list.pop() if current_pho in self.children: current_child = self.children[current_pho] curr_child_output = current_child.find(phoneme_list, original_string) if curr_child_output is not None: return curr_child_output rnd_child = self.find_random() if isinstance(rnd_child, Leaf) and levenshtein( seq1=original_string, seq2=rnd_child.text) <= 2: return None else: return rnd_child #nothing worked
def trim_silences(pho_list: PhonemeList) -> PhonemeList: return PhonemeList(pho_list[:-2])
else: rate, beats_track = wavfile.read(BEATS_TRACK_FILE) beat_duration = len(beats_track) / (rate * BEATS_TRACK_BEAT_COUNT) beats_track_looped = np.tile( beats_track, LOOPS_COUNT * BEATS_PER_MEASURE * len(CHORDS_PROGRESSION) // BEATS_TRACK_BEAT_COUNT) logging.info("Beat time : %dms" % (beat_duration * 1000)) logging.info("Measure time : %dms" % (beat_duration * 1000 * 4)) prog_freqs = get_trinote_progression_freqs(CHORDS_PROGRESSION) logging.info("First freq progression: \n %s" % (str(prog_freqs))) track_freqs = prog_freqs * LOOPS_COUNT progression_phonems = PhonemeList([]) for freq in track_freqs: progression_phonems.append( Phoneme("a", int(beat_duration * 1000), [(0, freq), (100, freq)])) logging.info("Rendering audio") voice = Voice(lang="fr", voice_id=2) wav = voice.to_audio(progression_phonems) if BEATS_TRACK_FILE is not None: rate, wave_array = to_f32_16k(wav) mixed_tracks = mix_tracks(beats_track_looped * 0.6, wave_array * 0.3, align="left") wav = to_wav_bytes(mixed_tracks, 16000) player = AudioPlayer() player.set_file(BytesIO(wav))
async def process(self, text: str, lang: str) -> Union[str, PhonemeList]: """Beeps out parts of the text that are tagged with double asterisks. It basicaly replaces the opening and closing asterisk with two opening and closing 'stop words' then finds the phonemic form of these two and replaces the phonems inside with an equivalently long beep""" occ_list = re.findall(r"\*\*.+?\*\*", text) if occ_list: # replace the "**text**" by "king text gink" tagged_occ_list = [" king %s gink " % occ.strip("*") for occ in occ_list] for occ, tagged_occ in zip(occ_list, tagged_occ_list): text = text.replace(occ, tagged_occ) # getting the phonemic form of the text phonemes = await self.renderer.string_to_phonemes(text, lang, self.voice_params) # then using a simple state machine (in_beep is the state), replaces the phonems between # the right phonemic occurence with the phonems of a beep in_beep = False output, buffer = PhonemeList([]), PhonemeList([]) while phonemes: if PhonemeList(phonemes[:3]).phonemes_str == self._tags_phonems[lang][0] and not in_beep: in_beep, buffer = True, PhonemeList([]) phonemes = PhonemeList(phonemes[3:]) elif PhonemeList(phonemes[:4]).phonemes_str == self._tags_phonems[lang][1] and in_beep: in_beep = False # creating a beep of the buffer's duration if buffer: output += self._gen_beep(sum([pho.duration for pho in buffer]), lang) phonemes = phonemes[4:] elif not in_beep: output.append(phonemes.pop(0)) elif in_beep: buffer.append(phonemes.pop(0)) return output else: return text
beat_duration = len(beats_track) / (rate * BEATS_TRACK_BEAT_COUNT) beats_track_looped = np.tile(beats_track, PROG_LOOP_COUNT * BEATS_PER_MEASURE * FULL_LOOP_COUNT * \ (FIRST_PROG_REPEAT + SECOND_PROG_REPEAT) // BEATS_TRACK_BEAT_COUNT) logging.info("Beat time : %dms" % (beat_duration * 1000)) logging.info("Measure time : %dms" % (beat_duration * 1000 * 4)) first_prog_freqs = get_trinote_progression_freqs(FIRST_CHORDS_PROGRESSION) logging.info("First freq progression: \n %s" % (str(first_prog_freqs))) second_prog_freqs = get_trinote_progression_freqs(SECOND_CHORDS_PROGRESSION) logging.info("First freq progression: \n %s" % (str(second_prog_freqs))) track_freqs = first_prog_freqs * FIRST_PROG_REPEAT + second_prog_freqs * SECOND_PROG_REPEAT progression_phonems = PhonemeList([]) for freq in track_freqs * FULL_LOOP_COUNT: progression_phonems.append( Phoneme("w", int(beat_duration * 1000), [(0,freq), (100,freq)]) ) logging.info("Rendering audio") voice = Voice(lang="fr", voice_id=3) wav = voice.to_audio(progression_phonems) if BEATS_TRACK_FILE is not None: rate, wave_array = to_f32_16k(wav) mixed_tracks = mix_tracks(beats_track_looped * 0.6, wave_array * 0.4, align="left") wav = to_wav_bytes(mixed_tracks, 16000) player = AudioPlayer() player.set_file(BytesIO(wav)) player.play()