rendchevi / nix-tts

🐤 Nix-TTS: Lightweight and End-to-end Text-to-Speech via Module-wise Distillation
MIT License
229 stars 30 forks source link

Mp3 File #11

Open Tom-Mirowski opened 1 year ago

Tom-Mirowski commented 1 year ago

Is there a way to save the output to mp3 or wav files?

janzhen commented 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")