How do I convert PCM 16bit signed LE 16000kHz to WAV in python given only the bytes?

I pulled an object from amazon s3 buckets like so:

s3_response_object = S3.get_object(Bucket=bucket_name, Key=key_name)
object_content = s3_response_object['Body'].read()
# object content --> b'\x06\x10...'

Inside the bucket you have a PCM 16bit signed LE 16000kHz audio file. If you write that file to a .wav file like so:

with open("audio.wav", "wb") as f:
    f.write()

The wav file cannot be played by quicktime VLC or any audio player. How can I convert these bytes into a wav file. The end goal is to pass these bytes into a view with django, and I have already verified that my django view works properly using a drop in replacement stream of bytes to use for the view, and the audio plays back no problem. I think the bytes that I get from AWS need some type of conversion to be valid for passing into my view. Any help would be much appreciated.

END GOAL INSIDE DJANGO VIEW:

def get_context_data(self, **kwargs: Any) -> Dict[str, Any]:
    """" get_context_data gets audio from S3 and pass to django view"""
    context = super().get_context_data(**kwargs)

    s3_response_object = S3.get_object(Bucket=bucket_name, Key=key_name)
    object_content = s3_response_object['Body'].read()

    context["audio"] = b64encode(object_content)
    return context

INSIDE DJANGO TEMPLATE

<audio controls>
    <source src="data:audio/wav;base64,{{ audio }}" type="audio/wav">
</audio>

Any help would be much appreciated!

Back to Top