Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send audio wav file generated at the server to client browser? [closed]

I'm using flask for my application. I would like to send an audio wav file from the server side to client with or without saving the wav file on the disk.

Any idea how to do this ?

like image 474
U-571 Avatar asked Jun 28 '13 12:06

U-571


People also ask

Do browsers support WAV?

WAV files have a . wav extension and are supported by Google Chrome, Firefox, Opera, and Safari browsers.

Can you email a WAV file?

Wave files, commonly called WAVs, are digital audio files that contain more data and reproduce audio content much better than MP3s and other compressed media. Because a WAV is larger than an MP3, you may not be able to email a WAV if its size exceeds the size limitation that your email service imposes.

Is WAV supported by Chrome?

Wav audio format is Fully Supported on Google Chrome 39. If you use Wav audio format on your website or web app, you can double-check that by testing your website's URL on Google Chrome 39 with LambdaTest.


1 Answers

You can create an in-memory file with StringIO:

from cStringIO import StringIO
from flask import make_response

from somewhere import generate_wav_file  # TODO your code here

@app.route('/path')
def view_method():

    buf = StringIO()

    # generate_wav_file should take a file as parameter and write a wav in it
    generate_wav_file(buf) 

    response = make_response(buf.getvalue())
    buf.close()
    response.headers['Content-Type'] = 'audio/wav'
    response.headers['Content-Disposition'] = 'attachment; filename=sound.wav'
    return response

If you have file on disk:

from flask import send_file

@app.route('/path')
def view_method():
     path_to_file = "/test.wav"

     return send_file(
         path_to_file, 
         mimetype="audio/wav", 
         as_attachment=True, 
         attachment_filename="test.wav")
like image 88
Seppo Erviälä Avatar answered Sep 18 '22 23:09

Seppo Erviälä