mirror of
https://github.com/tcsenpai/agenticSeek.git
synced 2025-06-06 19:15:28 +00:00
Update text_to_speech.py
updated the speak function , make it more sturctured , also added to play sound using afplay in macos and redirected linux or other to aplay , make sure the display of audio only work for jupyter notebooks not in terminal. the name of audio file is directly used in code , standerized it.
This commit is contained in:
parent
d51f17fdad
commit
290b75de3f
@ -4,6 +4,7 @@ import soundfile as sf
|
|||||||
import subprocess
|
import subprocess
|
||||||
import re
|
import re
|
||||||
import platform
|
import platform
|
||||||
|
from sys import modules
|
||||||
|
|
||||||
class Speech():
|
class Speech():
|
||||||
"""
|
"""
|
||||||
@ -24,7 +25,7 @@ class Speech():
|
|||||||
self.voice = self.voice_map[language][2]
|
self.voice = self.voice_map[language][2]
|
||||||
self.speed = 1.2
|
self.speed = 1.2
|
||||||
|
|
||||||
def speak(self, sentence: str, voice_number: int = 1):
|
def speak(self, sentence: str, voice_number: int = 1 , audio_file: str = 'sample.wav'):
|
||||||
"""
|
"""
|
||||||
Convert text to speech using an AI model and play the audio.
|
Convert text to speech using an AI model and play the audio.
|
||||||
|
|
||||||
@ -38,17 +39,17 @@ class Speech():
|
|||||||
sentence, voice=self.voice,
|
sentence, voice=self.voice,
|
||||||
speed=self.speed, split_pattern=r'\n+'
|
speed=self.speed, split_pattern=r'\n+'
|
||||||
)
|
)
|
||||||
for i, (gs, ps, audio) in enumerate(generator):
|
for i, (_, _, audio) in enumerate(generator):
|
||||||
audio_file = 'sample.wav'
|
if 'ipykernel' in modules: #only display in jupyter notebook.
|
||||||
display(Audio(data=audio, rate=24000, autoplay=i==0), display_id=False)
|
display(Audio(data=audio, rate=24000, autoplay=i==0), display_id=False)
|
||||||
sf.write(audio_file, audio, 24000) # save each audio file
|
sf.write(audio_file, audio, 24000) # save each audio file
|
||||||
if platform.system().lower() == "windows":
|
if platform.system().lower() == "windows":
|
||||||
import winsound
|
import winsound
|
||||||
winsound.PlaySound(audio_file, winsound.SND_FILENAME)
|
winsound.PlaySound(audio_file, winsound.SND_FILENAME)
|
||||||
elif platform.system().lower() == "linux":
|
elif platform.system().lower() == "darwin": # macOS
|
||||||
subprocess.call(["aplay", audio_file])
|
|
||||||
else:
|
|
||||||
subprocess.call(["afplay", audio_file])
|
subprocess.call(["afplay", audio_file])
|
||||||
|
else: # linux or other.
|
||||||
|
subprocess.call(["aplay", audio_file])
|
||||||
|
|
||||||
def replace_url(self, url: re.Match) -> str:
|
def replace_url(self, url: re.Match) -> str:
|
||||||
"""
|
"""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user