File size: 2,660 Bytes
c1bb5d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<script lang="ts">
	import { uploadToHuggingFace } from "@gradio/utils";
	import { Empty } from "@gradio/atoms";
	import {
		ShareButton,
		IconButton,
		BlockLabel,
		DownloadLink,
		IconButtonWrapper
	} from "@gradio/atoms";
	import { Download, Music } from "@gradio/icons";
	import type { I18nFormatter } from "@gradio/utils";
	import AudioPlayer from "../player/AudioPlayer.svelte";
	import MinimalAudioPlayer from "../shared/MinimalAudioPlayer.svelte";
	import { createEventDispatcher } from "svelte";
	import type { FileData } from "@gradio/client";
	import type { WaveformOptions, SubtitleData } from "../shared/types";

	export let value: null | FileData = null;
	export let subtitles: null | FileData | SubtitleData[] = null;
	export let label: string;
	export let show_label = true;
	export let buttons: string[] | null = null;
	export let i18n: I18nFormatter;
	export let waveform_settings: Record<string, any> = {};
	export let waveform_options: WaveformOptions = {
		show_recording_waveform: true
	};
	export let editable = true;
	export let loop: boolean;
	export let display_icon_button_wrapper_top_corner = false;
	export let minimal = false;

	const dispatch = createEventDispatcher<{
		change: FileData;
		play: undefined;
		pause: undefined;
		end: undefined;
		stop: undefined;
	}>();

	$: value && dispatch("change", value);
</script>

<BlockLabel
	{show_label}
	Icon={Music}
	float={false}
	label={label || i18n("audio.audio")}
/>

{#if value !== null}
	{#if minimal}
		<MinimalAudioPlayer {value} label={label || i18n("audio.audio")} {loop} />
	{:else}
		<IconButtonWrapper
			display_top_corner={display_icon_button_wrapper_top_corner}
		>
			{#if buttons === null ? true : buttons.includes("download")}
				<DownloadLink
					href={value.is_stream
						? value.url?.replace("playlist.m3u8", "playlist-file")
						: value.url}
					download={value.orig_name || value.path}
				>
					<IconButton Icon={Download} label={i18n("common.download")} />
				</DownloadLink>
			{/if}
			{#if buttons === null ? true : buttons.includes("share")}
				<ShareButton
					{i18n}
					on:error
					on:share
					formatter={async (value) => {
						if (!value) return "";
						let url = await uploadToHuggingFace(value.url, "url");
						return `<audio controls src="${url}"></audio>`;
					}}
					{value}
				/>
			{/if}
		</IconButtonWrapper>

		<AudioPlayer
			{value}
			subtitles={Array.isArray(subtitles) ? subtitles : subtitles?.url}
			{label}
			{i18n}
			{waveform_settings}
			{waveform_options}
			{editable}
			{loop}
			on:pause
			on:play
			on:stop
			on:load
		/>
	{/if}
{:else}
	<Empty size="small">
		<Music />
	</Empty>
{/if}