Upload folder using huggingface_hub
Browse files- 6.0.0-dev.4/audio/Example.svelte +19 -0
- 6.0.0-dev.4/audio/Index.svelte +233 -0
- 6.0.0-dev.4/audio/index.ts +7 -0
- 6.0.0-dev.4/audio/interactive/InteractiveAudio.svelte +357 -0
- 6.0.0-dev.4/audio/package.json +61 -0
- 6.0.0-dev.4/audio/player/AudioPlayer.svelte +463 -0
- 6.0.0-dev.4/audio/recorder/AudioRecorder.svelte +293 -0
- 6.0.0-dev.4/audio/shared/Audio.svelte +20 -0
- 6.0.0-dev.4/audio/shared/DeviceSelect.svelte +71 -0
- 6.0.0-dev.4/audio/shared/VolumeControl.svelte +77 -0
- 6.0.0-dev.4/audio/shared/VolumeLevels.svelte +12 -0
- 6.0.0-dev.4/audio/shared/WaveformControls.svelte +434 -0
- 6.0.0-dev.4/audio/shared/WaveformRecordControls.svelte +301 -0
- 6.0.0-dev.4/audio/shared/audioBufferToWav.ts +59 -0
- 6.0.0-dev.4/audio/shared/index.ts +1 -0
- 6.0.0-dev.4/audio/shared/types.ts +57 -0
- 6.0.0-dev.4/audio/shared/utils.ts +79 -0
- 6.0.0-dev.4/audio/static/StaticAudio.svelte +97 -0
- 6.0.0-dev.4/audio/streaming/StreamAudio.svelte +205 -0
- 6.0.0-dev.4/audio/streaming/media_recorder.ts +14 -0
6.0.0-dev.4/audio/Example.svelte
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts">
|
| 2 |
+
export let value: string | null;
|
| 3 |
+
export let type: "gallery" | "table";
|
| 4 |
+
export let selected = false;
|
| 5 |
+
</script>
|
| 6 |
+
|
| 7 |
+
<div
|
| 8 |
+
class:table={type === "table"}
|
| 9 |
+
class:gallery={type === "gallery"}
|
| 10 |
+
class:selected
|
| 11 |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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<svelte:options accessors={true} />
|
| 2 |
+
|
| 3 |
+
<script lang="ts">
|
| 4 |
+
import { tick } from "svelte";
|
| 5 |
+
import { Gradio } from "@gradio/utils";
|
| 6 |
+
import { StatusTracker } from "@gradio/statustracker";
|
| 7 |
+
import type { LoadingStatus } from "@gradio/statustracker";
|
| 8 |
+
import { onMount } from "svelte";
|
| 9 |
+
|
| 10 |
+
import StaticAudio from "./static/StaticAudio.svelte";
|
| 11 |
+
import InteractiveAudio from "./interactive/InteractiveAudio.svelte";
|
| 12 |
+
import { Block, UploadText } from "@gradio/atoms";
|
| 13 |
+
import type { AudioEvents, AudioProps } from "./shared/types";
|
| 14 |
+
|
| 15 |
+
let props = $props();
|
| 16 |
+
let upload_promise = $state<Promise<any>>();
|
| 17 |
+
|
| 18 |
+
props.props.stream_every = 0.1; // default to 0.1s stream interval
|
| 19 |
+
|
| 20 |
+
class AudioGradio extends Gradio<AudioEvents, AudioProps> {
|
| 21 |
+
async get_data() {
|
| 22 |
+
if (upload_promise) {
|
| 23 |
+
await upload_promise;
|
| 24 |
+
await tick();
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
const data = await super.get_data();
|
| 28 |
+
|
| 29 |
+
return data;
|
| 30 |
+
}
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
const gradio = new AudioGradio(props);
|
| 34 |
+
let label = $derived(gradio.shared.label || gradio.i18n("audio.audio"));
|
| 35 |
+
|
| 36 |
+
// let uploading = $state(false);
|
| 37 |
+
let active_source = $derived.by(() =>
|
| 38 |
+
gradio.props.sources ? gradio.props.sources[0] : null
|
| 39 |
+
);
|
| 40 |
+
let initial_value = gradio.props.value;
|
| 41 |
+
|
| 42 |
+
const handle_reset_value = (): void => {
|
| 43 |
+
if (initial_value === null || gradio.props.value === initial_value) {
|
| 44 |
+
return;
|
| 45 |
+
}
|
| 46 |
+
gradio.props.value = initial_value;
|
| 47 |
+
};
|
| 48 |
+
|
| 49 |
+
let dragging: boolean;
|
| 50 |
+
|
| 51 |
+
let color_accent = "darkorange";
|
| 52 |
+
|
| 53 |
+
let waveform_settings = $derived({
|
| 54 |
+
height: 50,
|
| 55 |
+
barWidth: 2,
|
| 56 |
+
barGap: 3,
|
| 57 |
+
cursorWidth: 2,
|
| 58 |
+
cursorColor: "#ddd5e9",
|
| 59 |
+
autoplay: gradio.props.autoplay,
|
| 60 |
+
barRadius: 10,
|
| 61 |
+
dragToSeek: true,
|
| 62 |
+
normalize: true,
|
| 63 |
+
minPxPerSec: 20
|
| 64 |
+
});
|
| 65 |
+
|
| 66 |
+
const trim_region_settings = {
|
| 67 |
+
color: gradio.props.waveform_options.trim_region_color,
|
| 68 |
+
drag: true,
|
| 69 |
+
resize: true
|
| 70 |
+
};
|
| 71 |
+
|
| 72 |
+
function set_trim_region_colour(): void {
|
| 73 |
+
document.documentElement.style.setProperty(
|
| 74 |
+
"--trim-region-color",
|
| 75 |
+
trim_region_settings.color || color_accent
|
| 76 |
+
);
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
function handle_error({ detail }: CustomEvent<string>): void {
|
| 80 |
+
const [level, status] = detail.includes("Invalid file type")
|
| 81 |
+
? ["warning", "complete"]
|
| 82 |
+
: ["error", "error"];
|
| 83 |
+
if (gradio.shared.loading_status) {
|
| 84 |
+
gradio.shared.loading_status.status = status as LoadingStatus["status"];
|
| 85 |
+
gradio.shared.loading_status.message = detail;
|
| 86 |
+
}
|
| 87 |
+
gradio.dispatch(level as "error" | "warning", detail);
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
let old_value = $state(gradio.props.value);
|
| 91 |
+
$effect(() => {
|
| 92 |
+
if (old_value != gradio.props.value) {
|
| 93 |
+
old_value = gradio.props.value;
|
| 94 |
+
gradio.dispatch("change");
|
| 95 |
+
}
|
| 96 |
+
});
|
| 97 |
+
|
| 98 |
+
onMount(() => {
|
| 99 |
+
color_accent = getComputedStyle(document?.documentElement).getPropertyValue(
|
| 100 |
+
"--color-accent"
|
| 101 |
+
);
|
| 102 |
+
set_trim_region_colour();
|
| 103 |
+
waveform_settings.waveColor =
|
| 104 |
+
gradio.props.waveform_options.waveform_color || "#9ca3af";
|
| 105 |
+
waveform_settings.progressColor =
|
| 106 |
+
gradio.props.waveform_options.waveform_progress_color || color_accent;
|
| 107 |
+
waveform_settings.mediaControls =
|
| 108 |
+
gradio.props.waveform_options.show_controls;
|
| 109 |
+
waveform_settings.sampleRate =
|
| 110 |
+
gradio.props.waveform_options.sample_rate || 44100;
|
| 111 |
+
});
|
| 112 |
+
</script>
|
| 113 |
+
|
| 114 |
+
{#if !gradio.shared.interactive}
|
| 115 |
+
<Block
|
| 116 |
+
variant={"solid"}
|
| 117 |
+
border_mode={dragging ? "focus" : "base"}
|
| 118 |
+
padding={false}
|
| 119 |
+
allow_overflow={false}
|
| 120 |
+
elem_id={gradio.shared.elem_id}
|
| 121 |
+
elem_classes={gradio.shared.elem_classes}
|
| 122 |
+
visible={gradio.shared.visible}
|
| 123 |
+
container={gradio.shared.container}
|
| 124 |
+
scale={gradio.shared.scale}
|
| 125 |
+
min_width={gradio.shared.min_width}
|
| 126 |
+
>
|
| 127 |
+
<StatusTracker
|
| 128 |
+
autoscroll={gradio.shared.autoscroll}
|
| 129 |
+
i18n={gradio.i18n}
|
| 130 |
+
{...gradio.shared.loading_status}
|
| 131 |
+
on:clear_status={() =>
|
| 132 |
+
gradio.dispatch("clear_status", gradio.shared.loading_status)}
|
| 133 |
+
/>
|
| 134 |
+
|
| 135 |
+
<StaticAudio
|
| 136 |
+
i18n={gradio.i18n}
|
| 137 |
+
show_label={gradio.shared.show_label}
|
| 138 |
+
buttons={gradio.props.buttons ?? ["download", "share"]}
|
| 139 |
+
value={gradio.props.value}
|
| 140 |
+
subtitles={gradio.props.subtitles}
|
| 141 |
+
{label}
|
| 142 |
+
loop={gradio.props.loop}
|
| 143 |
+
{waveform_settings}
|
| 144 |
+
waveform_options={gradio.props.waveform_options}
|
| 145 |
+
editable={gradio.props.editable}
|
| 146 |
+
on:share={(e) => gradio.dispatch("share", e.detail)}
|
| 147 |
+
on:error={(e) => gradio.dispatch("error", e.detail)}
|
| 148 |
+
on:play={() => gradio.dispatch("play")}
|
| 149 |
+
on:pause={() => gradio.dispatch("pause")}
|
| 150 |
+
on:stop={() => gradio.dispatch("stop")}
|
| 151 |
+
/>
|
| 152 |
+
</Block>
|
| 153 |
+
{:else}
|
| 154 |
+
<Block
|
| 155 |
+
variant={gradio.props.value === null && active_source === "upload"
|
| 156 |
+
? "dashed"
|
| 157 |
+
: "solid"}
|
| 158 |
+
border_mode={dragging ? "focus" : "base"}
|
| 159 |
+
padding={false}
|
| 160 |
+
allow_overflow={false}
|
| 161 |
+
elem_id={gradio.shared.elem_id}
|
| 162 |
+
elem_classes={gradio.shared.elem_classes}
|
| 163 |
+
visible={gradio.shared.visible}
|
| 164 |
+
container={gradio.shared.container}
|
| 165 |
+
scale={gradio.shared.scale}
|
| 166 |
+
min_width={gradio.shared.min_width}
|
| 167 |
+
>
|
| 168 |
+
<StatusTracker
|
| 169 |
+
autoscroll={gradio.shared.autoscroll}
|
| 170 |
+
i18n={gradio.i18n}
|
| 171 |
+
{...gradio.shared.loading_status}
|
| 172 |
+
on:clear_status={() =>
|
| 173 |
+
gradio.dispatch("clear_status", gradio.shared.loading_status)}
|
| 174 |
+
/>
|
| 175 |
+
<InteractiveAudio
|
| 176 |
+
bind:upload_promise
|
| 177 |
+
bind:initial_value
|
| 178 |
+
{label}
|
| 179 |
+
show_label={gradio.shared.show_label}
|
| 180 |
+
buttons={gradio.props.buttons ?? []}
|
| 181 |
+
value={gradio.props.value}
|
| 182 |
+
subtitles={gradio.props.subtitles}
|
| 183 |
+
on:change={({ detail }) => (gradio.props.value = detail)}
|
| 184 |
+
on:stream={({ detail }) => {
|
| 185 |
+
gradio.props.value = detail;
|
| 186 |
+
gradio.dispatch("stream", gradio.props.value);
|
| 187 |
+
}}
|
| 188 |
+
on:drag={({ detail }) => (dragging = detail)}
|
| 189 |
+
root={gradio.shared.root}
|
| 190 |
+
sources={gradio.props.sources}
|
| 191 |
+
{active_source}
|
| 192 |
+
pending={gradio.shared.loading_status.pending}
|
| 193 |
+
streaming={gradio.props.streaming}
|
| 194 |
+
bind:recording={gradio.props.recording}
|
| 195 |
+
loop={gradio.props.loop}
|
| 196 |
+
max_file_size={gradio.shared.max_file_size}
|
| 197 |
+
{handle_reset_value}
|
| 198 |
+
editable={gradio.props.editable}
|
| 199 |
+
bind:dragging
|
| 200 |
+
on:edit={() => gradio.dispatch("edit")}
|
| 201 |
+
on:play={() => gradio.dispatch("play")}
|
| 202 |
+
on:pause={() => gradio.dispatch("pause")}
|
| 203 |
+
on:stop={() => gradio.dispatch("stop")}
|
| 204 |
+
on:start_recording={() => gradio.dispatch("start_recording")}
|
| 205 |
+
on:pause_recording={() => gradio.dispatch("pause_recording")}
|
| 206 |
+
on:stop_recording={(e) => {
|
| 207 |
+
gradio.dispatch("stop_recording");
|
| 208 |
+
gradio.dispatch("input");
|
| 209 |
+
}}
|
| 210 |
+
on:upload={() => {
|
| 211 |
+
gradio.dispatch("upload");
|
| 212 |
+
gradio.dispatch("input");
|
| 213 |
+
}}
|
| 214 |
+
on:clear={() => {
|
| 215 |
+
gradio.dispatch("clear");
|
| 216 |
+
gradio.dispatch("input");
|
| 217 |
+
}}
|
| 218 |
+
on:error={handle_error}
|
| 219 |
+
on:close_stream={() => gradio.dispatch("close_stream", "stream")}
|
| 220 |
+
i18n={gradio.i18n}
|
| 221 |
+
{waveform_settings}
|
| 222 |
+
waveform_options={gradio.props.waveform_options}
|
| 223 |
+
{trim_region_settings}
|
| 224 |
+
stream_every={gradio.props.stream_every}
|
| 225 |
+
stream_state={gradio.shared.loading_status.stream_state}
|
| 226 |
+
upload={(...args) => gradio.shared.client.upload(...args)}
|
| 227 |
+
stream_handler={(...args) => gradio.shared.client.stream(...args)}
|
| 228 |
+
time_limit={gradio.shared.loading_status.time_limit}
|
| 229 |
+
>
|
| 230 |
+
<UploadText i18n={gradio.i18n} type="audio" />
|
| 231 |
+
</InteractiveAudio>
|
| 232 |
+
</Block>
|
| 233 |
+
{/if}
|
6.0.0-dev.4/audio/index.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { default as Index } from "./Index.svelte";
|
| 2 |
+
export default Index;
|
| 3 |
+
export { default as BaseStaticAudio } from "./static/StaticAudio.svelte";
|
| 4 |
+
export { default as BaseInteractiveAudio } from "./interactive/InteractiveAudio.svelte";
|
| 5 |
+
export { default as BasePlayer } from "./player/AudioPlayer.svelte";
|
| 6 |
+
export type { WaveformOptions } from "./shared/types";
|
| 7 |
+
export { default as BaseExample } from "./Example.svelte";
|
6.0.0-dev.4/audio/interactive/InteractiveAudio.svelte
ADDED
|
@@ -0,0 +1,357 @@
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts">
|
| 2 |
+
import { onDestroy, createEventDispatcher, tick } from "svelte";
|
| 3 |
+
import { Upload, ModifyUpload } from "@gradio/upload";
|
| 4 |
+
import { prepare_files, type FileData, type Client } from "@gradio/client";
|
| 5 |
+
import { BlockLabel } from "@gradio/atoms";
|
| 6 |
+
import { Music } from "@gradio/icons";
|
| 7 |
+
import { StreamingBar } from "@gradio/statustracker";
|
| 8 |
+
import AudioPlayer from "../player/AudioPlayer.svelte";
|
| 9 |
+
|
| 10 |
+
import type { IBlobEvent, IMediaRecorder } from "extendable-media-recorder";
|
| 11 |
+
import type { I18nFormatter } from "js/core/src/gradio_helper";
|
| 12 |
+
import AudioRecorder from "../recorder/AudioRecorder.svelte";
|
| 13 |
+
import StreamAudio from "../streaming/StreamAudio.svelte";
|
| 14 |
+
import { init_media_recorder } from "../streaming/media_recorder";
|
| 15 |
+
import type { IMediaRecorderConstructor } from "extendable-media-recorder";
|
| 16 |
+
import { SelectSource } from "@gradio/atoms";
|
| 17 |
+
import type { WaveformOptions, SubtitleData } from "../shared/types";
|
| 18 |
+
|
| 19 |
+
export let value: null | FileData = null;
|
| 20 |
+
export let subtitles: null | FileData | SubtitleData[] = null;
|
| 21 |
+
export let label: string;
|
| 22 |
+
export let root: string;
|
| 23 |
+
export let loop: boolean;
|
| 24 |
+
export let show_label = true;
|
| 25 |
+
export let buttons: string[] = ["download", "share"];
|
| 26 |
+
export let sources:
|
| 27 |
+
| ["microphone"]
|
| 28 |
+
| ["upload"]
|
| 29 |
+
| ["microphone", "upload"]
|
| 30 |
+
| ["upload", "microphone"] = ["microphone", "upload"];
|
| 31 |
+
export let pending = false;
|
| 32 |
+
export let streaming = false;
|
| 33 |
+
export let i18n: I18nFormatter;
|
| 34 |
+
export let waveform_settings: Record<string, any>;
|
| 35 |
+
export let trim_region_settings = {};
|
| 36 |
+
export let waveform_options: WaveformOptions = {};
|
| 37 |
+
export let dragging: boolean;
|
| 38 |
+
export let active_source: "microphone" | "upload";
|
| 39 |
+
export let handle_reset_value: () => void = () => {};
|
| 40 |
+
export let editable = true;
|
| 41 |
+
export let max_file_size: number | null = null;
|
| 42 |
+
export let upload: Client["upload"];
|
| 43 |
+
export let stream_handler: Client["stream"];
|
| 44 |
+
export let stream_every: number = 0.1;
|
| 45 |
+
export let uploading = false;
|
| 46 |
+
export let recording = false;
|
| 47 |
+
export let class_name = "";
|
| 48 |
+
export let upload_promise: Promise<any> | null = null;
|
| 49 |
+
export let initial_value: FileData | null = null;
|
| 50 |
+
|
| 51 |
+
export let time_limit: number | null = null;
|
| 52 |
+
export let stream_state: "open" | "waiting" | "closed" = "closed";
|
| 53 |
+
|
| 54 |
+
$: dispatch("drag", dragging);
|
| 55 |
+
|
| 56 |
+
// TODO: make use of this
|
| 57 |
+
// export let type: "normal" | "numpy" = "normal";
|
| 58 |
+
let recorder: IMediaRecorder;
|
| 59 |
+
let mode = "";
|
| 60 |
+
let header: Uint8Array | undefined = undefined;
|
| 61 |
+
let pending_stream: Uint8Array[] = [];
|
| 62 |
+
let submit_pending_stream_on_pending_end = false;
|
| 63 |
+
let inited = false;
|
| 64 |
+
let streaming_media_recorder: IMediaRecorderConstructor;
|
| 65 |
+
|
| 66 |
+
const NUM_HEADER_BYTES = 44;
|
| 67 |
+
let audio_chunks: Blob[] = [];
|
| 68 |
+
const is_browser = typeof window !== "undefined";
|
| 69 |
+
if (is_browser && streaming) {
|
| 70 |
+
init_media_recorder().then((a) => {
|
| 71 |
+
streaming_media_recorder = a;
|
| 72 |
+
});
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
const dispatch = createEventDispatcher<{
|
| 76 |
+
change: FileData | null;
|
| 77 |
+
stream: FileData;
|
| 78 |
+
edit: never;
|
| 79 |
+
play: never;
|
| 80 |
+
pause: never;
|
| 81 |
+
stop: never;
|
| 82 |
+
end: never;
|
| 83 |
+
drag: boolean;
|
| 84 |
+
error: string;
|
| 85 |
+
upload: FileData;
|
| 86 |
+
clear: undefined;
|
| 87 |
+
start_recording: undefined;
|
| 88 |
+
pause_recording: undefined;
|
| 89 |
+
stop_recording: undefined;
|
| 90 |
+
close_stream: undefined;
|
| 91 |
+
}>();
|
| 92 |
+
|
| 93 |
+
const to_blob_parts = (parts: Uint8Array[] | Blob[]): BlobPart[] =>
|
| 94 |
+
parts.map((part) => {
|
| 95 |
+
if (part instanceof Blob) return part;
|
| 96 |
+
return part.slice();
|
| 97 |
+
});
|
| 98 |
+
|
| 99 |
+
const dispatch_blob = async (
|
| 100 |
+
blobs: Uint8Array[] | Blob[],
|
| 101 |
+
event: "stream" | "change" | "stop_recording"
|
| 102 |
+
): Promise<void> => {
|
| 103 |
+
let _audio_blob = new File(to_blob_parts(blobs), "audio.wav", {
|
| 104 |
+
type: "audio/wav"
|
| 105 |
+
});
|
| 106 |
+
if (_audio_blob.size === 0) {
|
| 107 |
+
return;
|
| 108 |
+
}
|
| 109 |
+
const val = await prepare_files([_audio_blob], event === "stream");
|
| 110 |
+
initial_value = value;
|
| 111 |
+
value = (
|
| 112 |
+
(await upload(val, root, undefined, max_file_size || undefined))?.filter(
|
| 113 |
+
Boolean
|
| 114 |
+
) as FileData[]
|
| 115 |
+
)[0];
|
| 116 |
+
dispatch(event, value);
|
| 117 |
+
};
|
| 118 |
+
|
| 119 |
+
onDestroy(() => {
|
| 120 |
+
if (streaming && recorder && recorder.state !== "inactive") {
|
| 121 |
+
recorder.stop();
|
| 122 |
+
}
|
| 123 |
+
});
|
| 124 |
+
|
| 125 |
+
async function prepare_audio(): Promise<void> {
|
| 126 |
+
let stream: MediaStream | null;
|
| 127 |
+
|
| 128 |
+
try {
|
| 129 |
+
stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
| 130 |
+
} catch (err) {
|
| 131 |
+
if (!navigator.mediaDevices) {
|
| 132 |
+
dispatch("error", i18n("audio.no_device_support"));
|
| 133 |
+
return;
|
| 134 |
+
}
|
| 135 |
+
if (err instanceof DOMException && err.name == "NotAllowedError") {
|
| 136 |
+
dispatch("error", i18n("audio.allow_recording_access"));
|
| 137 |
+
return;
|
| 138 |
+
}
|
| 139 |
+
throw err;
|
| 140 |
+
}
|
| 141 |
+
if (stream == null) return;
|
| 142 |
+
if (streaming) {
|
| 143 |
+
recorder = new streaming_media_recorder(stream, {
|
| 144 |
+
mimeType: "audio/wav"
|
| 145 |
+
});
|
| 146 |
+
|
| 147 |
+
recorder.addEventListener("dataavailable", handle_chunk);
|
| 148 |
+
} else {
|
| 149 |
+
recorder = new MediaRecorder(stream);
|
| 150 |
+
recorder.addEventListener("dataavailable", (event) => {
|
| 151 |
+
audio_chunks.push(event.data);
|
| 152 |
+
});
|
| 153 |
+
}
|
| 154 |
+
recorder.addEventListener("stop", async () => {
|
| 155 |
+
recording = false;
|
| 156 |
+
recorder.stop();
|
| 157 |
+
await dispatch_blob(audio_chunks, "change");
|
| 158 |
+
await dispatch_blob(audio_chunks, "stop_recording");
|
| 159 |
+
audio_chunks = [];
|
| 160 |
+
});
|
| 161 |
+
inited = true;
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
async function handle_chunk(event: IBlobEvent): Promise<void> {
|
| 165 |
+
let buffer = await event.data.arrayBuffer();
|
| 166 |
+
let payload = new Uint8Array(buffer);
|
| 167 |
+
if (!header) {
|
| 168 |
+
header = new Uint8Array(buffer.slice(0, NUM_HEADER_BYTES));
|
| 169 |
+
payload = new Uint8Array(buffer.slice(NUM_HEADER_BYTES));
|
| 170 |
+
}
|
| 171 |
+
if (pending) {
|
| 172 |
+
pending_stream.push(payload);
|
| 173 |
+
} else {
|
| 174 |
+
let blobParts = [header].concat(pending_stream, [payload]);
|
| 175 |
+
if (!recording || stream_state === "waiting") return;
|
| 176 |
+
dispatch_blob(blobParts, "stream");
|
| 177 |
+
pending_stream = [];
|
| 178 |
+
}
|
| 179 |
+
}
|
| 180 |
+
|
| 181 |
+
$: if (submit_pending_stream_on_pending_end && pending === false) {
|
| 182 |
+
submit_pending_stream_on_pending_end = false;
|
| 183 |
+
if (header && pending_stream) {
|
| 184 |
+
let blobParts: Uint8Array[] = [header].concat(pending_stream);
|
| 185 |
+
pending_stream = [];
|
| 186 |
+
dispatch_blob(blobParts, "stream");
|
| 187 |
+
}
|
| 188 |
+
}
|
| 189 |
+
|
| 190 |
+
async function record(): Promise<void> {
|
| 191 |
+
recording = true;
|
| 192 |
+
dispatch("start_recording");
|
| 193 |
+
if (!inited) await prepare_audio();
|
| 194 |
+
|
| 195 |
+
header = undefined;
|
| 196 |
+
if (streaming && recorder.state != "recording") {
|
| 197 |
+
recorder.start(stream_every * 1000);
|
| 198 |
+
}
|
| 199 |
+
}
|
| 200 |
+
|
| 201 |
+
function clear(): void {
|
| 202 |
+
dispatch("change", null);
|
| 203 |
+
dispatch("clear");
|
| 204 |
+
mode = "";
|
| 205 |
+
value = null;
|
| 206 |
+
}
|
| 207 |
+
|
| 208 |
+
function handle_load({ detail }: { detail: FileData }): void {
|
| 209 |
+
value = detail;
|
| 210 |
+
dispatch("change", detail);
|
| 211 |
+
dispatch("upload", detail);
|
| 212 |
+
}
|
| 213 |
+
|
| 214 |
+
async function stop(): Promise<void> {
|
| 215 |
+
recording = false;
|
| 216 |
+
|
| 217 |
+
if (streaming) {
|
| 218 |
+
dispatch("close_stream");
|
| 219 |
+
dispatch("stop_recording");
|
| 220 |
+
recorder.stop();
|
| 221 |
+
|
| 222 |
+
if (pending) {
|
| 223 |
+
submit_pending_stream_on_pending_end = true;
|
| 224 |
+
}
|
| 225 |
+
dispatch_blob(audio_chunks, "stop_recording");
|
| 226 |
+
dispatch("clear");
|
| 227 |
+
mode = "";
|
| 228 |
+
}
|
| 229 |
+
}
|
| 230 |
+
|
| 231 |
+
$: if (!recording && recorder) stop();
|
| 232 |
+
$: if (recording && recorder) record();
|
| 233 |
+
</script>
|
| 234 |
+
|
| 235 |
+
<BlockLabel
|
| 236 |
+
{show_label}
|
| 237 |
+
Icon={Music}
|
| 238 |
+
float={active_source === "upload" && value === null}
|
| 239 |
+
label={label || i18n("audio.audio")}
|
| 240 |
+
/>
|
| 241 |
+
<div class="audio-container {class_name}">
|
| 242 |
+
<StreamingBar {time_limit} />
|
| 243 |
+
{#if value === null || streaming}
|
| 244 |
+
{#if active_source === "microphone"}
|
| 245 |
+
<ModifyUpload {i18n} on:clear={clear} />
|
| 246 |
+
{#if streaming}
|
| 247 |
+
<StreamAudio
|
| 248 |
+
{record}
|
| 249 |
+
{recording}
|
| 250 |
+
{stop}
|
| 251 |
+
{i18n}
|
| 252 |
+
{waveform_settings}
|
| 253 |
+
{waveform_options}
|
| 254 |
+
waiting={stream_state === "waiting"}
|
| 255 |
+
/>
|
| 256 |
+
{:else}
|
| 257 |
+
<AudioRecorder
|
| 258 |
+
bind:mode
|
| 259 |
+
{i18n}
|
| 260 |
+
{editable}
|
| 261 |
+
{recording}
|
| 262 |
+
{dispatch_blob}
|
| 263 |
+
{waveform_settings}
|
| 264 |
+
{waveform_options}
|
| 265 |
+
{handle_reset_value}
|
| 266 |
+
on:start_recording
|
| 267 |
+
on:pause_recording
|
| 268 |
+
on:stop_recording
|
| 269 |
+
/>
|
| 270 |
+
{/if}
|
| 271 |
+
{:else if active_source === "upload"}
|
| 272 |
+
<!-- explicitly listed out audio mimetypes due to iOS bug not recognizing audio/* -->
|
| 273 |
+
<Upload
|
| 274 |
+
bind:upload_promise
|
| 275 |
+
filetype="audio/aac,audio/midi,audio/mpeg,audio/ogg,audio/wav,audio/x-wav,audio/opus,audio/webm,audio/flac,audio/vnd.rn-realaudio,audio/x-ms-wma,audio/x-aiff,audio/amr,audio/*"
|
| 276 |
+
on:load={handle_load}
|
| 277 |
+
bind:dragging
|
| 278 |
+
bind:uploading
|
| 279 |
+
on:error={({ detail }) => dispatch("error", detail)}
|
| 280 |
+
{root}
|
| 281 |
+
{max_file_size}
|
| 282 |
+
{upload}
|
| 283 |
+
{stream_handler}
|
| 284 |
+
aria_label={i18n("audio.drop_to_upload")}
|
| 285 |
+
>
|
| 286 |
+
<slot />
|
| 287 |
+
</Upload>
|
| 288 |
+
{/if}
|
| 289 |
+
{:else}
|
| 290 |
+
<ModifyUpload
|
| 291 |
+
{i18n}
|
| 292 |
+
on:clear={clear}
|
| 293 |
+
on:edit={() => (mode = "edit")}
|
| 294 |
+
download={buttons === null
|
| 295 |
+
? value.url
|
| 296 |
+
: buttons.includes("download")
|
| 297 |
+
? value.url
|
| 298 |
+
: null}
|
| 299 |
+
/>
|
| 300 |
+
|
| 301 |
+
<AudioPlayer
|
| 302 |
+
bind:mode
|
| 303 |
+
{value}
|
| 304 |
+
subtitles={Array.isArray(subtitles) ? subtitles : subtitles?.url}
|
| 305 |
+
{label}
|
| 306 |
+
{i18n}
|
| 307 |
+
{dispatch_blob}
|
| 308 |
+
{waveform_settings}
|
| 309 |
+
{waveform_options}
|
| 310 |
+
{trim_region_settings}
|
| 311 |
+
{handle_reset_value}
|
| 312 |
+
{editable}
|
| 313 |
+
{loop}
|
| 314 |
+
interactive
|
| 315 |
+
on:stop
|
| 316 |
+
on:play
|
| 317 |
+
on:pause
|
| 318 |
+
on:edit
|
| 319 |
+
/>
|
| 320 |
+
{/if}
|
| 321 |
+
<SelectSource {sources} bind:active_source handle_clear={clear} />
|
| 322 |
+
</div>
|
| 323 |
+
|
| 324 |
+
<style>
|
| 325 |
+
.audio-container {
|
| 326 |
+
height: calc(var(--size-full) - var(--size-6));
|
| 327 |
+
display: flex;
|
| 328 |
+
flex-direction: column;
|
| 329 |
+
justify-content: space-between;
|
| 330 |
+
}
|
| 331 |
+
|
| 332 |
+
.audio-container.compact-audio {
|
| 333 |
+
margin-top: calc(var(--size-8) * -1);
|
| 334 |
+
height: auto;
|
| 335 |
+
padding: 0px;
|
| 336 |
+
gap: var(--size-2);
|
| 337 |
+
min-height: var(--size-5);
|
| 338 |
+
}
|
| 339 |
+
|
| 340 |
+
.compact-audio :global(.audio-player) {
|
| 341 |
+
padding: 0px;
|
| 342 |
+
}
|
| 343 |
+
|
| 344 |
+
.compact-audio :global(.controls) {
|
| 345 |
+
gap: 0px;
|
| 346 |
+
padding: 0px;
|
| 347 |
+
}
|
| 348 |
+
|
| 349 |
+
.compact-audio :global(.waveform-container) {
|
| 350 |
+
height: var(--size-12) !important;
|
| 351 |
+
}
|
| 352 |
+
|
| 353 |
+
.compact-audio :global(.player-container) {
|
| 354 |
+
min-height: unset;
|
| 355 |
+
height: auto;
|
| 356 |
+
}
|
| 357 |
+
</style>
|
6.0.0-dev.4/audio/package.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "@gradio/audio",
|
| 3 |
+
"version": "0.19.4-dev.3",
|
| 4 |
+
"description": "Gradio UI packages",
|
| 5 |
+
"type": "module",
|
| 6 |
+
"author": "",
|
| 7 |
+
"license": "ISC",
|
| 8 |
+
"private": false,
|
| 9 |
+
"dependencies": {
|
| 10 |
+
"@gradio/atoms": "workspace:^",
|
| 11 |
+
"@gradio/button": "workspace:^",
|
| 12 |
+
"@gradio/client": "workspace:^",
|
| 13 |
+
"@gradio/icons": "workspace:^",
|
| 14 |
+
"@gradio/statustracker": "workspace:^",
|
| 15 |
+
"@gradio/upload": "workspace:^",
|
| 16 |
+
"@gradio/utils": "workspace:^",
|
| 17 |
+
"@types/wavesurfer.js": "6.0.10",
|
| 18 |
+
"extendable-media-recorder": "9.0.0",
|
| 19 |
+
"extendable-media-recorder-wav-encoder": "7.0.76",
|
| 20 |
+
"hls.js": "1.5.13",
|
| 21 |
+
"resize-observer-polyfill": "1.5.1",
|
| 22 |
+
"svelte-range-slider-pips": "4.1.0",
|
| 23 |
+
"wavesurfer.js": "7.4.2"
|
| 24 |
+
},
|
| 25 |
+
"devDependencies": {
|
| 26 |
+
"@gradio/preview": "workspace:^"
|
| 27 |
+
},
|
| 28 |
+
"main_changeset": true,
|
| 29 |
+
"main": "index.ts",
|
| 30 |
+
"exports": {
|
| 31 |
+
"./package.json": "./package.json",
|
| 32 |
+
".": {
|
| 33 |
+
"gradio": "./index.ts",
|
| 34 |
+
"svelte": "./dist/index.js",
|
| 35 |
+
"types": "./dist/index.d.ts"
|
| 36 |
+
},
|
| 37 |
+
"./example": {
|
| 38 |
+
"gradio": "./Example.svelte",
|
| 39 |
+
"svelte": "./dist/Example.svelte",
|
| 40 |
+
"types": "./dist/Example.svelte.d.ts"
|
| 41 |
+
},
|
| 42 |
+
"./shared": {
|
| 43 |
+
"gradio": "./shared/index.ts",
|
| 44 |
+
"svelte": "./dist/shared/index.js",
|
| 45 |
+
"types": "./dist/shared/index.d.ts"
|
| 46 |
+
},
|
| 47 |
+
"./base": {
|
| 48 |
+
"gradio": "./static/StaticAudio.svelte",
|
| 49 |
+
"svelte": "./dist/static/StaticAudio.svelte",
|
| 50 |
+
"types": "./dist/static/StaticAudio.svelte.d.ts"
|
| 51 |
+
}
|
| 52 |
+
},
|
| 53 |
+
"peerDependencies": {
|
| 54 |
+
"svelte": "^5.43.4"
|
| 55 |
+
},
|
| 56 |
+
"repository": {
|
| 57 |
+
"type": "git",
|
| 58 |
+
"url": "git+https://github.com/gradio-app/gradio.git",
|
| 59 |
+
"directory": "js/audio"
|
| 60 |
+
}
|
| 61 |
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts">
|
| 2 |
+
import { onMount } from "svelte";
|
| 3 |
+
import { Music } from "@gradio/icons";
|
| 4 |
+
import { format_time, type I18nFormatter } from "@gradio/utils";
|
| 5 |
+
import WaveSurfer from "wavesurfer.js";
|
| 6 |
+
import { skip_audio, process_audio } from "../shared/utils";
|
| 7 |
+
import WaveformControls from "../shared/WaveformControls.svelte";
|
| 8 |
+
import { Empty } from "@gradio/atoms";
|
| 9 |
+
import type { FileData } from "@gradio/client";
|
| 10 |
+
import type { WaveformOptions, SubtitleData } from "../shared/types";
|
| 11 |
+
import { createEventDispatcher } from "svelte";
|
| 12 |
+
|
| 13 |
+
import Hls from "hls.js";
|
| 14 |
+
|
| 15 |
+
export let value: null | FileData = null;
|
| 16 |
+
export let subtitles: null | string | SubtitleData[] = null;
|
| 17 |
+
$: url = value?.url;
|
| 18 |
+
export let label: string;
|
| 19 |
+
export let i18n: I18nFormatter;
|
| 20 |
+
export let dispatch_blob: (
|
| 21 |
+
blobs: Uint8Array[] | Blob[],
|
| 22 |
+
event: "stream" | "change" | "stop_recording"
|
| 23 |
+
) => Promise<void> = () => Promise.resolve();
|
| 24 |
+
export let interactive = false;
|
| 25 |
+
export let editable = true;
|
| 26 |
+
export let trim_region_settings = {};
|
| 27 |
+
export let waveform_settings: Record<string, any>;
|
| 28 |
+
export let waveform_options: WaveformOptions;
|
| 29 |
+
export let mode = "";
|
| 30 |
+
export let loop: boolean;
|
| 31 |
+
export let handle_reset_value: () => void = () => {};
|
| 32 |
+
|
| 33 |
+
let container: HTMLDivElement;
|
| 34 |
+
let waveform: WaveSurfer | undefined;
|
| 35 |
+
let waveform_component_wrapper: HTMLDivElement;
|
| 36 |
+
let playing = false;
|
| 37 |
+
|
| 38 |
+
let subtitle_container: HTMLDivElement;
|
| 39 |
+
|
| 40 |
+
let timeRef: HTMLTimeElement;
|
| 41 |
+
let durationRef: HTMLTimeElement;
|
| 42 |
+
let audio_duration: number;
|
| 43 |
+
|
| 44 |
+
let trimDuration = 0;
|
| 45 |
+
|
| 46 |
+
let show_volume_slider = false;
|
| 47 |
+
let audio_player: HTMLAudioElement;
|
| 48 |
+
|
| 49 |
+
let stream_active = false;
|
| 50 |
+
let subtitles_toggle = true;
|
| 51 |
+
let subtitle_event_handlers: (() => void)[] = [];
|
| 52 |
+
|
| 53 |
+
const dispatch = createEventDispatcher<{
|
| 54 |
+
stop: undefined;
|
| 55 |
+
play: undefined;
|
| 56 |
+
pause: undefined;
|
| 57 |
+
edit: undefined;
|
| 58 |
+
end: undefined;
|
| 59 |
+
load: undefined;
|
| 60 |
+
}>();
|
| 61 |
+
|
| 62 |
+
$: use_waveform =
|
| 63 |
+
waveform_options.show_recording_waveform && !value?.is_stream;
|
| 64 |
+
|
| 65 |
+
const create_waveform = (): void => {
|
| 66 |
+
waveform = WaveSurfer.create({
|
| 67 |
+
container: container,
|
| 68 |
+
...waveform_settings
|
| 69 |
+
});
|
| 70 |
+
|
| 71 |
+
if (subtitles && waveform) {
|
| 72 |
+
if (subtitles_toggle) {
|
| 73 |
+
add_subtitles_to_waveform(waveform, subtitles);
|
| 74 |
+
} else {
|
| 75 |
+
hide_subtitles();
|
| 76 |
+
}
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
if (value?.url && waveform) {
|
| 80 |
+
waveform.load(value?.url);
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
waveform?.on("decode", (duration: any) => {
|
| 84 |
+
audio_duration = duration;
|
| 85 |
+
durationRef && (durationRef.textContent = format_time(duration));
|
| 86 |
+
});
|
| 87 |
+
|
| 88 |
+
waveform?.on(
|
| 89 |
+
"timeupdate",
|
| 90 |
+
(currentTime: any) =>
|
| 91 |
+
timeRef && (timeRef.textContent = format_time(currentTime))
|
| 92 |
+
);
|
| 93 |
+
|
| 94 |
+
waveform?.on("ready", () => {
|
| 95 |
+
if (!waveform_settings.autoplay) {
|
| 96 |
+
waveform?.stop();
|
| 97 |
+
} else {
|
| 98 |
+
waveform?.play();
|
| 99 |
+
}
|
| 100 |
+
});
|
| 101 |
+
|
| 102 |
+
waveform?.on("finish", () => {
|
| 103 |
+
if (loop) {
|
| 104 |
+
waveform?.play();
|
| 105 |
+
} else {
|
| 106 |
+
playing = false;
|
| 107 |
+
dispatch("stop");
|
| 108 |
+
}
|
| 109 |
+
});
|
| 110 |
+
waveform?.on("pause", () => {
|
| 111 |
+
playing = false;
|
| 112 |
+
dispatch("pause");
|
| 113 |
+
});
|
| 114 |
+
waveform?.on("play", () => {
|
| 115 |
+
playing = true;
|
| 116 |
+
dispatch("play");
|
| 117 |
+
});
|
| 118 |
+
|
| 119 |
+
waveform?.on("load", () => {
|
| 120 |
+
dispatch("load");
|
| 121 |
+
});
|
| 122 |
+
};
|
| 123 |
+
|
| 124 |
+
$: if (use_waveform && container !== undefined && container !== null) {
|
| 125 |
+
if (waveform !== undefined) waveform.destroy();
|
| 126 |
+
container.innerHTML = "";
|
| 127 |
+
create_waveform();
|
| 128 |
+
playing = false;
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
const handle_trim_audio = async (
|
| 132 |
+
start: number,
|
| 133 |
+
end: number
|
| 134 |
+
): Promise<void> => {
|
| 135 |
+
mode = "";
|
| 136 |
+
const decodedData = waveform?.getDecodedData();
|
| 137 |
+
if (decodedData)
|
| 138 |
+
await process_audio(
|
| 139 |
+
decodedData,
|
| 140 |
+
start,
|
| 141 |
+
end,
|
| 142 |
+
waveform_settings.sampleRate
|
| 143 |
+
).then(async (trimmedBlob: Uint8Array) => {
|
| 144 |
+
await dispatch_blob([trimmedBlob], "change");
|
| 145 |
+
waveform?.destroy();
|
| 146 |
+
container.innerHTML = "";
|
| 147 |
+
});
|
| 148 |
+
dispatch("edit");
|
| 149 |
+
};
|
| 150 |
+
|
| 151 |
+
async function load_audio(data: string): Promise<void> {
|
| 152 |
+
stream_active = false;
|
| 153 |
+
|
| 154 |
+
if (waveform_options.show_recording_waveform) {
|
| 155 |
+
waveform?.load(data);
|
| 156 |
+
} else if (audio_player) {
|
| 157 |
+
audio_player.src = data;
|
| 158 |
+
}
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
$: if (subtitles && waveform) {
|
| 162 |
+
if (subtitles_toggle) {
|
| 163 |
+
add_subtitles_to_waveform(waveform, subtitles);
|
| 164 |
+
} else {
|
| 165 |
+
hide_subtitles();
|
| 166 |
+
}
|
| 167 |
+
}
|
| 168 |
+
|
| 169 |
+
function load_stream(value: FileData | null): void {
|
| 170 |
+
if (!value || !value.is_stream || !value.url) return;
|
| 171 |
+
|
| 172 |
+
if (Hls.isSupported() && !stream_active) {
|
| 173 |
+
// Set config to start playback after 1 second of data received
|
| 174 |
+
const hls = new Hls({
|
| 175 |
+
maxBufferLength: 1,
|
| 176 |
+
maxMaxBufferLength: 1,
|
| 177 |
+
lowLatencyMode: true
|
| 178 |
+
});
|
| 179 |
+
hls.loadSource(value.url);
|
| 180 |
+
hls.attachMedia(audio_player);
|
| 181 |
+
hls.on(Hls.Events.MANIFEST_PARSED, function () {
|
| 182 |
+
if (waveform_settings.autoplay) audio_player.play();
|
| 183 |
+
});
|
| 184 |
+
hls.on(Hls.Events.ERROR, function (event, data) {
|
| 185 |
+
console.error("HLS error:", event, data);
|
| 186 |
+
if (data.fatal) {
|
| 187 |
+
switch (data.type) {
|
| 188 |
+
case Hls.ErrorTypes.NETWORK_ERROR:
|
| 189 |
+
console.error(
|
| 190 |
+
"Fatal network error encountered, trying to recover"
|
| 191 |
+
);
|
| 192 |
+
hls.startLoad();
|
| 193 |
+
break;
|
| 194 |
+
case Hls.ErrorTypes.MEDIA_ERROR:
|
| 195 |
+
console.error("Fatal media error encountered, trying to recover");
|
| 196 |
+
hls.recoverMediaError();
|
| 197 |
+
break;
|
| 198 |
+
default:
|
| 199 |
+
console.error("Fatal error, cannot recover");
|
| 200 |
+
hls.destroy();
|
| 201 |
+
break;
|
| 202 |
+
}
|
| 203 |
+
}
|
| 204 |
+
});
|
| 205 |
+
stream_active = true;
|
| 206 |
+
} else if (!stream_active) {
|
| 207 |
+
audio_player.src = value.url;
|
| 208 |
+
if (waveform_settings.autoplay) audio_player.play();
|
| 209 |
+
stream_active = true;
|
| 210 |
+
}
|
| 211 |
+
}
|
| 212 |
+
|
| 213 |
+
$: if (audio_player && url) {
|
| 214 |
+
load_audio(url);
|
| 215 |
+
}
|
| 216 |
+
|
| 217 |
+
$: if (audio_player && value?.is_stream) {
|
| 218 |
+
load_stream(value);
|
| 219 |
+
}
|
| 220 |
+
|
| 221 |
+
onMount(() => {
|
| 222 |
+
window.addEventListener("keydown", (e) => {
|
| 223 |
+
if (!waveform || show_volume_slider) return;
|
| 224 |
+
|
| 225 |
+
const is_focused_in_waveform =
|
| 226 |
+
waveform_component_wrapper &&
|
| 227 |
+
waveform_component_wrapper.contains(document.activeElement);
|
| 228 |
+
if (!is_focused_in_waveform) return;
|
| 229 |
+
if (e.key === "ArrowRight" && mode !== "edit") {
|
| 230 |
+
skip_audio(waveform, 0.1);
|
| 231 |
+
} else if (e.key === "ArrowLeft" && mode !== "edit") {
|
| 232 |
+
skip_audio(waveform, -0.1);
|
| 233 |
+
}
|
| 234 |
+
});
|
| 235 |
+
});
|
| 236 |
+
|
| 237 |
+
async function add_subtitles_to_waveform(
|
| 238 |
+
wavesurfer: WaveSurfer,
|
| 239 |
+
subtitle_data: string | SubtitleData[]
|
| 240 |
+
): Promise<void> {
|
| 241 |
+
clear_subtitles();
|
| 242 |
+
try {
|
| 243 |
+
let subtitles: SubtitleData[];
|
| 244 |
+
if (Array.isArray(subtitle_data)) {
|
| 245 |
+
subtitles = subtitle_data;
|
| 246 |
+
} else {
|
| 247 |
+
const response = await fetch(subtitle_data);
|
| 248 |
+
const subtitle_content = await response.text();
|
| 249 |
+
subtitles = parse_subtitles(subtitle_content);
|
| 250 |
+
}
|
| 251 |
+
|
| 252 |
+
if (subtitles.length > 0) {
|
| 253 |
+
let current_subtitle = "";
|
| 254 |
+
if (subtitle_container) {
|
| 255 |
+
subtitle_container.style.display = "";
|
| 256 |
+
const audioProcessHandler = (time: number): void => {
|
| 257 |
+
const subtitle = subtitles.find(
|
| 258 |
+
(s) => time >= s.start && time <= s.end
|
| 259 |
+
);
|
| 260 |
+
if (subtitle && subtitle.text !== current_subtitle) {
|
| 261 |
+
current_subtitle = subtitle.text;
|
| 262 |
+
subtitle_container.textContent = current_subtitle;
|
| 263 |
+
} else if (!subtitle && current_subtitle !== "") {
|
| 264 |
+
current_subtitle = "";
|
| 265 |
+
subtitle_container.textContent = "";
|
| 266 |
+
}
|
| 267 |
+
};
|
| 268 |
+
wavesurfer.on("audioprocess", audioProcessHandler);
|
| 269 |
+
subtitle_event_handlers.push(() => {
|
| 270 |
+
wavesurfer.un("audioprocess", audioProcessHandler);
|
| 271 |
+
});
|
| 272 |
+
}
|
| 273 |
+
}
|
| 274 |
+
} catch (error) {}
|
| 275 |
+
}
|
| 276 |
+
|
| 277 |
+
function hide_subtitles(): void {
|
| 278 |
+
if (subtitle_container) {
|
| 279 |
+
subtitle_container.style.display = "none";
|
| 280 |
+
}
|
| 281 |
+
}
|
| 282 |
+
|
| 283 |
+
function clear_subtitles(): void {
|
| 284 |
+
if (subtitle_container) {
|
| 285 |
+
subtitle_container.textContent = "";
|
| 286 |
+
}
|
| 287 |
+
subtitle_event_handlers.forEach((handler) => handler());
|
| 288 |
+
subtitle_event_handlers = [];
|
| 289 |
+
}
|
| 290 |
+
|
| 291 |
+
function parse_subtitles(subtitle_content: string): SubtitleData[] {
|
| 292 |
+
const lines = subtitle_content.split("\n");
|
| 293 |
+
const subtitles: SubtitleData[] = [];
|
| 294 |
+
|
| 295 |
+
for (let i = 0; i < lines.length; i++) {
|
| 296 |
+
const line = lines[i].trim();
|
| 297 |
+
if (line.includes(" --> ")) {
|
| 298 |
+
const [start_time, end_time] = line.split(" --> ");
|
| 299 |
+
const start = parse_time_to_seconds(start_time);
|
| 300 |
+
const end = parse_time_to_seconds(end_time);
|
| 301 |
+
|
| 302 |
+
let text = "";
|
| 303 |
+
for (let j = i + 1; j < lines.length && lines[j].trim() !== ""; j++) {
|
| 304 |
+
if (text) text += " ";
|
| 305 |
+
text += lines[j].trim();
|
| 306 |
+
}
|
| 307 |
+
|
| 308 |
+
if (text) {
|
| 309 |
+
subtitles.push({ start, end, text });
|
| 310 |
+
}
|
| 311 |
+
}
|
| 312 |
+
}
|
| 313 |
+
|
| 314 |
+
return subtitles;
|
| 315 |
+
}
|
| 316 |
+
|
| 317 |
+
function parse_time_to_seconds(time_str: string): number {
|
| 318 |
+
const parts = time_str.split(":");
|
| 319 |
+
if (parts.length === 3) {
|
| 320 |
+
const hours = parseInt(parts[0]);
|
| 321 |
+
const minutes = parseInt(parts[1]);
|
| 322 |
+
const seconds = parseFloat(parts[2]);
|
| 323 |
+
return hours * 3600 + minutes * 60 + seconds;
|
| 324 |
+
}
|
| 325 |
+
return 0;
|
| 326 |
+
}
|
| 327 |
+
</script>
|
| 328 |
+
|
| 329 |
+
<audio
|
| 330 |
+
class="standard-player"
|
| 331 |
+
class:hidden={use_waveform}
|
| 332 |
+
controls
|
| 333 |
+
autoplay={waveform_settings.autoplay}
|
| 334 |
+
on:load
|
| 335 |
+
bind:this={audio_player}
|
| 336 |
+
on:ended={() => dispatch("stop")}
|
| 337 |
+
on:play={() => dispatch("play")}
|
| 338 |
+
preload="metadata"
|
| 339 |
+
/>
|
| 340 |
+
{#if value === null}
|
| 341 |
+
<Empty size="small">
|
| 342 |
+
<Music />
|
| 343 |
+
</Empty>
|
| 344 |
+
{:else if use_waveform}
|
| 345 |
+
<div
|
| 346 |
+
class="component-wrapper"
|
| 347 |
+
data-testid={label ? "waveform-" + label : "unlabelled-audio"}
|
| 348 |
+
bind:this={waveform_component_wrapper}
|
| 349 |
+
>
|
| 350 |
+
<div class="waveform-container">
|
| 351 |
+
<div
|
| 352 |
+
id="waveform"
|
| 353 |
+
bind:this={container}
|
| 354 |
+
style:height={container ? null : "58px"}
|
| 355 |
+
/>
|
| 356 |
+
</div>
|
| 357 |
+
|
| 358 |
+
<div class="timestamps">
|
| 359 |
+
<time bind:this={timeRef} id="time">0:00</time>
|
| 360 |
+
<div>
|
| 361 |
+
{#if mode === "edit" && trimDuration > 0}
|
| 362 |
+
<time id="trim-duration">{format_time(trimDuration)}</time>
|
| 363 |
+
{/if}
|
| 364 |
+
<time bind:this={durationRef} id="duration">0:00</time>
|
| 365 |
+
</div>
|
| 366 |
+
</div>
|
| 367 |
+
|
| 368 |
+
<div
|
| 369 |
+
bind:this={subtitle_container}
|
| 370 |
+
class="subtitle-display"
|
| 371 |
+
data-testid="subtitle-display"
|
| 372 |
+
></div>
|
| 373 |
+
|
| 374 |
+
<WaveformControls
|
| 375 |
+
{container}
|
| 376 |
+
{waveform}
|
| 377 |
+
{playing}
|
| 378 |
+
{audio_duration}
|
| 379 |
+
{i18n}
|
| 380 |
+
{interactive}
|
| 381 |
+
{handle_trim_audio}
|
| 382 |
+
bind:mode
|
| 383 |
+
bind:trimDuration
|
| 384 |
+
bind:show_volume_slider
|
| 385 |
+
bind:subtitles_toggle
|
| 386 |
+
show_redo={interactive}
|
| 387 |
+
{handle_reset_value}
|
| 388 |
+
{waveform_options}
|
| 389 |
+
{trim_region_settings}
|
| 390 |
+
{editable}
|
| 391 |
+
show_subtitles={subtitles !== null}
|
| 392 |
+
/>
|
| 393 |
+
</div>
|
| 394 |
+
{/if}
|
| 395 |
+
|
| 396 |
+
<style>
|
| 397 |
+
.component-wrapper {
|
| 398 |
+
padding: var(--size-3);
|
| 399 |
+
width: 100%;
|
| 400 |
+
}
|
| 401 |
+
|
| 402 |
+
:global(::part(wrapper)) {
|
| 403 |
+
margin-bottom: var(--size-2);
|
| 404 |
+
}
|
| 405 |
+
|
| 406 |
+
.timestamps {
|
| 407 |
+
display: flex;
|
| 408 |
+
justify-content: space-between;
|
| 409 |
+
align-items: center;
|
| 410 |
+
width: 100%;
|
| 411 |
+
padding: var(--size-1) 0;
|
| 412 |
+
}
|
| 413 |
+
|
| 414 |
+
#time {
|
| 415 |
+
color: var(--neutral-400);
|
| 416 |
+
}
|
| 417 |
+
|
| 418 |
+
#duration {
|
| 419 |
+
color: var(--neutral-400);
|
| 420 |
+
}
|
| 421 |
+
|
| 422 |
+
#trim-duration {
|
| 423 |
+
color: var(--color-accent);
|
| 424 |
+
margin-right: var(--spacing-sm);
|
| 425 |
+
}
|
| 426 |
+
.waveform-container {
|
| 427 |
+
display: flex;
|
| 428 |
+
align-items: center;
|
| 429 |
+
justify-content: center;
|
| 430 |
+
width: var(--size-full);
|
| 431 |
+
}
|
| 432 |
+
|
| 433 |
+
#waveform {
|
| 434 |
+
width: 100%;
|
| 435 |
+
height: 100%;
|
| 436 |
+
position: relative;
|
| 437 |
+
}
|
| 438 |
+
|
| 439 |
+
.standard-player {
|
| 440 |
+
width: 100%;
|
| 441 |
+
padding: var(--size-2);
|
| 442 |
+
}
|
| 443 |
+
|
| 444 |
+
.subtitle-display {
|
| 445 |
+
color: var(--text-secondary);
|
| 446 |
+
font-size: var(--text-lg);
|
| 447 |
+
text-align: center;
|
| 448 |
+
max-width: 600px;
|
| 449 |
+
line-height: 1.3;
|
| 450 |
+
min-height: var(--size-4);
|
| 451 |
+
font-family: var(--font-sans);
|
| 452 |
+
font-weight: normal;
|
| 453 |
+
margin: var(--size-2) auto;
|
| 454 |
+
padding: var(--size-1) var(--size-2);
|
| 455 |
+
border-radius: 2px;
|
| 456 |
+
transition: opacity 0.2s ease-in-out;
|
| 457 |
+
}
|
| 458 |
+
|
| 459 |
+
.hidden,
|
| 460 |
+
.subtitle-display:empty {
|
| 461 |
+
display: none;
|
| 462 |
+
}
|
| 463 |
+
</style>
|
6.0.0-dev.4/audio/recorder/AudioRecorder.svelte
ADDED
|
@@ -0,0 +1,293 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts">
|
| 2 |
+
import { onMount } from "svelte";
|
| 3 |
+
import type { I18nFormatter } from "@gradio/utils";
|
| 4 |
+
import { createEventDispatcher } from "svelte";
|
| 5 |
+
import WaveSurfer from "wavesurfer.js";
|
| 6 |
+
import { skip_audio, process_audio } from "../shared/utils";
|
| 7 |
+
import WSRecord from "wavesurfer.js/dist/plugins/record.js";
|
| 8 |
+
import WaveformControls from "../shared/WaveformControls.svelte";
|
| 9 |
+
import WaveformRecordControls from "../shared/WaveformRecordControls.svelte";
|
| 10 |
+
import RecordPlugin from "wavesurfer.js/dist/plugins/record.js";
|
| 11 |
+
import type { WaveformOptions } from "../shared/types";
|
| 12 |
+
import { format_time } from "@gradio/utils";
|
| 13 |
+
|
| 14 |
+
export let mode: string;
|
| 15 |
+
export let i18n: I18nFormatter;
|
| 16 |
+
export let dispatch_blob: (
|
| 17 |
+
blobs: Uint8Array[] | Blob[],
|
| 18 |
+
event: "stream" | "change" | "stop_recording"
|
| 19 |
+
) => Promise<void> | undefined;
|
| 20 |
+
export let waveform_settings: Record<string, any>;
|
| 21 |
+
export let waveform_options: WaveformOptions = {
|
| 22 |
+
show_recording_waveform: true
|
| 23 |
+
};
|
| 24 |
+
export let handle_reset_value: () => void;
|
| 25 |
+
export let editable = true;
|
| 26 |
+
export let recording = false;
|
| 27 |
+
|
| 28 |
+
let micWaveform: WaveSurfer;
|
| 29 |
+
let recordingWaveform: WaveSurfer;
|
| 30 |
+
let playing = false;
|
| 31 |
+
|
| 32 |
+
let recordingContainer: HTMLDivElement;
|
| 33 |
+
let microphoneContainer: HTMLDivElement;
|
| 34 |
+
|
| 35 |
+
let record: WSRecord;
|
| 36 |
+
let recordedAudio: string | null = null;
|
| 37 |
+
|
| 38 |
+
// timestamps
|
| 39 |
+
let timeRef: HTMLTimeElement;
|
| 40 |
+
let durationRef: HTMLTimeElement;
|
| 41 |
+
let audio_duration: number;
|
| 42 |
+
let seconds = 0;
|
| 43 |
+
let interval: NodeJS.Timeout;
|
| 44 |
+
let timing = false;
|
| 45 |
+
// trimming
|
| 46 |
+
let trimDuration = 0;
|
| 47 |
+
|
| 48 |
+
const start_interval = (): void => {
|
| 49 |
+
clearInterval(interval);
|
| 50 |
+
interval = setInterval(() => {
|
| 51 |
+
seconds++;
|
| 52 |
+
}, 1000);
|
| 53 |
+
};
|
| 54 |
+
|
| 55 |
+
const dispatch = createEventDispatcher<{
|
| 56 |
+
start_recording: undefined;
|
| 57 |
+
pause_recording: undefined;
|
| 58 |
+
stop_recording: undefined;
|
| 59 |
+
stop: undefined;
|
| 60 |
+
play: undefined;
|
| 61 |
+
pause: undefined;
|
| 62 |
+
end: undefined;
|
| 63 |
+
edit: undefined;
|
| 64 |
+
}>();
|
| 65 |
+
|
| 66 |
+
function record_start_callback(): void {
|
| 67 |
+
start_interval();
|
| 68 |
+
timing = true;
|
| 69 |
+
dispatch("start_recording");
|
| 70 |
+
if (waveform_options.show_recording_waveform) {
|
| 71 |
+
let waveformCanvas = microphoneContainer;
|
| 72 |
+
if (waveformCanvas) waveformCanvas.style.display = "block";
|
| 73 |
+
}
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
async function record_end_callback(blob: Blob): Promise<void> {
|
| 77 |
+
seconds = 0;
|
| 78 |
+
timing = false;
|
| 79 |
+
clearInterval(interval);
|
| 80 |
+
try {
|
| 81 |
+
const array_buffer = await blob.arrayBuffer();
|
| 82 |
+
const context = new AudioContext({
|
| 83 |
+
sampleRate: waveform_settings.sampleRate
|
| 84 |
+
});
|
| 85 |
+
const audio_buffer = await context.decodeAudioData(array_buffer);
|
| 86 |
+
|
| 87 |
+
if (audio_buffer)
|
| 88 |
+
await process_audio(audio_buffer).then(async (audio: Uint8Array) => {
|
| 89 |
+
await dispatch_blob([audio], "change");
|
| 90 |
+
await dispatch_blob([audio], "stop_recording");
|
| 91 |
+
});
|
| 92 |
+
} catch (e) {
|
| 93 |
+
console.error(e);
|
| 94 |
+
}
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
$: record?.on("record-resume", () => {
|
| 98 |
+
start_interval();
|
| 99 |
+
});
|
| 100 |
+
|
| 101 |
+
$: recordingWaveform?.on("decode", (duration: any) => {
|
| 102 |
+
audio_duration = duration;
|
| 103 |
+
durationRef && (durationRef.textContent = format_time(duration));
|
| 104 |
+
});
|
| 105 |
+
|
| 106 |
+
$: recordingWaveform?.on(
|
| 107 |
+
"timeupdate",
|
| 108 |
+
(currentTime: any) =>
|
| 109 |
+
timeRef && (timeRef.textContent = format_time(currentTime))
|
| 110 |
+
);
|
| 111 |
+
|
| 112 |
+
$: recordingWaveform?.on("pause", () => {
|
| 113 |
+
dispatch("pause");
|
| 114 |
+
playing = false;
|
| 115 |
+
});
|
| 116 |
+
|
| 117 |
+
$: recordingWaveform?.on("play", () => {
|
| 118 |
+
dispatch("play");
|
| 119 |
+
playing = true;
|
| 120 |
+
});
|
| 121 |
+
|
| 122 |
+
$: recordingWaveform?.on("finish", () => {
|
| 123 |
+
dispatch("stop");
|
| 124 |
+
playing = false;
|
| 125 |
+
});
|
| 126 |
+
|
| 127 |
+
let record_mounted = false;
|
| 128 |
+
|
| 129 |
+
const create_mic_waveform = (): void => {
|
| 130 |
+
if (microphoneContainer) microphoneContainer.innerHTML = "";
|
| 131 |
+
if (micWaveform !== undefined) micWaveform.destroy();
|
| 132 |
+
if (!microphoneContainer) return;
|
| 133 |
+
micWaveform = WaveSurfer.create({
|
| 134 |
+
...waveform_settings,
|
| 135 |
+
normalize: false,
|
| 136 |
+
container: microphoneContainer
|
| 137 |
+
});
|
| 138 |
+
|
| 139 |
+
record = micWaveform.registerPlugin(RecordPlugin.create());
|
| 140 |
+
record?.on("record-end", record_end_callback);
|
| 141 |
+
record?.on("record-start", record_start_callback);
|
| 142 |
+
record?.on("record-pause", () => {
|
| 143 |
+
dispatch("pause_recording");
|
| 144 |
+
clearInterval(interval);
|
| 145 |
+
});
|
| 146 |
+
|
| 147 |
+
record?.on("record-end", (blob) => {
|
| 148 |
+
recordedAudio = URL.createObjectURL(blob);
|
| 149 |
+
|
| 150 |
+
const microphone = microphoneContainer;
|
| 151 |
+
const recording = recordingContainer;
|
| 152 |
+
|
| 153 |
+
if (microphone) microphone.style.display = "none";
|
| 154 |
+
if (recording && recordedAudio) {
|
| 155 |
+
recording.innerHTML = "";
|
| 156 |
+
create_recording_waveform();
|
| 157 |
+
}
|
| 158 |
+
});
|
| 159 |
+
record_mounted = true;
|
| 160 |
+
};
|
| 161 |
+
|
| 162 |
+
const create_recording_waveform = (): void => {
|
| 163 |
+
let recording = recordingContainer;
|
| 164 |
+
if (!recordedAudio || !recording) return;
|
| 165 |
+
recordingWaveform = WaveSurfer.create({
|
| 166 |
+
container: recording,
|
| 167 |
+
url: recordedAudio,
|
| 168 |
+
...waveform_settings
|
| 169 |
+
});
|
| 170 |
+
};
|
| 171 |
+
|
| 172 |
+
const handle_trim_audio = async (
|
| 173 |
+
start: number,
|
| 174 |
+
end: number
|
| 175 |
+
): Promise<void> => {
|
| 176 |
+
mode = "edit";
|
| 177 |
+
const decodedData = recordingWaveform.getDecodedData();
|
| 178 |
+
if (decodedData)
|
| 179 |
+
await process_audio(decodedData, start, end).then(
|
| 180 |
+
async (trimmedAudio: Uint8Array) => {
|
| 181 |
+
await dispatch_blob([trimmedAudio], "change");
|
| 182 |
+
await dispatch_blob([trimmedAudio], "stop_recording");
|
| 183 |
+
recordingWaveform.destroy();
|
| 184 |
+
create_recording_waveform();
|
| 185 |
+
}
|
| 186 |
+
);
|
| 187 |
+
dispatch("edit");
|
| 188 |
+
};
|
| 189 |
+
|
| 190 |
+
onMount(() => {
|
| 191 |
+
create_mic_waveform();
|
| 192 |
+
|
| 193 |
+
window.addEventListener("keydown", (e) => {
|
| 194 |
+
const is_focused_in_waveform =
|
| 195 |
+
recordingContainer &&
|
| 196 |
+
recordingContainer.contains(document.activeElement);
|
| 197 |
+
if (!is_focused_in_waveform) return;
|
| 198 |
+
if (e.key === "ArrowRight") {
|
| 199 |
+
skip_audio(recordingWaveform, 0.1);
|
| 200 |
+
} else if (e.key === "ArrowLeft") {
|
| 201 |
+
skip_audio(recordingWaveform, -0.1);
|
| 202 |
+
}
|
| 203 |
+
});
|
| 204 |
+
});
|
| 205 |
+
</script>
|
| 206 |
+
|
| 207 |
+
<div class="component-wrapper">
|
| 208 |
+
<div
|
| 209 |
+
class="microphone"
|
| 210 |
+
bind:this={microphoneContainer}
|
| 211 |
+
data-testid="microphone-waveform"
|
| 212 |
+
/>
|
| 213 |
+
<div bind:this={recordingContainer} data-testid="recording-waveform" />
|
| 214 |
+
|
| 215 |
+
{#if (timing || recordedAudio) && waveform_options.show_recording_waveform}
|
| 216 |
+
<div class="timestamps">
|
| 217 |
+
<time bind:this={timeRef} class="time">0:00</time>
|
| 218 |
+
<div>
|
| 219 |
+
{#if mode === "edit" && trimDuration > 0}
|
| 220 |
+
<time class="trim-duration">{format_time(trimDuration)}</time>
|
| 221 |
+
{/if}
|
| 222 |
+
{#if timing}
|
| 223 |
+
<time class="duration">{format_time(seconds)}</time>
|
| 224 |
+
{:else}
|
| 225 |
+
<time bind:this={durationRef} class="duration">0:00</time>
|
| 226 |
+
{/if}
|
| 227 |
+
</div>
|
| 228 |
+
</div>
|
| 229 |
+
{/if}
|
| 230 |
+
|
| 231 |
+
{#if microphoneContainer && !recordedAudio && record_mounted}
|
| 232 |
+
<WaveformRecordControls
|
| 233 |
+
bind:record
|
| 234 |
+
{i18n}
|
| 235 |
+
{timing}
|
| 236 |
+
{recording}
|
| 237 |
+
show_recording_waveform={waveform_options.show_recording_waveform}
|
| 238 |
+
record_time={format_time(seconds)}
|
| 239 |
+
/>
|
| 240 |
+
{/if}
|
| 241 |
+
|
| 242 |
+
{#if recordingWaveform && recordedAudio}
|
| 243 |
+
<WaveformControls
|
| 244 |
+
bind:waveform={recordingWaveform}
|
| 245 |
+
container={recordingContainer}
|
| 246 |
+
{playing}
|
| 247 |
+
{audio_duration}
|
| 248 |
+
{i18n}
|
| 249 |
+
{editable}
|
| 250 |
+
interactive={true}
|
| 251 |
+
{handle_trim_audio}
|
| 252 |
+
bind:trimDuration
|
| 253 |
+
bind:mode
|
| 254 |
+
show_redo
|
| 255 |
+
{handle_reset_value}
|
| 256 |
+
{waveform_options}
|
| 257 |
+
/>
|
| 258 |
+
{/if}
|
| 259 |
+
</div>
|
| 260 |
+
|
| 261 |
+
<style>
|
| 262 |
+
.microphone {
|
| 263 |
+
width: 100%;
|
| 264 |
+
display: none;
|
| 265 |
+
}
|
| 266 |
+
|
| 267 |
+
.component-wrapper {
|
| 268 |
+
padding: var(--size-3);
|
| 269 |
+
width: 100%;
|
| 270 |
+
}
|
| 271 |
+
|
| 272 |
+
.timestamps {
|
| 273 |
+
display: flex;
|
| 274 |
+
justify-content: space-between;
|
| 275 |
+
align-items: center;
|
| 276 |
+
width: 100%;
|
| 277 |
+
padding: var(--size-1) 0;
|
| 278 |
+
margin: var(--spacing-md) 0;
|
| 279 |
+
}
|
| 280 |
+
|
| 281 |
+
.time {
|
| 282 |
+
color: var(--neutral-400);
|
| 283 |
+
}
|
| 284 |
+
|
| 285 |
+
.duration {
|
| 286 |
+
color: var(--neutral-400);
|
| 287 |
+
}
|
| 288 |
+
|
| 289 |
+
.trim-duration {
|
| 290 |
+
color: var(--color-accent);
|
| 291 |
+
margin-right: var(--spacing-sm);
|
| 292 |
+
}
|
| 293 |
+
</style>
|
6.0.0-dev.4/audio/shared/Audio.svelte
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts">
|
| 2 |
+
import type { HTMLAudioAttributes } from "svelte/elements";
|
| 3 |
+
import { createEventDispatcher } from "svelte";
|
| 4 |
+
interface Props extends HTMLAudioAttributes {
|
| 5 |
+
"data-testid"?: string;
|
| 6 |
+
}
|
| 7 |
+
type $$Props = Props;
|
| 8 |
+
|
| 9 |
+
export let src: HTMLAudioAttributes["src"] = undefined;
|
| 10 |
+
|
| 11 |
+
const dispatch = createEventDispatcher();
|
| 12 |
+
</script>
|
| 13 |
+
|
| 14 |
+
<audio
|
| 15 |
+
{src}
|
| 16 |
+
{...$$restProps}
|
| 17 |
+
on:play={dispatch.bind(null, "play")}
|
| 18 |
+
on:pause={dispatch.bind(null, "pause")}
|
| 19 |
+
on:ended={dispatch.bind(null, "ended")}
|
| 20 |
+
/>
|
6.0.0-dev.4/audio/shared/DeviceSelect.svelte
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts">
|
| 2 |
+
import RecordPlugin from "wavesurfer.js/dist/plugins/record.js";
|
| 3 |
+
import type { I18nFormatter } from "@gradio/utils";
|
| 4 |
+
import { createEventDispatcher } from "svelte";
|
| 5 |
+
|
| 6 |
+
export let i18n: I18nFormatter;
|
| 7 |
+
export let micDevices: MediaDeviceInfo[] = [];
|
| 8 |
+
|
| 9 |
+
const dispatch = createEventDispatcher<{
|
| 10 |
+
error: string;
|
| 11 |
+
}>();
|
| 12 |
+
|
| 13 |
+
$: if (typeof window !== "undefined") {
|
| 14 |
+
try {
|
| 15 |
+
let tempDevices: MediaDeviceInfo[] = [];
|
| 16 |
+
RecordPlugin.getAvailableAudioDevices().then(
|
| 17 |
+
(devices: MediaDeviceInfo[]) => {
|
| 18 |
+
micDevices = devices;
|
| 19 |
+
devices.forEach((device) => {
|
| 20 |
+
if (device.deviceId) {
|
| 21 |
+
tempDevices.push(device);
|
| 22 |
+
}
|
| 23 |
+
});
|
| 24 |
+
micDevices = tempDevices;
|
| 25 |
+
}
|
| 26 |
+
);
|
| 27 |
+
} catch (err) {
|
| 28 |
+
if (err instanceof DOMException && err.name == "NotAllowedError") {
|
| 29 |
+
dispatch("error", i18n("audio.allow_recording_access"));
|
| 30 |
+
}
|
| 31 |
+
throw err;
|
| 32 |
+
}
|
| 33 |
+
}
|
| 34 |
+
</script>
|
| 35 |
+
|
| 36 |
+
<select
|
| 37 |
+
class="mic-select"
|
| 38 |
+
aria-label="Select input device"
|
| 39 |
+
disabled={micDevices.length === 0}
|
| 40 |
+
>
|
| 41 |
+
{#if micDevices.length === 0}
|
| 42 |
+
<option value="">{i18n("audio.no_microphone")}</option>
|
| 43 |
+
{:else}
|
| 44 |
+
{#each micDevices as micDevice}
|
| 45 |
+
<option value={micDevice.deviceId}>{micDevice.label}</option>
|
| 46 |
+
{/each}
|
| 47 |
+
{/if}
|
| 48 |
+
</select>
|
| 49 |
+
|
| 50 |
+
<style>
|
| 51 |
+
.mic-select {
|
| 52 |
+
height: var(--size-8);
|
| 53 |
+
background: var(--block-background-fill);
|
| 54 |
+
padding: 0px var(--spacing-xxl);
|
| 55 |
+
border-radius: var(--button-large-radius);
|
| 56 |
+
font-size: var(--text-md);
|
| 57 |
+
border: 1px solid var(--block-border-color);
|
| 58 |
+
gap: var(--size-1);
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
select {
|
| 62 |
+
text-overflow: ellipsis;
|
| 63 |
+
max-width: var(--size-40);
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
@media (max-width: 375px) {
|
| 67 |
+
select {
|
| 68 |
+
width: 100%;
|
| 69 |
+
}
|
| 70 |
+
}
|
| 71 |
+
</style>
|
6.0.0-dev.4/audio/shared/VolumeControl.svelte
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts">
|
| 2 |
+
import { onMount } from "svelte";
|
| 3 |
+
import WaveSurfer from "wavesurfer.js";
|
| 4 |
+
|
| 5 |
+
export let currentVolume = 1;
|
| 6 |
+
export let show_volume_slider = false;
|
| 7 |
+
export let waveform: WaveSurfer | undefined;
|
| 8 |
+
|
| 9 |
+
let volumeElement: HTMLInputElement;
|
| 10 |
+
|
| 11 |
+
onMount(() => {
|
| 12 |
+
adjustSlider();
|
| 13 |
+
});
|
| 14 |
+
|
| 15 |
+
const adjustSlider = (): void => {
|
| 16 |
+
let slider = volumeElement;
|
| 17 |
+
if (!slider) return;
|
| 18 |
+
|
| 19 |
+
slider.style.background = `linear-gradient(to right, var(--color-accent) ${
|
| 20 |
+
currentVolume * 100
|
| 21 |
+
}%, var(--neutral-400) ${currentVolume * 100}%)`;
|
| 22 |
+
};
|
| 23 |
+
|
| 24 |
+
$: (currentVolume, adjustSlider());
|
| 25 |
+
</script>
|
| 26 |
+
|
| 27 |
+
<input
|
| 28 |
+
bind:this={volumeElement}
|
| 29 |
+
id="volume"
|
| 30 |
+
class="volume-slider"
|
| 31 |
+
type="range"
|
| 32 |
+
min="0"
|
| 33 |
+
max="1"
|
| 34 |
+
step="0.01"
|
| 35 |
+
value={currentVolume}
|
| 36 |
+
on:focusout={() => (show_volume_slider = false)}
|
| 37 |
+
on:input={(e) => {
|
| 38 |
+
if (e.target instanceof HTMLInputElement) {
|
| 39 |
+
currentVolume = parseFloat(e.target.value);
|
| 40 |
+
waveform?.setVolume(currentVolume);
|
| 41 |
+
}
|
| 42 |
+
}}
|
| 43 |
+
/>
|
| 44 |
+
|
| 45 |
+
<style>
|
| 46 |
+
.volume-slider {
|
| 47 |
+
-webkit-appearance: none;
|
| 48 |
+
appearance: none;
|
| 49 |
+
width: var(--size-20);
|
| 50 |
+
accent-color: var(--color-accent);
|
| 51 |
+
height: 4px;
|
| 52 |
+
cursor: pointer;
|
| 53 |
+
outline: none;
|
| 54 |
+
border-radius: 15px;
|
| 55 |
+
background-color: var(--neutral-400);
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
input[type="range"]::-webkit-slider-thumb {
|
| 59 |
+
-webkit-appearance: none;
|
| 60 |
+
appearance: none;
|
| 61 |
+
height: 15px;
|
| 62 |
+
width: 15px;
|
| 63 |
+
background-color: var(--color-accent);
|
| 64 |
+
border-radius: 50%;
|
| 65 |
+
border: none;
|
| 66 |
+
transition: 0.2s ease-in-out;
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
input[type="range"]::-moz-range-thumb {
|
| 70 |
+
height: 15px;
|
| 71 |
+
width: 15px;
|
| 72 |
+
background-color: var(--color-accent);
|
| 73 |
+
border-radius: 50%;
|
| 74 |
+
border: none;
|
| 75 |
+
transition: 0.2s ease-in-out;
|
| 76 |
+
}
|
| 77 |
+
</style>
|
6.0.0-dev.4/audio/shared/VolumeLevels.svelte
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts">
|
| 2 |
+
import { VolumeMuted, VolumeHigh, VolumeLow } from "@gradio/icons";
|
| 3 |
+
export let currentVolume: number;
|
| 4 |
+
</script>
|
| 5 |
+
|
| 6 |
+
{#if currentVolume == 0}
|
| 7 |
+
<VolumeMuted />
|
| 8 |
+
{:else if currentVolume < 0.5}
|
| 9 |
+
<VolumeLow />
|
| 10 |
+
{:else if currentVolume >= 0.5}
|
| 11 |
+
<VolumeHigh />
|
| 12 |
+
{/if}
|
6.0.0-dev.4/audio/shared/WaveformControls.svelte
ADDED
|
@@ -0,0 +1,434 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts">
|
| 2 |
+
import {
|
| 3 |
+
Play,
|
| 4 |
+
Pause,
|
| 5 |
+
Forward,
|
| 6 |
+
Backward,
|
| 7 |
+
ClosedCaption,
|
| 8 |
+
Undo,
|
| 9 |
+
Trim
|
| 10 |
+
} from "@gradio/icons";
|
| 11 |
+
import { get_skip_rewind_amount } from "../shared/utils";
|
| 12 |
+
import type { I18nFormatter } from "@gradio/utils";
|
| 13 |
+
import WaveSurfer from "wavesurfer.js";
|
| 14 |
+
import RegionsPlugin, {
|
| 15 |
+
type Region
|
| 16 |
+
} from "wavesurfer.js/dist/plugins/regions.js";
|
| 17 |
+
import type { WaveformOptions } from "./types";
|
| 18 |
+
import VolumeLevels from "./VolumeLevels.svelte";
|
| 19 |
+
import VolumeControl from "./VolumeControl.svelte";
|
| 20 |
+
|
| 21 |
+
export let waveform: WaveSurfer | undefined;
|
| 22 |
+
export let audio_duration: number;
|
| 23 |
+
export let i18n: I18nFormatter;
|
| 24 |
+
export let playing: boolean;
|
| 25 |
+
export let show_redo = false;
|
| 26 |
+
export let interactive = false;
|
| 27 |
+
export let handle_trim_audio: (start: number, end: number) => void;
|
| 28 |
+
export let mode = "";
|
| 29 |
+
export let container: HTMLDivElement;
|
| 30 |
+
export let handle_reset_value: () => void;
|
| 31 |
+
export let waveform_options: WaveformOptions = {};
|
| 32 |
+
export let trim_region_settings: WaveformOptions = {};
|
| 33 |
+
export let show_volume_slider = false;
|
| 34 |
+
export let editable = true;
|
| 35 |
+
export let subtitles_toggle = true;
|
| 36 |
+
export let show_subtitles = false;
|
| 37 |
+
export let trimDuration = 0;
|
| 38 |
+
|
| 39 |
+
let playbackSpeeds = [0.5, 1, 1.5, 2];
|
| 40 |
+
let playbackSpeed = playbackSpeeds[1];
|
| 41 |
+
|
| 42 |
+
let trimRegion: RegionsPlugin | null = null;
|
| 43 |
+
let activeRegion: Region | null = null;
|
| 44 |
+
|
| 45 |
+
let leftRegionHandle: HTMLDivElement | null;
|
| 46 |
+
let rightRegionHandle: HTMLDivElement | null;
|
| 47 |
+
let activeHandle = "";
|
| 48 |
+
|
| 49 |
+
let currentVolume = 1;
|
| 50 |
+
|
| 51 |
+
$: trimRegion =
|
| 52 |
+
container && waveform
|
| 53 |
+
? waveform.registerPlugin(RegionsPlugin.create())
|
| 54 |
+
: null;
|
| 55 |
+
|
| 56 |
+
$: trimRegion?.on("region-out", (region) => {
|
| 57 |
+
region.play();
|
| 58 |
+
});
|
| 59 |
+
|
| 60 |
+
$: trimRegion?.on("region-updated", (region) => {
|
| 61 |
+
trimDuration = region.end - region.start;
|
| 62 |
+
});
|
| 63 |
+
|
| 64 |
+
$: trimRegion?.on("region-clicked", (region, e) => {
|
| 65 |
+
e.stopPropagation(); // prevent triggering a click on the waveform
|
| 66 |
+
activeRegion = region;
|
| 67 |
+
region.play();
|
| 68 |
+
});
|
| 69 |
+
|
| 70 |
+
const addTrimRegion = (): void => {
|
| 71 |
+
if (!trimRegion) return;
|
| 72 |
+
activeRegion = trimRegion?.addRegion({
|
| 73 |
+
start: audio_duration / 4,
|
| 74 |
+
end: audio_duration / 2,
|
| 75 |
+
...trim_region_settings
|
| 76 |
+
});
|
| 77 |
+
|
| 78 |
+
trimDuration = activeRegion.end - activeRegion.start;
|
| 79 |
+
};
|
| 80 |
+
|
| 81 |
+
$: if (activeRegion) {
|
| 82 |
+
const shadowRoot = container.children[0]!.shadowRoot!;
|
| 83 |
+
|
| 84 |
+
rightRegionHandle = shadowRoot.querySelector('[data-resize="right"]');
|
| 85 |
+
leftRegionHandle = shadowRoot.querySelector('[data-resize="left"]');
|
| 86 |
+
|
| 87 |
+
if (leftRegionHandle && rightRegionHandle) {
|
| 88 |
+
leftRegionHandle.setAttribute("role", "button");
|
| 89 |
+
rightRegionHandle.setAttribute("role", "button");
|
| 90 |
+
leftRegionHandle?.setAttribute("aria-label", "Drag to adjust start time");
|
| 91 |
+
rightRegionHandle?.setAttribute("aria-label", "Drag to adjust end time");
|
| 92 |
+
leftRegionHandle?.setAttribute("tabindex", "0");
|
| 93 |
+
rightRegionHandle?.setAttribute("tabindex", "0");
|
| 94 |
+
|
| 95 |
+
leftRegionHandle.addEventListener("focus", () => {
|
| 96 |
+
if (trimRegion) activeHandle = "left";
|
| 97 |
+
});
|
| 98 |
+
|
| 99 |
+
rightRegionHandle.addEventListener("focus", () => {
|
| 100 |
+
if (trimRegion) activeHandle = "right";
|
| 101 |
+
});
|
| 102 |
+
}
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
const trimAudio = (): void => {
|
| 106 |
+
if (waveform && trimRegion) {
|
| 107 |
+
if (activeRegion) {
|
| 108 |
+
const start = activeRegion.start;
|
| 109 |
+
const end = activeRegion.end;
|
| 110 |
+
handle_trim_audio(start, end);
|
| 111 |
+
mode = "";
|
| 112 |
+
activeRegion = null;
|
| 113 |
+
}
|
| 114 |
+
}
|
| 115 |
+
};
|
| 116 |
+
|
| 117 |
+
const clearRegions = (): void => {
|
| 118 |
+
trimRegion?.getRegions().forEach((region) => {
|
| 119 |
+
region.remove();
|
| 120 |
+
});
|
| 121 |
+
trimRegion?.clearRegions();
|
| 122 |
+
};
|
| 123 |
+
|
| 124 |
+
const toggleTrimmingMode = (): void => {
|
| 125 |
+
clearRegions();
|
| 126 |
+
if (mode === "edit") {
|
| 127 |
+
mode = "";
|
| 128 |
+
} else {
|
| 129 |
+
mode = "edit";
|
| 130 |
+
addTrimRegion();
|
| 131 |
+
}
|
| 132 |
+
};
|
| 133 |
+
|
| 134 |
+
const toggleSubtitles = (): void => {
|
| 135 |
+
subtitles_toggle = !subtitles_toggle;
|
| 136 |
+
};
|
| 137 |
+
|
| 138 |
+
const adjustRegionHandles = (handle: string, key: string): void => {
|
| 139 |
+
let newStart;
|
| 140 |
+
let newEnd;
|
| 141 |
+
|
| 142 |
+
if (!activeRegion) return;
|
| 143 |
+
if (handle === "left") {
|
| 144 |
+
if (key === "ArrowLeft") {
|
| 145 |
+
newStart = activeRegion.start - 0.05;
|
| 146 |
+
newEnd = activeRegion.end;
|
| 147 |
+
} else {
|
| 148 |
+
newStart = activeRegion.start + 0.05;
|
| 149 |
+
newEnd = activeRegion.end;
|
| 150 |
+
}
|
| 151 |
+
} else {
|
| 152 |
+
if (key === "ArrowLeft") {
|
| 153 |
+
newStart = activeRegion.start;
|
| 154 |
+
newEnd = activeRegion.end - 0.05;
|
| 155 |
+
} else {
|
| 156 |
+
newStart = activeRegion.start;
|
| 157 |
+
newEnd = activeRegion.end + 0.05;
|
| 158 |
+
}
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
activeRegion.setOptions({
|
| 162 |
+
start: newStart,
|
| 163 |
+
end: newEnd
|
| 164 |
+
});
|
| 165 |
+
|
| 166 |
+
trimDuration = activeRegion.end - activeRegion.start;
|
| 167 |
+
};
|
| 168 |
+
|
| 169 |
+
$: trimRegion &&
|
| 170 |
+
window.addEventListener("keydown", (e) => {
|
| 171 |
+
if (e.key === "ArrowLeft") {
|
| 172 |
+
adjustRegionHandles(activeHandle, "ArrowLeft");
|
| 173 |
+
} else if (e.key === "ArrowRight") {
|
| 174 |
+
adjustRegionHandles(activeHandle, "ArrowRight");
|
| 175 |
+
}
|
| 176 |
+
});
|
| 177 |
+
</script>
|
| 178 |
+
|
| 179 |
+
<div class="controls" data-testid="waveform-controls">
|
| 180 |
+
<div class="control-wrapper">
|
| 181 |
+
<button
|
| 182 |
+
class="action icon volume"
|
| 183 |
+
style:color={show_volume_slider
|
| 184 |
+
? "var(--color-accent)"
|
| 185 |
+
: "var(--neutral-400)"}
|
| 186 |
+
aria-label="Adjust volume"
|
| 187 |
+
on:click={() => (show_volume_slider = !show_volume_slider)}
|
| 188 |
+
>
|
| 189 |
+
<VolumeLevels {currentVolume} />
|
| 190 |
+
</button>
|
| 191 |
+
|
| 192 |
+
{#if show_volume_slider}
|
| 193 |
+
<VolumeControl bind:currentVolume bind:show_volume_slider {waveform} />
|
| 194 |
+
{/if}
|
| 195 |
+
|
| 196 |
+
<button
|
| 197 |
+
class:hidden={show_volume_slider}
|
| 198 |
+
class="playback icon"
|
| 199 |
+
aria-label={`Adjust playback speed to ${
|
| 200 |
+
playbackSpeeds[
|
| 201 |
+
(playbackSpeeds.indexOf(playbackSpeed) + 1) % playbackSpeeds.length
|
| 202 |
+
]
|
| 203 |
+
}x`}
|
| 204 |
+
on:click={() => {
|
| 205 |
+
playbackSpeed =
|
| 206 |
+
playbackSpeeds[
|
| 207 |
+
(playbackSpeeds.indexOf(playbackSpeed) + 1) % playbackSpeeds.length
|
| 208 |
+
];
|
| 209 |
+
|
| 210 |
+
waveform?.setPlaybackRate(playbackSpeed);
|
| 211 |
+
}}
|
| 212 |
+
>
|
| 213 |
+
<span>{playbackSpeed}x</span>
|
| 214 |
+
</button>
|
| 215 |
+
</div>
|
| 216 |
+
|
| 217 |
+
<div class="play-pause-wrapper">
|
| 218 |
+
<button
|
| 219 |
+
class="rewind icon"
|
| 220 |
+
aria-label={`Skip backwards by ${get_skip_rewind_amount(
|
| 221 |
+
audio_duration,
|
| 222 |
+
waveform_options.skip_length
|
| 223 |
+
)} seconds`}
|
| 224 |
+
on:click={() =>
|
| 225 |
+
waveform?.skip(
|
| 226 |
+
get_skip_rewind_amount(audio_duration, waveform_options.skip_length) *
|
| 227 |
+
-1
|
| 228 |
+
)}
|
| 229 |
+
>
|
| 230 |
+
<Backward />
|
| 231 |
+
</button>
|
| 232 |
+
<button
|
| 233 |
+
class="play-pause-button icon"
|
| 234 |
+
on:click={() => waveform?.playPause()}
|
| 235 |
+
aria-label={playing ? i18n("audio.pause") : i18n("audio.play")}
|
| 236 |
+
>
|
| 237 |
+
{#if playing}
|
| 238 |
+
<Pause />
|
| 239 |
+
{:else}
|
| 240 |
+
<Play />
|
| 241 |
+
{/if}
|
| 242 |
+
</button>
|
| 243 |
+
<button
|
| 244 |
+
class="skip icon"
|
| 245 |
+
aria-label="Skip forward by {get_skip_rewind_amount(
|
| 246 |
+
audio_duration,
|
| 247 |
+
waveform_options.skip_length
|
| 248 |
+
)} seconds"
|
| 249 |
+
on:click={() =>
|
| 250 |
+
waveform?.skip(
|
| 251 |
+
get_skip_rewind_amount(audio_duration, waveform_options.skip_length)
|
| 252 |
+
)}
|
| 253 |
+
>
|
| 254 |
+
<Forward />
|
| 255 |
+
</button>
|
| 256 |
+
</div>
|
| 257 |
+
|
| 258 |
+
<div class="settings-wrapper">
|
| 259 |
+
{#if show_subtitles}
|
| 260 |
+
<button
|
| 261 |
+
class="action icon cc-button"
|
| 262 |
+
data-testid="subtitles-toggle"
|
| 263 |
+
style="color: {subtitles_toggle
|
| 264 |
+
? 'var(--color-accent)'
|
| 265 |
+
: 'var(--neutral-400)'}"
|
| 266 |
+
on:click={toggleSubtitles}
|
| 267 |
+
>
|
| 268 |
+
<ClosedCaption /></button
|
| 269 |
+
>
|
| 270 |
+
{/if}
|
| 271 |
+
{#if editable && interactive}
|
| 272 |
+
{#if show_redo && mode === ""}
|
| 273 |
+
<button
|
| 274 |
+
class="action icon"
|
| 275 |
+
aria-label="Reset audio"
|
| 276 |
+
on:click={() => {
|
| 277 |
+
handle_reset_value();
|
| 278 |
+
clearRegions();
|
| 279 |
+
mode = "";
|
| 280 |
+
}}
|
| 281 |
+
>
|
| 282 |
+
<Undo />
|
| 283 |
+
</button>
|
| 284 |
+
{/if}
|
| 285 |
+
|
| 286 |
+
{#if mode === ""}
|
| 287 |
+
<button
|
| 288 |
+
class="action icon"
|
| 289 |
+
aria-label="Trim audio to selection"
|
| 290 |
+
on:click={toggleTrimmingMode}
|
| 291 |
+
>
|
| 292 |
+
<Trim />
|
| 293 |
+
</button>
|
| 294 |
+
{:else}
|
| 295 |
+
<button class="text-button" on:click={trimAudio}>Trim</button>
|
| 296 |
+
<button class="text-button" on:click={toggleTrimmingMode}>Cancel</button
|
| 297 |
+
>
|
| 298 |
+
{/if}
|
| 299 |
+
{/if}
|
| 300 |
+
</div>
|
| 301 |
+
</div>
|
| 302 |
+
|
| 303 |
+
<style>
|
| 304 |
+
.settings-wrapper {
|
| 305 |
+
display: flex;
|
| 306 |
+
justify-self: self-end;
|
| 307 |
+
align-items: center;
|
| 308 |
+
grid-area: editing;
|
| 309 |
+
}
|
| 310 |
+
.text-button {
|
| 311 |
+
border: 1px solid var(--neutral-400);
|
| 312 |
+
border-radius: var(--radius-sm);
|
| 313 |
+
font-weight: 300;
|
| 314 |
+
font-size: var(--size-3);
|
| 315 |
+
text-align: center;
|
| 316 |
+
color: var(--neutral-400);
|
| 317 |
+
height: var(--size-5);
|
| 318 |
+
font-weight: bold;
|
| 319 |
+
padding: 0 5px;
|
| 320 |
+
margin-left: 5px;
|
| 321 |
+
}
|
| 322 |
+
|
| 323 |
+
.text-button:hover,
|
| 324 |
+
.text-button:focus {
|
| 325 |
+
color: var(--color-accent);
|
| 326 |
+
border-color: var(--color-accent);
|
| 327 |
+
}
|
| 328 |
+
|
| 329 |
+
.controls {
|
| 330 |
+
display: grid;
|
| 331 |
+
grid-template-columns: 1fr 1fr 1fr;
|
| 332 |
+
grid-template-areas: "controls playback editing";
|
| 333 |
+
margin-top: 5px;
|
| 334 |
+
align-items: center;
|
| 335 |
+
position: relative;
|
| 336 |
+
flex-wrap: wrap;
|
| 337 |
+
justify-content: space-between;
|
| 338 |
+
}
|
| 339 |
+
.controls div {
|
| 340 |
+
margin: var(--size-1) 0;
|
| 341 |
+
}
|
| 342 |
+
|
| 343 |
+
@media (max-width: 600px) {
|
| 344 |
+
.controls {
|
| 345 |
+
grid-template-columns: 1fr 1fr;
|
| 346 |
+
grid-template-rows: auto auto;
|
| 347 |
+
grid-template-areas:
|
| 348 |
+
"playback playback"
|
| 349 |
+
"controls editing";
|
| 350 |
+
}
|
| 351 |
+
}
|
| 352 |
+
|
| 353 |
+
@media (max-width: 319px) {
|
| 354 |
+
.controls {
|
| 355 |
+
overflow-x: scroll;
|
| 356 |
+
}
|
| 357 |
+
}
|
| 358 |
+
|
| 359 |
+
.hidden {
|
| 360 |
+
display: none;
|
| 361 |
+
}
|
| 362 |
+
|
| 363 |
+
.control-wrapper {
|
| 364 |
+
display: flex;
|
| 365 |
+
justify-self: self-start;
|
| 366 |
+
align-items: center;
|
| 367 |
+
justify-content: space-between;
|
| 368 |
+
grid-area: controls;
|
| 369 |
+
}
|
| 370 |
+
|
| 371 |
+
.action {
|
| 372 |
+
width: var(--size-5);
|
| 373 |
+
color: var(--neutral-400);
|
| 374 |
+
margin-left: var(--spacing-md);
|
| 375 |
+
}
|
| 376 |
+
.icon:hover,
|
| 377 |
+
.icon:focus {
|
| 378 |
+
color: var(--color-accent);
|
| 379 |
+
}
|
| 380 |
+
.play-pause-wrapper {
|
| 381 |
+
display: flex;
|
| 382 |
+
justify-self: center;
|
| 383 |
+
grid-area: playback;
|
| 384 |
+
}
|
| 385 |
+
.cc-button {
|
| 386 |
+
width: var(--size-8);
|
| 387 |
+
}
|
| 388 |
+
|
| 389 |
+
@media (max-width: 600px) {
|
| 390 |
+
.play-pause-wrapper {
|
| 391 |
+
margin: var(--spacing-md);
|
| 392 |
+
}
|
| 393 |
+
}
|
| 394 |
+
.playback {
|
| 395 |
+
border: 1px solid var(--neutral-400);
|
| 396 |
+
border-radius: var(--radius-sm);
|
| 397 |
+
width: 5.5ch;
|
| 398 |
+
font-weight: 300;
|
| 399 |
+
font-size: var(--size-3);
|
| 400 |
+
text-align: center;
|
| 401 |
+
color: var(--neutral-400);
|
| 402 |
+
height: var(--size-5);
|
| 403 |
+
font-weight: bold;
|
| 404 |
+
}
|
| 405 |
+
|
| 406 |
+
.playback:hover,
|
| 407 |
+
.playback:focus {
|
| 408 |
+
color: var(--color-accent);
|
| 409 |
+
border-color: var(--color-accent);
|
| 410 |
+
}
|
| 411 |
+
|
| 412 |
+
.rewind,
|
| 413 |
+
.skip {
|
| 414 |
+
margin: 0 10px;
|
| 415 |
+
color: var(--neutral-400);
|
| 416 |
+
}
|
| 417 |
+
|
| 418 |
+
.play-pause-button {
|
| 419 |
+
width: var(--size-8);
|
| 420 |
+
display: flex;
|
| 421 |
+
align-items: center;
|
| 422 |
+
justify-content: center;
|
| 423 |
+
color: var(--neutral-400);
|
| 424 |
+
fill: var(--neutral-400);
|
| 425 |
+
}
|
| 426 |
+
|
| 427 |
+
.volume {
|
| 428 |
+
position: relative;
|
| 429 |
+
display: flex;
|
| 430 |
+
justify-content: center;
|
| 431 |
+
margin-right: var(--spacing-xl);
|
| 432 |
+
width: var(--size-5);
|
| 433 |
+
}
|
| 434 |
+
</style>
|
6.0.0-dev.4/audio/shared/WaveformRecordControls.svelte
ADDED
|
@@ -0,0 +1,301 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts">
|
| 2 |
+
import { onMount, onDestroy } from "svelte";
|
| 3 |
+
import { Pause } from "@gradio/icons";
|
| 4 |
+
import type { I18nFormatter } from "@gradio/utils";
|
| 5 |
+
import RecordPlugin from "wavesurfer.js/dist/plugins/record.js";
|
| 6 |
+
import DeviceSelect from "./DeviceSelect.svelte";
|
| 7 |
+
|
| 8 |
+
export let record: RecordPlugin;
|
| 9 |
+
export let i18n: I18nFormatter;
|
| 10 |
+
export let recording = false;
|
| 11 |
+
|
| 12 |
+
let micDevices: MediaDeviceInfo[] = [];
|
| 13 |
+
let recordButton: HTMLButtonElement;
|
| 14 |
+
let pauseButton: HTMLButtonElement;
|
| 15 |
+
let resumeButton: HTMLButtonElement;
|
| 16 |
+
let stopButton: HTMLButtonElement;
|
| 17 |
+
let stopButtonPaused: HTMLButtonElement;
|
| 18 |
+
let recording_ongoing = false;
|
| 19 |
+
|
| 20 |
+
export let record_time: string;
|
| 21 |
+
export let show_recording_waveform: boolean | undefined;
|
| 22 |
+
export let timing = false;
|
| 23 |
+
|
| 24 |
+
const handleRecordStart = (): void => {
|
| 25 |
+
recordButton.style.display = "none";
|
| 26 |
+
stopButton.style.display = "flex";
|
| 27 |
+
pauseButton.style.display = "block";
|
| 28 |
+
};
|
| 29 |
+
|
| 30 |
+
const handleRecordEnd = (): void => {
|
| 31 |
+
if (record.isPaused()) {
|
| 32 |
+
record.resumeRecording();
|
| 33 |
+
record.stopRecording();
|
| 34 |
+
}
|
| 35 |
+
record.stopMic();
|
| 36 |
+
|
| 37 |
+
recordButton.style.display = "flex";
|
| 38 |
+
stopButton.style.display = "none";
|
| 39 |
+
pauseButton.style.display = "none";
|
| 40 |
+
recordButton.disabled = false;
|
| 41 |
+
};
|
| 42 |
+
|
| 43 |
+
const handleRecordPause = (): void => {
|
| 44 |
+
pauseButton.style.display = "none";
|
| 45 |
+
resumeButton.style.display = "block";
|
| 46 |
+
stopButton.style.display = "none";
|
| 47 |
+
stopButtonPaused.style.display = "flex";
|
| 48 |
+
};
|
| 49 |
+
|
| 50 |
+
const handleRecordResume = (): void => {
|
| 51 |
+
pauseButton.style.display = "block";
|
| 52 |
+
resumeButton.style.display = "none";
|
| 53 |
+
recordButton.style.display = "none";
|
| 54 |
+
stopButton.style.display = "flex";
|
| 55 |
+
stopButtonPaused.style.display = "none";
|
| 56 |
+
};
|
| 57 |
+
|
| 58 |
+
onMount(() => {
|
| 59 |
+
record.on("record-start", handleRecordStart);
|
| 60 |
+
record.on("record-end", handleRecordEnd);
|
| 61 |
+
record.on("record-pause", handleRecordPause);
|
| 62 |
+
record.on("record-resume", handleRecordResume);
|
| 63 |
+
});
|
| 64 |
+
|
| 65 |
+
onDestroy(() => {
|
| 66 |
+
record.un("record-start", handleRecordStart);
|
| 67 |
+
record.un("record-end", handleRecordEnd);
|
| 68 |
+
record.un("record-pause", handleRecordPause);
|
| 69 |
+
record.un("record-resume", handleRecordResume);
|
| 70 |
+
});
|
| 71 |
+
|
| 72 |
+
$: if (recording && !recording_ongoing) {
|
| 73 |
+
record.startMic().then(() => {
|
| 74 |
+
record.startRecording();
|
| 75 |
+
recording_ongoing = true;
|
| 76 |
+
});
|
| 77 |
+
} else if (!recording && recording_ongoing) {
|
| 78 |
+
if (record.isPaused()) {
|
| 79 |
+
record.resumeRecording();
|
| 80 |
+
}
|
| 81 |
+
record.stopRecording();
|
| 82 |
+
recording_ongoing = false;
|
| 83 |
+
}
|
| 84 |
+
</script>
|
| 85 |
+
|
| 86 |
+
<div class="controls">
|
| 87 |
+
<div class="wrapper">
|
| 88 |
+
<button
|
| 89 |
+
bind:this={recordButton}
|
| 90 |
+
class="record record-button"
|
| 91 |
+
on:click={() => record.startRecording()}>{i18n("audio.record")}</button
|
| 92 |
+
>
|
| 93 |
+
|
| 94 |
+
<button
|
| 95 |
+
bind:this={stopButton}
|
| 96 |
+
class="stop-button {record.isPaused() ? 'stop-button-paused' : ''}"
|
| 97 |
+
on:click={() => {
|
| 98 |
+
if (record.isPaused()) {
|
| 99 |
+
record.resumeRecording();
|
| 100 |
+
record.stopRecording();
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
record.stopRecording();
|
| 104 |
+
}}>{i18n("audio.stop")}</button
|
| 105 |
+
>
|
| 106 |
+
|
| 107 |
+
<button
|
| 108 |
+
bind:this={stopButtonPaused}
|
| 109 |
+
id="stop-paused"
|
| 110 |
+
class="stop-button-paused"
|
| 111 |
+
on:click={() => {
|
| 112 |
+
if (record.isPaused()) {
|
| 113 |
+
record.resumeRecording();
|
| 114 |
+
record.stopRecording();
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
record.stopRecording();
|
| 118 |
+
}}>{i18n("audio.stop")}</button
|
| 119 |
+
>
|
| 120 |
+
|
| 121 |
+
<button
|
| 122 |
+
aria-label="pause"
|
| 123 |
+
bind:this={pauseButton}
|
| 124 |
+
class="pause-button"
|
| 125 |
+
on:click={() => record.pauseRecording()}><Pause /></button
|
| 126 |
+
>
|
| 127 |
+
<button
|
| 128 |
+
bind:this={resumeButton}
|
| 129 |
+
class="resume-button"
|
| 130 |
+
on:click={() => record.resumeRecording()}>{i18n("audio.resume")}</button
|
| 131 |
+
>
|
| 132 |
+
{#if timing && !show_recording_waveform}
|
| 133 |
+
<time class="duration-button duration">{record_time}</time>
|
| 134 |
+
{/if}
|
| 135 |
+
</div>
|
| 136 |
+
<DeviceSelect bind:micDevices {i18n} />
|
| 137 |
+
</div>
|
| 138 |
+
|
| 139 |
+
<style>
|
| 140 |
+
.controls {
|
| 141 |
+
display: flex;
|
| 142 |
+
align-items: center;
|
| 143 |
+
justify-content: space-between;
|
| 144 |
+
flex-wrap: wrap;
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
.wrapper {
|
| 148 |
+
display: flex;
|
| 149 |
+
align-items: center;
|
| 150 |
+
flex-wrap: wrap;
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
.record {
|
| 154 |
+
margin-right: var(--spacing-md);
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
.stop-button-paused {
|
| 158 |
+
display: none;
|
| 159 |
+
height: var(--size-8);
|
| 160 |
+
width: var(--size-20);
|
| 161 |
+
background-color: var(--block-background-fill);
|
| 162 |
+
border-radius: var(--button-large-radius);
|
| 163 |
+
align-items: center;
|
| 164 |
+
border: 1px solid var(--block-border-color);
|
| 165 |
+
margin: var(--size-1) var(--size-1) 0 0;
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
.stop-button-paused::before {
|
| 169 |
+
content: "";
|
| 170 |
+
height: var(--size-4);
|
| 171 |
+
width: var(--size-4);
|
| 172 |
+
border-radius: var(--radius-full);
|
| 173 |
+
background: var(--primary-600);
|
| 174 |
+
margin: 0 var(--spacing-xl);
|
| 175 |
+
}
|
| 176 |
+
.stop-button::before {
|
| 177 |
+
content: "";
|
| 178 |
+
height: var(--size-4);
|
| 179 |
+
width: var(--size-4);
|
| 180 |
+
border-radius: var(--radius-full);
|
| 181 |
+
background: var(--primary-600);
|
| 182 |
+
margin: 0 var(--spacing-xl);
|
| 183 |
+
animation: scaling 1800ms infinite;
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
.stop-button {
|
| 187 |
+
display: none;
|
| 188 |
+
height: var(--size-8);
|
| 189 |
+
width: var(--size-20);
|
| 190 |
+
background-color: var(--block-background-fill);
|
| 191 |
+
border-radius: var(--button-large-radius);
|
| 192 |
+
align-items: center;
|
| 193 |
+
border: 1px solid var(--primary-600);
|
| 194 |
+
margin: var(--size-1) var(--size-1) 0 0;
|
| 195 |
+
}
|
| 196 |
+
|
| 197 |
+
.record-button::before {
|
| 198 |
+
content: "";
|
| 199 |
+
height: var(--size-4);
|
| 200 |
+
width: var(--size-4);
|
| 201 |
+
border-radius: var(--radius-full);
|
| 202 |
+
background: var(--primary-600);
|
| 203 |
+
margin: 0 var(--spacing-xl);
|
| 204 |
+
}
|
| 205 |
+
|
| 206 |
+
.record-button {
|
| 207 |
+
height: var(--size-8);
|
| 208 |
+
width: var(--size-24);
|
| 209 |
+
background-color: var(--block-background-fill);
|
| 210 |
+
border-radius: var(--button-large-radius);
|
| 211 |
+
display: flex;
|
| 212 |
+
align-items: center;
|
| 213 |
+
border: 1px solid var(--block-border-color);
|
| 214 |
+
}
|
| 215 |
+
|
| 216 |
+
.duration-button {
|
| 217 |
+
border-radius: var(--button-large-radius);
|
| 218 |
+
}
|
| 219 |
+
|
| 220 |
+
.stop-button:disabled {
|
| 221 |
+
cursor: not-allowed;
|
| 222 |
+
}
|
| 223 |
+
|
| 224 |
+
.record-button:disabled {
|
| 225 |
+
cursor: not-allowed;
|
| 226 |
+
opacity: 0.5;
|
| 227 |
+
}
|
| 228 |
+
|
| 229 |
+
@keyframes scaling {
|
| 230 |
+
0% {
|
| 231 |
+
background-color: var(--primary-600);
|
| 232 |
+
scale: 1;
|
| 233 |
+
}
|
| 234 |
+
50% {
|
| 235 |
+
background-color: var(--primary-600);
|
| 236 |
+
scale: 1.2;
|
| 237 |
+
}
|
| 238 |
+
100% {
|
| 239 |
+
background-color: var(--primary-600);
|
| 240 |
+
scale: 1;
|
| 241 |
+
}
|
| 242 |
+
}
|
| 243 |
+
|
| 244 |
+
.pause-button {
|
| 245 |
+
display: none;
|
| 246 |
+
height: var(--size-8);
|
| 247 |
+
width: var(--size-20);
|
| 248 |
+
border: 1px solid var(--block-border-color);
|
| 249 |
+
border-radius: var(--button-large-radius);
|
| 250 |
+
padding: var(--spacing-md);
|
| 251 |
+
margin: var(--size-1) var(--size-1) 0 0;
|
| 252 |
+
}
|
| 253 |
+
|
| 254 |
+
.resume-button {
|
| 255 |
+
display: none;
|
| 256 |
+
height: var(--size-8);
|
| 257 |
+
width: var(--size-20);
|
| 258 |
+
border: 1px solid var(--block-border-color);
|
| 259 |
+
border-radius: var(--button-large-radius);
|
| 260 |
+
padding: var(--spacing-xl);
|
| 261 |
+
line-height: 1px;
|
| 262 |
+
font-size: var(--text-md);
|
| 263 |
+
margin: var(--size-1) var(--size-1) 0 0;
|
| 264 |
+
}
|
| 265 |
+
|
| 266 |
+
.duration {
|
| 267 |
+
display: flex;
|
| 268 |
+
height: var(--size-8);
|
| 269 |
+
width: var(--size-20);
|
| 270 |
+
border: 1px solid var(--block-border-color);
|
| 271 |
+
padding: var(--spacing-md);
|
| 272 |
+
align-items: center;
|
| 273 |
+
justify-content: center;
|
| 274 |
+
margin: var(--size-1) var(--size-1) 0 0;
|
| 275 |
+
}
|
| 276 |
+
|
| 277 |
+
:global(::part(region)) {
|
| 278 |
+
border-radius: var(--radius-md);
|
| 279 |
+
height: 98% !important;
|
| 280 |
+
border: 1px solid var(--trim-region-color);
|
| 281 |
+
background-color: unset;
|
| 282 |
+
border-width: 1px 3px;
|
| 283 |
+
}
|
| 284 |
+
|
| 285 |
+
:global(::part(region))::after {
|
| 286 |
+
content: "";
|
| 287 |
+
position: absolute;
|
| 288 |
+
top: 0;
|
| 289 |
+
left: 0;
|
| 290 |
+
width: 100%;
|
| 291 |
+
height: 100%;
|
| 292 |
+
background: var(--trim-region-color);
|
| 293 |
+
opacity: 0.2;
|
| 294 |
+
border-radius: var(--radius-md);
|
| 295 |
+
}
|
| 296 |
+
|
| 297 |
+
:global(::part(region-handle)) {
|
| 298 |
+
width: 5px !important;
|
| 299 |
+
border: none;
|
| 300 |
+
}
|
| 301 |
+
</style>
|
6.0.0-dev.4/audio/shared/audioBufferToWav.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export function audioBufferToWav(audioBuffer: AudioBuffer): Uint8Array {
|
| 2 |
+
const numOfChan = audioBuffer.numberOfChannels;
|
| 3 |
+
const length = audioBuffer.length * numOfChan * 2 + 44;
|
| 4 |
+
const buffer = new ArrayBuffer(length);
|
| 5 |
+
const view = new DataView(buffer);
|
| 6 |
+
let offset = 0;
|
| 7 |
+
|
| 8 |
+
// Write WAV header
|
| 9 |
+
const writeString = function (
|
| 10 |
+
view: DataView,
|
| 11 |
+
offset: number,
|
| 12 |
+
string: string
|
| 13 |
+
): void {
|
| 14 |
+
for (let i = 0; i < string.length; i++) {
|
| 15 |
+
view.setUint8(offset + i, string.charCodeAt(i));
|
| 16 |
+
}
|
| 17 |
+
};
|
| 18 |
+
|
| 19 |
+
writeString(view, offset, "RIFF");
|
| 20 |
+
offset += 4;
|
| 21 |
+
view.setUint32(offset, length - 8, true);
|
| 22 |
+
offset += 4;
|
| 23 |
+
writeString(view, offset, "WAVE");
|
| 24 |
+
offset += 4;
|
| 25 |
+
writeString(view, offset, "fmt ");
|
| 26 |
+
offset += 4;
|
| 27 |
+
view.setUint32(offset, 16, true);
|
| 28 |
+
offset += 4; // Sub-chunk size, 16 for PCM
|
| 29 |
+
view.setUint16(offset, 1, true);
|
| 30 |
+
offset += 2; // PCM format
|
| 31 |
+
view.setUint16(offset, numOfChan, true);
|
| 32 |
+
offset += 2;
|
| 33 |
+
view.setUint32(offset, audioBuffer.sampleRate, true);
|
| 34 |
+
offset += 4;
|
| 35 |
+
view.setUint32(offset, audioBuffer.sampleRate * 2 * numOfChan, true);
|
| 36 |
+
offset += 4;
|
| 37 |
+
view.setUint16(offset, numOfChan * 2, true);
|
| 38 |
+
offset += 2;
|
| 39 |
+
view.setUint16(offset, 16, true);
|
| 40 |
+
offset += 2;
|
| 41 |
+
writeString(view, offset, "data");
|
| 42 |
+
offset += 4;
|
| 43 |
+
view.setUint32(offset, audioBuffer.length * numOfChan * 2, true);
|
| 44 |
+
offset += 4;
|
| 45 |
+
|
| 46 |
+
// Write PCM audio data
|
| 47 |
+
for (let i = 0; i < audioBuffer.length; i++) {
|
| 48 |
+
for (let channel = 0; channel < numOfChan; channel++) {
|
| 49 |
+
const sample = Math.max(
|
| 50 |
+
-1,
|
| 51 |
+
Math.min(1, audioBuffer.getChannelData(channel)[i])
|
| 52 |
+
);
|
| 53 |
+
view.setInt16(offset, sample * 0x7fff, true);
|
| 54 |
+
offset += 2;
|
| 55 |
+
}
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
return new Uint8Array(buffer);
|
| 59 |
+
}
|
6.0.0-dev.4/audio/shared/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
export { default as Audio } from "./Audio.svelte";
|
6.0.0-dev.4/audio/shared/types.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { FileData } from "@gradio/client";
|
| 2 |
+
|
| 3 |
+
export type WaveformOptions = {
|
| 4 |
+
waveform_color?: string;
|
| 5 |
+
waveform_progress_color?: string;
|
| 6 |
+
show_controls?: boolean;
|
| 7 |
+
skip_length?: number;
|
| 8 |
+
trim_region_color?: string;
|
| 9 |
+
show_recording_waveform?: boolean;
|
| 10 |
+
sample_rate?: number;
|
| 11 |
+
};
|
| 12 |
+
|
| 13 |
+
export interface SubtitleData {
|
| 14 |
+
start: number;
|
| 15 |
+
end: number;
|
| 16 |
+
text: string;
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
export interface AudioProps {
|
| 20 |
+
sources:
|
| 21 |
+
| ["microphone"]
|
| 22 |
+
| ["upload"]
|
| 23 |
+
| ["microphone", "upload"]
|
| 24 |
+
| ["upload", "microphone"];
|
| 25 |
+
value: FileData | null;
|
| 26 |
+
type: "numpy" | "filepath";
|
| 27 |
+
autoplay: boolean;
|
| 28 |
+
buttons: ("play" | "download")[];
|
| 29 |
+
recording: boolean;
|
| 30 |
+
loop: boolean;
|
| 31 |
+
subtitles: FileData | SubtitleData[] | null;
|
| 32 |
+
waveform_options: WaveformOptions;
|
| 33 |
+
editable: boolean;
|
| 34 |
+
pending: boolean;
|
| 35 |
+
streaming: boolean;
|
| 36 |
+
stream_every: number;
|
| 37 |
+
input_ready: boolean;
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
export interface AudioEvents {
|
| 41 |
+
change: any;
|
| 42 |
+
upload: any;
|
| 43 |
+
stream: any;
|
| 44 |
+
clear: any;
|
| 45 |
+
play: any;
|
| 46 |
+
pause: any;
|
| 47 |
+
stop: any;
|
| 48 |
+
start_recording: any;
|
| 49 |
+
pause_recording: any;
|
| 50 |
+
stop_recording: any;
|
| 51 |
+
input: any;
|
| 52 |
+
error: any;
|
| 53 |
+
warning: any;
|
| 54 |
+
clear_status: any;
|
| 55 |
+
close_stream: any;
|
| 56 |
+
edit: any;
|
| 57 |
+
}
|
6.0.0-dev.4/audio/shared/utils.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import type WaveSurfer from "wavesurfer.js";
|
| 2 |
+
import { audioBufferToWav } from "./audioBufferToWav";
|
| 3 |
+
|
| 4 |
+
export interface LoadedParams {
|
| 5 |
+
autoplay?: boolean;
|
| 6 |
+
}
|
| 7 |
+
|
| 8 |
+
export function blob_to_data_url(blob: Blob): Promise<string> {
|
| 9 |
+
return new Promise((fulfill, reject) => {
|
| 10 |
+
let reader = new FileReader();
|
| 11 |
+
reader.onerror = reject;
|
| 12 |
+
reader.onload = () => fulfill(reader.result as string);
|
| 13 |
+
reader.readAsDataURL(blob);
|
| 14 |
+
});
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
export const process_audio = async (
|
| 18 |
+
audioBuffer: AudioBuffer,
|
| 19 |
+
start?: number,
|
| 20 |
+
end?: number,
|
| 21 |
+
waveform_sample_rate?: number
|
| 22 |
+
): Promise<Uint8Array> => {
|
| 23 |
+
const audioContext = new AudioContext({
|
| 24 |
+
sampleRate: waveform_sample_rate || audioBuffer.sampleRate
|
| 25 |
+
});
|
| 26 |
+
const numberOfChannels = audioBuffer.numberOfChannels;
|
| 27 |
+
const sampleRate = waveform_sample_rate || audioBuffer.sampleRate;
|
| 28 |
+
|
| 29 |
+
let trimmedLength = audioBuffer.length;
|
| 30 |
+
let startOffset = 0;
|
| 31 |
+
|
| 32 |
+
if (start && end) {
|
| 33 |
+
startOffset = Math.round(start * sampleRate);
|
| 34 |
+
const endOffset = Math.round(end * sampleRate);
|
| 35 |
+
trimmedLength = endOffset - startOffset;
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
const trimmedAudioBuffer = audioContext.createBuffer(
|
| 39 |
+
numberOfChannels,
|
| 40 |
+
trimmedLength,
|
| 41 |
+
sampleRate
|
| 42 |
+
);
|
| 43 |
+
|
| 44 |
+
for (let channel = 0; channel < numberOfChannels; channel++) {
|
| 45 |
+
const channelData = audioBuffer.getChannelData(channel);
|
| 46 |
+
const trimmedData = trimmedAudioBuffer.getChannelData(channel);
|
| 47 |
+
for (let i = 0; i < trimmedLength; i++) {
|
| 48 |
+
trimmedData[i] = channelData[startOffset + i];
|
| 49 |
+
}
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
return audioBufferToWav(trimmedAudioBuffer);
|
| 53 |
+
};
|
| 54 |
+
|
| 55 |
+
export function loaded(
|
| 56 |
+
node: HTMLAudioElement,
|
| 57 |
+
{ autoplay }: LoadedParams = {}
|
| 58 |
+
): void {
|
| 59 |
+
async function handle_playback(): Promise<void> {
|
| 60 |
+
if (!autoplay) return;
|
| 61 |
+
node.pause();
|
| 62 |
+
await node.play();
|
| 63 |
+
}
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
export const skip_audio = (waveform: WaveSurfer, amount: number): void => {
|
| 67 |
+
if (!waveform) return;
|
| 68 |
+
waveform.skip(amount);
|
| 69 |
+
};
|
| 70 |
+
|
| 71 |
+
export const get_skip_rewind_amount = (
|
| 72 |
+
audio_duration: number,
|
| 73 |
+
skip_length?: number | null
|
| 74 |
+
): number => {
|
| 75 |
+
if (!skip_length) {
|
| 76 |
+
skip_length = 5;
|
| 77 |
+
}
|
| 78 |
+
return (audio_duration / 100) * skip_length || 5;
|
| 79 |
+
};
|
6.0.0-dev.4/audio/static/StaticAudio.svelte
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts">
|
| 2 |
+
import { uploadToHuggingFace } from "@gradio/utils";
|
| 3 |
+
import { Empty } from "@gradio/atoms";
|
| 4 |
+
import {
|
| 5 |
+
ShareButton,
|
| 6 |
+
IconButton,
|
| 7 |
+
BlockLabel,
|
| 8 |
+
DownloadLink,
|
| 9 |
+
IconButtonWrapper
|
| 10 |
+
} from "@gradio/atoms";
|
| 11 |
+
import { Download, Music } from "@gradio/icons";
|
| 12 |
+
import type { I18nFormatter } from "@gradio/utils";
|
| 13 |
+
import AudioPlayer from "../player/AudioPlayer.svelte";
|
| 14 |
+
import { createEventDispatcher } from "svelte";
|
| 15 |
+
import type { FileData } from "@gradio/client";
|
| 16 |
+
import type { WaveformOptions, SubtitleData } from "../shared/types";
|
| 17 |
+
|
| 18 |
+
export let value: null | FileData = null;
|
| 19 |
+
export let subtitles: null | FileData | SubtitleData[] = null;
|
| 20 |
+
export let label: string;
|
| 21 |
+
export let show_label = true;
|
| 22 |
+
export let buttons: string[] | null = null;
|
| 23 |
+
export let i18n: I18nFormatter;
|
| 24 |
+
export let waveform_settings: Record<string, any> = {};
|
| 25 |
+
export let waveform_options: WaveformOptions = {
|
| 26 |
+
show_recording_waveform: true
|
| 27 |
+
};
|
| 28 |
+
export let editable = true;
|
| 29 |
+
export let loop: boolean;
|
| 30 |
+
export let display_icon_button_wrapper_top_corner = false;
|
| 31 |
+
|
| 32 |
+
const dispatch = createEventDispatcher<{
|
| 33 |
+
change: FileData;
|
| 34 |
+
play: undefined;
|
| 35 |
+
pause: undefined;
|
| 36 |
+
end: undefined;
|
| 37 |
+
stop: undefined;
|
| 38 |
+
}>();
|
| 39 |
+
|
| 40 |
+
$: value && dispatch("change", value);
|
| 41 |
+
</script>
|
| 42 |
+
|
| 43 |
+
<BlockLabel
|
| 44 |
+
{show_label}
|
| 45 |
+
Icon={Music}
|
| 46 |
+
float={false}
|
| 47 |
+
label={label || i18n("audio.audio")}
|
| 48 |
+
/>
|
| 49 |
+
|
| 50 |
+
{#if value !== null}
|
| 51 |
+
<IconButtonWrapper
|
| 52 |
+
display_top_corner={display_icon_button_wrapper_top_corner}
|
| 53 |
+
>
|
| 54 |
+
{#if buttons === null ? true : buttons.includes("download")}
|
| 55 |
+
<DownloadLink
|
| 56 |
+
href={value.is_stream
|
| 57 |
+
? value.url?.replace("playlist.m3u8", "playlist-file")
|
| 58 |
+
: value.url}
|
| 59 |
+
download={value.orig_name || value.path}
|
| 60 |
+
>
|
| 61 |
+
<IconButton Icon={Download} label={i18n("common.download")} />
|
| 62 |
+
</DownloadLink>
|
| 63 |
+
{/if}
|
| 64 |
+
{#if buttons === null ? true : buttons.includes("share")}
|
| 65 |
+
<ShareButton
|
| 66 |
+
{i18n}
|
| 67 |
+
on:error
|
| 68 |
+
on:share
|
| 69 |
+
formatter={async (value) => {
|
| 70 |
+
if (!value) return "";
|
| 71 |
+
let url = await uploadToHuggingFace(value.url, "url");
|
| 72 |
+
return `<audio controls src="${url}"></audio>`;
|
| 73 |
+
}}
|
| 74 |
+
{value}
|
| 75 |
+
/>
|
| 76 |
+
{/if}
|
| 77 |
+
</IconButtonWrapper>
|
| 78 |
+
|
| 79 |
+
<AudioPlayer
|
| 80 |
+
{value}
|
| 81 |
+
subtitles={Array.isArray(subtitles) ? subtitles : subtitles?.url}
|
| 82 |
+
{label}
|
| 83 |
+
{i18n}
|
| 84 |
+
{waveform_settings}
|
| 85 |
+
{waveform_options}
|
| 86 |
+
{editable}
|
| 87 |
+
{loop}
|
| 88 |
+
on:pause
|
| 89 |
+
on:play
|
| 90 |
+
on:stop
|
| 91 |
+
on:load
|
| 92 |
+
/>
|
| 93 |
+
{:else}
|
| 94 |
+
<Empty size="small">
|
| 95 |
+
<Music />
|
| 96 |
+
</Empty>
|
| 97 |
+
{/if}
|
6.0.0-dev.4/audio/streaming/StreamAudio.svelte
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts">
|
| 2 |
+
import { onMount } from "svelte";
|
| 3 |
+
import type { I18nFormatter } from "@gradio/utils";
|
| 4 |
+
import { Spinner } from "@gradio/icons";
|
| 5 |
+
import WaveSurfer from "wavesurfer.js";
|
| 6 |
+
import RecordPlugin from "wavesurfer.js/dist/plugins/record.js";
|
| 7 |
+
import type { WaveformOptions } from "../shared/types";
|
| 8 |
+
import DeviceSelect from "../shared/DeviceSelect.svelte";
|
| 9 |
+
|
| 10 |
+
export let recording = false;
|
| 11 |
+
export let paused_recording = false;
|
| 12 |
+
export let stop: () => void;
|
| 13 |
+
export let record: () => void;
|
| 14 |
+
export let i18n: I18nFormatter;
|
| 15 |
+
export let waveform_settings: Record<string, any>;
|
| 16 |
+
export let waveform_options: WaveformOptions = {
|
| 17 |
+
show_recording_waveform: true
|
| 18 |
+
};
|
| 19 |
+
export let waiting = false;
|
| 20 |
+
|
| 21 |
+
let micWaveform: WaveSurfer;
|
| 22 |
+
let waveformRecord: RecordPlugin;
|
| 23 |
+
|
| 24 |
+
let microphoneContainer: HTMLDivElement;
|
| 25 |
+
|
| 26 |
+
let micDevices: MediaDeviceInfo[] = [];
|
| 27 |
+
|
| 28 |
+
onMount(() => {
|
| 29 |
+
create_mic_waveform();
|
| 30 |
+
});
|
| 31 |
+
|
| 32 |
+
const create_mic_waveform = (): void => {
|
| 33 |
+
if (micWaveform !== undefined) micWaveform.destroy();
|
| 34 |
+
if (!microphoneContainer) return;
|
| 35 |
+
micWaveform = WaveSurfer.create({
|
| 36 |
+
...waveform_settings,
|
| 37 |
+
normalize: false,
|
| 38 |
+
container: microphoneContainer
|
| 39 |
+
});
|
| 40 |
+
|
| 41 |
+
waveformRecord = micWaveform.registerPlugin(RecordPlugin.create());
|
| 42 |
+
};
|
| 43 |
+
</script>
|
| 44 |
+
|
| 45 |
+
<div class="mic-wrap">
|
| 46 |
+
{#if waveform_options.show_recording_waveform}
|
| 47 |
+
<div
|
| 48 |
+
bind:this={microphoneContainer}
|
| 49 |
+
style:display={recording ? "block" : "none"}
|
| 50 |
+
/>
|
| 51 |
+
{/if}
|
| 52 |
+
<div class="controls">
|
| 53 |
+
{#if recording && !waiting}
|
| 54 |
+
<button
|
| 55 |
+
class={paused_recording ? "stop-button-paused" : "stop-button"}
|
| 56 |
+
on:click={() => {
|
| 57 |
+
waveformRecord?.stopMic();
|
| 58 |
+
stop();
|
| 59 |
+
}}
|
| 60 |
+
>
|
| 61 |
+
<span class="record-icon">
|
| 62 |
+
<span class="pinger" />
|
| 63 |
+
<span class="dot" />
|
| 64 |
+
</span>
|
| 65 |
+
{paused_recording ? i18n("audio.pause") : i18n("audio.stop")}
|
| 66 |
+
</button>
|
| 67 |
+
{:else if recording && waiting}
|
| 68 |
+
<button
|
| 69 |
+
class="spinner-button"
|
| 70 |
+
on:click={() => {
|
| 71 |
+
stop();
|
| 72 |
+
}}
|
| 73 |
+
>
|
| 74 |
+
<div class="icon">
|
| 75 |
+
<Spinner />
|
| 76 |
+
</div>
|
| 77 |
+
{i18n("audio.waiting")}
|
| 78 |
+
</button>
|
| 79 |
+
{:else}
|
| 80 |
+
<button
|
| 81 |
+
class="record-button"
|
| 82 |
+
on:click={() => {
|
| 83 |
+
waveformRecord?.startMic();
|
| 84 |
+
record();
|
| 85 |
+
}}
|
| 86 |
+
>
|
| 87 |
+
<span class="record-icon">
|
| 88 |
+
<span class="dot" />
|
| 89 |
+
</span>
|
| 90 |
+
{i18n("audio.record")}
|
| 91 |
+
</button>
|
| 92 |
+
{/if}
|
| 93 |
+
|
| 94 |
+
<DeviceSelect bind:micDevices {i18n} />
|
| 95 |
+
</div>
|
| 96 |
+
</div>
|
| 97 |
+
|
| 98 |
+
<style>
|
| 99 |
+
.controls {
|
| 100 |
+
display: flex;
|
| 101 |
+
align-items: center;
|
| 102 |
+
justify-content: space-between;
|
| 103 |
+
flex-wrap: wrap;
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
.mic-wrap {
|
| 107 |
+
display: block;
|
| 108 |
+
align-items: center;
|
| 109 |
+
margin: var(--spacing-xl);
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
.icon {
|
| 113 |
+
width: var(--size-4);
|
| 114 |
+
height: var(--size-4);
|
| 115 |
+
fill: var(--primary-600);
|
| 116 |
+
stroke: var(--primary-600);
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
.stop-button-paused {
|
| 120 |
+
display: none;
|
| 121 |
+
height: var(--size-8);
|
| 122 |
+
width: var(--size-20);
|
| 123 |
+
background-color: var(--block-background-fill);
|
| 124 |
+
border-radius: var(--button-large-radius);
|
| 125 |
+
align-items: center;
|
| 126 |
+
border: 1px solid var(--block-border-color);
|
| 127 |
+
margin-right: 5px;
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
.stop-button-paused::before {
|
| 131 |
+
content: "";
|
| 132 |
+
height: var(--size-4);
|
| 133 |
+
width: var(--size-4);
|
| 134 |
+
border-radius: var(--radius-full);
|
| 135 |
+
background: var(--primary-600);
|
| 136 |
+
margin: 0 var(--spacing-xl);
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
.stop-button::before {
|
| 140 |
+
content: "";
|
| 141 |
+
height: var(--size-4);
|
| 142 |
+
width: var(--size-4);
|
| 143 |
+
border-radius: var(--radius-full);
|
| 144 |
+
background: var(--primary-600);
|
| 145 |
+
margin: 0 var(--spacing-xl);
|
| 146 |
+
animation: scaling 1800ms infinite;
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
.stop-button {
|
| 150 |
+
height: var(--size-8);
|
| 151 |
+
width: var(--size-20);
|
| 152 |
+
background-color: var(--block-background-fill);
|
| 153 |
+
border-radius: var(--button-large-radius);
|
| 154 |
+
align-items: center;
|
| 155 |
+
border: 1px solid var(--primary-600);
|
| 156 |
+
margin-right: 5px;
|
| 157 |
+
display: flex;
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
.spinner-button {
|
| 161 |
+
height: var(--size-8);
|
| 162 |
+
width: var(--size-24);
|
| 163 |
+
background-color: var(--block-background-fill);
|
| 164 |
+
border-radius: var(--radius-3xl);
|
| 165 |
+
align-items: center;
|
| 166 |
+
border: 1px solid var(--primary-600);
|
| 167 |
+
margin: 0 var(--spacing-xl);
|
| 168 |
+
display: flex;
|
| 169 |
+
justify-content: space-evenly;
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
.record-button::before {
|
| 173 |
+
content: "";
|
| 174 |
+
height: var(--size-4);
|
| 175 |
+
width: var(--size-4);
|
| 176 |
+
border-radius: var(--radius-full);
|
| 177 |
+
background: var(--primary-600);
|
| 178 |
+
margin: 0 var(--spacing-xl);
|
| 179 |
+
}
|
| 180 |
+
|
| 181 |
+
.record-button {
|
| 182 |
+
height: var(--size-8);
|
| 183 |
+
width: var(--size-24);
|
| 184 |
+
background-color: var(--block-background-fill);
|
| 185 |
+
border-radius: var(--button-large-radius);
|
| 186 |
+
display: flex;
|
| 187 |
+
align-items: center;
|
| 188 |
+
border: 1px solid var(--block-border-color);
|
| 189 |
+
}
|
| 190 |
+
|
| 191 |
+
@keyframes scaling {
|
| 192 |
+
0% {
|
| 193 |
+
background-color: var(--primary-600);
|
| 194 |
+
scale: 1;
|
| 195 |
+
}
|
| 196 |
+
50% {
|
| 197 |
+
background-color: var(--primary-600);
|
| 198 |
+
scale: 1.2;
|
| 199 |
+
}
|
| 200 |
+
100% {
|
| 201 |
+
background-color: var(--primary-600);
|
| 202 |
+
scale: 1;
|
| 203 |
+
}
|
| 204 |
+
}
|
| 205 |
+
</style>
|
6.0.0-dev.4/audio/streaming/media_recorder.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import type { IMediaRecorderConstructor } from "extendable-media-recorder";
|
| 2 |
+
|
| 3 |
+
let media_recorder_initialized = false;
|
| 4 |
+
let media_recorder;
|
| 5 |
+
|
| 6 |
+
export async function init_media_recorder(): Promise<IMediaRecorderConstructor> {
|
| 7 |
+
const { MediaRecorder, register } = await import("extendable-media-recorder");
|
| 8 |
+
const { connect } = await import("extendable-media-recorder-wav-encoder");
|
| 9 |
+
|
| 10 |
+
register(await connect());
|
| 11 |
+
media_recorder_initialized = true;
|
| 12 |
+
media_recorder = MediaRecorder;
|
| 13 |
+
return media_recorder;
|
| 14 |
+
}
|