0

So I am trying to write a python program to speak out a sentence . For this purpose I am trying to use the "gTTS" Python library . It works fine in my Laptop (windows 10) and the sound plays well . However , when I run the same code in Raspberry Pi 2 , the audio file is not played . I have used omxplayer as well as mplayer , but the result remains the same . I have tried using other mp3 files , directly downloaded from the internet , and they work fine . But the files saved through the python program (see below) , do not work .

from gtts import gTTS
import os    
print("Converting your text to sound . . .")
tts = gTTS(text="hello world I am doing fine", lang='en')
tts.save("voice.mp3")
print("Starting audio. . .")
os.system("omxplayer  voice.mp3")
print("Thank You !!")

The omxplayer just shows the following message and exits .

omxplayer Output

Please help .

Boudhayan Dev
  • 970
  • 4
  • 14
  • 42
  • Do you really need `-o both`? – MEE Feb 01 '18 at 16:48
  • Nope , I'll just edit it . I tried with just "hdmi" , just "local" and have also used "both" . I have even tried running the file as "omxplayer filename.mp3" . It does not work. – Boudhayan Dev Feb 01 '18 at 16:49

1 Answers1

0

So , I figured out why the mp3 files were not able to be played . It turns out that the text that was supposed to be converted to audio was all in CAPS .I changed the starting letter of each sentences to capital and remaining letters to small , the script worked fine .

Assuming , sentence is the variable storing the information in all CAPS , use the following to convert in a manner described above -

new_sentence=""

for i in sentence.split():

   new_sentence+=(i[0].upper())+(i[1:].lower())+" "

print(new_sentence)

Now , use the new_sentence in the above code , and it will work !

Boudhayan Dev
  • 970
  • 4
  • 14
  • 42
  • There are also other ways to achieve this: https://stackoverflow.com/questions/352478/capitalize-a-string#16212385 – MEE Feb 01 '18 at 18:37