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 ?
WAV files have a . wav extension and are supported by Google Chrome, Firefox, Opera, and Safari browsers.
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.
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.
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With