Search code examples
pythonaudiopython-requestschunked-encodingwit.ai

Sending Chunked Audio to Wit.ai


I am attempting to use Wit.ai Speech recognition. I have successfully been able to send a wave file to the website for conversion to text but now I am trying to send it in chunks to reduce latency, but whenever I try to do this It gives me the error

"content-type-mismatch"

even though I am still sending a wav file like i said I was in the headers. Can someone take a look at my code and tell me what I am doing wrong? I appreciate it. I am using the python requests library

commandPath = "Command.wav"
#Headers for http request
headers = {"authorization": "Bearer " + TOKEN,
    "Content-Type": "audio/wav",
    "Transfer-encoding": "chunked"}

#open Audio file to send
audioFile = open(commandPath, "rb")

def gen():
    #Keep getting audio until it has all been read
    while audioFile.read(2048) != "":
        yield audioFile.read(2048)
    print("Finished")


r = requests.post(ENDPOINT, headers=headers, data=gen())
print(r.text)

Solution

  • I guess the problem is that you are calling audioFile.read(2048) twice per loop, ignoring (and discarding) the result of the first call.

    The very first chunk (which you discard) holds the header information, which therefore never reaches the server, who rightfully complains.