This commit is contained in:
10 files changed
+471
-49
No files matched your search
@@ -1,16 +1,77 @@
|
|||||||
import { api } from "@/convex/_generated/api"
|
import { api } from "@/convex/_generated/api"
|
||||||
import type { Id } from "@/convex/_generated/dataModel"
|
import type { Id } from "@/convex/_generated/dataModel"
|
||||||
|
import { audioContentTypeForPath } from "@/lib/audio-content-type"
|
||||||
import { getConvexClient } from "@/lib/convex-server"
|
import { getConvexClient } from "@/lib/convex-server"
|
||||||
import { serveCoverFile } from "@/lib/serve-cover"
|
|
||||||
import { NextRequest, NextResponse } from "next/server"
|
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 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")
|
const DATA_DIR = process.env.NEXT_PUBLIC_CONVEX_URL?.includes("127.0.0.1")
|
||||||
? "/Users/joshuafhiggins/Downloads/test"
|
? "/Users/joshuafhiggins/Downloads/test"
|
||||||
: process.env.DATA || "/data"
|
: 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(
|
export async function GET(
|
||||||
request: NextRequest,
|
request: NextRequest,
|
||||||
{ params }: { params: Promise<{ id: string }> }
|
{ params }: { params: Promise<{ id: string }> }
|
||||||
@@ -34,9 +95,8 @@ export async function GET(
|
|||||||
| "lossless"
|
| "lossless"
|
||||||
| null
|
| null
|
||||||
|
|
||||||
const preferAtmos = request.nextUrl.searchParams.get("preferAtmos") as
|
const preferAtmos =
|
||||||
| boolean
|
request.nextUrl.searchParams.get("preferAtmos") === "true"
|
||||||
| null
|
|
||||||
|
|
||||||
let kbps = 0
|
let kbps = 0
|
||||||
if (quality === "low") {
|
if (quality === "low") {
|
||||||
@@ -56,54 +116,43 @@ export async function GET(
|
|||||||
} else if (kbps === 0 && "lossless" in song.files) {
|
} else if (kbps === 0 && "lossless" in song.files) {
|
||||||
originalCodec = "lossless"
|
originalCodec = "lossless"
|
||||||
transcodeNeeded = false
|
transcodeNeeded = false
|
||||||
|
} else if (kbps === 0) {
|
||||||
|
transcodeNeeded = false
|
||||||
}
|
}
|
||||||
|
|
||||||
const file = `${DATA_DIR}/${originalCodec}/${artist.name}/${album.name}/${song.files[originalCodec]}`
|
const file = `${DATA_DIR}/${originalCodec}/${artist.name}/${album.name}/${song.files[originalCodec]}`
|
||||||
if (!transcodeNeeded) {
|
if (!transcodeNeeded) {
|
||||||
const data = await fs.readFile(file)
|
const data = await fs.readFile(file)
|
||||||
const metadata = await music.parseFile(file, { skipCovers: true })
|
|
||||||
return new NextResponse(data, {
|
return new NextResponse(data, {
|
||||||
status: 200,
|
status: 200,
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": `audio/${metadata.format.container || "aac"}`,
|
"Content-Type": audioContentTypeForPath(file),
|
||||||
"Transfer-Encoding": "chunked",
|
|
||||||
"Cache-Control": "no-cache",
|
"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 {
|
try {
|
||||||
const ffmpegProcess = spawn("ffmpeg", ffmpegArgs)
|
const ffmpegProcess = spawn("ffmpeg", ffmpegArgs)
|
||||||
|
const stream = ffmpegToReadableStream(ffmpegProcess)
|
||||||
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")
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
return new NextResponse(stream, {
|
return new NextResponse(stream, {
|
||||||
status: 200,
|
status: 200,
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "audio/mpegts",
|
"Content-Type": "audio/aac",
|
||||||
"Transfer-Encoding": "chunked",
|
"Transfer-Encoding": "chunked",
|
||||||
"Cache-Control": "no-cache",
|
"Cache-Control": "no-cache",
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -196,7 +196,7 @@ function ProfileSection({ user }: { user: Doc<"users"> }) {
|
|||||||
|
|
||||||
async function handleClearImage() {
|
async function handleClearImage() {
|
||||||
try {
|
try {
|
||||||
await updateImage({})
|
await updateImage({ image: null })
|
||||||
toast.success("Profile photo removed")
|
toast.success("Profile photo removed")
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
toast.error(
|
toast.error(
|
||||||
|
|||||||
@@ -2,10 +2,12 @@
|
|||||||
|
|
||||||
import * as React from "react"
|
import * as React from "react"
|
||||||
import { useConvex, useMutation, useQuery } from "convex/react"
|
import { useConvex, useMutation, useQuery } from "convex/react"
|
||||||
|
import { toast } from "sonner"
|
||||||
|
|
||||||
import { api } from "@/convex/_generated/api"
|
import { api } from "@/convex/_generated/api"
|
||||||
import type { Id } from "@/convex/_generated/dataModel"
|
import type { Id } from "@/convex/_generated/dataModel"
|
||||||
import type { BrowseSong } from "@/convex/library"
|
import type { BrowseSong } from "@/convex/library"
|
||||||
|
import { songPlayUrl } from "@/lib/song-play-url"
|
||||||
|
|
||||||
type RepeatMode = "none" | "once" | "always"
|
type RepeatMode = "none" | "once" | "always"
|
||||||
|
|
||||||
@@ -17,6 +19,9 @@ type PlaybackContextValue = {
|
|||||||
queueOpen: boolean
|
queueOpen: boolean
|
||||||
isPlaying: boolean
|
isPlaying: boolean
|
||||||
volume: number
|
volume: number
|
||||||
|
currentTime: number
|
||||||
|
duration: number
|
||||||
|
seekTo: (seconds: number) => void
|
||||||
setQueueOpen: (open: boolean) => void
|
setQueueOpen: (open: boolean) => void
|
||||||
toggleQueueOpen: () => void
|
toggleQueueOpen: () => void
|
||||||
setIsPlaying: (playing: boolean) => void
|
setIsPlaying: (playing: boolean) => void
|
||||||
@@ -43,6 +48,49 @@ type PlaybackContextValue = {
|
|||||||
|
|
||||||
const PlaybackContext = React.createContext<PlaybackContextValue | null>(null)
|
const PlaybackContext = React.createContext<PlaybackContextValue | null>(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() {
|
export function usePlayback() {
|
||||||
const context = React.useContext(PlaybackContext)
|
const context = React.useContext(PlaybackContext)
|
||||||
if (!context) {
|
if (!context) {
|
||||||
@@ -53,7 +101,12 @@ export function usePlayback() {
|
|||||||
|
|
||||||
export function PlaybackProvider({ children }: { children: React.ReactNode }) {
|
export function PlaybackProvider({ children }: { children: React.ReactNode }) {
|
||||||
const convex = useConvex()
|
const convex = useConvex()
|
||||||
|
const user = useQuery(api.auth.currentUser)
|
||||||
const playback = useQuery(api.playback.getPlaybackState)
|
const playback = useQuery(api.playback.getPlaybackState)
|
||||||
|
const audioRef = React.useRef<HTMLAudioElement | null>(null)
|
||||||
|
const loadedSongIdRef = React.useRef<string | null>(null)
|
||||||
|
const playbackErrorHandledRef = React.useRef(false)
|
||||||
|
const isPlayingRef = React.useRef(false)
|
||||||
const playInOrderMutation = useMutation(api.playback.playInOrder)
|
const playInOrderMutation = useMutation(api.playback.playInOrder)
|
||||||
const playShuffledMutation = useMutation(api.playback.playShuffled)
|
const playShuffledMutation = useMutation(api.playback.playShuffled)
|
||||||
const addToQueueNextMutation = useMutation(api.playback.addToQueueNext)
|
const addToQueueNextMutation = useMutation(api.playback.addToQueueNext)
|
||||||
@@ -69,15 +122,236 @@ export function PlaybackProvider({ children }: { children: React.ReactNode }) {
|
|||||||
|
|
||||||
const [queueOpen, setQueueOpen] = React.useState(false)
|
const [queueOpen, setQueueOpen] = React.useState(false)
|
||||||
const [isPlaying, setIsPlaying] = 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(() => {
|
React.useEffect(() => {
|
||||||
if (playback?.currentSong) {
|
setCurrentTime(0)
|
||||||
setIsPlaying(true)
|
setDuration(currentSong?.durationSeconds ?? 0)
|
||||||
} else {
|
}, [currentSong?.id, currentSong?.durationSeconds])
|
||||||
setIsPlaying(false)
|
|
||||||
|
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(
|
const playInOrder = React.useCallback(
|
||||||
async (songIds: Id<"songs">[]) => {
|
async (songIds: Id<"songs">[]) => {
|
||||||
@@ -202,8 +476,11 @@ export function PlaybackProvider({ children }: { children: React.ReactNode }) {
|
|||||||
shuffleEnabled: playback?.shuffleEnabled ?? false,
|
shuffleEnabled: playback?.shuffleEnabled ?? false,
|
||||||
repeatEnabled: playback?.repeatEnabled ?? "none",
|
repeatEnabled: playback?.repeatEnabled ?? "none",
|
||||||
queueOpen,
|
queueOpen,
|
||||||
isPlaying,
|
isPlaying: isPlaybackActive,
|
||||||
volume,
|
volume,
|
||||||
|
currentTime,
|
||||||
|
duration,
|
||||||
|
seekTo,
|
||||||
setQueueOpen,
|
setQueueOpen,
|
||||||
toggleQueueOpen,
|
toggleQueueOpen,
|
||||||
setIsPlaying,
|
setIsPlaying,
|
||||||
@@ -245,9 +522,13 @@ export function PlaybackProvider({ children }: { children: React.ReactNode }) {
|
|||||||
[
|
[
|
||||||
playback,
|
playback,
|
||||||
queueOpen,
|
queueOpen,
|
||||||
isPlaying,
|
isPlaybackActive,
|
||||||
volume,
|
volume,
|
||||||
|
currentTime,
|
||||||
|
duration,
|
||||||
|
seekTo,
|
||||||
toggleQueueOpen,
|
toggleQueueOpen,
|
||||||
|
setVolume,
|
||||||
playInOrder,
|
playInOrder,
|
||||||
playShuffled,
|
playShuffled,
|
||||||
addToQueueNext,
|
addToQueueNext,
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
"use client"
|
"use client"
|
||||||
|
|
||||||
|
import * as React from "react"
|
||||||
import Image from "next/image"
|
import Image from "next/image"
|
||||||
import {
|
import {
|
||||||
ListMusic,
|
ListMusic,
|
||||||
@@ -22,6 +23,13 @@ import { useSidebar } from "@/components/ui/sidebar"
|
|||||||
import { albumCoverUrl } from "@/lib/cover-urls"
|
import { albumCoverUrl } from "@/lib/cover-urls"
|
||||||
import { cn } from "@/lib/utils"
|
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() {
|
export function PlayerBar() {
|
||||||
const {
|
const {
|
||||||
currentSong,
|
currentSong,
|
||||||
@@ -30,9 +38,12 @@ export function PlayerBar() {
|
|||||||
queueOpen,
|
queueOpen,
|
||||||
isPlaying,
|
isPlaying,
|
||||||
volume,
|
volume,
|
||||||
|
currentTime,
|
||||||
|
duration,
|
||||||
queue,
|
queue,
|
||||||
setIsPlaying,
|
setIsPlaying,
|
||||||
setVolume,
|
setVolume,
|
||||||
|
seekTo,
|
||||||
toggleQueueOpen,
|
toggleQueueOpen,
|
||||||
toggleShuffle,
|
toggleShuffle,
|
||||||
cycleRepeat,
|
cycleRepeat,
|
||||||
@@ -40,9 +51,13 @@ export function PlayerBar() {
|
|||||||
skipPrevious,
|
skipPrevious,
|
||||||
} = usePlayback()
|
} = usePlayback()
|
||||||
const { state, isMobile } = useSidebar()
|
const { state, isMobile } = useSidebar()
|
||||||
|
const [isSeeking, setIsSeeking] = React.useState(false)
|
||||||
|
const [seekValue, setSeekValue] = React.useState(0)
|
||||||
|
|
||||||
const hasQueue = queue.length > 0
|
const hasQueue = queue.length > 0
|
||||||
const sidebarExpanded = !isMobile && state === "expanded"
|
const sidebarExpanded = !isMobile && state === "expanded"
|
||||||
|
const displayTime = isSeeking ? seekValue : currentTime
|
||||||
|
const maxDuration = Math.max(duration, 1)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
@@ -52,7 +67,8 @@ export function PlayerBar() {
|
|||||||
queueOpen && !isMobile && "right-72"
|
queueOpen && !isMobile && "right-72"
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
<div className="pointer-events-auto grid w-full max-w-3xl grid-cols-[1fr_auto_1fr] items-center gap-2 rounded-xl border bg-card/80 px-3 py-2 shadow-lg backdrop-blur">
|
<div className="pointer-events-auto flex w-full max-w-3xl flex-col gap-2 rounded-xl border bg-card/80 px-3 py-2 shadow-lg backdrop-blur">
|
||||||
|
<div className="grid grid-cols-[1fr_auto_1fr] items-center gap-2">
|
||||||
<div className="flex items-center justify-start gap-1">
|
<div className="flex items-center justify-start gap-1">
|
||||||
<Button
|
<Button
|
||||||
variant="ghost"
|
variant="ghost"
|
||||||
@@ -166,6 +182,41 @@ export function PlayerBar() {
|
|||||||
<Volume2 />
|
<Volume2 />
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{hasQueue ? (
|
||||||
|
<div className="flex items-center gap-2 px-1">
|
||||||
|
<span
|
||||||
|
className="w-9 shrink-0 text-right text-xs tabular-nums text-muted-foreground"
|
||||||
|
aria-hidden
|
||||||
|
>
|
||||||
|
{formatDuration(displayTime)}
|
||||||
|
</span>
|
||||||
|
<Slider
|
||||||
|
className="flex-1"
|
||||||
|
min={0}
|
||||||
|
max={maxDuration}
|
||||||
|
step={0.1}
|
||||||
|
value={[displayTime]}
|
||||||
|
disabled={!currentSong}
|
||||||
|
onValueChange={([value]) => {
|
||||||
|
setIsSeeking(true)
|
||||||
|
setSeekValue(value)
|
||||||
|
}}
|
||||||
|
onValueCommit={([value]) => {
|
||||||
|
seekTo(value)
|
||||||
|
setIsSeeking(false)
|
||||||
|
}}
|
||||||
|
aria-label="Seek"
|
||||||
|
/>
|
||||||
|
<span
|
||||||
|
className="w-9 shrink-0 text-xs tabular-nums text-muted-foreground"
|
||||||
|
aria-hidden
|
||||||
|
>
|
||||||
|
{formatDuration(duration)}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
) : null}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ function Calendar({
|
|||||||
: "flex items-center gap-1 rounded-(--cell-radius) text-sm [&>svg]:size-3.5 [&>svg]:text-muted-foreground",
|
: "flex items-center gap-1 rounded-(--cell-radius) text-sm [&>svg]:size-3.5 [&>svg]:text-muted-foreground",
|
||||||
defaultClassNames.caption_label
|
defaultClassNames.caption_label
|
||||||
),
|
),
|
||||||
table: "w-full border-collapse",
|
month_grid: cn("w-full border-collapse", defaultClassNames.month_grid),
|
||||||
weekdays: cn("flex", defaultClassNames.weekdays),
|
weekdays: cn("flex", defaultClassNames.weekdays),
|
||||||
weekday: cn(
|
weekday: cn(
|
||||||
"flex-1 rounded-(--cell-radius) text-[0.8rem] font-normal text-muted-foreground select-none",
|
"flex-1 rounded-(--cell-radius) text-[0.8rem] font-normal text-muted-foreground select-none",
|
||||||
|
|||||||
@@ -43,6 +43,8 @@ export const { auth, signIn, signOut, store, isAuthenticated } = convexAuth({
|
|||||||
} else {
|
} else {
|
||||||
await ctx.db.patch(userId, { isAdmin: false })
|
await ctx.db.patch(userId, { isAdmin: false })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await ctx.db.patch(userId, { playbackQuality: "high" })
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
+3
-3
@@ -322,7 +322,7 @@ async function unfavoriteSongHandler(ctx: MutationCtx, songId: Id<"songs">) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
let favoriteSongs = (user.favoriteSongs ?? []).filter((id) => id !== songId)
|
const favoriteSongs = (user.favoriteSongs ?? []).filter((id) => id !== songId)
|
||||||
let favoriteAlbums = user.favoriteAlbums ?? []
|
let favoriteAlbums = user.favoriteAlbums ?? []
|
||||||
let favoriteArtists = user.favoriteArtists ?? []
|
let favoriteArtists = user.favoriteArtists ?? []
|
||||||
|
|
||||||
@@ -392,8 +392,8 @@ export const unfavoriteAlbum = mutation({
|
|||||||
const albumSongIds = await getSongIdsForAlbum(ctx, albumId)
|
const albumSongIds = await getSongIdsForAlbum(ctx, albumId)
|
||||||
const removeSongs = new Set(albumSongIds)
|
const removeSongs = new Set(albumSongIds)
|
||||||
|
|
||||||
let favoriteAlbums = (user.favoriteAlbums ?? []).filter((id) => id !== albumId)
|
const favoriteAlbums = (user.favoriteAlbums ?? []).filter((id) => id !== albumId)
|
||||||
let favoriteSongs = (user.favoriteSongs ?? []).filter((id) => !removeSongs.has(id))
|
const favoriteSongs = (user.favoriteSongs ?? []).filter((id) => !removeSongs.has(id))
|
||||||
let favoriteArtists = user.favoriteArtists ?? []
|
let favoriteArtists = user.favoriteArtists ?? []
|
||||||
|
|
||||||
if (favoriteArtists.includes(album.artist)) {
|
if (favoriteArtists.includes(album.artist)) {
|
||||||
|
|||||||
+1
-1
@@ -6,7 +6,7 @@ import convexPlugin from "@convex-dev/eslint-plugin"
|
|||||||
const eslintConfig = defineConfig([
|
const eslintConfig = defineConfig([
|
||||||
...nextVitals,
|
...nextVitals,
|
||||||
...nextTs,
|
...nextTs,
|
||||||
...convexPlugin,
|
...convexPlugin.configs.recommended,
|
||||||
|
|
||||||
// Override default ignores of eslint-config-next.
|
// Override default ignores of eslint-config-next.
|
||||||
globalIgnores([
|
globalIgnores([
|
||||||
|
|||||||
@@ -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"
|
||||||
|
}
|
||||||
@@ -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}` : ""}`
|
||||||
|
}
|
||||||
Reference in new issue
Block a user