I am retrieving the sound from:
and writing it to a WAV file, when i double-click the file the sound plays ok, but when i use the WAVE module from python to open it, it gives me this error:
wave.Error: file does not start with RIFF id
I want to know if there is a way for openning this file, or if it is possible to play the sound without writing it before.
Here is the relevant code:
url = "http://translate.google.com/translate_tts?tl=%s&q=%s" % (lang, text)
hrs = {"User-Agent":
"Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.63 Safari/535.7"}
request = urllib.request.Request(url, headers = hrs)
page = urllib.request.urlopen(request)
fname = "Play"+str(int(time.time()))+".wav"
file = open(fname, 'wb')
file.write(page.read())
file.close()
And the code that reads this file is:
INPUT_FRAMES_PER_BLOCK = 1024
wf = wave.open(fname, 'r')
pa = pyaudio.PyAudio()
stream = pa.open(format=pa.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True)
data = wf.readframes(INPUT_FRAMES_PER_BLOCK)
while data != '':
stream.write(data)
data = wf.readframes(INPUT_FRAMES_PER_BLOCK)
stream.stop_stream()
stream.close()
pa.terminate()
Thanks in advance! I am using Python 3 BTW.
You have this error because you're trying to play a file that isn't a WAV
.
The sound generated by Google Translate is encoded as an MP3
.
As for how to play an MP3
sound with Python, I'd recommend you read this StackOverflow question.
(Basically you have to install some library like Pyglet)