-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(overlays): correct link to files
- Loading branch information
Showing
1 changed file
with
45 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,99 +1,99 @@ | ||
<script lang="ts" setup> | ||
import { useWebSocket } from '@vueuse/core'; | ||
import { ref, watch } from 'vue'; | ||
import { useRoute } from 'vue-router'; | ||
import { useWebSocket } from '@vueuse/core' | ||
import { ref, watch } from 'vue' | ||
import { useRoute } from 'vue-router' | ||
import { generateSocketUrlWithParams } from '@/helpers.js'; | ||
import { generateSocketUrlWithParams } from '@/helpers.js' | ||
declare global { | ||
interface Window { | ||
webkitAudioContext: typeof AudioContext; | ||
webkitAudioContext: typeof AudioContext | ||
} | ||
} | ||
const queue = ref<Array<{ | ||
id: string, | ||
channel_id: string, | ||
audio_id: string, | ||
id: string | ||
channel_id: string | ||
audio_id: string | ||
audio_volume: number | ||
}>>([]); | ||
}>>([]) | ||
const currentAudioBuffer = ref<AudioBufferSourceNode | null>(null); | ||
const route = useRoute(); | ||
const currentAudioBuffer = ref<AudioBufferSourceNode | null>(null) | ||
const route = useRoute() | ||
const apiKey = route.params.apiKey as string; | ||
const apiKey = route.params.apiKey as string | ||
const alertsUrl = generateSocketUrlWithParams('/overlays/alerts', { | ||
apiKey, | ||
}); | ||
apiKey | ||
}) | ||
const socket = useWebSocket(alertsUrl, { | ||
immediate: true, | ||
autoReconnect: { | ||
delay: 500, | ||
}, | ||
}); | ||
delay: 500 | ||
} | ||
}) | ||
watch(socket.data, (message) => { | ||
const parsedData = JSON.parse(message); | ||
const parsedData = JSON.parse(message) | ||
if (parsedData.eventName === 'trigger') { | ||
queue.value.push(parsedData.data); | ||
queue.value.push(parsedData.data) | ||
if (queue.value.length === 1) { | ||
processQueue(); | ||
processQueue() | ||
} | ||
} | ||
}); | ||
}) | ||
async function processQueue(): Promise<void> { | ||
if (queue.value.length === 0) { | ||
return; | ||
return | ||
} | ||
const current = queue.value[0]; | ||
const current = queue.value[0] | ||
if (current.audio_id) { | ||
await playAudio(current.channel_id, current.audio_id, current.audio_volume); | ||
await playAudio(current.channel_id, current.audio_id, current.audio_volume) | ||
} | ||
// change next val | ||
queue.value = queue.value.slice(1); | ||
queue.value = queue.value.slice(1) | ||
// Process the next item in the queue | ||
processQueue(); | ||
processQueue() | ||
} | ||
async function playAudio(channelId: string, audioId: string, volume: number): Promise<unknown> { | ||
const query = new URLSearchParams({ | ||
channel_id: channelId, | ||
file_id: audioId, | ||
}); | ||
const req = await fetch(`${window.location.origin}/api/files/?${query}`); | ||
file_id: audioId | ||
}) | ||
const req = await fetch(`${window.location.origin}/api-old/files/?${query}`) | ||
if (!req.ok) { | ||
console.error(await req.text()); | ||
return; | ||
console.error(await req.text()) | ||
return | ||
} | ||
const audioContext = new (window.AudioContext || window.webkitAudioContext)(); | ||
const gainNode = audioContext.createGain(); | ||
const audioContext = new (window.AudioContext || window.webkitAudioContext)() | ||
const gainNode = audioContext.createGain() | ||
const data = await req.arrayBuffer(); | ||
const data = await req.arrayBuffer() | ||
const source = audioContext.createBufferSource(); | ||
currentAudioBuffer.value = source; | ||
const source = audioContext.createBufferSource() | ||
currentAudioBuffer.value = source | ||
source.buffer = await audioContext.decodeAudioData(data); | ||
source.buffer = await audioContext.decodeAudioData(data) | ||
gainNode.gain.value = volume / 100; | ||
source.connect(gainNode); | ||
gainNode.connect(audioContext.destination); | ||
gainNode.gain.value = volume / 100 | ||
source.connect(gainNode) | ||
gainNode.connect(audioContext.destination) | ||
return new Promise((resolve) => { | ||
source.onended = () => { | ||
currentAudioBuffer.value = null; | ||
resolve(null); | ||
}; | ||
currentAudioBuffer.value = null | ||
resolve(null) | ||
} | ||
source.start(0); | ||
}); | ||
source.start(0) | ||
}) | ||
} | ||
</script> |