import os from gtts import gTTS from dotenv import load_dotenv load_dotenv() def text_to_speech_with_gtts_old_no_autoplay_functionality(input_text , output_file): language = 'en' gttsObj = gTTS( text=input_text, lang=language, slow=False ) gttsObj.save(output_file) text = "hello this is me danish with ai voice" text_to_speech_with_gtts_old_no_autoplay_functionality(input_text=text,output_file= "output_gtts.mp3") # import elevenlabs # from elevenlabs.client import ElevenLabs # ELEVENLABS_API_KEY = os.environ.get("ELEVENLABS_API_KEY") # def text_to_speech_with_elevenlabs_old_no_autoplay_functionality(input_text , output_file): # client = ElevenLabs(api_key = ELEVENLABS_API_KEY) # audio = client.text_to_speech.convert( # text = input_text, # voice_id = "sunJnCSgZmOuefCgCWBd", # model_id = "eleven_turbo_v2", # output_format = "mp3_22050_32" # ) # elevenlabs.save(audio , output_file) # text_to_speech_with_elevenlabs_old_no_autoplay_functionality(input_text=text,output_file= "output_elevenlabs.mp3") import subprocess import platform from pydub import AudioSegment def text_to_speech_with_gtts_new_autoplay(input_text , output_file): language = 'en' #create gtts object gttsObj = gTTS( text=input_text, lang=language, slow=False ) #save the audio file gttsObj.save(output_file) #converstion from mp3 to wav wav_file = output_file.replace('.mp3', '.wav') AudioSegment.from_mp3(output_file).export(wav_file,format="wav") # Optionally remove the original mp3 file # os.remove(output_file) # Play the wav file based on the operating system os_name = platform.system() try: if os_name == "Darwin": subprocess.run(['afplay', wav_file]) elif os_name == "Windows": subprocess.run(['powershell', '-c', f'(New-Object Media.SoundPlayer "{wav_file}").PlaySync();']) elif os_name == "Linux": subprocess.run(['aplay', wav_file]) else: raise OSError("Unsupported operating system") except Exception as e: print(f"An error occurred while trying to play the audio: {e}") text = "hello this is me danish with ai voice" text_to_speech_with_gtts_new_autoplay(input_text=text,output_file= "gtts_testing_autoplay.mp3") # def text_to_speech_with_elevenlabs_new_autoplay(input_text , output_file): # client = ElevenLabs(api_key = ELEVENLABS_API_KEY) # audio = client.text_to_speech.convert( # text = input_text, # voice_id = "sunJnCSgZmOuefCgCWBd", # model_id = "eleven_turbo_v2", # output_format = "mp3_22050_32" # ) # elevenlabs.save(audio , output_file) # wav_file = output_file.replace("mp3", "wav") # AudioSegment.from_mp3(output_file).export(wav_file,format="wav") # # os.remove(output_file) # os_name = platform.system() # try: # if os_name == "Darwin": # subprocess.run(['afplay', wav_file]) # elif os_name == "Windows": # subprocess.run(['powershell', '-c', f'(New-Object Media.SoundPlayer "{wav_file}").PlaySync();']) # elif os_name == "Linux": # subprocess.run(['aplay', wav_file]) # else: # raise OSError("Unsupported operating system") # except Exception as e: # print(f"An error occurred while trying to play the audio: {e}") # text = "hello this is me danish with ai voice" # text_to_speech_with_elevenlabs_new_autoplay(input_text=text,output_file= "output_elevenlabs_autoplay.mp3")