68 lines
1.6 KiB
TypeScript
68 lines
1.6 KiB
TypeScript
export type PlaybackQuality = "low" | "medium" | "high" | "lossless"
|
|
|
|
export function qualityKbps(quality: PlaybackQuality | null | undefined) {
|
|
if (quality === "low") {
|
|
return 64
|
|
}
|
|
|
|
if (quality === "high") {
|
|
return 256
|
|
}
|
|
|
|
if (quality === "medium") {
|
|
return 128
|
|
}
|
|
|
|
return 0
|
|
}
|
|
|
|
export type PlaybackStreamPlan = {
|
|
playbackQuality: PlaybackQuality
|
|
preferAtmos: boolean
|
|
transcodeNeeded: boolean
|
|
useHls: boolean
|
|
}
|
|
|
|
export function playbackStreamPlan(
|
|
files: Record<string, unknown>,
|
|
quality: PlaybackQuality | undefined,
|
|
preferAtmos: boolean
|
|
): PlaybackStreamPlan {
|
|
const playbackQuality = quality ?? "medium"
|
|
const kbps = playbackQuality === "lossless" ? 0 : qualityKbps(playbackQuality)
|
|
const effectivePreferAtmos = preferAtmos && "atmos" in files
|
|
|
|
let transcodeNeeded = true
|
|
|
|
if (effectivePreferAtmos) {
|
|
transcodeNeeded = false
|
|
} else if (kbps === 0 && "lossless" in files) {
|
|
transcodeNeeded = false
|
|
} else if (kbps === 0 || (kbps === 256 && "normal" in files)) {
|
|
transcodeNeeded = false
|
|
}
|
|
|
|
return {
|
|
playbackQuality,
|
|
preferAtmos: effectivePreferAtmos,
|
|
transcodeNeeded,
|
|
useHls: transcodeNeeded,
|
|
}
|
|
}
|
|
|
|
export type PlaybackFormat = "atmos" | "lossless"
|
|
|
|
/** The labeled format a song streams in under the plan, if any. */
|
|
export function playbackFormat(
|
|
files: Record<string, unknown>,
|
|
quality: PlaybackQuality | undefined,
|
|
preferAtmos: boolean
|
|
): PlaybackFormat | null {
|
|
const plan = playbackStreamPlan(files, quality, preferAtmos)
|
|
if (plan.preferAtmos) return "atmos"
|
|
if (plan.playbackQuality === "lossless" && "lossless" in files) {
|
|
return "lossless"
|
|
}
|
|
return null
|
|
}
|