move wav2 to server.py

This commit is contained in:
源文雨
2023-09-02 23:50:30 +08:00
parent c43df2ac72
commit fa292e920d
2 changed files with 17 additions and 17 deletions

View File

@@ -1,6 +1,7 @@
from flask import Flask, request, Response from flask import Flask, request, Response
from io import BytesIO from io import BytesIO
import torch import torch
from av import open as avopen
import commons import commons
import utils import utils
@@ -54,6 +55,21 @@ def replace_punctuation(text, i=2):
text = text.replace(char, char * i) text = text.replace(char, char * i)
return text 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 # Load Generator
hps = utils.get_hparams_from_file("./configs/config.json") hps = utils.get_hparams_from_file("./configs/config.json")
@@ -100,7 +116,7 @@ def main():
return Response(wav.getvalue(), mimetype="audio/wav") return Response(wav.getvalue(), mimetype="audio/wav")
wav.seek(0, 0) wav.seek(0, 0)
with BytesIO() as ofp: with BytesIO() as ofp:
utils.wav2(wav, ofp, fmt) wav2(wav, ofp, fmt)
return Response( return Response(
ofp.getvalue(), ofp.getvalue(),
mimetype="audio/mpeg" if fmt == "mp3" else "audio/ogg" mimetype="audio/mpeg" if fmt == "mp3" else "audio/ogg"

View File

@@ -8,7 +8,6 @@ import subprocess
import numpy as np import numpy as np
from scipy.io.wavfile import read from scipy.io.wavfile import read
import torch import torch
from av import open as avopen
MATPLOTLIB_FLAG = False MATPLOTLIB_FLAG = False
@@ -292,18 +291,3 @@ class HParams():
def __repr__(self): def __repr__(self):
return self.__dict__.__repr__() 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()