Skip to content

Commit

Permalink
fix(webui): crash on non-ffmpeg env. (2noise#466)
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiama authored Jun 26, 2024
1 parent 608bd19 commit cc58be2
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 15 deletions.
12 changes: 3 additions & 9 deletions examples/cmd/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,17 @@

import ChatTTS

from tools.audio import unsafe_float_to_int16, wav2
from tools.audio import wav_arr_to_mp3_view
from tools.logger import get_logger

logger = get_logger("Command")


def save_mp3_file(wav, index):
buf = BytesIO()
with wave.open(buf, "wb") as wf:
wf.setnchannels(1) # Mono channel
wf.setsampwidth(2) # Sample width in bytes
wf.setframerate(24000) # Sample rate in Hz
wf.writeframes(unsafe_float_to_int16(wav))
buf.seek(0, 0)
data = wav_arr_to_mp3_view(wav)
mp3_filename = f"output_audio_{index}.mp3"
with open(mp3_filename, "wb") as f:
wav2(buf, f, "mp3")
f.write(data)
logger.info(f"Audio saved to {mp3_filename}")


Expand Down
6 changes: 3 additions & 3 deletions examples/web/funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import gradio as gr
import numpy as np

from tools.audio import unsafe_float_to_int16
from tools.audio import wav_arr_to_mp3_view
from tools.logger import get_logger

logger = get_logger(" WebUI ")
Expand Down Expand Up @@ -146,10 +146,10 @@ def generate_audio(text, temperature, top_P, top_K, audio_seed_input, stream):
for gen in wav:
audio = gen[0]
if audio is not None and len(audio) > 0:
yield 24000, unsafe_float_to_int16(audio[0])
yield wav_arr_to_mp3_view(audio[0]).tobytes()
del audio
else:
yield 24000, unsafe_float_to_int16(np.array(wav[0]).flatten())
yield wav_arr_to_mp3_view(np.array(wav[0]).flatten()).tobytes()


def interrupt_generate():
Expand Down
1 change: 0 additions & 1 deletion examples/web/webui.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ def make_audio(autoplay, stream):
streaming=stream,
interactive=False,
show_label=True,
format="mp3",
)
generate_button.click(fn=set_buttons_before_generate, inputs=[generate_button, interrupt_button], outputs=[generate_button, interrupt_button]).then(
refine_text,
Expand Down
3 changes: 1 addition & 2 deletions tools/audio/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
from .np import unsafe_float_to_int16
from .av import wav2
from .mp3 import wav_arr_to_mp3_view
20 changes: 20 additions & 0 deletions tools/audio/mp3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import wave
from io import BytesIO

import numpy as np

from .np import unsafe_float_to_int16
from .av import wav2

def wav_arr_to_mp3_view(wav: np.ndarray):
buf = BytesIO()
with wave.open(buf, "wb") as wf:
wf.setnchannels(1) # Mono channel
wf.setsampwidth(2) # Sample width in bytes
wf.setframerate(24000) # Sample rate in Hz
wf.writeframes(unsafe_float_to_int16(wav))
buf.seek(0, 0)
buf2 = BytesIO()
wav2(buf, buf2, "mp3")
buf.seek(0, 0)
return buf2.getbuffer()

0 comments on commit cc58be2

Please sign in to comment.