<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Ultrasonic Sound Detector</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      text-align: center;
      margin: 0;
      padding: 0;
      background-color: #f4f4f9;
    }
    header {
      background-color: #007BFF;
      color: white;
      padding: 1em 0;
    }
    main {
      margin: 20px;
    }
    #status {
      font-size: 18px;
      color: #333;
      padding: 20px;
      border: 2px solid #007BFF;
      border-radius: 5px;
      display: inline-block;
    }
  </style>
</head>
<body>
  <header>
    <h1>Ultrasonic Sound Detector</h1>
  </header>
  <main>
    <div id="status">Listening for ultrasonic sound...</div>
  </main>
  <script>
    async function detectUltrasonicSound() {
      try {
        const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
        const audioContext = new (window.AudioContext || window.webkitAudioContext)();
        const analyser = audioContext.createAnalyser();
        const microphone = audioContext.createMediaStreamSource(stream);
        microphone.connect(analyser);
        analyser.fftSize = 256;
        const dataArray = new Uint8Array(analyser.frequencyBinCount);

        function analyze() {
          analyser.getByteFrequencyData(dataArray);
          const highFrequencyValue = dataArray.slice(-10).reduce((a, b) => a + b, 0); // Check high frequencies

          if (highFrequencyValue > 1000) { // Threshold for detecting high-frequency sound
            document.getElementById('status').textContent = 'Ultrasonic sound detected!';
          } else {
            document.getElementById('status').textContent = 'Listening for ultrasonic sound...';
          }

          requestAnimationFrame(analyze);
        }

        analyze();
      } catch (error) {
        console.error('Error accessing audio stream:', error);
        document.getElementById('status').textContent = 'Error accessing microphone. Please allow microphone access.';
      }
    }

    detectUltrasonicSound();
  </script>
</body>
</html>

