DIR : /home/kozerus/public_html/ruffle/audio.html

/home/kozerus/public_html/ruffle

<!DOCTYPE html>
<html>
<head>
  <title>Audio Visualization</title>
  <style>
    canvas {
      border: 1px solid black;
    }
  </style>
</head>
<body>
  <audio id="myAudio" src="./ko_builder/chord.mp3" controls></audio>
  <canvas id="myCanvas"></canvas>

  <script>
    const audio = document.getElementById('myAudio');
    const canvas = document.getElementById('myCanvas');
    const ctx = canvas.getContext('2d');

    const audioContext = new AudioContext();
    const source = audioContext.createMediaElementSource(audio);
    const analyser = audioContext.createAnalyser();

    source.connect(analyser);
    analyser.connect(audioContext.destination); // Connect to speakers

    // Set up analyser parameters
    analyser.fftSize = 256;
    const bufferLength = analyser.frequencyBinCount;
    const dataArray = new Uint8Array(bufferLength);

    function draw() {
      requestAnimationFrame(draw);

      analyser.getByteTimeDomainData(dataArray);

      ctx.clearRect(0, 0, canvas.width, canvas.height);

      ctx.lineWidth = 2;
      ctx.strokeStyle = 'rgb(0, 0, 0)';

      ctx.beginPath();

      const sliceWidth = canvas.width * 1.0 / bufferLength;
      let x = 0;

      for (let i = 0; i < bufferLength; i++) {
        const v = dataArray[i] / 128.0;
        const y = v * canvas.height / 2;

        if (i === 0) {
          ctx.moveTo(x, y);
        } else {
          ctx.lineTo(x, y);
        }

        x += sliceWidth;
      }

      ctx.lineTo(canvas.width, canvas.height / 2);
      ctx.stroke();
    }

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

//


koh5_pano



Your browser does not support the HTML5 canvas element.


Drag mouse to navigate.

Navigation





17.Aug.2010, Martin Wengenmayer