From fa292e920d8fe8be16f957519806d0eb12b28049 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Sat, 2 Sep 2023 23:50:30 +0800 Subject: [PATCH] move wav2 to server.py --- server.py | 18 +++++++++++++++++- utils.py | 16 ---------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/server.py b/server.py index 552f491..c736ca4 100644 --- a/server.py +++ b/server.py @@ -1,6 +1,7 @@ from flask import Flask, request, Response from io import BytesIO import torch +from av import open as avopen import commons import utils @@ -54,6 +55,21 @@ def replace_punctuation(text, i=2): text = text.replace(char, char * i) return text +def wav2(i, o, format): + inp = avopen(i, 'rb') + out = avopen(o, 'wb', format=format) + if format == "ogg": format = "libvorbis" + + ostream = out.add_stream(format) + + for frame in inp.decode(audio=0): + for p in ostream.encode(frame): out.mux(p) + + for p in ostream.encode(None): out.mux(p) + + out.close() + inp.close() + # Load Generator hps = utils.get_hparams_from_file("./configs/config.json") @@ -100,7 +116,7 @@ def main(): return Response(wav.getvalue(), mimetype="audio/wav") wav.seek(0, 0) with BytesIO() as ofp: - utils.wav2(wav, ofp, fmt) + wav2(wav, ofp, fmt) return Response( ofp.getvalue(), mimetype="audio/mpeg" if fmt == "mp3" else "audio/ogg" diff --git a/utils.py b/utils.py index e917a78..fbd7149 100644 --- a/utils.py +++ b/utils.py @@ -8,7 +8,6 @@ import subprocess import numpy as np from scipy.io.wavfile import read import torch -from av import open as avopen MATPLOTLIB_FLAG = False @@ -292,18 +291,3 @@ class HParams(): def __repr__(self): return self.__dict__.__repr__() - -def wav2(i, o, format): - inp = avopen(i, 'rb') - out = avopen(o, 'wb', format=format) - if format == "ogg": format = "libvorbis" - - ostream = out.add_stream(format) - - for frame in inp.decode(audio=0): - for p in ostream.encode(frame): out.mux(p) - - for p in ostream.encode(None): out.mux(p) - - out.close() - inp.close()