Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
PraaneshSelvaraj committed Mar 14, 2024
1 parent bb6f80d commit c0223cb
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 35 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ from speech_engine import TTS_Google, FileExtensionError
tts = TTS_Google()

# Set the language and other options
tts.lang = 'en'
tts.slow = False
tts.set_language('en')
tts.set_slow(False)

# Synthesize and play speech
tts.speak("Hello, world!")
Expand All @@ -44,7 +44,7 @@ if not tts.is_valid_token:
raise InvalidTokenError()

# Set the voice
tts.voice = 'Colin'
tts.set_voice('Colin')

# Synthesize and play speech
tts.speak("Hello, world!")
Expand Down
82 changes: 66 additions & 16 deletions speech_engine/tts_google.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,81 @@
class TTS_Google:
"""
The TTS_Google class provides functionality to synthesize text into speech using the gTTS library.
Attributes:
lang (str): The language code for the synthesized speech (e.g., 'en' for English).
tld (str): The top-level domain for the gTTS service.
slow (bool): Whether to generate speech at a slower speed.
"""

def __init__(self):
self.lang = 'en'
self.tld = ''
self.slow = False

self._lang = 'en'
self._tld = ''
self._slow = False

def get_language(self) -> str:
"""
Returns the current language.
Returns:
str: The current language.
"""
return self._lang

def set_language(self, lang : str):
"""
Set the language for text synthesis.
Args:
lang (str): The language code.
"""
self._lang = lang

def get_tld(self) -> str:
"""
Returns the current top-level domain (TLD).
Returns:
str: The current top-level domain (TLD).
"""
return self._tld

def set_tld(self, tld : str):
"""
Set the top-level domain (TLD) for regional language accents.
Args:
tld (str): The top-level domain (TLD) code.
"""
self._tld = tld

def get_slow(self) -> bool:
"""
Returns the current speech speed setting.
Returns:
bool: The current speech speed setting.
"""
return self._slow

def set_slow(self, slow : bool):
"""
Set the speech speed.
Args:
slow (bool): True to enable slow speech, False otherwise.
"""
self._slow = slow

def speak(self, text):
"""
Synthesizes the given text into speech and plays it.
Args:
text (str): The text to be synthesized into speech.
"""
if self.tld:
gtts = gTTS(text=text, tld=self.tld, lang=self.lang, slow=self.slow)
if self._tld:
gtts = gTTS(text=text, tld=self._tld, lang=self._lang, slow=self._slow)
else:
gtts = gTTS(text=text, lang=self.lang, slow=self.slow)
gtts = gTTS(text=text, lang=self._lang, slow=self._slow)

gtts.save("speech.mp3")
sleep(0.5)
Expand All @@ -53,9 +103,9 @@ def save(self, text, filename):
if not filename.endswith('.mp3'):
raise FileExtensionError()

if self.tld:
gtts = gTTS(text=text, tld=self.tld, lang=self.lang, slow=self.slow)
if self._tld:
gtts = gTTS(text=text, tld=self._tld, lang=self._lang, slow=self._slow)
else:
gtts = gTTS(text=text, lang=self.lang, slow=self.slow)
gtts = gTTS(text=text, lang=self._lang, slow=self._slow)

gtts.save(filename)
97 changes: 81 additions & 16 deletions speech_engine/tts_witai.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,72 @@ class TTS_Witai:
Args:
authToken (str): The Wit.ai auth token.
Attributes:
voice (str): The selected voice for synthesis.
"""

def __init__(self, authToken):
self.auth_token = authToken
self.is_valid_token = self._validate_token()

if not self.is_valid_token:
raise InvalidTokenError()
self.voice = 'Colin'
self.speed = None
self.pitch = None

self._voice = 'Colin'
self._speed = None
self._pitch = None

def get_voice(self) -> str:
"""
Returns the current voice.
Returns:
str: The current voice.
"""
return self._voice

def set_voice(self, voice : str):
"""
Sets the voice to be used for synthesis.
Args:
voice (str): The voice to be set.
"""
self._voice = voice

def get_speed(self) -> int:
"""
Returns the current speed.
Returns:
int: The current speed.
"""
return self._speed

def set_speed(self, speed : int):
"""
Sets the speed of speech synthesis.
Args:
speed (int): The speed to be set.
"""
self._speed = speed

def get_pitch(self) -> int:
"""
Returns the current pitch.
Returns:
int: The current pitch.
"""
return self._pitch

def set_pitch(self, pitch : int):
"""
Sets the pitch of speech synthesis.
Args:
pitch (int): The pitch to be set.
"""
self._pitch = pitch

def _validate_token(self):
"""
Expand All @@ -46,12 +99,12 @@ def speak(self, text):
text (str): The text to be synthesized into speech.
"""

data = { 'q': text, 'voice': self.voice }
if self.speed:
data['speed'] = self.speed
data = { 'q': text, 'voice': self._voice }
if self._speed:
data['speed'] = self._speed

if self.pitch:
data['speed'] = self.pitch
if self._pitch:
data['speed'] = self._pitch

audio= requests.post(
'https://api.wit.ai/synthesize',
Expand Down Expand Up @@ -83,8 +136,13 @@ def save(self, text, filename):
FileExtensionError: If the provided filename doesn't have a .mp3 extension.
"""
if not filename.endswith('.mp3'):
raise FileExtensionError()
data = { 'q': text, 'voice': self._voice }
if self._speed:
data['speed'] = self._speed

if self._pitch:
data['speed'] = self._pitch

audio= requests.post(
'https://api.wit.ai/synthesize',
params={
Expand All @@ -93,14 +151,21 @@ def save(self, text, filename):
headers={
'Authorization': f'Bearer {self.auth_token}',
},
json={ 'q': text, 'voice': self.voice },
json=data,
)

with open("speech.mp3", "wb") as f:
f.write(audio.content)
f.close()


def get_voices(self):
"""
Fetches available voices from the Wit.ai API.
Returns:
list: A list of available voices.
"""
headers = {
'Authorization': f'Bearer {self.auth_token}',
}
Expand All @@ -110,6 +175,6 @@ def get_voices(self):
voices = []
for locale_voices in resp.values():
for voice in locale_voices:
voices.append(voice['name'])
voices.append(voice['name'].replace("wit$", ""))

return voices

0 comments on commit c0223cb

Please sign in to comment.