Tung Nguyen
Tung Nguyen

Reputation: 390

How to receive wav file from blob in flask sever?

I'm quite new in Flask framework, I'm building an speech-to-text app base on restAPI, I have been following this tutorial to implement audio recorder and the using ajax in order to send blob to flask server:

https://blog.addpipe.com/using-recorder-js-to-capture-wav-audio-in-your-html5-web-site/

But I have stuck in the step get data in flask server and also don't know how to convert blob into wav file too because the API just accept wav format. Here is all my code:

This is ajax function to sending flask server

function sendAudioToSever(blob) {
let fd = new FormData();
fd.append("audio_data",blob); # Does it need 3rd param here? I found in somewhere people put the name wav in here
$.ajax({
    url: '/convert-speech',
    method: 'POST',
    processData: false,
    ContentType: false,
    dataType: 'script',
    data: fd,
    success: function(data) {
        console.log(data)
    }
 });
}

This is code in flask server where to receive audio file but I don't know what is the chain of request and how to convert blob into wav extension file:

@app.route('/convert-speech', methods=['POST'])
def translate_text():
    audio_input = request.??? # What is the chain in here to receive blob file
                              # And also how to convert it into wav format file :(

    response = speechAPI.get_translation(audio_input) # Here is the function receives wav file and return detected speech in json
    return jsonify(response)

Please help me, very thank you for your help

Upvotes: 1

Views: 1766

Answers (1)

Mohamed Diaby
Mohamed Diaby

Reputation: 226

You can use request.files['file'] to get the blob file like this:

@app.route('/convert-speech', methods=['POST'])
def translate_text():
     audio_input = request.files['file']
     response = speechAPI.get_translation(audio_input)
     return jsonify(response)

About converting the blob file to wav, you can take a look at: Create a wav file from blob audio django

Upvotes: 1

Related Questions