-
Notifications
You must be signed in to change notification settings - Fork 529
feat: add WaveAudioPlayer with waveform visualization and authenticated audio fetch #10158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+651
−29
Merged
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a468d25
feat: add WaveAudioPlayer with waveform visualization for audio previews
viva-jinyi 834e24a
fix: use authenticated API fetch for audio playback in cloud environment
viva-jinyi 6c72039
Merge branch 'main' into feature/new-wave-audio
viva-jinyi 5f30b11
fix: guard short audio buffers, validate fetch response, and clean up…
viva-jinyi 5b7a38e
feat: add volume control to expanded WaveAudioPlayer variant
viva-jinyi da3abe9
refactor: deduplicate formatTime into shared formatUtil
viva-jinyi 59653a7
Merge branch 'main' into feature/new-wave-audio
viva-jinyi b6568a6
fix: add race condition guard and improve test mock fidelity for audi…
viva-jinyi ae9721b
Merge branch 'main' into feature/new-wave-audio
viva-jinyi 7d7152c
fix: address PR #10158 code review feedback
viva-jinyi 77c77b6
Merge branch 'main' into feature/new-wave-audio
DrJKL File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import type { Meta, StoryObj } from '@storybook/vue3-vite' | ||
|
|
||
| import WaveAudioPlayer from './WaveAudioPlayer.vue' | ||
|
|
||
| const meta: Meta<typeof WaveAudioPlayer> = { | ||
| title: 'Components/Audio/WaveAudioPlayer', | ||
| component: WaveAudioPlayer, | ||
| tags: ['autodocs'], | ||
| parameters: { layout: 'centered' } | ||
| } | ||
|
|
||
| export default meta | ||
| type Story = StoryObj<typeof meta> | ||
|
|
||
| export const Default: Story = { | ||
| args: { | ||
| src: '/assets/audio/sample.wav', | ||
| barCount: 40, | ||
| height: 32 | ||
| }, | ||
| decorators: [ | ||
| (story) => ({ | ||
| components: { story }, | ||
| template: | ||
| '<div class="w-80 rounded-lg bg-base-background p-4"><story /></div>' | ||
| }) | ||
| ] | ||
| } | ||
|
|
||
| export const BottomAligned: Story = { | ||
| args: { | ||
| src: '/assets/audio/sample.wav', | ||
| barCount: 40, | ||
| height: 48, | ||
| align: 'bottom' | ||
| }, | ||
| decorators: [ | ||
| (story) => ({ | ||
| components: { story }, | ||
| template: | ||
| '<div class="w-80 rounded-lg bg-base-background p-4"><story /></div>' | ||
| }) | ||
| ] | ||
| } | ||
|
|
||
| export const Expanded: Story = { | ||
| args: { | ||
| src: '/assets/audio/sample.wav', | ||
| variant: 'expanded', | ||
| barCount: 80, | ||
| height: 120 | ||
| }, | ||
| decorators: [ | ||
| (story) => ({ | ||
| components: { story }, | ||
| template: | ||
| '<div class="w-[600px] rounded-2xl bg-base-background/80 p-8 backdrop-blur-sm"><story /></div>' | ||
| }) | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,226 @@ | ||
| <template> | ||
| <!-- Compact: [▶] [waveform] [time] --> | ||
| <div | ||
| v-if="variant === 'compact'" | ||
| :class=" | ||
| cn('flex w-full gap-2', align === 'center' ? 'items-center' : 'items-end') | ||
| " | ||
| @pointerdown.stop | ||
| > | ||
| <Button | ||
| variant="textonly" | ||
| size="icon-sm" | ||
| class="size-7 shrink-0 rounded-full bg-muted-foreground/15 hover:bg-muted-foreground/25" | ||
| :aria-label="isPlaying ? $t('g.pause') : $t('g.play')" | ||
| :loading="loading" | ||
| @click.stop="togglePlayPause" | ||
| > | ||
| <i | ||
| v-if="!isPlaying" | ||
| class="ml-0.5 icon-[lucide--play] size-3 text-base-foreground" | ||
| /> | ||
| <i v-else class="icon-[lucide--pause] size-3 text-base-foreground" /> | ||
| </Button> | ||
|
|
||
| <div | ||
| :ref="(el) => (waveformRef = el as HTMLElement)" | ||
| :class=" | ||
| cn( | ||
| 'flex min-w-0 flex-1 cursor-pointer gap-px', | ||
| align === 'center' ? 'items-center' : 'items-end' | ||
| ) | ||
| " | ||
| :style="{ height: height + 'px' }" | ||
viva-jinyi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @click="handleWaveformClick" | ||
| > | ||
| <div | ||
| v-for="(bar, index) in bars" | ||
| :key="index" | ||
| :class=" | ||
| cn( | ||
| 'flex-1 rounded-full', | ||
| loading | ||
| ? 'bg-muted-foreground/20' | ||
| : index <= playedBarIndex | ||
| ? 'bg-base-foreground' | ||
| : 'bg-muted-foreground/40' | ||
| ) | ||
| " | ||
| :style="{ | ||
| height: (bar.height / 100) * height + 'px', | ||
| minHeight: '2px' | ||
viva-jinyi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }" | ||
| /> | ||
| </div> | ||
|
|
||
| <span class="shrink-0 text-xs text-muted-foreground tabular-nums"> | ||
| {{ formattedCurrentTime }} / {{ formattedDuration }} | ||
| </span> | ||
| </div> | ||
|
|
||
| <!-- Expanded: waveform / progress bar + times / transport --> | ||
| <div v-else class="flex w-full flex-col gap-4" @pointerdown.stop> | ||
| <div | ||
| class="flex w-full items-center gap-0.5" | ||
| :style="{ height: height + 'px' }" | ||
| > | ||
| <div | ||
| v-for="(bar, index) in bars" | ||
| :key="index" | ||
| :class=" | ||
| cn( | ||
| 'flex-1 rounded-full', | ||
| loading ? 'bg-muted-foreground/20' : 'bg-base-foreground' | ||
| ) | ||
| " | ||
| :style="{ | ||
| height: (bar.height / 100) * height + 'px', | ||
| minHeight: '2px' | ||
| }" | ||
| /> | ||
| </div> | ||
|
|
||
| <div class="flex flex-col gap-1"> | ||
| <div | ||
| ref="progressRef" | ||
| class="relative h-1 w-full cursor-pointer rounded-full bg-muted-foreground/20" | ||
| @click="handleProgressClick" | ||
| > | ||
| <div | ||
| class="absolute top-0 left-0 h-full rounded-full bg-base-foreground" | ||
| :style="{ width: progressRatio + '%' }" | ||
| /> | ||
| </div> | ||
| <div | ||
| class="flex justify-between text-xs text-muted-foreground tabular-nums" | ||
| > | ||
| <span>{{ formattedCurrentTime }}</span> | ||
| <span>{{ formattedDuration }}</span> | ||
| </div> | ||
| </div> | ||
|
|
||
| <div class="flex items-center gap-2"> | ||
| <div class="w-20" /> | ||
|
|
||
| <div class="flex flex-1 items-center justify-center gap-2"> | ||
| <Button | ||
| variant="textonly" | ||
| size="icon-sm" | ||
| class="size-8 rounded-full" | ||
| :aria-label="$t('g.skipToStart')" | ||
| :disabled="loading" | ||
| @click="seekToStart" | ||
| > | ||
| <i class="icon-[lucide--skip-back] size-4 text-base-foreground" /> | ||
| </Button> | ||
| <Button | ||
| variant="textonly" | ||
| size="icon-sm" | ||
| class="size-10 rounded-full bg-muted-foreground/15 hover:bg-muted-foreground/25" | ||
| :aria-label="isPlaying ? $t('g.pause') : $t('g.play')" | ||
| :loading="loading" | ||
| @click="togglePlayPause" | ||
| > | ||
| <i | ||
| v-if="!isPlaying" | ||
| class="ml-0.5 icon-[lucide--play] size-5 text-base-foreground" | ||
| /> | ||
| <i v-else class="icon-[lucide--pause] size-5 text-base-foreground" /> | ||
| </Button> | ||
| <Button | ||
| variant="textonly" | ||
| size="icon-sm" | ||
| class="size-8 rounded-full" | ||
| :aria-label="$t('g.skipToEnd')" | ||
| :disabled="loading" | ||
| @click="seekToEnd" | ||
| > | ||
| <i class="icon-[lucide--skip-forward] size-4 text-base-foreground" /> | ||
| </Button> | ||
| </div> | ||
|
|
||
| <div class="flex w-20 items-center gap-1"> | ||
| <Button | ||
| variant="textonly" | ||
| size="icon-sm" | ||
| class="size-8 shrink-0 rounded-full" | ||
| :aria-label="$t('g.volume')" | ||
| :disabled="loading" | ||
| @click="toggleMute" | ||
| > | ||
| <i :class="cn(volumeIcon, 'size-4 text-base-foreground')" /> | ||
| </Button> | ||
| <Slider | ||
| :model-value="[volume * 100]" | ||
| :min="0" | ||
| :max="100" | ||
| :step="1" | ||
| class="flex-1" | ||
| @update:model-value="(v) => (volume = (v?.[0] ?? 100) / 100)" | ||
| /> | ||
| </div> | ||
| </div> | ||
| </div> | ||
|
|
||
| <audio | ||
| :ref="(el) => (audioRef = el as HTMLAudioElement)" | ||
| :src="audioSrc" | ||
| preload="metadata" | ||
| class="hidden" | ||
| /> | ||
| </template> | ||
|
|
||
| <script setup lang="ts"> | ||
| import { ref, toRef } from 'vue' | ||
|
|
||
| import Button from '@/components/ui/button/Button.vue' | ||
| import Slider from '@/components/ui/slider/Slider.vue' | ||
| import { useWaveAudioPlayer } from '@/composables/useWaveAudioPlayer' | ||
| import { cn } from '@/utils/tailwindUtil' | ||
|
|
||
| const { | ||
| src, | ||
| barCount = 40, | ||
| height = 32, | ||
| align = 'center', | ||
| variant = 'compact' | ||
| } = defineProps<{ | ||
| src: string | ||
| barCount?: number | ||
| height?: number | ||
| align?: 'center' | 'bottom' | ||
| variant?: 'compact' | 'expanded' | ||
viva-jinyi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }>() | ||
|
|
||
| const progressRef = ref<HTMLElement>() | ||
|
|
||
| const { | ||
| audioRef, | ||
| waveformRef, | ||
| audioSrc, | ||
| bars, | ||
| loading, | ||
| isPlaying, | ||
| playedBarIndex, | ||
| progressRatio, | ||
| formattedCurrentTime, | ||
| formattedDuration, | ||
| togglePlayPause, | ||
| seekToStart, | ||
| seekToEnd, | ||
| volume, | ||
| volumeIcon, | ||
| toggleMute, | ||
| seekToRatio, | ||
| handleWaveformClick | ||
| } = useWaveAudioPlayer({ | ||
| src: toRef(() => src), | ||
| barCount | ||
| }) | ||
|
|
||
| function handleProgressClick(event: MouseEvent) { | ||
| if (!progressRef.value) return | ||
| const rect = progressRef.value.getBoundingClientRect() | ||
| seekToRatio((event.clientX - rect.left) / rect.width) | ||
| } | ||
| </script> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,21 @@ | ||
| <template> | ||
| <audio controls width="100%" height="100%"> | ||
| <source :src="url" :type="htmlAudioType" /> | ||
| {{ $t('g.audioFailedToLoad') }} | ||
| </audio> | ||
| <div | ||
| class="m-auto w-[min(90vw,42rem)] rounded-2xl bg-base-background/80 p-8 backdrop-blur-sm" | ||
| > | ||
| <WaveAudioPlayer | ||
| :src="result.url" | ||
| variant="expanded" | ||
| :height="120" | ||
| :bar-count="80" | ||
| /> | ||
| </div> | ||
| </template> | ||
|
|
||
| <script setup lang="ts"> | ||
| import { computed } from 'vue' | ||
| import WaveAudioPlayer from '@/components/common/WaveAudioPlayer.vue' | ||
| import type { ResultItemImpl } from '@/stores/queueStore' | ||
| const { result } = defineProps<{ | ||
| defineProps<{ | ||
| result: ResultItemImpl | ||
| }>() | ||
| const url = computed(() => result.url) | ||
| const htmlAudioType = computed(() => result.htmlAudioType) | ||
| </script> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.