-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Audio.svelte
116 lines (105 loc) 路 2.65 KB
/
Audio.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<svelte:options accessors={true} />
<script lang="ts">
import { createEventDispatcher } from "svelte";
import { _ } from "svelte-i18n";
import UploadText from "../UploadText.svelte";
import type { FileData } from "@gradio/upload";
import type { LoadingStatus } from "../StatusTracker/types";
import { Audio, StaticAudio } from "@gradio/audio";
import StatusTracker from "../StatusTracker/StatusTracker.svelte";
import { Block } from "@gradio/atoms";
import { normalise_file } from "@gradio/upload";
const dispatch = createEventDispatcher<{
change: typeof value;
stream: typeof value;
error: string;
}>();
export let elem_id = "";
export let elem_classes: string[] = [];
export let visible = true;
export let mode: "static" | "dynamic";
export let value: null | FileData | string = null;
let old_value: null | FileData | string = null;
export let name: string;
export let source: "microphone" | "upload";
export let label: string;
export let root: string;
export let show_label: boolean;
export let pending: boolean;
export let streaming: boolean;
export let root_url: null | string;
export let container = false;
export let scale: number | null = null;
export let min_width: number | undefined = undefined;
export let loading_status: LoadingStatus;
export let autoplay = false;
export let show_share_button: boolean = false;
let _value: null | FileData;
$: _value = normalise_file(value, root, root_url);
$: {
if (JSON.stringify(value) !== JSON.stringify(old_value)) {
old_value = value;
dispatch("change");
}
}
let dragging: boolean;
</script>
<Block
variant={mode === "dynamic" && value === null && source === "upload"
? "dashed"
: "solid"}
border_mode={dragging ? "focus" : "base"}
padding={false}
{elem_id}
{elem_classes}
{visible}
{container}
{scale}
{min_width}
>
<StatusTracker {...loading_status} />
{#if mode === "dynamic"}
<Audio
{label}
{show_label}
value={_value}
on:change={({ detail }) => (value = detail)}
on:stream={({ detail }) => {
value = detail;
dispatch("stream", value);
}}
on:drag={({ detail }) => (dragging = detail)}
{name}
{source}
{pending}
{streaming}
{autoplay}
on:edit
on:play
on:pause
on:stop
on:end
on:start_recording
on:stop_recording
on:upload
on:error={({ detail }) => {
loading_status = loading_status || {};
loading_status.status = "error";
dispatch("error", detail);
}}
>
<UploadText type="audio" />
</Audio>
{:else}
<StaticAudio
{autoplay}
{show_label}
{show_share_button}
value={_value}
name={_value?.name || "audio_file"}
{label}
on:share
on:error
/>
{/if}
</Block>