Open Tom-Mirowski opened 1 year ago
I've tried the below code, and it works.
import numpy as np
import wave
import pydub
def write_wav(xw):
with wave.open('output.wav', 'wb') as wav_file:
wav_file.setnchannels(1)
wav_file.setsampwidth(2)
wav_file.setframerate(22050)
wav_file.writeframes((2 ** 15 * xw).astype(np.int16).tobytes())
def write_mp3(xw):
audio_segment = pydub.AudioSegment(
(2 ** 15 * xw).astype(np.int16).tobytes(),
frame_rate=22050,
sample_width=2,
channels=1,
)
audio_segment.export("output.mp3", format="mp3")
Is there a way to save the output to mp3 or wav files?