Search code examples
pythonpython-3.xaudioibm-cloud

Writing ByteIO as audio python for use with Bluemix


I use cURL to make a request. What it returns is suppose to be audio, but it gets stored in a ByteIO object first. My question is how do I save the ByteIO as an audio file? I have successfully saved an audio file, however, I am not able to read it with an audio player.

Here is what I have so far:

import pycurl, json
from io import BytesIO

with open('cred.json') as data_file:
    data = json.load(data_file)

user = data["credentials"]['username']
password = data["credentials"]['password']

buffer = BytesIO()
c = pycurl.Curl()
c.setopt(c.URL, "https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize")
c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json', 'Accept: audio/mp3'])
c.setopt(pycurl.USERNAME, user)
c.setopt(pycurl.PASSWORD, password)
c.setopt(pycurl.POST, 1)
c.setopt(pycurl.POSTFIELDS, '{\"text\":\"Hello World\"}')
c.setopt(c.WRITEDATA, buffer)
c.perform()
c.close()

open('helloworld.mp3', 'wb').write(buffer.getvalue())

Edit: I am using cURL because I am trying to talk to a service hosted on IBM's Bluemix system. It says to use cURL to make requests to the service. What I am trying to do is use IBM's text-to-speech service. I make a request with the text to be converted to speech (the Hello World portion) and it returns the audio. However, when I save the received buffer and try to open the file, it says it cannot read it. Hope that answers your questions.


Solution

  • There is no option to generate MP3; the only options available are to generate Ogg, WAV or FLAC files. Specify the accept parameter as a header (as you've done with the pycurl interface) or as a URL query parameter. Posting it with the JSON parameters resulted in a 400 error for me.

    Don't use the pycurl interface here. It is a hugely cumbersome API. You'd be much better of using the requests library here:

    import requests
    import shutil
    
    url = 'https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize'
    user = 'username'
    password = 'password'
    
    json_params = {'text': 'Hello World'}
    query_params = {'accept': 'audio/wav'}
    
    response = requests.post(
        url, auth=(user, password), 
        json=json_params, params=query_params, stream=True)
    if response.status_code == 200:
        response.raw.decode_content = True
        with open('helloworld.wav', 'wb') as wavfile:
            shutil.copyfileobj(response.raw, wavfile)