I am receiving my ".wav" file in a django view as an "InMemoryUploadedFile" then converting it to bytes:
views.py
import functions.bytes_to_wav_wave
if voice_value:
# convert to bytes
file_content_bytes = voice_value.read()
wav_sound = bytes_to_wav_in_memory(file_content_bytes,1,2,10)
buffer = io.BytesIO(wav_sound)
try:
file_w = bytes_to_wav_wave(file_content_bytes,'output_wave.wav', 1, 2, 44100)
except Exception as e:
print(e)
with open('output_wave.wav', 'r') as file:
content = file.read()
While the function "bytes_to_wav_wave" is:
functions.py
def bytes_to_wav_wave(audio_bytes, filename, nchannels, sampwidth, framerate):
"""
Converts raw audio bytes to a WAV file using the wave module.
Args:
audio_bytes (bytes): The raw audio data.
filename (str): The name of the output WAV file.
nchannels (int): Number of audio channels (e.g., 1 for mono, 2 for stereo).
sampwidth (int): Sample width in bytes (e.g., 2 for 16-bit audio).
framerate (int): Frame rate or sample rate in Hz (e.g., 44100).
"""
with wave.open(filename, 'wb') as wf:
wf.setnchannels(nchannels)
wf.setsampwidth(sampwidth)
wf.setframerate(framerate)
wf.writeframes(audio_bytes)
# Example usage:
# Assuming 'raw_audio_data' is your bytes object containing audio data
# bytes_to_wav_wave(raw_audio_data, 'output_wave.wav', 1, 2, 44100)
But once I finish the conversion and come to opening my file with:
Views.py
try:
file_w = bytes_to_wav_wave(file_content_bytes,'output_wave.wav', 1, 2, 44100)
except Exception as e:
print(e)
try:
with open('output_wave.wav', 'r') as file:
content = file.read()
except Exception as e:
print("in try")
print(e)
The "except" block catches the error:
**'utf-8' codec can't decode byte 0xc6 in position 4: invalid continuation byte **
Using 'r' opens a file in textmode. (see: https://docs.python.org/3/library/functions.html#open)
.wav needs to be opened in binary mode 'rb' (.wav file format definition: https://en.wikipedia.org/wiki/WAV).
In textmode, python tries to decode textmode files as utf by default - thus the utf decoding error.
Views.py should read:
with open('output_wave.wav', 'rb') as file: