diff --git a/js/components/home.mjs b/js/components/home.mjs index 0d8dd6b..89bbca9 100644 --- a/js/components/home.mjs +++ b/js/components/home.mjs @@ -2,6 +2,7 @@ import { checkConnection } from "../api/checkconnection.mjs"; import { sendkml } from "../api/sendkml.mjs"; import { showballoon } from "../api/balloon.mjs"; import { flytoview } from "../api/flytoview.mjs"; +import { stopAllSounds } from "../utils/audio.mjs"; import "./voice.mjs"; import {exportprocessQueryExternally} from "./voice.mjs" import "./sample-queries-tab.mjs"; @@ -223,18 +224,3 @@ export class Home extends HTMLElement { } } } - -const oceanSound = new Audio('./assets/ocean.mp3'); -const fireSound = new Audio('./assets/fire.mp3'); - -oceanSound.loop = true; -fireSound.loop = true; -fireSound.volume = 0.25; -oceanSound.volume = 0.25; - -function stopAllSounds() { - oceanSound.pause(); - fireSound.pause(); - oceanSound.currentTime = 0; - fireSound.currentTime = 0; -} diff --git a/js/components/voice.mjs b/js/components/voice.mjs index 506c251..7bf9ebd 100644 --- a/js/components/voice.mjs +++ b/js/components/voice.mjs @@ -3,6 +3,7 @@ import { flytoview } from "../api/flytoview.mjs"; import { startOrbit } from "../api/orbit.mjs"; import { stopOrbit } from "../api/orbit.mjs"; import { showballoon } from "../api/balloon.mjs"; +import { stopAllSounds, playSoundscapeBasedOnText } from "../utils/audio.mjs"; import ReadAloudComponent from "./read-aloud.mjs"; customElements.define("read-aloud", ReadAloudComponent); @@ -823,49 +824,7 @@ export class LGVoice extends HTMLElement { } } -const oceanSound = new Audio('./assets/ocean.mp3'); -const fireSound = new Audio('./assets/fire.mp3'); - -oceanSound.loop = true; -fireSound.loop = true; -fireSound.volume = 0.15; -oceanSound.volume = 0.15; - -function stopAllSounds() { - oceanSound.pause(); - fireSound.pause(); - oceanSound.currentTime = 0; - fireSound.currentTime = 0; -} - -function playSoundscapeBasedOnText(text) { - stopAllSounds(); - - const lower = text.toLowerCase(); - let soundToPlay = null; - - if (lower.includes("ocean") || lower.includes("sea") || lower.includes("wave") || lower.includes("beach") || lower.includes("coast") || lower.includes("island") || lower.includes("tsunami") || lower.includes("flood") || lower.includes("water")) { - soundToPlay = oceanSound; - } else if (lower.includes("fire") || lower.includes("burning") || lower.includes("flame") || lower.includes("campfire") || lower.includes("wildfire")) { - soundToPlay = fireSound; - } - - if (soundToPlay) { - soundToPlay.play().then(() => { - console.log("Sound started"); - }).catch(err => { - console.error("Play error:", err); - }); - - setTimeout(() => { - soundToPlay.pause(); - soundToPlay.currentTime = 0; - console.log("Sound stopped"); - }, 10000); - } -} - -customElements.define("lg-voice", LGVoice); +customElements.define("lg-voice", LGVoice); export async function exportprocessQueryExternally(query) { const lgVoiceInstance = document.querySelector("lg-voice"); diff --git a/js/utils/audio.mjs b/js/utils/audio.mjs new file mode 100644 index 0000000..5450e34 --- /dev/null +++ b/js/utils/audio.mjs @@ -0,0 +1,43 @@ +const oceanSound = new Audio('./assets/ocean.mp3'); +const fireSound = new Audio('./assets/fire.mp3'); + +oceanSound.loop = true; +fireSound.loop = true; +oceanSound.volume = 0.25; +fireSound.volume = 0.25; + +function stopAllSounds() { + oceanSound.pause(); + fireSound.pause(); + oceanSound.currentTime = 0; + fireSound.currentTime = 0; +} + +function playSoundscapeBasedOnText(text) { + stopAllSounds(); + + const lower = text.toLowerCase(); + let soundToPlay = null; + + if (lower.includes("ocean") || lower.includes("sea") || lower.includes("wave") || lower.includes("beach") || lower.includes("coast") || lower.includes("island") || lower.includes("tsunami") || lower.includes("flood") || lower.includes("water")) { + soundToPlay = oceanSound; + } else if (lower.includes("fire") || lower.includes("burning") || lower.includes("flame") || lower.includes("campfire") || lower.includes("wildfire")) { + soundToPlay = fireSound; + } + + if (soundToPlay) { + soundToPlay.play().then(() => { + console.log("Sound started"); + }).catch(err => { + console.error("Play error:", err); + }); + + setTimeout(() => { + soundToPlay.pause(); + soundToPlay.currentTime = 0; + console.log("Sound stopped"); + }, 10000); + } +} + +export { oceanSound, fireSound, stopAllSounds, playSoundscapeBasedOnText };