2022-03-07 21:36:49 +08:00
|
|
|
<script context="module" lang="ts">
|
|
|
|
import type { FileData } from "@gradio/upload";
|
2023-08-04 06:01:18 +08:00
|
|
|
import { BaseButton } from "@gradio/button/static";
|
|
|
|
|
2022-03-07 21:36:49 +08:00
|
|
|
export interface AudioData extends FileData {
|
|
|
|
crop_min?: number;
|
|
|
|
crop_max?: number;
|
|
|
|
}
|
|
|
|
</script>
|
2022-02-23 19:17:41 +08:00
|
|
|
|
2022-03-07 21:36:49 +08:00
|
|
|
<script lang="ts">
|
2023-08-04 06:01:18 +08:00
|
|
|
import { onDestroy, createEventDispatcher } from "svelte";
|
2022-02-23 19:17:41 +08:00
|
|
|
import { Upload, ModifyUpload } from "@gradio/upload";
|
2022-05-10 08:26:09 +08:00
|
|
|
import { BlockLabel } from "@gradio/atoms";
|
|
|
|
import { Music } from "@gradio/icons";
|
2023-10-17 00:26:49 +08:00
|
|
|
import Audio from "../shared/Audio.svelte";
|
2022-07-22 02:12:46 +08:00
|
|
|
// @ts-ignore
|
2022-02-23 19:17:41 +08:00
|
|
|
import Range from "svelte-range-slider-pips";
|
2023-08-19 00:37:19 +08:00
|
|
|
import { _ } from "svelte-i18n";
|
2022-02-23 19:17:41 +08:00
|
|
|
|
2022-10-06 05:11:47 +08:00
|
|
|
import type { IBlobEvent, IMediaRecorder } from "extendable-media-recorder";
|
|
|
|
|
2022-02-25 20:24:32 +08:00
|
|
|
export let value: null | { name: string; data: string } = null;
|
2022-04-08 01:18:41 +08:00
|
|
|
export let label: string;
|
2023-06-29 02:37:21 +08:00
|
|
|
export let show_label = true;
|
|
|
|
export let name = "";
|
2022-03-07 21:36:49 +08:00
|
|
|
export let source: "microphone" | "upload" | "none";
|
2023-06-29 02:37:21 +08:00
|
|
|
export let pending = false;
|
|
|
|
export let streaming = false;
|
|
|
|
export let autoplay = false;
|
2023-08-10 04:38:07 +08:00
|
|
|
export let show_edit_button = true;
|
2022-02-23 19:17:41 +08:00
|
|
|
|
2022-03-17 00:34:30 +08:00
|
|
|
// TODO: make use of this
|
2022-07-22 02:12:46 +08:00
|
|
|
// export let type: "normal" | "numpy" = "normal";
|
2022-03-17 00:34:30 +08:00
|
|
|
|
2022-02-23 19:17:41 +08:00
|
|
|
let recording = false;
|
2022-10-06 05:11:47 +08:00
|
|
|
let recorder: IMediaRecorder;
|
2022-02-23 19:17:41 +08:00
|
|
|
let mode = "";
|
2022-10-06 05:11:47 +08:00
|
|
|
let header: Uint8Array | undefined = undefined;
|
2023-06-29 02:37:21 +08:00
|
|
|
let pending_stream: Uint8Array[] = [];
|
|
|
|
let submit_pending_stream_on_pending_end = false;
|
2023-06-09 07:27:58 +08:00
|
|
|
let player: HTMLAudioElement;
|
2022-02-23 19:17:41 +08:00
|
|
|
let inited = false;
|
2023-06-29 02:37:21 +08:00
|
|
|
let crop_values: [number, number] = [0, 100];
|
2022-10-06 05:11:47 +08:00
|
|
|
const STREAM_TIMESLICE = 500;
|
|
|
|
const NUM_HEADER_BYTES = 44;
|
2023-06-29 02:37:21 +08:00
|
|
|
let audio_chunks: Blob[] = [];
|
2023-08-04 06:01:18 +08:00
|
|
|
let module_promises: [
|
|
|
|
Promise<typeof import("extendable-media-recorder")>,
|
|
|
|
Promise<typeof import("extendable-media-recorder-wav-encoder")>
|
|
|
|
];
|
2022-10-06 05:11:47 +08:00
|
|
|
|
2023-06-29 02:37:21 +08:00
|
|
|
function get_modules(): void {
|
2022-10-06 05:11:47 +08:00
|
|
|
module_promises = [
|
|
|
|
import("extendable-media-recorder"),
|
2023-09-11 17:45:06 +08:00
|
|
|
import("extendable-media-recorder-wav-encoder"),
|
2022-10-06 05:11:47 +08:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (streaming) {
|
|
|
|
get_modules();
|
|
|
|
}
|
2022-02-23 19:17:41 +08:00
|
|
|
|
2022-03-07 21:36:49 +08:00
|
|
|
const dispatch = createEventDispatcher<{
|
2023-06-29 05:40:53 +08:00
|
|
|
change: AudioData | null;
|
2022-05-17 02:51:09 +08:00
|
|
|
stream: AudioData;
|
2023-06-29 05:40:53 +08:00
|
|
|
edit: never;
|
|
|
|
play: never;
|
|
|
|
pause: never;
|
|
|
|
stop: never;
|
|
|
|
end: never;
|
2022-04-26 22:48:39 +08:00
|
|
|
drag: boolean;
|
2022-09-29 22:33:22 +08:00
|
|
|
error: string;
|
2022-10-26 08:58:12 +08:00
|
|
|
upload: FileData;
|
2023-06-29 05:40:53 +08:00
|
|
|
clear: never;
|
|
|
|
start_recording: never;
|
|
|
|
stop_recording: never;
|
2022-03-07 21:36:49 +08:00
|
|
|
}>();
|
2022-02-23 19:17:41 +08:00
|
|
|
|
|
|
|
function blob_to_data_url(blob: Blob): Promise<string> {
|
|
|
|
return new Promise((fulfill, reject) => {
|
|
|
|
let reader = new FileReader();
|
|
|
|
reader.onerror = reject;
|
2022-07-22 02:12:46 +08:00
|
|
|
reader.onload = () => fulfill(reader.result as string);
|
2022-02-23 19:17:41 +08:00
|
|
|
reader.readAsDataURL(blob);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-10-06 05:11:47 +08:00
|
|
|
const dispatch_blob = async (
|
2023-06-29 02:37:21 +08:00
|
|
|
blobs: Uint8Array[] | Blob[],
|
2023-06-20 22:44:25 +08:00
|
|
|
event: "stream" | "change" | "stop_recording"
|
2023-06-29 02:37:21 +08:00
|
|
|
): Promise<void> => {
|
|
|
|
let _audio_blob = new Blob(blobs, { type: "audio/wav" });
|
2022-10-06 05:11:47 +08:00
|
|
|
value = {
|
2023-06-29 02:37:21 +08:00
|
|
|
data: await blob_to_data_url(_audio_blob),
|
2023-09-11 17:45:06 +08:00
|
|
|
name: "audio.wav",
|
2022-10-06 05:11:47 +08:00
|
|
|
};
|
|
|
|
dispatch(event, value);
|
|
|
|
};
|
|
|
|
|
2023-06-29 02:37:21 +08:00
|
|
|
async function prepare_audio(): Promise<void> {
|
2022-09-29 22:33:22 +08:00
|
|
|
let stream: MediaStream | null;
|
2022-10-06 05:11:47 +08:00
|
|
|
|
2022-09-29 22:33:22 +08:00
|
|
|
try {
|
|
|
|
stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
|
|
|
} catch (err) {
|
2023-10-05 03:31:00 +08:00
|
|
|
if (!navigator.mediaDevices) {
|
|
|
|
dispatch("error", $_("audio.no_device_support"));
|
|
|
|
return;
|
|
|
|
}
|
2022-09-29 22:33:22 +08:00
|
|
|
if (err instanceof DOMException && err.name == "NotAllowedError") {
|
2023-08-18 19:36:52 +08:00
|
|
|
dispatch("error", $_("audio.allow_recording_access"));
|
2022-09-29 22:33:22 +08:00
|
|
|
return;
|
|
|
|
}
|
2023-06-29 02:37:21 +08:00
|
|
|
throw err;
|
2022-09-29 22:33:22 +08:00
|
|
|
}
|
|
|
|
|
2022-10-06 05:11:47 +08:00
|
|
|
if (stream == null) return;
|
2022-09-29 22:33:22 +08:00
|
|
|
|
2022-10-06 05:11:47 +08:00
|
|
|
if (streaming) {
|
|
|
|
const [{ MediaRecorder, register }, { connect }] = await Promise.all(
|
|
|
|
module_promises
|
|
|
|
);
|
2022-02-23 19:17:41 +08:00
|
|
|
|
2022-10-06 05:11:47 +08:00
|
|
|
await register(await connect());
|
2022-02-23 19:17:41 +08:00
|
|
|
|
2022-10-06 05:11:47 +08:00
|
|
|
recorder = new MediaRecorder(stream, { mimeType: "audio/wav" });
|
|
|
|
|
|
|
|
recorder.addEventListener("dataavailable", handle_chunk);
|
|
|
|
} else {
|
|
|
|
recorder = new MediaRecorder(stream);
|
|
|
|
|
|
|
|
recorder.addEventListener("dataavailable", (event) => {
|
|
|
|
audio_chunks.push(event.data);
|
|
|
|
});
|
|
|
|
|
|
|
|
recorder.addEventListener("stop", async () => {
|
|
|
|
recording = false;
|
|
|
|
await dispatch_blob(audio_chunks, "change");
|
2023-06-20 22:44:25 +08:00
|
|
|
await dispatch_blob(audio_chunks, "stop_recording");
|
2022-10-06 05:11:47 +08:00
|
|
|
audio_chunks = [];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-07-20 02:37:07 +08:00
|
|
|
inited = true;
|
2022-02-23 19:17:41 +08:00
|
|
|
}
|
|
|
|
|
2023-08-04 06:01:18 +08:00
|
|
|
async function handle_chunk(event: IBlobEvent): Promise<void> {
|
|
|
|
let buffer = await event.data.arrayBuffer();
|
|
|
|
let payload = new Uint8Array(buffer);
|
|
|
|
if (!header) {
|
|
|
|
header = new Uint8Array(buffer.slice(0, NUM_HEADER_BYTES));
|
|
|
|
payload = new Uint8Array(buffer.slice(NUM_HEADER_BYTES));
|
|
|
|
}
|
|
|
|
if (pending) {
|
|
|
|
pending_stream.push(payload);
|
|
|
|
} else {
|
|
|
|
let blobParts = [header].concat(pending_stream, [payload]);
|
|
|
|
dispatch_blob(blobParts, "stream");
|
|
|
|
pending_stream = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-06 05:11:47 +08:00
|
|
|
$: if (submit_pending_stream_on_pending_end && pending === false) {
|
|
|
|
submit_pending_stream_on_pending_end = false;
|
|
|
|
if (header && pending_stream) {
|
2023-06-29 02:37:21 +08:00
|
|
|
let blobParts: Uint8Array[] = [header].concat(pending_stream);
|
2022-10-06 05:11:47 +08:00
|
|
|
pending_stream = [];
|
|
|
|
dispatch_blob(blobParts, "stream");
|
|
|
|
}
|
|
|
|
}
|
2022-02-23 19:17:41 +08:00
|
|
|
|
2023-06-29 02:37:21 +08:00
|
|
|
async function record(): Promise<void> {
|
2023-10-05 03:31:00 +08:00
|
|
|
if (!navigator.mediaDevices) {
|
|
|
|
dispatch("error", $_("audio.no_device_support"));
|
|
|
|
return;
|
|
|
|
}
|
2022-10-06 05:11:47 +08:00
|
|
|
recording = true;
|
2023-06-08 11:54:02 +08:00
|
|
|
dispatch("start_recording");
|
2022-10-06 05:11:47 +08:00
|
|
|
if (!inited) await prepare_audio();
|
|
|
|
header = undefined;
|
|
|
|
if (streaming) {
|
|
|
|
recorder.start(STREAM_TIMESLICE);
|
|
|
|
} else {
|
2022-09-29 22:33:22 +08:00
|
|
|
recorder.start();
|
|
|
|
}
|
2022-02-23 19:17:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
onDestroy(() => {
|
2022-02-25 20:24:32 +08:00
|
|
|
if (recorder && recorder.state !== "inactive") {
|
2022-02-23 19:17:41 +08:00
|
|
|
recorder.stop();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-06-29 02:37:21 +08:00
|
|
|
function stop(): void {
|
2022-02-23 19:17:41 +08:00
|
|
|
recorder.stop();
|
2022-05-17 02:51:09 +08:00
|
|
|
if (streaming) {
|
|
|
|
recording = false;
|
2023-09-11 17:45:06 +08:00
|
|
|
dispatch("stop_recording");
|
2022-10-06 05:11:47 +08:00
|
|
|
if (pending) {
|
|
|
|
submit_pending_stream_on_pending_end = true;
|
|
|
|
}
|
2022-05-17 02:51:09 +08:00
|
|
|
}
|
2023-06-29 02:37:21 +08:00
|
|
|
}
|
2022-02-23 19:17:41 +08:00
|
|
|
|
2023-06-29 02:37:21 +08:00
|
|
|
function clear(): void {
|
2023-06-29 05:40:53 +08:00
|
|
|
dispatch("change", null);
|
2022-10-26 08:58:12 +08:00
|
|
|
dispatch("clear");
|
2022-02-23 19:17:41 +08:00
|
|
|
mode = "";
|
2022-02-25 20:24:32 +08:00
|
|
|
value = null;
|
2022-02-23 19:17:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function handle_change({
|
2023-09-11 17:45:06 +08:00
|
|
|
detail: { values },
|
2022-02-23 19:17:41 +08:00
|
|
|
}: {
|
|
|
|
detail: { values: [number, number] };
|
2023-06-29 02:37:21 +08:00
|
|
|
}): void {
|
2022-04-08 01:18:41 +08:00
|
|
|
if (!value) return;
|
2022-02-23 19:17:41 +08:00
|
|
|
|
|
|
|
dispatch("change", {
|
2022-02-25 20:24:32 +08:00
|
|
|
data: value.data,
|
2022-02-23 19:17:41 +08:00
|
|
|
name,
|
|
|
|
crop_min: values[0],
|
2023-09-11 17:45:06 +08:00
|
|
|
crop_max: values[1],
|
2022-02-23 19:17:41 +08:00
|
|
|
});
|
2022-03-17 00:34:30 +08:00
|
|
|
|
|
|
|
dispatch("edit");
|
2022-02-23 19:17:41 +08:00
|
|
|
}
|
2022-02-25 23:12:22 +08:00
|
|
|
|
|
|
|
function handle_load({
|
2023-09-11 17:45:06 +08:00
|
|
|
detail,
|
2022-02-25 23:12:22 +08:00
|
|
|
}: {
|
2023-05-09 09:35:52 +08:00
|
|
|
detail: {
|
|
|
|
data: string;
|
|
|
|
name: string;
|
|
|
|
size: number;
|
|
|
|
is_example: boolean;
|
|
|
|
};
|
2023-06-29 02:37:21 +08:00
|
|
|
}): void {
|
2022-02-25 23:12:22 +08:00
|
|
|
value = detail;
|
|
|
|
dispatch("change", { data: detail.data, name: detail.name });
|
2022-10-26 08:58:12 +08:00
|
|
|
dispatch("upload", detail);
|
2022-02-25 23:12:22 +08:00
|
|
|
}
|
2022-04-06 01:11:29 +08:00
|
|
|
|
2023-06-29 02:37:21 +08:00
|
|
|
function handle_ended(): void {
|
2023-06-08 11:54:02 +08:00
|
|
|
dispatch("stop");
|
|
|
|
dispatch("end");
|
|
|
|
}
|
|
|
|
|
2022-04-06 01:11:29 +08:00
|
|
|
export let dragging = false;
|
2022-04-26 22:48:39 +08:00
|
|
|
$: dispatch("drag", dragging);
|
2022-02-23 19:17:41 +08:00
|
|
|
</script>
|
|
|
|
|
2023-03-07 04:52:31 +08:00
|
|
|
<BlockLabel
|
|
|
|
{show_label}
|
|
|
|
Icon={Music}
|
|
|
|
float={source === "upload" && value === null}
|
2023-08-18 19:36:52 +08:00
|
|
|
label={label || $_("audio.audio")}
|
2023-03-07 04:52:31 +08:00
|
|
|
/>
|
2022-05-17 02:51:09 +08:00
|
|
|
{#if value === null || streaming}
|
2022-04-26 22:48:39 +08:00
|
|
|
{#if source === "microphone"}
|
2023-01-18 04:47:40 +08:00
|
|
|
<div class="mic-wrap">
|
2022-04-26 22:48:39 +08:00
|
|
|
{#if recording}
|
2023-08-04 06:01:18 +08:00
|
|
|
<BaseButton size="sm" on:click={stop}>
|
2023-01-18 04:47:40 +08:00
|
|
|
<span class="record-icon">
|
|
|
|
<span class="pinger" />
|
|
|
|
<span class="dot" />
|
2022-04-26 22:48:39 +08:00
|
|
|
</span>
|
2023-08-19 00:37:19 +08:00
|
|
|
{$_("audio.stop_recording")}
|
2023-08-04 06:01:18 +08:00
|
|
|
</BaseButton>
|
2022-04-26 22:48:39 +08:00
|
|
|
{:else}
|
2023-08-04 06:01:18 +08:00
|
|
|
<BaseButton size="sm" on:click={record}>
|
2023-01-18 04:47:40 +08:00
|
|
|
<span class="record-icon">
|
|
|
|
<span class="dot" />
|
2022-04-26 22:48:39 +08:00
|
|
|
</span>
|
2023-08-19 00:37:19 +08:00
|
|
|
{$_("audio.record_from_microphone")}
|
2023-08-04 06:01:18 +08:00
|
|
|
</BaseButton>
|
2022-04-26 22:48:39 +08:00
|
|
|
{/if}
|
|
|
|
</div>
|
|
|
|
{:else if source === "upload"}
|
2023-05-09 09:35:52 +08:00
|
|
|
<!-- explicitly listed out audio mimetypes due to iOS bug not recognizing audio/* -->
|
|
|
|
<Upload
|
|
|
|
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/*"
|
|
|
|
on:load={handle_load}
|
|
|
|
bind:dragging
|
|
|
|
>
|
2023-01-18 04:47:40 +08:00
|
|
|
<slot />
|
2022-04-26 22:48:39 +08:00
|
|
|
</Upload>
|
|
|
|
{/if}
|
|
|
|
{:else}
|
|
|
|
<ModifyUpload
|
|
|
|
on:clear={clear}
|
|
|
|
on:edit={() => (mode = "edit")}
|
2023-08-10 04:38:07 +08:00
|
|
|
editable={show_edit_button}
|
2023-03-07 04:52:31 +08:00
|
|
|
absolute={true}
|
2022-04-26 22:48:39 +08:00
|
|
|
/>
|
|
|
|
|
2023-10-17 00:26:49 +08:00
|
|
|
<div class="container">
|
|
|
|
<Audio
|
|
|
|
controls
|
|
|
|
{autoplay}
|
|
|
|
{crop_values}
|
|
|
|
bind:node={player}
|
|
|
|
preload="metadata"
|
|
|
|
src={value?.data}
|
|
|
|
on:play
|
|
|
|
on:pause
|
|
|
|
on:ended={handle_ended}
|
|
|
|
data-testid={`${label}-audio`}
|
|
|
|
/>
|
|
|
|
</div>
|
2022-04-26 22:48:39 +08:00
|
|
|
|
|
|
|
{#if mode === "edit" && player?.duration}
|
|
|
|
<Range
|
|
|
|
bind:values={crop_values}
|
|
|
|
range
|
|
|
|
min={0}
|
|
|
|
max={100}
|
|
|
|
step={1}
|
|
|
|
on:change={handle_change}
|
2022-02-23 19:17:41 +08:00
|
|
|
/>
|
|
|
|
{/if}
|
2022-04-26 22:48:39 +08:00
|
|
|
{/if}
|
2023-01-18 04:47:40 +08:00
|
|
|
|
|
|
|
<style>
|
|
|
|
.mic-wrap {
|
|
|
|
padding: var(--size-2);
|
|
|
|
}
|
|
|
|
|
|
|
|
.record-icon {
|
|
|
|
display: flex;
|
|
|
|
position: relative;
|
|
|
|
margin-right: var(--size-2);
|
|
|
|
width: 6px;
|
|
|
|
height: 6px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.dot {
|
|
|
|
display: inline-flex;
|
|
|
|
position: relative;
|
|
|
|
border-radius: var(--radius-full);
|
|
|
|
background: var(--color-red-500);
|
|
|
|
width: 6px;
|
|
|
|
height: 6px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.pinger {
|
|
|
|
display: inline-flex;
|
|
|
|
position: absolute;
|
|
|
|
opacity: 0.9;
|
|
|
|
animation: ping 1s cubic-bezier(0, 0, 0.2, 1) infinite;
|
|
|
|
border-radius: var(--radius-full);
|
|
|
|
background: var(--color-red-500);
|
|
|
|
width: var(--size-full);
|
|
|
|
height: var(--size-full);
|
|
|
|
}
|
|
|
|
|
|
|
|
@keyframes ping {
|
|
|
|
75%,
|
|
|
|
100% {
|
|
|
|
transform: scale(2);
|
|
|
|
opacity: 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|