|
| 1 | +<script lang="ts"> |
| 2 | + import { onMount, onDestroy } from "svelte"; |
| 3 | + import WaveSurfer from "wavesurfer.js"; |
| 4 | + import type { FileData } from "@gradio/client"; |
| 5 | + import { format_time } from "@gradio/utils"; |
| 6 | +
|
| 7 | + export let value: FileData; |
| 8 | + export let label: string; |
| 9 | + export let loop = false; |
| 10 | +
|
| 11 | + let container: HTMLDivElement; |
| 12 | + let waveform: WaveSurfer | undefined; |
| 13 | + let playing = false; |
| 14 | + let duration = 0; |
| 15 | + let currentTime = 0; |
| 16 | + let waveform_ready = false; |
| 17 | +
|
| 18 | + $: resolved_src = value.url; |
| 19 | +
|
| 20 | + const create_waveform = async (): Promise<void> => { |
| 21 | + if (!container || !resolved_src || waveform_ready) return; |
| 22 | +
|
| 23 | + if (waveform) { |
| 24 | + waveform.destroy(); |
| 25 | + } |
| 26 | +
|
| 27 | + const accentColor = |
| 28 | + getComputedStyle(document.documentElement).getPropertyValue( |
| 29 | + "--color-accent" |
| 30 | + ) || "#ff7c00"; |
| 31 | +
|
| 32 | + waveform = WaveSurfer.create({ |
| 33 | + container, |
| 34 | + height: 32, |
| 35 | + waveColor: "rgba(128, 128, 128, 0.5)", |
| 36 | + progressColor: accentColor, |
| 37 | + cursorColor: "transparent", |
| 38 | + barWidth: 2, |
| 39 | + barGap: 2, |
| 40 | + barRadius: 2, |
| 41 | + normalize: true, |
| 42 | + interact: true, |
| 43 | + dragToSeek: true, |
| 44 | + hideScrollbar: true |
| 45 | + }); |
| 46 | +
|
| 47 | + waveform.on("play", () => (playing = true)); |
| 48 | + waveform.on("pause", () => (playing = false)); |
| 49 | + waveform.on("ready", () => { |
| 50 | + duration = waveform?.getDuration() || 0; |
| 51 | + waveform_ready = true; |
| 52 | + }); |
| 53 | + waveform.on("audioprocess", () => { |
| 54 | + currentTime = waveform?.getCurrentTime() || 0; |
| 55 | + }); |
| 56 | + waveform.on("interaction", () => { |
| 57 | + currentTime = waveform?.getCurrentTime() || 0; |
| 58 | + }); |
| 59 | + waveform.on("finish", () => { |
| 60 | + playing = false; |
| 61 | + if (loop) { |
| 62 | + waveform?.play(); |
| 63 | + } |
| 64 | + }); |
| 65 | +
|
| 66 | + await waveform.load(resolved_src); |
| 67 | + }; |
| 68 | +
|
| 69 | + onMount(async () => { |
| 70 | + await create_waveform(); |
| 71 | + }); |
| 72 | +
|
| 73 | + onDestroy(() => { |
| 74 | + if (waveform) { |
| 75 | + waveform.destroy(); |
| 76 | + } |
| 77 | + }); |
| 78 | +
|
| 79 | + const togglePlay = (): void => { |
| 80 | + if (waveform) { |
| 81 | + waveform.playPause(); |
| 82 | + } |
| 83 | + }; |
| 84 | +</script> |
| 85 | + |
| 86 | +<div |
| 87 | + class="minimal-audio-player" |
| 88 | + aria-label={label || "Audio"} |
| 89 | + data-testid={label && typeof label === "string" && label.trim() |
| 90 | + ? "waveform-" + label |
| 91 | + : "unlabelled-audio"} |
| 92 | +> |
| 93 | + <button |
| 94 | + class="play-btn" |
| 95 | + on:click={togglePlay} |
| 96 | + aria-label={playing ? "Pause" : "Play"} |
| 97 | + > |
| 98 | + {#if playing} |
| 99 | + <svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"> |
| 100 | + <rect x="6" y="5" width="4" height="14" rx="1" fill="currentColor" /> |
| 101 | + <rect x="14" y="5" width="4" height="14" rx="1" fill="currentColor" /> |
| 102 | + </svg> |
| 103 | + {:else} |
| 104 | + <svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"> |
| 105 | + <path |
| 106 | + d="M8 5.74537C8 5.06444 8.77346 4.64713 9.35139 5.02248L18.0227 10.2771C18.5518 10.6219 18.5518 11.3781 18.0227 11.7229L9.35139 16.9775C8.77346 17.3529 8 16.9356 8 16.2546V5.74537Z" |
| 107 | + fill="currentColor" |
| 108 | + /> |
| 109 | + </svg> |
| 110 | + {/if} |
| 111 | + </button> |
| 112 | + |
| 113 | + <div class="waveform-wrapper" bind:this={container}></div> |
| 114 | + |
| 115 | + <div class="timestamp">{format_time(playing ? currentTime : duration)}</div> |
| 116 | +</div> |
| 117 | + |
| 118 | +<style> |
| 119 | + .minimal-audio-player { |
| 120 | + display: flex; |
| 121 | + align-items: center; |
| 122 | + gap: var(--spacing-sm); |
| 123 | + border-radius: var(--radius-sm); |
| 124 | + width: var(--size-52); |
| 125 | + padding: var(--spacing-sm); |
| 126 | + } |
| 127 | +
|
| 128 | + .play-btn { |
| 129 | + display: inline-flex; |
| 130 | + align-items: center; |
| 131 | + justify-content: center; |
| 132 | + padding: 0; |
| 133 | + border: none; |
| 134 | + background: none; |
| 135 | + color: var(--body-text-color); |
| 136 | + opacity: 0.7; |
| 137 | + cursor: pointer; |
| 138 | + border-radius: 50%; |
| 139 | + transition: all 0.2s ease; |
| 140 | + flex-shrink: 0; |
| 141 | + } |
| 142 | +
|
| 143 | + .play-btn:hover { |
| 144 | + color: var(--color-accent); |
| 145 | + opacity: 1; |
| 146 | + } |
| 147 | +
|
| 148 | + .play-btn:active { |
| 149 | + transform: scale(0.95); |
| 150 | + } |
| 151 | +
|
| 152 | + .play-btn svg { |
| 153 | + width: var(--size-5); |
| 154 | + height: var(--size-5); |
| 155 | + display: block; |
| 156 | + } |
| 157 | +
|
| 158 | + .waveform-wrapper { |
| 159 | + flex: 1 1 auto; |
| 160 | + cursor: pointer; |
| 161 | + width: auto; |
| 162 | + } |
| 163 | +
|
| 164 | + .waveform-wrapper :global(::part(wrapper)) { |
| 165 | + margin-bottom: 0; |
| 166 | + } |
| 167 | +
|
| 168 | + .timestamp { |
| 169 | + font-size: 13px; |
| 170 | + font-weight: 500; |
| 171 | + color: var(--body-text-color); |
| 172 | + opacity: 0.7; |
| 173 | + font-variant-numeric: tabular-nums; |
| 174 | + flex-shrink: 0; |
| 175 | + min-width: 40px; |
| 176 | + text-align: center; |
| 177 | + } |
| 178 | +
|
| 179 | + @media (prefers-reduced-motion: reduce) { |
| 180 | + .play-btn { |
| 181 | + transition: none; |
| 182 | + } |
| 183 | + } |
| 184 | +</style> |
0 commit comments