Question

I am retrieving the sound from:

http://translate.google.com/translate_tts

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.

Was it helpful?

Solution

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)

OTHER TIPS

Alternatively, if you want a wav file, you could install something to convert it to a wav file. Pymedia is a good module for that. The pymedia version made for current versions of python could be difficult to find, however, I have found a good place to get it at: http://www.lfd.uci.edu/~gohlke/pythonlibs/#pymedia

Here is a function that will convert the mp3 file to a wav file, and save a wav file in your file system:

def dumpWAV( name ):
    import pymedia.audio.acodec as acodec
    import pymedia.muxer as muxer
    import time, wave, string, os
    name1= str.split( name, '.' )
    name2= string.join( name1[ : len( name1 )- 1 ] )
    # Open demuxer first
    dm= muxer.Demuxer( name1[ -1 ].lower() )
    dec= None
    f= open( name, 'rb' )
    snd= None
    s= " "
    while len( s ):
        s= f.read( 20000 )
        if len( s ):
            frames= dm.parse( s )
            for fr in frames:
                if dec== None:
                    # Open decoder
                    dec= acodec.Decoder( dm.streams[ 0 ] )
                r= dec.decode( fr[ 1 ] )
                if r and r.data:
                    if snd== None:
                        snd= wave.open( name2+ '.wav', 'wb' )
                        snd.setparams( (r.channels, 2, r.sample_rate, 0, 'NONE','') )
                    snd.writeframes( r.data )

You may want to just play the mp3, but it is significantly easier to play a wav file in python. For example, pygame's support of mp3 files is limited, but it can always play a wav file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top