From 76bead7449c5de35a123024d9b2c3259d4ba38cf Mon Sep 17 00:00:00 2001 From: Joshua Higgins Date: Thu, 4 Jun 2026 22:47:22 -0400 Subject: [PATCH] feat: actual, honest-to-god playback --- app/api/song/[id]/play/route.ts | 117 ++++++++---- components/options-modal.tsx | 2 +- components/playback-provider.tsx | 297 ++++++++++++++++++++++++++++++- components/player-bar.tsx | 53 +++++- components/ui/calendar.tsx | 2 +- convex/auth.ts | 2 + convex/favorites.ts | 6 +- eslint.config.mjs | 2 +- lib/audio-content-type.ts | 22 +++ lib/song-play-url.ts | 17 ++ 10 files changed, 471 insertions(+), 49 deletions(-) create mode 100644 lib/audio-content-type.ts create mode 100644 lib/song-play-url.ts diff --git a/app/api/song/[id]/play/route.ts b/app/api/song/[id]/play/route.ts index 351dd86..8ba7aa0 100644 --- a/app/api/song/[id]/play/route.ts +++ b/app/api/song/[id]/play/route.ts @@ -1,16 +1,77 @@ import { api } from "@/convex/_generated/api" import type { Id } from "@/convex/_generated/dataModel" +import { audioContentTypeForPath } from "@/lib/audio-content-type" import { getConvexClient } from "@/lib/convex-server" -import { serveCoverFile } from "@/lib/serve-cover" import { NextRequest, NextResponse } from "next/server" -import { spawn } from "node:child_process" +import { spawn, type ChildProcessWithoutNullStreams } from "node:child_process" import fs from "node:fs/promises" -import * as music from "music-metadata" const DATA_DIR = process.env.NEXT_PUBLIC_CONVEX_URL?.includes("127.0.0.1") ? "/Users/joshuafhiggins/Downloads/test" : process.env.DATA || "/data" +function ffmpegToReadableStream(ffmpegProcess: ChildProcessWithoutNullStreams) { + let closed = false + + return new ReadableStream({ + start(controller) { + const finalize = (err?: Error) => { + if (closed) { + return + } + closed = true + + try { + if (err) { + controller.error(err) + } else { + controller.close() + } + } catch { + // Consumer may have already closed the stream (e.g. track change). + } + } + + ffmpegProcess.stdout.on("data", (chunk: Buffer) => { + if (closed) { + return + } + + try { + controller.enqueue(new Uint8Array(chunk)) + } catch { + closed = true + ffmpegProcess.kill("SIGKILL") + } + }) + + ffmpegProcess.stderr.on("data", (data) => { + //console.error(`FFmpeg log: ${data.toString()}`) + }) + + ffmpegProcess.stdout.on("end", () => finalize()) + ffmpegProcess.stdout.on("error", (err) => finalize(err)) + + ffmpegProcess.on("close", (code) => { + if (code !== 0 && !closed) { + finalize(new Error(`ffmpeg exited with code ${code}`)) + } else { + finalize() + } + }) + + ffmpegProcess.on("error", (err) => finalize(err)) + }, + cancel() { + if (closed) { + return + } + closed = true + ffmpegProcess.kill("SIGKILL") + }, + }) +} + export async function GET( request: NextRequest, { params }: { params: Promise<{ id: string }> } @@ -34,9 +95,8 @@ export async function GET( | "lossless" | null - const preferAtmos = request.nextUrl.searchParams.get("preferAtmos") as - | boolean - | null + const preferAtmos = + request.nextUrl.searchParams.get("preferAtmos") === "true" let kbps = 0 if (quality === "low") { @@ -56,54 +116,43 @@ export async function GET( } else if (kbps === 0 && "lossless" in song.files) { originalCodec = "lossless" transcodeNeeded = false + } else if (kbps === 0) { + transcodeNeeded = false } const file = `${DATA_DIR}/${originalCodec}/${artist.name}/${album.name}/${song.files[originalCodec]}` if (!transcodeNeeded) { const data = await fs.readFile(file) - const metadata = await music.parseFile(file, { skipCovers: true }) return new NextResponse(data, { status: 200, headers: { - "Content-Type": `audio/${metadata.format.container || "aac"}`, - "Transfer-Encoding": "chunked", + "Content-Type": audioContentTypeForPath(file), "Cache-Control": "no-cache", }, }) } - let ffmpegArgs = ["-i", file, "-c:a", "aac", "-b:a", `${kbps}k`, "-f", "mpegts", "pipe:1"] + const ffmpegArgs = [ + "-nostdin", + "-i", + file, + "-c:a", + `${quality === "low" ? "libfdk_aac" : "aac"}`, + "-b:a", + `${kbps}k`, + "-f", + "adts", + "pipe:1", + ] try { const ffmpegProcess = spawn("ffmpeg", ffmpegArgs) - - const stream = new ReadableStream({ - start(controller) { - ffmpegProcess.stdout.on("data", (chunk: Buffer) => { - controller.enqueue(new Uint8Array(chunk)) - }) - - ffmpegProcess.stdout.on("end", () => { - controller.close() - }) - - ffmpegProcess.stdout.on("error", (err) => { - controller.error(err) - }) - - ffmpegProcess.stderr.on("data", (data) => { - //console.error(`FFmpeg log: ${data.toString()}`) - }) - }, - cancel() { - ffmpegProcess.kill("SIGKILL") - }, - }) + const stream = ffmpegToReadableStream(ffmpegProcess) return new NextResponse(stream, { status: 200, headers: { - "Content-Type": "audio/mpegts", + "Content-Type": "audio/aac", "Transfer-Encoding": "chunked", "Cache-Control": "no-cache", }, diff --git a/components/options-modal.tsx b/components/options-modal.tsx index 0d83af8..7ac20fd 100644 --- a/components/options-modal.tsx +++ b/components/options-modal.tsx @@ -196,7 +196,7 @@ function ProfileSection({ user }: { user: Doc<"users"> }) { async function handleClearImage() { try { - await updateImage({}) + await updateImage({ image: null }) toast.success("Profile photo removed") } catch (error) { toast.error( diff --git a/components/playback-provider.tsx b/components/playback-provider.tsx index 0e57477..42e0cf0 100644 --- a/components/playback-provider.tsx +++ b/components/playback-provider.tsx @@ -2,10 +2,12 @@ import * as React from "react" import { useConvex, useMutation, useQuery } from "convex/react" +import { toast } from "sonner" import { api } from "@/convex/_generated/api" import type { Id } from "@/convex/_generated/dataModel" import type { BrowseSong } from "@/convex/library" +import { songPlayUrl } from "@/lib/song-play-url" type RepeatMode = "none" | "once" | "always" @@ -17,6 +19,9 @@ type PlaybackContextValue = { queueOpen: boolean isPlaying: boolean volume: number + currentTime: number + duration: number + seekTo: (seconds: number) => void setQueueOpen: (open: boolean) => void toggleQueueOpen: () => void setIsPlaying: (playing: boolean) => void @@ -43,6 +48,49 @@ type PlaybackContextValue = { const PlaybackContext = React.createContext(null) +const VOLUME_STORAGE_KEY = "otunes:volume" +const DEFAULT_VOLUME = 80 + +const volumeListeners = new Set<() => void>() + +function subscribeVolume(listener: () => void) { + volumeListeners.add(listener) + const onStorage = (event: StorageEvent) => { + if (event.key === null || event.key === VOLUME_STORAGE_KEY) { + listener() + } + } + window.addEventListener("storage", onStorage) + return () => { + volumeListeners.delete(listener) + window.removeEventListener("storage", onStorage) + } +} + +function notifyVolumeChange() { + for (const listener of volumeListeners) { + listener() + } +} + +function readStoredVolume(): number { + try { + const stored = localStorage.getItem(VOLUME_STORAGE_KEY) + if (stored === null) { + return DEFAULT_VOLUME + } + + const parsed = Number(stored) + if (!Number.isFinite(parsed)) { + return DEFAULT_VOLUME + } + + return Math.min(100, Math.max(0, Math.round(parsed))) + } catch { + return DEFAULT_VOLUME + } +} + export function usePlayback() { const context = React.useContext(PlaybackContext) if (!context) { @@ -53,7 +101,12 @@ export function usePlayback() { export function PlaybackProvider({ children }: { children: React.ReactNode }) { const convex = useConvex() + const user = useQuery(api.auth.currentUser) const playback = useQuery(api.playback.getPlaybackState) + const audioRef = React.useRef(null) + const loadedSongIdRef = React.useRef(null) + const playbackErrorHandledRef = React.useRef(false) + const isPlayingRef = React.useRef(false) const playInOrderMutation = useMutation(api.playback.playInOrder) const playShuffledMutation = useMutation(api.playback.playShuffled) const addToQueueNextMutation = useMutation(api.playback.addToQueueNext) @@ -69,15 +122,236 @@ export function PlaybackProvider({ children }: { children: React.ReactNode }) { const [queueOpen, setQueueOpen] = React.useState(false) const [isPlaying, setIsPlaying] = React.useState(false) - const [volume, setVolume] = React.useState(80) + const [currentTime, setCurrentTime] = React.useState(0) + const [duration, setDuration] = React.useState(0) + const volume = React.useSyncExternalStore( + subscribeVolume, + readStoredVolume, + () => DEFAULT_VOLUME + ) + + const setVolume = React.useCallback((nextVolume: number) => { + const clamped = Math.min(100, Math.max(0, Math.round(nextVolume))) + + try { + localStorage.setItem(VOLUME_STORAGE_KEY, String(clamped)) + notifyVolumeChange() + } catch { + // Ignore storage failures (private browsing, quota, etc.) + } + }, []) + + const currentSong = playback?.currentSong ?? null + const isPlaybackActive = Boolean(currentSong && isPlaying) + const repeatEnabled = playback?.repeatEnabled ?? "none" React.useEffect(() => { - if (playback?.currentSong) { - setIsPlaying(true) - } else { - setIsPlaying(false) + setCurrentTime(0) + setDuration(currentSong?.durationSeconds ?? 0) + }, [currentSong?.id, currentSong?.durationSeconds]) + + const seekTo = React.useCallback((seconds: number) => { + const audio = audioRef.current + if (!audio) { + return } - }, [playback?.currentSong?.id]) + + const maxDuration = + Number.isFinite(audio.duration) && audio.duration > 0 + ? audio.duration + : duration + const clamped = Math.max(0, Math.min(seconds, maxDuration || 0)) + audio.currentTime = clamped + setCurrentTime(clamped) + }, [duration]) + + React.useEffect(() => { + isPlayingRef.current = isPlaying + }, [isPlaying]) + + React.useEffect(() => { + const audio = new Audio() + audioRef.current = audio + + return () => { + audio.pause() + audio.removeAttribute("src") + audioRef.current = null + loadedSongIdRef.current = null + } + }, []) + + React.useEffect(() => { + const audio = audioRef.current + if (!audio) { + return + } + audio.volume = volume / 100 + }, [volume]) + + React.useEffect(() => { + const audio = audioRef.current + if (!audio) { + return + } + + if (!currentSong) { + audio.pause() + audio.removeAttribute("src") + loadedSongIdRef.current = null + return + } + + if (user === undefined || !user) { + return + } + + if (loadedSongIdRef.current === currentSong.id) { + return + } + + const url = songPlayUrl( + currentSong.id, + user.playbackQuality ?? "medium", + user.preferAtmos ?? false + ) + + playbackErrorHandledRef.current = false + audio.pause() + + const onCanPlay = () => { + audio.removeEventListener("canplay", onCanPlay) + if (isPlayingRef.current) { + void audio.play().catch(() => { + setIsPlaying(false) + }) + } + } + + audio.addEventListener("canplay", onCanPlay) + audio.src = url + audio.load() + loadedSongIdRef.current = currentSong.id + + return () => { + audio.removeEventListener("canplay", onCanPlay) + } + }, [currentSong?.id, user === undefined]) + + React.useEffect(() => { + const audio = audioRef.current + if (!audio || !currentSong) { + return + } + + if (!isPlaying) { + audio.pause() + return + } + + if (audio.readyState >= HTMLMediaElement.HAVE_FUTURE_DATA) { + void audio.play().catch(() => { + setIsPlaying(false) + }) + return + } + + const onCanPlay = () => { + audio.removeEventListener("canplay", onCanPlay) + if (isPlayingRef.current) { + void audio.play().catch(() => { + setIsPlaying(false) + }) + } + } + + audio.addEventListener("canplay", onCanPlay) + return () => { + audio.removeEventListener("canplay", onCanPlay) + } + }, [isPlaying, currentSong]) + + React.useEffect(() => { + const audio = audioRef.current + if (!audio) { + return + } + + const handleEnded = () => { + if (repeatEnabled === "once") { + audio.currentTime = 0 + void audio.play() + return + } + + void skipNextMutation() + } + + const handleError = () => { + if (!currentSong || playbackErrorHandledRef.current) { + return + } + playbackErrorHandledRef.current = true + + const { error } = audio + if (error) { + console.error("Audio playback error:", { + code: error.code, + message: error.message, + src: audio.src, + }) + } + + audio.pause() + audio.removeAttribute("src") + loadedSongIdRef.current = null + setIsPlaying(false) + + toast.error("Playback error", { + description: "Could not play this track. The queue has been cleared.", + }) + void clearQueueMutation() + } + + audio.addEventListener("ended", handleEnded) + audio.addEventListener("error", handleError) + + return () => { + audio.removeEventListener("ended", handleEnded) + audio.removeEventListener("error", handleError) + } + }, [repeatEnabled, currentSong, skipNextMutation, clearQueueMutation]) + + React.useEffect(() => { + const audio = audioRef.current + if (!audio) { + return + } + + const syncDuration = () => { + if (Number.isFinite(audio.duration) && audio.duration > 0) { + setDuration(audio.duration) + } else if (currentSong) { + setDuration(currentSong.durationSeconds) + } + } + + const syncTime = () => { + setCurrentTime(audio.currentTime) + } + + audio.addEventListener("timeupdate", syncTime) + audio.addEventListener("durationchange", syncDuration) + audio.addEventListener("loadedmetadata", syncDuration) + audio.addEventListener("seeked", syncTime) + + return () => { + audio.removeEventListener("timeupdate", syncTime) + audio.removeEventListener("durationchange", syncDuration) + audio.removeEventListener("loadedmetadata", syncDuration) + audio.removeEventListener("seeked", syncTime) + } + }, [currentSong]) const playInOrder = React.useCallback( async (songIds: Id<"songs">[]) => { @@ -202,8 +476,11 @@ export function PlaybackProvider({ children }: { children: React.ReactNode }) { shuffleEnabled: playback?.shuffleEnabled ?? false, repeatEnabled: playback?.repeatEnabled ?? "none", queueOpen, - isPlaying, + isPlaying: isPlaybackActive, volume, + currentTime, + duration, + seekTo, setQueueOpen, toggleQueueOpen, setIsPlaying, @@ -245,9 +522,13 @@ export function PlaybackProvider({ children }: { children: React.ReactNode }) { [ playback, queueOpen, - isPlaying, + isPlaybackActive, volume, + currentTime, + duration, + seekTo, toggleQueueOpen, + setVolume, playInOrder, playShuffled, addToQueueNext, diff --git a/components/player-bar.tsx b/components/player-bar.tsx index fddacc7..77ca1b2 100644 --- a/components/player-bar.tsx +++ b/components/player-bar.tsx @@ -1,5 +1,6 @@ "use client" +import * as React from "react" import Image from "next/image" import { ListMusic, @@ -22,6 +23,13 @@ import { useSidebar } from "@/components/ui/sidebar" import { albumCoverUrl } from "@/lib/cover-urls" import { cn } from "@/lib/utils" +function formatDuration(seconds: number): string { + const total = Math.max(0, Math.floor(seconds)) + const minutes = Math.floor(total / 60) + const remainingSeconds = total % 60 + return `${minutes}:${remainingSeconds.toString().padStart(2, "0")}` +} + export function PlayerBar() { const { currentSong, @@ -30,9 +38,12 @@ export function PlayerBar() { queueOpen, isPlaying, volume, + currentTime, + duration, queue, setIsPlaying, setVolume, + seekTo, toggleQueueOpen, toggleShuffle, cycleRepeat, @@ -40,9 +51,13 @@ export function PlayerBar() { skipPrevious, } = usePlayback() const { state, isMobile } = useSidebar() + const [isSeeking, setIsSeeking] = React.useState(false) + const [seekValue, setSeekValue] = React.useState(0) const hasQueue = queue.length > 0 const sidebarExpanded = !isMobile && state === "expanded" + const displayTime = isSeeking ? seekValue : currentTime + const maxDuration = Math.max(duration, 1) return (
-
+
+
+
+ + {hasQueue ? ( +
+ + {formatDuration(displayTime)} + + { + setIsSeeking(true) + setSeekValue(value) + }} + onValueCommit={([value]) => { + seekTo(value) + setIsSeeking(false) + }} + aria-label="Seek" + /> + + {formatDuration(duration)} + +
+ ) : null}
) diff --git a/components/ui/calendar.tsx b/components/ui/calendar.tsx index 66b65f5..d7bc63f 100644 --- a/components/ui/calendar.tsx +++ b/components/ui/calendar.tsx @@ -87,7 +87,7 @@ function Calendar({ : "flex items-center gap-1 rounded-(--cell-radius) text-sm [&>svg]:size-3.5 [&>svg]:text-muted-foreground", defaultClassNames.caption_label ), - table: "w-full border-collapse", + month_grid: cn("w-full border-collapse", defaultClassNames.month_grid), weekdays: cn("flex", defaultClassNames.weekdays), weekday: cn( "flex-1 rounded-(--cell-radius) text-[0.8rem] font-normal text-muted-foreground select-none", diff --git a/convex/auth.ts b/convex/auth.ts index 1cfcaae..83be786 100644 --- a/convex/auth.ts +++ b/convex/auth.ts @@ -43,6 +43,8 @@ export const { auth, signIn, signOut, store, isAuthenticated } = convexAuth({ } else { await ctx.db.patch(userId, { isAdmin: false }) } + + await ctx.db.patch(userId, { playbackQuality: "high" }) return } }, diff --git a/convex/favorites.ts b/convex/favorites.ts index d7c074c..dae6f3b 100644 --- a/convex/favorites.ts +++ b/convex/favorites.ts @@ -322,7 +322,7 @@ async function unfavoriteSongHandler(ctx: MutationCtx, songId: Id<"songs">) { return } - let favoriteSongs = (user.favoriteSongs ?? []).filter((id) => id !== songId) + const favoriteSongs = (user.favoriteSongs ?? []).filter((id) => id !== songId) let favoriteAlbums = user.favoriteAlbums ?? [] let favoriteArtists = user.favoriteArtists ?? [] @@ -392,8 +392,8 @@ export const unfavoriteAlbum = mutation({ const albumSongIds = await getSongIdsForAlbum(ctx, albumId) const removeSongs = new Set(albumSongIds) - let favoriteAlbums = (user.favoriteAlbums ?? []).filter((id) => id !== albumId) - let favoriteSongs = (user.favoriteSongs ?? []).filter((id) => !removeSongs.has(id)) + const favoriteAlbums = (user.favoriteAlbums ?? []).filter((id) => id !== albumId) + const favoriteSongs = (user.favoriteSongs ?? []).filter((id) => !removeSongs.has(id)) let favoriteArtists = user.favoriteArtists ?? [] if (favoriteArtists.includes(album.artist)) { diff --git a/eslint.config.mjs b/eslint.config.mjs index f69c448..93e9ca7 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -6,7 +6,7 @@ import convexPlugin from "@convex-dev/eslint-plugin" const eslintConfig = defineConfig([ ...nextVitals, ...nextTs, - ...convexPlugin, + ...convexPlugin.configs.recommended, // Override default ignores of eslint-config-next. globalIgnores([ diff --git a/lib/audio-content-type.ts b/lib/audio-content-type.ts new file mode 100644 index 0000000..ccbefcc --- /dev/null +++ b/lib/audio-content-type.ts @@ -0,0 +1,22 @@ +export function audioContentTypeForPath(filePath: string): string { + const lower = filePath.toLowerCase() + if (lower.endsWith(".mp3")) { + return "audio/mpeg" + } + if (lower.endsWith(".m4a") || lower.endsWith(".mp4")) { + return "audio/mp4" + } + if (lower.endsWith(".flac")) { + return "audio/flac" + } + if (lower.endsWith(".ogg") || lower.endsWith(".opus")) { + return "audio/ogg" + } + if (lower.endsWith(".wav")) { + return "audio/wav" + } + if (lower.endsWith(".aac")) { + return "audio/aac" + } + return "application/octet-stream" +} diff --git a/lib/song-play-url.ts b/lib/song-play-url.ts new file mode 100644 index 0000000..5a1b67c --- /dev/null +++ b/lib/song-play-url.ts @@ -0,0 +1,17 @@ +export type PlaybackQuality = "low" | "medium" | "high" | "lossless" + +export function songPlayUrl( + songId: string, + quality: PlaybackQuality | undefined, + preferAtmos: boolean | undefined +) { + const params = new URLSearchParams() + if (quality) { + params.set("quality", quality) + } + if (preferAtmos) { + params.set("preferAtmos", "true") + } + const query = params.toString() + return `/api/song/${songId}/play${query ? `?${query}` : ""}` +}