gradio-app / gradio

Build and share delightful machine learning apps, all in Python. 🌟 Star to support our work!
http://www.gradio.app
Apache License 2.0
30.56k stars 2.27k forks source link

Gr.Microphone uses the microphone at all time even when you're not recording. #8550

Open chigkim opened 1 week ago

chigkim commented 1 week ago

Describe the bug

As soon as you initiate gr.Microphone, it triggers microphone permission, and starts using the microphone source for the entire session even if you click stop. This creates a problem on iOS because when the microphone is actively being used, it goes into a audio playback mode with lower quality. I'm not sure why, but I suspect maybe it does in order to run echo cancelation, noise reduction, etc. Once you have gr.Microphone in your code, and if you try to play a stereo audio, it plays in mono with lower quality even if when it's not recording. Try running this example on an iPhone twice: one with mic off and one with mic on. https://huggingface.co/spaces/chibop/mic_test

First, test with microphone off. Open the page, and click cancel when it asks for mic permission. Then click play music. Notice the music plays in stereo with full quality.

This time, test with microphone on. Refresh the page, and allow microphone permission. Record something and click stop. Then click "Play Music", (not the play button to play the audio you just recorded). Notice the music plays in mono with poor quality.

Another related bug is that you can't replay the music using the html audio player control. I can only trigger the playback with autoplay for some reason.

Have you searched existing issues? 🔎

Reproduction

If you want to try the script below locally, generate ssl keys and add the following to launch parameter. IOS doesn't seem to let you use microphone without ssl.

server_name="0.0.0.0", ssl_certfile="host.cert", ssl_keyfile="host.key", ssl_verify=False

import gradio as gr

def get_audio(audio):
    src = "https://onj.me/shorts/audio/02-Who%27s%20the%20bossa%21.mp3"
    return f"<audio src='{src}' autoplay controls></audio>"

with gr.Blocks(title="Mic Test") as demo:
    audio_element = gr.HTML()
    play = gr.Button("Play Music")
    play.click(get_audio, None, audio_element)
    mic = gr.Microphone(editable=False, waveform_options={"show_controls":False})
    mic.stop_recording(None, mic, None)

demo.launch()

Screenshot

No response

Logs

No response

System Info

4.36.0

Severity

I can work around it