<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dog Repeller</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/howler/2.2.3/howler.min.js"></script>
    <style>
        button {
            padding: 10px 20px;
            font-size: 16px;
            margin: 10px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <button id="controlButton" onclick="toggleSound()">Play</button>

    <script>
        let context = new (window.AudioContext || window.webkitAudioContext)();
        let oscillator;
        let gainNode;
        let isPlaying = false;

        // Δημιουργία ήχων με τη βιβλιοθήκη Howler.js
        const fireworksSound = new Howl({
            src: ['https://freesound.org/data/previews/542/542494_134150-lq.mp3'],
            volume: 1.0,
            loop: false
        });

        const machinegunSound = new Howl({
            src: ['https://freesound.org/data/previews/84/84811_969434-lq.mp3'],
            volume: 1.0,
            loop: false
        });

        const grenadesSound = new Howl({
            src: ['https://freesound.org/data/previews/166/166766_2437352-lq.mp3'],
            volume: 1.0,
            loop: false
        });

        function toggleSound() {
            if (isPlaying) {
                stopSound();
                document.getElementById("controlButton").textContent = "Play";
            } else {
                isPlaying = true;
                document.getElementById("controlButton").textContent = "Pause";
                startSoundLoop();
            }
        }

        function startSoundLoop() {
            if (isPlaying) {
                playSoundSequence().then(() => {
                    startSoundLoop(); // Επαναλαμβάνει τη διαδικασία
                });
            }
        }

        function playSoundSequence() {
            return new Promise((resolve) => {
                oscillator = context.createOscillator();
                gainNode = context.createGain();

                oscillator.type = 'sine';
                oscillator.frequency.setValueAtTime(100, context.currentTime); // Ξεκινάμε από τα 100Hz
                gainNode.gain.setValueAtTime(1, context.currentTime); // Ξεκινάμε με gain 1

                oscillator.connect(gainNode);
                gainNode.connect(context.destination);

                oscillator.start();

                sweepFrequency(resolve);
            });
        }

        function sweepFrequency(resolve) {
            let currentTime = context.currentTime;

            oscillator.frequency.linearRampToValueAtTime(19000, currentTime + 1.5); // Φτάνει στα 19kHz σε 1.5 δευτερόλεπτα
            oscillator.frequency.linearRampToValueAtTime(100, currentTime + 3); // Επιστρέφει στα 100Hz τα επόμενα 1.5 δευτερόλεπτα

            // Αναπαραγωγή των ηχητικών εφέ ανάλογα με τη συχνότητα
            setTimeout(() => {
                if (isPlaying) fireworksSound.play();
            }, 1500); // Παίζει μετά από 1.5 δευτερόλεπτα

            setTimeout(() => {
                if (isPlaying) machinegunSound.play();
            }, 1750); // Παίζει μετά από 1.75 δευτερόλεπτα

            setTimeout(() => {
                if (isPlaying) grenadesSound.play();
            }, 2000); // Παίζει μετά από 2 δευτερόλεπτα

            // Όταν τελειώσει ο κύκλος, καλούμε το resolve για να συνεχίσει ο βρόχος
            setTimeout(() => {
                if (isPlaying) {
                    oscillator.stop();
                    resolve(); // Συνεχίζει τον βρόχο
                }
            }, 3000); // Τερματίζει μετά από 3 δευτερόλεπτα
        }

        function stopSound() {
            isPlaying = false;
            if (oscillator) {
                oscillator.stop();
                oscillator.disconnect();
            }
        }
    </script>
</body>
</html>
