in wearable/main.py [0:0]
def generate_audio(text: str) -> str:
# Use the credentials to authenticate your API request
headers = {
"Authorization": f"Bearer {get_token()}",
"Content-Type": "application/json",
}
# Speech recognition API endpoint
url = urlunparse(
URLComponents(
netloc="texttospeech.googleapis.com",
path=["v1", "text:synthesize"],
).to_tuple()
)
# Sample request data
data = {
"audioConfig": {
"audioEncoding": "LINEAR16",
},
"input": {"text": text},
"voice": {"languageCode": "en-US", "name": "en-US-Journey-F"},
}
# Make the API request
response = requests.post(url, headers=headers, data=json.dumps(data))
audio_data = base64.b64decode(response.json()["audioContent"])
segment = AudioSegment.from_file(io.BytesIO(audio_data), format="wav")
# Inject a little silence at the beginning to prime the bluetooth
# device; otherwise, first few words get truncated.
silence = AudioSegment.silent(duration=500)
# pydub can also play without forcing recourse to serialization;
# cost-benefit?
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as f:
logging.info(f"Synthesizing to {f.name}.")
(silence + segment).export(f.name, format="wav")
return f.name