<!-- Auto-modified logic added -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Animal Sounds with Frequency Modulation</title>
    <style>
        .button-container {
            display: flex;
            align-items: center;
            justify-content: center;
            height: 100vh;
        }
    </style>
    <script>
        var audioCtx;
        var oscillator;
        var gainNode;
        var isPlaying = false;
        var playButton, pauseButton;

        function playSound(frequency, duration) {
            return new Promise(resolve => {
                // Δημιουργία context ήχου
                audioCtx = new (window.AudioContext || window.webkitAudioContext)();
                oscillator = audioCtx.createOscillator();
                gainNode = audioCtx.createGain();

                oscillator.frequency.setValueAtTime(frequency, audioCtx.currentTime);
                oscillator.type = 'sine';
                oscillator.connect(gainNode);
                gainNode.connect(audioCtx.destination);

                oscillator.start();

                setTimeout(() => {
                    if (!isPlaying) {
                        oscillator.stop();
                        resolve();
                    }
                }, duration);
            });
        }

        async function playSoundsInSequence() {
            isPlaying = true;
            toggleButtons(); // Εναλλαγή σε κουμπί Pause

            // Ήχος επιθετικής γάτας (διαρκεί 4 δευτερόλεπτα)
            await playSound(3000, 4000);  // Υπερήχοι για γάτα, υψηλή συχνότητα

            if (!isPlaying) return; // Ελέγχει αν έχει πατηθεί το Pause

            // Παύση 2 δευτερολέπτων
            await new Promise(resolve => setTimeout(resolve, 2000));

            if (!isPlaying) return;

            // Ήχος φιδιού που αμύνεται (διαμόρφωση στα 30Hz με 3kHz)
            await playModulatedTone(30, 3000, 4000); // Συνδυασμός αρμονικών για φίδι, 4 δευτερόλεπτα

            if (!isPlaying) return;

            // Παύση 2 δευτερολέπτων
            await new Promise(resolve => setTimeout(resolve, 2000));

            if (!isPlaying) return;

            // Ήχος σκαντζόχοιρου (διαρκεί 6 δευτερόλεπτα)
            await playSound(500, 6000); // Ήχος σκαντζόχοιρου σε χαμηλή συχνότητα

            toggleButtons(); // Επαναφορά στο κουμπί Play
        }

        function playModulatedTone(carrierFreq, modulatorFreq, duration) {
            return new Promise(resolve => {
                audioCtx = new (window.AudioContext || window.webkitAudioContext)();

                // Carrier
                var carrierOscillator = audioCtx.createOscillator();
                carrierOscillator.frequency.setValueAtTime(carrierFreq, audioCtx.currentTime);

                // Modulator
                var modulatorOscillator = audioCtx.createOscillator();
                var gainNode = audioCtx.createGain();
                modulatorOscillator.frequency.setValueAtTime(modulatorFreq, audioCtx.currentTime);
                gainNode.gain.setValueAtTime(100, audioCtx.currentTime);

                // Σύνδεση του διαμορφωτή στη συχνότητα του carrier
                modulatorOscillator.connect(gainNode);
                gainNode.connect(carrierOscillator.frequency);

                carrierOscillator.connect(audioCtx.destination);
                
                // Έναρξη ταλαντωτών
                carrierOscillator.start();
                modulatorOscillator.start();

                setTimeout(() => {
                    if (!isPlaying) {
                        carrierOscillator.stop();
                        modulatorOscillator.stop();
                        resolve();
                    }
                }, duration);
            });
        }

        function startSequence() {
            if (!isPlaying) {
                playSoundsInSequence();
            }
        }

        function stopSequence() {
            isPlaying = false;
            toggleButtons(); // Επαναφορά στο κουμπί Play
        }

        function toggleButtons() {
            playButton.style.display = isPlaying ? 'none' : 'inline-block';
            pauseButton.style.display = isPlaying ? 'inline-block' : 'none';
        }

        window.onload = function() {
            playButton = document.getElementById('playButton');
            pauseButton = document.getElementById('pauseButton');
            pauseButton.style.display = 'none'; // Κρύψε το κουμπί pause στην αρχή
        };
    </script>
</head>
<body>
    <div class="button-container">
        <button id="playButton" onclick="startSequence()">Play vs snake</button>
        <button id="pauseButton" onclick="stopSequence()">Pause vs snake</button>
    </div>
</body>
</html>

<!-- End of auto-modifications -->