208 lines
5.4 KiB
TypeScript
Raw Normal View History

2018-03-11 02:00:06 +00:00
import { AnimeNotifier } from "../AnimeNotifier"
2018-03-11 03:45:01 +00:00
var audioContext: AudioContext
var audioNode: AudioBufferSourceNode
2018-03-11 17:17:35 +00:00
var gainNode: GainNode
2018-03-11 19:29:22 +00:00
var volume = 0.5
2018-03-11 22:12:21 +00:00
var volumeTimeConstant = 0.01
var volumeSmoothingDelay = 0.05
2018-03-11 16:20:52 +00:00
var playId = 0
2018-03-11 04:10:32 +00:00
var audioPlayer = document.getElementById("audio-player")
var audioPlayerPlay = document.getElementById("audio-player-play")
var audioPlayerPause = document.getElementById("audio-player-pause")
2018-03-11 20:59:48 +00:00
var trackLink = document.getElementById("audio-player-track-title") as HTMLLinkElement
var animeInfo = document.getElementById("audio-player-anime-info") as HTMLElement
2018-03-11 22:12:21 +00:00
var animeLink = document.getElementById("audio-player-anime-link") as HTMLLinkElement
var animeImage = document.getElementById("audio-player-anime-image") as HTMLImageElement
2018-03-11 03:45:01 +00:00
2018-03-11 19:22:33 +00:00
// Play audio
2018-03-11 14:43:17 +00:00
export function playAudio(arn: AnimeNotifier, element: HTMLElement) {
2018-03-11 19:22:33 +00:00
playAudioFile(arn, element.dataset.soundtrackId, element.dataset.audioSrc)
}
// Play audio file
function playAudioFile(arn: AnimeNotifier, trackId: string, trackUrl: string) {
2018-03-11 03:45:01 +00:00
if(!audioContext) {
audioContext = new AudioContext()
2018-03-11 17:17:35 +00:00
gainNode = audioContext.createGain()
2018-03-11 22:12:21 +00:00
gainNode.gain.setTargetAtTime(volume, audioContext.currentTime + volumeSmoothingDelay, volumeTimeConstant)
2018-03-11 03:45:01 +00:00
}
2018-03-11 16:20:52 +00:00
playId++
let currentPlayId = playId
2018-03-11 04:10:32 +00:00
2018-03-11 14:43:17 +00:00
// Stop current track
stopAudio(arn)
2018-03-11 19:22:33 +00:00
arn.currentSoundTrackId = trackId
arn.markPlayingSoundTrack()
2018-03-11 20:37:07 +00:00
arn.loading(true)
2018-03-11 02:00:06 +00:00
2018-03-11 04:10:32 +00:00
// Request
2018-03-11 03:45:01 +00:00
let request = new XMLHttpRequest()
2018-03-11 19:22:33 +00:00
request.open("GET", trackUrl, true)
2018-03-11 03:45:01 +00:00
request.responseType = "arraybuffer"
2018-03-11 04:10:32 +00:00
2018-03-11 03:45:01 +00:00
request.onload = () => {
2018-03-11 20:37:07 +00:00
arn.loading(false)
2018-03-11 16:20:52 +00:00
if(currentPlayId !== playId) {
return
}
2018-03-11 20:59:48 +00:00
audioContext.decodeAudioData(request.response, async buffer => {
2018-03-11 16:20:52 +00:00
if(currentPlayId !== playId) {
return
}
2018-03-11 03:45:01 +00:00
audioNode = audioContext.createBufferSource()
audioNode.buffer = buffer
2018-03-11 17:17:35 +00:00
audioNode.connect(gainNode)
gainNode.connect(audioContext.destination)
2018-03-11 03:45:01 +00:00
audioNode.start(0)
2018-03-11 04:10:32 +00:00
audioNode.onended = (event: MediaStreamErrorEvent) => {
2018-03-11 16:20:52 +00:00
if(currentPlayId !== playId) {
return
2018-03-11 04:10:32 +00:00
}
2018-03-11 16:20:52 +00:00
2018-03-11 19:22:33 +00:00
playNextTrack(arn)
// stopAudio(arn)
2018-03-11 03:51:10 +00:00
}
2018-03-11 20:59:48 +00:00
// Set track title
let trackInfoResponse = await fetch("/api/soundtrack/" + trackId)
let track = await trackInfoResponse.json()
trackLink.href = "/soundtrack/" + track.id
trackLink.innerText = track.title
2018-03-11 22:12:21 +00:00
let animeId = ""
for(let tag of (track.tags as string[])) {
if(tag.startsWith("anime:")) {
animeId = tag.split(":")[1]
break
}
}
// Set anime info
if(animeId !== "") {
animeInfo.classList.remove("hidden")
2018-03-11 22:12:21 +00:00
let animeResponse = await fetch("/api/anime/" + animeId)
let anime = await animeResponse.json()
animeLink.title = anime.title.canonical
animeLink.href = "/anime/" + anime.id
animeImage.dataset.src = "//media.notify.moe/images/anime/medium/" + anime.id + anime.imageExtension
2018-03-11 22:12:21 +00:00
animeImage.classList.remove("hidden")
animeImage["became visible"]()
}
2018-03-11 03:45:01 +00:00
}, console.error)
}
2018-03-11 04:10:32 +00:00
2018-03-11 20:37:07 +00:00
request.onerror = () => {
arn.loading(false)
}
2018-03-11 03:45:01 +00:00
request.send()
// Show audio player
2018-03-11 04:10:32 +00:00
audioPlayer.classList.remove("fade-out")
audioPlayerPlay.classList.add("fade-out")
audioPlayerPause.classList.remove("fade-out")
2018-03-11 03:45:01 +00:00
}
2018-03-11 14:43:17 +00:00
// Stop audio
export function stopAudio(arn: AnimeNotifier) {
arn.currentSoundTrackId = undefined
// Remove CSS class "playing"
let playingElements = document.getElementsByClassName("playing")
for(let playing of playingElements) {
playing.classList.remove("playing")
}
// Fade out sidebar player
2018-03-12 18:02:23 +00:00
// audioPlayer.classList.add("fade-out")
2018-03-11 16:20:52 +00:00
2018-03-11 20:59:48 +00:00
// Remove title
trackLink.href = ""
trackLink.innerText = ""
2018-03-11 22:12:21 +00:00
// Hide anime info
animeLink.href = ""
animeInfo.classList.add("hidden")
2018-03-11 22:12:21 +00:00
animeImage.classList.add("hidden")
2018-03-11 17:17:35 +00:00
if(gainNode) {
gainNode.disconnect()
2018-03-11 16:20:52 +00:00
}
2018-03-11 17:17:35 +00:00
if(audioNode) {
audioNode.stop()
audioNode.disconnect()
audioNode = null
}
2018-03-11 14:43:17 +00:00
}
// Toggle audio
export function toggleAudio(arn: AnimeNotifier, element: HTMLElement) {
2018-03-11 15:42:49 +00:00
// If we're clicking on the same track again, stop playing.
// Otherwise, start the track we clicked on.
if(arn.currentSoundTrackId && element.dataset.soundtrackId === arn.currentSoundTrackId) {
2018-03-11 14:43:17 +00:00
stopAudio(arn)
2018-03-11 15:42:49 +00:00
} else {
playAudio(arn, element)
2018-03-11 14:43:17 +00:00
}
}
2018-03-11 20:37:07 +00:00
// Play previous track
export async function playPreviousTrack(arn: AnimeNotifier) {
alert("Previous track is currently work in progress! Check back later :)")
}
2018-03-11 19:22:33 +00:00
// Play next track
export async function playNextTrack(arn: AnimeNotifier) {
// Get random track
let response = await fetch("/api/next/soundtrack")
let track = await response.json()
playAudioFile(arn, track.id, "https://notify.moe/audio/" + track.file)
2018-03-11 20:37:07 +00:00
arn.statusMessage.showInfo("Now playing: " + track.title)
2018-03-11 19:22:33 +00:00
return track
}
2018-03-11 16:20:52 +00:00
// Set volume
2018-03-11 17:17:35 +00:00
export function setVolume(arn: AnimeNotifier, element: HTMLInputElement) {
volume = parseFloat(element.value) / 100.0
2018-03-11 16:20:52 +00:00
2018-03-11 17:17:35 +00:00
if(gainNode) {
2018-03-11 22:12:21 +00:00
gainNode.gain.setTargetAtTime(volume, audioContext.currentTime + volumeSmoothingDelay, volumeTimeConstant)
2018-03-11 17:17:35 +00:00
}
2018-03-11 16:20:52 +00:00
}
2018-03-11 03:45:01 +00:00
// Pause audio
export function pauseAudio(arn: AnimeNotifier, button: HTMLButtonElement) {
if(!audioNode) {
return
}
audioNode.playbackRate.setValueAtTime(0.0, 0)
2018-03-11 04:10:32 +00:00
audioPlayerPlay.classList.remove("fade-out")
audioPlayerPause.classList.add("fade-out")
2018-03-11 03:45:01 +00:00
}
// Resume audio
2018-03-11 20:37:07 +00:00
export function resumeAudio(arn: AnimeNotifier, button: HTMLButtonElement) {
2018-03-11 03:45:01 +00:00
if(!audioNode) {
2018-03-11 20:37:07 +00:00
playNextTrack(arn)
2018-03-11 03:45:01 +00:00
return
}
audioNode.playbackRate.setValueAtTime(1.0, 0)
2018-03-11 04:10:32 +00:00
audioPlayerPlay.classList.add("fade-out")
audioPlayerPause.classList.remove("fade-out")
2018-03-11 02:00:06 +00:00
}