forked from shakedzy/companion
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspeech.py
More file actions
164 lines (127 loc) · 4.43 KB
/
speech.py
File metadata and controls
164 lines (127 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import os
import openai
import pyaudio
import wave
import logging
import pygame
from typing import Dict, List
from pydub import AudioSegment
from google.oauth2.service_account import Credentials
from google.cloud import texttospeech
from python.config import Config
_is_recording = False
t2s_audio_config = texttospeech.AudioConfig(audio_encoding=texttospeech.AudioEncoding.MP3)
t2s_client = texttospeech.TextToSpeechClient()
t2s_voice = None
def init_speech(config: Config, credentials: Credentials) -> None:
"""
Initialize Google text-to-speech client
:param config: Config
:param credentials: a Google Credentials object
"""
global t2s_client, t2s_voice
t2s_client = texttospeech.TextToSpeechClient(credentials=credentials)
l = config.bot.voice.split("-")
t2s_voice = texttospeech.VoiceSelectionParams(name=config.bot.voice, language_code=f"{l[0]}-{l[1]}")
def stop_recording() -> None:
"""
Stop recording loop (as it happens on a parallel thread)
"""
global _is_recording
_is_recording = False
def record(filename: str) -> str:
"""
Record user audio
:param filename: filename to save recording to (a mp3 file)
:return: filename of mp3
"""
global _is_recording
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
CHUNK = 1024
audio = pyaudio.PyAudio()
# Start recording
_is_recording = True
stream = audio.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK)
logging.info(f"Recording started ({filename})")
frames = []
while _is_recording:
data = stream.read(CHUNK)
frames.append(data)
logging.info("Recording ended")
# Stop recording
stream.stop_stream()
stream.close()
audio.terminate()
# Save the recorded audio as a WAV file
wav_filename = f"{filename}_TEMP.wav"
wf = wave.open(wav_filename, "wb")
wf.setnchannels(CHANNELS)
wf.setsampwidth(audio.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b"".join(frames))
wf.close()
# Convert the WAV file to an MP3 file
mp3_filename = filename
wav_audio = AudioSegment.from_wav(wav_filename)
wav_audio.export(mp3_filename, format="mp3")
# Remove the temporary WAV file
os.remove(wav_filename)
logging.info(f"Saved recording as {mp3_filename}")
return mp3_filename
def play_mp3(filename: str) -> None:
"""
play a mp3 file
:param filename: file to play
"""
pygame.mixer.init()
pygame.mixer.music.load(filename)
pygame.mixer.music.play()
def speech2text(filename: str, language: str) -> str:
"""
Convert mp3 filename containing recording to text
:param filename: mp3 filename with recording
:param language: ISO 639-1 code of the language spoken in the recording.
If None, STT service will try to figure it out, by it might hurt performances
:return: transcribed text
"""
audio_file = open(filename, "rb")
if language is None:
transcript = openai.Audio.transcribe("whisper-1", audio_file)
else:
transcript = openai.Audio.transcribe("whisper-1", audio_file, language=language)
return transcript["text"]
def text2speech(text: str, filename: str) -> str:
"""
Convert text to speech
:param text: text to be converted
:param filename: mp3 file with saved speech audio
:return: filename
"""
synthesis_input = texttospeech.SynthesisInput(text=text)
speech = t2s_client.synthesize_speech(
input=synthesis_input, voice=t2s_voice, audio_config=t2s_audio_config
).audio_content
with open(filename, "wb") as mp3_file:
mp3_file.write(speech)
return filename
def voices_by_features() -> Dict[str, Dict[str, List[str]]]:
"""
Return voices supported by TTS, by language and gender
:return: Dict: {lang: {gender: [list of voices]}}
"""
voices_dict = {}
voices = t2s_client.list_voices().voices
for voice in voices:
language_codes = voice.language_codes
name = voice.name
gender = texttospeech.SsmlVoiceGender(voice.ssml_gender).name.lower()
for language_code in language_codes:
if language_code not in voices_dict:
voices_dict[language_code] = {}
lang_dict = voices_dict[language_code]
if gender not in lang_dict:
lang_dict[gender] = []
lang_dict[gender].append(name)
return voices_dict