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

Hide the waveform when playing recorded audio if show_recording_waveform is False #10405

Merged
merged 17 commits into from
Jan 24, 2025
8 changes: 8 additions & 0 deletions .changeset/afraid-things-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@gradio/audio": patch
"@gradio/chatbot": patch
"@gradio/multimodaltextbox": patch
"gradio": patch
---

fix:Hide the waveform when playing recorded audio if `show_recording_waveform` is `False`
14 changes: 9 additions & 5 deletions gradio/components/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ class WaveformOptions:
waveform_color: The color (as a hex string or valid CSS color) of the full waveform representing the amplitude of the audio. Defaults to a light gray color.
waveform_progress_color: The color (as a hex string or valid CSS color) that the waveform fills with to as the audio plays. Defaults to the accent color.
trim_region_color: The color (as a hex string or valid CSS color) of the trim region. Defaults to the accent color.
show_recording_waveform: Whether to show the waveform when recording audio. Defaults to True.
show_controls: Whether to show the standard HTML audio player below the waveform when recording audio or playing recorded audio. Defaults to False.
skip_length: The percentage (between 0 and 100) of the audio to skip when clicking on the skip forward / skip backward buttons. Defaults to 5.
sample_rate: The output sample rate (in Hz) of the audio after editing. Defaults to 44100.
show_recording_waveform: Whether to show the waveform when recording audio or playing audio.
show_controls: Whether to show the standard HTML audio player below the waveform when recording audio or playing audio. By default, this is set to False if `show_recording_waveform` is True, and vice versa.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is the most sensible way to handle the case when show_recording_waveform=False without adding a new parameter.

skip_length: The percentage (between 0 and 100) of the audio to skip when clicking on the skip forward / skip backward buttons.
sample_rate: The output sample rate (in Hz) of the audio after editing.
"""

waveform_color: str | None = None
waveform_progress_color: str | None = None
trim_region_color: str | None = None
show_recording_waveform: bool = True
show_controls: bool = False
show_controls: bool | None = None
skip_length: int | float = 5
sample_rate: int = 44100

Expand Down Expand Up @@ -190,6 +190,10 @@ def __init__(
self.waveform_options = WaveformOptions(**waveform_options)
else:
self.waveform_options = waveform_options
if self.waveform_options.show_controls is None:
self.waveform_options.show_controls = (
not self.waveform_options.show_recording_waveform
)
self.min_length = min_length
self.max_length = max_length
self.recording = recording
Expand Down
16 changes: 16 additions & 0 deletions js/audio/Audio.stories.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@
}}
/>

<Story
name="Audio Player without recording waveform"
args={{
value: {
path: "https://audio-samples.github.io/samples/mp3/blizzard_unconditional/sample-0.mp3",
url: "https://audio-samples.github.io/samples/mp3/blizzard_unconditional/sample-0.mp3",
orig_name: "sample-0.mp3"
},
waveform_options: {
show_recording_waveform: false,
show_controls: true
},
label: "Audio Player"
}}
/>

<Story
name="Audio Recorder"
args={{
Expand Down
5 changes: 4 additions & 1 deletion js/audio/Index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@
export let show_download_button: boolean;
export let show_share_button = false;
export let editable = true;
export let waveform_options: WaveformOptions = {};
export let waveform_options: WaveformOptions = {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added to preserve behavior for unit tests

show_recording_waveform: true,
show_controls: false
};
export let pending: boolean;
export let streaming: boolean;
export let stream_every: number;
Expand Down
18 changes: 13 additions & 5 deletions js/audio/player/AudioPlayer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@
load: undefined;
}>();

$: use_default_controls = waveform_options.show_controls || value?.is_stream;
$: use_waveform =
waveform_options.show_recording_waveform && !value?.is_stream;

const create_waveform = (): void => {
waveform = WaveSurfer.create({
container: container,
Expand Down Expand Up @@ -137,7 +141,13 @@
stream_active = false;
await resolve_wasm_src(data).then((resolved_src) => {
if (!resolved_src || value?.is_stream) return;
return waveform?.load(resolved_src);
if (waveform_options.show_recording_waveform) {
waveform?.load(resolved_src);
} else if (waveform_options.show_controls) {
if (audio_player) {
audio_player.src = resolved_src;
}
}
});
}

Expand Down Expand Up @@ -203,7 +213,7 @@

<audio
class="standard-player"
class:hidden={!(value && value.is_stream)}
class:hidden={use_waveform}
controls
autoplay={waveform_settings.autoplay}
on:load
Expand All @@ -215,7 +225,7 @@
<Empty size="small">
<Music />
</Empty>
{:else if !value.is_stream}
{:else if use_waveform}
<div
class="component-wrapper"
data-testid={label ? "waveform-" + label : "unlabelled-audio"}
Expand All @@ -238,7 +248,6 @@
</div>
</div>

<!-- {#if waveform} -->
<WaveformControls
{container}
{waveform}
Expand All @@ -256,7 +265,6 @@
{trim_region_settings}
{editable}
/>
<!-- {/if} -->
</div>
{/if}

Expand Down
4 changes: 4 additions & 0 deletions js/audio/shared/WaveformRecordControls.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@
border: 1px solid var(--block-border-color);
}

.duration-button {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a styling tweak for consistency:

image

border-radius: var(--button-large-radius);
}

.stop-button:disabled {
cursor: not-allowed;
}
Expand Down
7 changes: 5 additions & 2 deletions js/audio/static/StaticAudio.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
export let show_download_button = true;
export let show_share_button = false;
export let i18n: I18nFormatter;
export let waveform_settings: Record<string, any>;
export let waveform_options: WaveformOptions;
export let waveform_settings: Record<string, any> = {};
export let waveform_options: WaveformOptions = {
show_recording_waveform: true,
show_controls: false
};
export let editable = true;
export let loop: boolean;
export let display_icon_button_wrapper_top_corner = false;
Expand Down
1 change: 0 additions & 1 deletion js/chatbot/shared/Component.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@
{i18n}
label=""
waveform_settings={{ autoplay: props.autoplay }}
waveform_options={{}}
show_download_button={allow_file_downloads}
{display_icon_button_wrapper_top_corner}
on:load
Expand Down
5 changes: 4 additions & 1 deletion js/multimodaltextbox/shared/MultimodalTextbox.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@
export let file_count: "single" | "multiple" | "directory" = "multiple";
export let max_plain_text_length = 1000;
export let waveform_settings: Record<string, any>;
export let waveform_options: WaveformOptions = {};
export let waveform_options: WaveformOptions = {
Copy link
Member Author

@abidlabs abidlabs Jan 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

waveform_options and waveform_settings shouldn't even be props here, but when I started refactoring this, the PR quickly got out of hand. Will refactor the audio out of multimodaltextbox here in a separate PR

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add waveform_options as a param to multimodaltextbox.

show_recording_waveform: true,
show_controls: false
};
export let sources: ["microphone" | "upload"] = ["upload"];
export let active_source: "microphone" | null = null;
let upload_component: Upload;
Expand Down
Loading