Add pulsing music player
Some checks failed
On Push Deploy / deploy (push) Has been cancelled

This commit is contained in:
Johnny322
2026-02-08 15:33:31 +01:00
parent c4046c9d18
commit 9e95f847b1
2 changed files with 222 additions and 9 deletions

View File

@@ -154,11 +154,31 @@
</p>
</div>
<div class="player-body">
<div
v-if="currentClipUrl"
class="custom-player"
:style="{ '--pulse': pulseLevel.toFixed(3) }"
>
<div class="pulse-orb" :class="{ active: isPlaying }"></div>
<div class="player-controls">
<button class="ghost" @click="togglePlayback">
{{ isPlaying ? 'Pause' : 'Play' }}
</button>
<div class="player-meta">
<span class="label">Status</span>
<span class="value">{{ isPlaying ? 'Playing' : 'Paused' }}</span>
</div>
</div>
</div>
<audio
ref="player"
v-if="currentClipUrl"
:src="currentClipUrl"
controls
class="hidden-audio"
preload="auto"
@play="handlePlayerPlay"
@pause="handlePlayerPause"
@ended="handlePlayerPause"
></audio>
<div v-else class="player-empty">
Pick a tile to start the round.
@@ -234,7 +254,15 @@ export default {
currentTileKey: null,
currentClipUrl: '',
currentSelectorId: null,
lastAwardedTeamId: null
lastAwardedTeamId: null,
isPlaying: false,
pulseLevel: 0,
audioContext: null as AudioContext | null,
analyser: null as AnalyserNode | null,
analyserData: null as Uint8Array | null,
rafId: 0,
mediaSource: null as MediaElementAudioSourceNode | null,
mediaElement: null as HTMLAudioElement | null
}
},
computed: {
@@ -258,10 +286,122 @@ export default {
.join(', ')
}
},
watch: {
currentClipUrl(newValue: string) {
if (!newValue) {
this.teardownAudio()
return
}
nextTick(() => this.setupAudioGraph())
}
},
methods: {
getPlayer() {
return this.$refs.player as HTMLAudioElement | undefined
},
ensureAudioContext() {
if (!this.audioContext) {
this.audioContext = new AudioContext()
}
if (this.audioContext.state === 'suspended') {
this.audioContext.resume().catch(() => {})
}
},
setupAudioGraph() {
const player = this.getPlayer()
if (!player) return
this.ensureAudioContext()
if (this.analyser) {
this.analyser.disconnect()
this.analyser = null
}
const context = this.audioContext
if (!context) return
if (this.mediaElement !== player) {
if (this.mediaSource) {
this.mediaSource.disconnect()
}
this.mediaSource = context.createMediaElementSource(player)
this.mediaElement = player
}
const analyser = context.createAnalyser()
analyser.fftSize = 1024
this.mediaSource?.connect(analyser)
analyser.connect(context.destination)
this.analyser = analyser
this.analyserData = new Uint8Array(analyser.fftSize)
this.pulseLevel = 0
},
teardownAudio() {
if (this.rafId) {
cancelAnimationFrame(this.rafId)
this.rafId = 0
}
if (this.analyser) {
this.analyser.disconnect()
this.analyser = null
}
if (this.mediaSource) {
this.mediaSource.disconnect()
this.mediaSource = null
}
this.mediaElement = null
this.analyserData = null
this.isPlaying = false
this.pulseLevel = 0
},
startPulse() {
if (!this.analyser || !this.analyserData) return
const analyser = this.analyser
const data = this.analyserData
const tick = () => {
if (!this.isPlaying || !this.analyser || !this.analyserData) {
this.rafId = 0
return
}
analyser.getByteTimeDomainData(data)
let sumSquares = 0
for (let i = 0; i < data.length; i += 1) {
const centered = (data[i] - 128) / 128
sumSquares += centered * centered
}
const rms = Math.sqrt(sumSquares / data.length)
const target = Math.min(1, rms * 2.8)
this.pulseLevel = this.pulseLevel * 0.7 + target * 0.3
this.rafId = requestAnimationFrame(tick)
}
if (this.rafId) cancelAnimationFrame(this.rafId)
this.rafId = requestAnimationFrame(tick)
},
handlePlayerPlay() {
this.isPlaying = true
this.startPulse()
},
handlePlayerPause() {
this.isPlaying = false
if (this.rafId) {
cancelAnimationFrame(this.rafId)
this.rafId = 0
}
this.pulseLevel = 0
},
togglePlayback() {
const player = this.getPlayer()
if (!player) return
this.ensureAudioContext()
if (player.paused) {
player.play().catch(() => {})
} else {
player.pause()
}
},
teamGradient(team: Team) {
return {
background: `linear-gradient(135deg, ${team.colorA}, ${team.colorB})`
@@ -295,6 +435,7 @@ export default {
this.currentTileKey = null
this.currentClipUrl = ''
this.lastAwardedTeamId = null
this.teardownAudio()
},
tileKey(cIndex: number, qIndex: number) {
return `${cIndex}-${qIndex}`
@@ -341,6 +482,7 @@ export default {
await nextTick()
const player = this.getPlayer()
if (player) {
this.ensureAudioContext()
player.currentTime = 0
player.load()
player.play().catch(() => {})
@@ -363,6 +505,7 @@ export default {
await nextTick()
const player = this.getPlayer()
if (player) {
this.ensureAudioContext()
player.currentTime = 0
player.load()
player.play().catch(() => {})
@@ -400,6 +543,7 @@ export default {
this.currentClipUrl = ''
const player = this.getPlayer()
player?.pause()
this.teardownAudio()
this.checkEnd()
}
},
@@ -430,6 +574,9 @@ export default {
this.step = 'end'
}
}
},
unmounted() {
this.teardownAudio()
}
}
</script>