Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audio streaming can't play before all data sent #8177

Open
fancyerii opened this issue Apr 30, 2024 · 1 comment
Open

Audio streaming can't play before all data sent #8177

fancyerii opened this issue Apr 30, 2024 · 1 comment
Labels
🎵 Audio Related to Audio component bug Something isn't working

Comments

@fancyerii
Copy link

fancyerii commented Apr 30, 2024

I want to let audio output to play before the whole wav file been sent. For example, I have a online tts model that create output audio incrementally every 100ms.

I follow the document here(https://www.gradio.app/guides/reactive-interfaces).
I found the play button of html audio player is not clickable until all wav data been sent. To simulate this, I decrease the chunk size and print time information. I am pretty sure that the player can't play before all wav data sent.

import gradio as gr
from pydub import AudioSegment
from time import sleep

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
                chunk_size = 1000
                from datetime import datetime
                while chunk_size * i < len(audio):
                    chunk = audio[chunk_size * i : chunk_size * (i + 1)]
                    i += 1
                    if chunk:
                        file = f"/tmp/{i}.{format}"
                        chunk.export(file, format=format)
                        s = datetime.today().strftime('%Y-%m-%d %H:%M:%S')
                        print(s)
                        yield file
                        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,
            )

        with gr.Column():
            stream_as_bytes_btn = gr.Button("Stream as Bytes")
            stream_as_bytes_output = gr.Audio(format="bytes", streaming=True)

            def stream_bytes(audio_file):
                chunk_size = 2_000
                from datetime import datetime
                with open(audio_file, "rb") as f:
                    while True:
                        chunk = f.read(chunk_size)
                        if chunk:
                            s = datetime.today().strftime('%Y-%m-%d %H:%M:%S')
                            print(s)
                            yield chunk
                            sleep(1)
                        else:
                            break
            stream_as_bytes_btn.click(stream_bytes, input_audio, stream_as_bytes_output)

if __name__ == "__main__":
    demo.queue().launch()

So Is there any method to achieve my goal?

@abidlabs abidlabs added 🎵 Audio Related to Audio component bug Something isn't working labels Apr 30, 2024
@ittech17
Copy link

I am also facing same issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🎵 Audio Related to Audio component bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants