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.