Skip to content

Commit

Permalink
Fix audio streaming output issues in 4.0 (#6343)
Browse files Browse the repository at this point in the history
* changes

* Update processing_utils.py

* add changeset

* changes

* changes

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
  • Loading branch information
aliabid94 and gradio-pr-bot committed Nov 8, 2023
1 parent d64787b commit 37dd335
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .changeset/red-ties-turn.md
@@ -0,0 +1,6 @@
---
"@gradio/audio": minor
"gradio": minor
---

feat:Fix audio streaming output issues in 4.0
26 changes: 20 additions & 6 deletions js/audio/player/AudioPlayer.svelte
Expand Up @@ -10,6 +10,7 @@
import type { FileData } from "@gradio/client";
export let value: null | FileData = null;
$: url = value?.url;
export let label: string;
export let i18n: I18nFormatter;
export let dispatch: (event: any, detail?: any) => void;
Expand All @@ -18,12 +19,12 @@
event: "stream" | "change" | "stop_recording"
) => Promise<void> = () => Promise.resolve();
export let interactive = false;
export let waveform_settings = {};
export let waveform_settings: Record<string, any> = {};
export let mode = "";
export let handle_reset_value: () => void = () => {};
let container: HTMLDivElement;
let waveform: WaveSurfer;
let waveform: WaveSurfer | undefined;
let playing = false;
let timeRef: HTMLTimeElement;
Expand Down Expand Up @@ -84,12 +85,12 @@
end: number
): Promise<void> => {
mode = "";
const decodedData = waveform.getDecodedData();
const decodedData = waveform?.getDecodedData();
if (decodedData)
await process_audio(decodedData, start, end).then(
async (trimmedBlob: Uint8Array) => {
await dispatch_blob([trimmedBlob], "change");
waveform.destroy();
waveform?.destroy();
create_waveform();
}
);
Expand All @@ -98,15 +99,16 @@
async function load_audio(data: string): Promise<void> {
await resolve_wasm_src(data).then((resolved_src) => {
if (!resolved_src) return;
if (!resolved_src || value?.is_stream) return;
return waveform?.load(resolved_src);
});
}
$: value?.url && load_audio(value.url);
$: url && load_audio(url);
onMount(() => {
window.addEventListener("keydown", (e) => {
if (!waveform) return;
if (e.key === "ArrowRight" && mode !== "edit") {
skipAudio(waveform, 0.1);
} else if (e.key === "ArrowLeft" && mode !== "edit") {
Expand All @@ -120,6 +122,13 @@
<Empty size="small">
<Music />
</Empty>
{:else if value.is_stream}
<audio
class="standard-player"
src={value.url}
controls
autoplay={waveform_settings.autoplay}
/>
{:else}
<div
class="component-wrapper"
Expand Down Expand Up @@ -195,4 +204,9 @@
height: 100%;
position: relative;
}
.standard-player {
width: 100%;
padding: var(--size-2);
}
</style>

0 comments on commit 37dd335

Please sign in to comment.