import os, subprocess from google.cloud import texttospeech TTS_KEY_FILE = 'calendar-text-to-speech-your-key-file.json' CREDENTIALS_SUBFOLDER = '.credentials' home_dir = os.path.expanduser('~') credential_dir = os.path.join(home_dir, CREDENTIALS_SUBFOLDER) tts_credentials = os.path.join(credential_dir, TTS_KEY_FILE) # credentials for tts os.environ['GOOGLE_APPLICATION_CREDENTIALS']=tts_credentials def save_phrase(phrase, audioFile, tempo=0.8): # Optional parameter for tempo - slow it down if lower than 1 synthesis_input = texttospeech.SynthesisInput(text=phrase) # Perform the text-to-speech request on the text input with the selected # voice parameters and audio file type response = client.synthesize_speech(input=synthesis_input, voice=voice, audio_config=audio_config) # Write the response to the mp3 file. with open(audioFile, "wb") as out: out.write(response.audio_content) def setup_tts(maleOrFemale='male'): # Instantiates a client global client, voice, audio_config client = texttospeech.TextToSpeechClient() # Build the voice request, select the language code ("en-US") and ssml # https://cloud.google.com/text-to-speech/docs/voices (Test all available voices) if (maleOrFemale=='male'): voice = texttospeech.VoiceSelectionParams( language_code="en-US", name="en-US-Wavenet-D", ssml_gender=texttospeech.SsmlVoiceGender.MALE ) else: voice = texttospeech.VoiceSelectionParams( language_code="en-US", name="en-US-Wavenet-F", ssml_gender=texttospeech.SsmlVoiceGender.FEMALE ) # Select the type of audio file you want returned audio_config = texttospeech.AudioConfig(audio_encoding=texttospeech.AudioEncoding.MP3) def main(): setup_tts() save_phrase("Hey Dad. It's time to remind your local care people to give you your pre-breakfast pill", 'PRE_BREAKFAST_PILL.MP3') save_phrase("Hey Dad. It's time to remind your local care people to give you your after breakfast pills", 'POST_BREAKFAST_PILLS.MP3') save_phrase("Hey Dad. It's time to remind your local care people to give you your bedtime pills", 'BEDTIME_PILLS.MP3') exit(0) if __name__ == '__main__': main()