2023-08-09 06:08:28 +08:00
|
|
|
import gradio as gr
|
|
|
|
from pydub import AudioSegment
|
2023-08-23 06:01:34 +08:00
|
|
|
from time import sleep
|
2023-08-09 06:08:28 +08:00
|
|
|
|
2023-08-23 06:01:34 +08:00
|
|
|
with gr.Blocks() as demo:
|
|
|
|
input_audio = gr.Audio(label="Input Audio", type="filepath", format="mp3")
|
|
|
|
with gr.Row():
|
|
|
|
with gr.Column():
|
|
|
|
stream_as_file_btn = gr.Button("Stream as File")
|
|
|
|
format = gr.Radio(["wav", "mp3"], value="wav", label="Format")
|
|
|
|
stream_as_file_output = gr.Audio(streaming=True)
|
|
|
|
|
|
|
|
def stream_file(audio_file, format):
|
|
|
|
audio = AudioSegment.from_file(audio_file)
|
|
|
|
i = 0
|
2023-08-24 02:47:07 +08:00
|
|
|
chunk_size = 1000
|
|
|
|
while chunk_size * i < len(audio):
|
|
|
|
chunk = audio[chunk_size * i : chunk_size * (i + 1)]
|
2023-08-23 06:01:34 +08:00
|
|
|
i += 1
|
|
|
|
if chunk:
|
|
|
|
file = f"/tmp/{i}.{format}"
|
|
|
|
chunk.export(file, format=format)
|
|
|
|
yield file
|
2023-08-24 02:47:07 +08:00
|
|
|
sleep(0.5)
|
|
|
|
|
|
|
|
stream_as_file_btn.click(
|
|
|
|
stream_file, [input_audio, format], stream_as_file_output
|
|
|
|
)
|
|
|
|
|
|
|
|
gr.Examples(
|
|
|
|
[["audio/cantina.wav", "wav"], ["audio/cantina.wav", "mp3"]],
|
|
|
|
[input_audio, format],
|
|
|
|
fn=stream_file,
|
|
|
|
outputs=stream_as_file_output,
|
|
|
|
)
|
2023-08-23 06:01:34 +08:00
|
|
|
|
|
|
|
with gr.Column():
|
|
|
|
stream_as_bytes_btn = gr.Button("Stream as Bytes")
|
2024-07-20 09:34:34 +08:00
|
|
|
stream_as_bytes_output = gr.Audio(streaming=True)
|
2023-08-23 06:01:34 +08:00
|
|
|
|
|
|
|
def stream_bytes(audio_file):
|
|
|
|
chunk_size = 20_000
|
|
|
|
with open(audio_file, "rb") as f:
|
|
|
|
while True:
|
|
|
|
chunk = f.read(chunk_size)
|
|
|
|
if chunk:
|
|
|
|
yield chunk
|
|
|
|
sleep(1)
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
stream_as_bytes_btn.click(stream_bytes, input_audio, stream_as_bytes_output)
|
2023-08-09 06:08:28 +08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2024-07-30 13:08:51 +08:00
|
|
|
demo.launch()
|