I am trying to stream the output audio back to the twillio but as the output.wav file generated has these configurations
{‘numchannels’: 1, ‘samplerate’: 24000, ‘byterate’: 96000, ‘bytespersample’: 4, ‘bitspersample’: 32, ‘samplelength’: 306550, ‘audioformat’: 3}
Channels: 1
Bits per sample: 32
Sampling frequency: 24000
Length: 12.772916666666667 seconds
I tired changing the 24khz to 8 khz and mulaw format to send it to twillio, below code are the methods i tried each seperately but nothing converts the audio and plays the audio in the call.
def convert_chunk(chunk):
# print("Reading in the wave file...")
seg = audiosegment.from_file(chunk)
print("Information:")
print("Channels:", seg.channels)
print("Bits per sample:", seg.sample_width * 8)
print("Sampling frequency:", seg.frame_rate)
print("Length:", seg.duration_seconds, "seconds")
wave_read = pywav.WavRead(chunk)
print(wave_read.getparams())
**Method 1**
# # Simulate your audio data (replace with your actual audio data)
fs = 24000 # Original sampling frequency
t = np.arange(seg.duration_seconds) # Time vector for 17.2 seconds of audio
x = np.sin(2*np.pi*1000*t) # Simulate a 1000 Hz sine wave
# New desired sampling frequency
new_fs = 8000
# Resample the audio using scipy.signal.resample
resampled_x = signal.resample(x, int(len(x) * new_fs / fs))
with open('out.wav', 'wb') as f:
write_wav(f, new_fs, resampled_x)
# # quantize to PCM
# pcm_array = np.int16(resampled_x * 32768).tobytes()
# # take the 16-bit PCM linear down to 8-bit mu-law
# mulaw_array = audioop.lin2ulaw(pcm_array, 2)
# # Convert to base64
# wav_base64 = base64.b64encode(resampled_x).decode()
# # The number of samples in the resampled signal is the length of the resampled data
# num_samples = len(resampled_x)
# print("Original sampling frequency:", fs)
# print("Original signal length:", len(x))
# print("New desired sampling frequency:", new_fs)
# print("Number of samples in the resampled signal:", num_samples)
# return wav_base64
# Assuming your audio is stored in a WAV file named 'audio.wav'
**Method 2**
# audio = librosa.load(io.BytesIO(chunk), sr=44100, mono=True)
# chunk_8khz = librosa.resample(audio, 8000)
# chunk_ulaw = audioop.lin2ulaw(chunk_8khz, audio.sample_width)
# return chunk_ulaw
base64_data = convert_chunk(chatbot_response)
media_data = {
"event": "media",
"streamSid": stream_sid,
"media": {
"payload": base64_data
}
}
media = json.dumps(media_data)
await websocket.send(media)
# Send mark message after all audio chunks are sent
outbound_mark = {
"event": "mark",
"streamSid": stream_sid,
"mark": {
"name": "output_label"
}
}
mark = json.dumps(outbound_mark)
await websocket.send(mark)
can anyone check whats the issue and suggest any method to convert the audio of which twillio accepts it for streaming. I debugged the code but still no error occurs.