144 lines
5.3 KiB
TypeScript
144 lines
5.3 KiB
TypeScript
import type { Logger } from "pino"
|
|
import { useAudioStore } from "~/stores/audio"
|
|
import { useDevicesStore } from "~/stores/device"
|
|
import { useMicStore } from "~/stores/microphone"
|
|
import { useUserStore } from "~/stores/user"
|
|
import { getSoundscapeByTitle } from "~/tracks.config"
|
|
|
|
|
|
export const useRequirementChecker = () => {
|
|
|
|
const logger = useNuxtApp().$logger as Logger
|
|
|
|
|
|
const audio = useAudioStore()
|
|
const mic = useMicStore()
|
|
const rnbo = useDevicesStore()
|
|
const user = useUserStore().user as any
|
|
|
|
let audioContext = false
|
|
let audioUnlock = false
|
|
let microphone = false
|
|
let rnboDevice = false
|
|
let audioTags = false
|
|
|
|
const checkAudioContext = async () => {
|
|
if(audio.audioContext?.state === 'running'){
|
|
audioContext = true
|
|
} else {
|
|
await audio.ensureAudioContextRunning()
|
|
checkAudioContext()
|
|
}
|
|
return audioContext
|
|
}
|
|
|
|
const checkAudioUnlocked = async () => {
|
|
|
|
try {
|
|
let test: HTMLAudioElement | null = new Audio()
|
|
const sink = useUserStore().audioOutputDevice as MediaDeviceInfo
|
|
test.setSinkId(sink.deviceId)
|
|
test.src = 'https://localhost:3000/sounds/debug/LMusik_RSprache.mp3'
|
|
test.muted = true
|
|
await test.play().catch((e: any) => {
|
|
audioUnlock = false
|
|
})
|
|
} catch (error) {
|
|
audioUnlock = false
|
|
}
|
|
if(audioUnlock) {
|
|
return true
|
|
} else {
|
|
return false
|
|
}
|
|
}
|
|
|
|
const checkRNBODevice = async () => {
|
|
try {
|
|
const centerFrequencies3 = [150, 1500, 8000]
|
|
const centerFrequencies9 = [63, 125, 250, 500, 1000, 2000, 4000, 8000, 16000]
|
|
const mobile = /Mobi|Android|iPhone|iPad|iPod/i.test(navigator.userAgent)
|
|
if(mobile) {
|
|
await rnbo.createControlValuesDevice(`testControlValues_${centerFrequencies3[0]}Hz`, centerFrequencies3[0])
|
|
await rnbo.createControlValuesDevice(`testControlValues_${centerFrequencies3[1]}Hz`, centerFrequencies3[1])
|
|
await rnbo.createControlValuesDevice(`testControlValues_${centerFrequencies3[2]}Hz`, centerFrequencies3[2])
|
|
rnboDevice = true
|
|
}else {
|
|
await rnbo.createControlValuesDevice(`testControlValues_${centerFrequencies9[0]}Hz`, centerFrequencies9[0])
|
|
await rnbo.createControlValuesDevice(`testControlValues_${centerFrequencies9[1]}Hz`, centerFrequencies9[1])
|
|
await rnbo.createControlValuesDevice(`testControlValues_${centerFrequencies9[2]}Hz`, centerFrequencies9[2])
|
|
await rnbo.createControlValuesDevice(`testControlValues_${centerFrequencies9[3]}Hz`, centerFrequencies9[3])
|
|
await rnbo.createControlValuesDevice(`testControlValues_${centerFrequencies9[4]}Hz`, centerFrequencies9[4])
|
|
await rnbo.createControlValuesDevice(`testControlValues_${centerFrequencies9[5]}Hz`, centerFrequencies9[5])
|
|
await rnbo.createControlValuesDevice(`testControlValues_${centerFrequencies9[6]}Hz`, centerFrequencies9[6])
|
|
await rnbo.createControlValuesDevice(`testControlValues_${centerFrequencies9[7]}Hz`, centerFrequencies9[7])
|
|
await rnbo.createControlValuesDevice(`testControlValues_${centerFrequencies9[8]}Hz`, centerFrequencies9[8])
|
|
rnboDevice = true
|
|
}
|
|
} catch (error) {
|
|
rnboDevice = false
|
|
}
|
|
return rnboDevice
|
|
}
|
|
const checkAudioTags = async () => {
|
|
const lowPath = `/masking/3bands/low_band_256kbps.webm`
|
|
const midPath = `/masking/3bands/mid_band_256kbps.webm`
|
|
const highPath = `/masking/3bands/high_band_256kbps.webm`
|
|
const soundscape = user.settings.soundscape || 'Forest'
|
|
try {
|
|
const soundScapePath = getSoundscapeByTitle(soundscape as string)
|
|
const fullPaths = [`${window.location.origin}${encodeURI(lowPath)}`,
|
|
`${window.location.origin}${encodeURI(midPath)}`,
|
|
`${window.location.origin}${encodeURI(highPath)}`,
|
|
]
|
|
if(soundScapePath) {
|
|
fullPaths.push(soundScapePath)
|
|
}
|
|
const media = useMediaProvider()
|
|
await media.createPlayableAudioElement(fullPaths[0],3000)
|
|
await media.createPlayableAudioElement(fullPaths[1],3000)
|
|
await media.createPlayableAudioElement(fullPaths[2],3000)
|
|
await media.createPlayableAudioElement(fullPaths[3],3000)
|
|
audioTags = true
|
|
} catch (error) {
|
|
audioTags = false
|
|
}
|
|
return audioTags
|
|
}
|
|
|
|
const checkMicrophone = async () => {
|
|
try {
|
|
await mic.attachMicrophone()
|
|
microphone = true
|
|
return true
|
|
}catch{
|
|
microphone = false
|
|
return false
|
|
}
|
|
}
|
|
|
|
const ensureReadiness = async () => {
|
|
let ready = true
|
|
ready = await checkAudioUnlocked() || ready ? true : false
|
|
logger.info("Audio Unlocked")
|
|
ready = await checkAudioContext() || ready ? true : false
|
|
logger.info("AudioContext running")
|
|
ready = await checkRNBODevice() || ready ? true : false
|
|
logger.info("RNBODevices ready for connections")
|
|
ready = await checkAudioTags() || ready ? true : false
|
|
logger.info("AudioTags ready to play")
|
|
ready = await checkMicrophone() || ready ? true : false
|
|
logger.info("Microphone attached")
|
|
return ready
|
|
}
|
|
|
|
|
|
return {
|
|
ensureReadiness,
|
|
checkAudioUnlocked,
|
|
checkAudioContext,
|
|
audioUnlock, audioContext, microphone, rnboDevice, audioTags
|
|
|
|
}
|
|
}
|