webaudio_visualizer.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webaudio - visualizer</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. <script id="vertexShader" type="x-shader/x-vertex">
  9. varying vec2 vUv;
  10. void main() {
  11. vUv = uv;
  12. gl_Position = vec4( position, 1.0 );
  13. }
  14. </script>
  15. <script id="fragmentShader" type="x-shader/x-fragment">
  16. uniform sampler2D tAudioData;
  17. varying vec2 vUv;
  18. void main() {
  19. vec3 backgroundColor = vec3( 0.125, 0.125, 0.125 );
  20. vec3 color = vec3( 1.0, 1.0, 0.0 );
  21. float f = texture2D( tAudioData, vec2( vUv.x, 0.0 ) ).r;
  22. float i = step( vUv.y, f ) * step( f - 0.0125, vUv.y );
  23. gl_FragColor = vec4( mix( backgroundColor, color, i ), 1.0 );
  24. }
  25. </script>
  26. </head>
  27. <body>
  28. <div id="overlay">
  29. <div>
  30. <button id="startButton">Click to Play</button>
  31. <p>Audio playback requires user interaction.</p>
  32. </div>
  33. </div>
  34. <div id="container"></div>
  35. <div id="info">
  36. <a href="https://threejs.org" target="_blank" rel="noopener noreferrer">three.js</a> webaudio - visualizer<br/>
  37. music by <a href="http://www.newgrounds.com/audio/listen/376737" target="_blank" rel="noopener">skullbeatz</a>
  38. </div>
  39. <script type="module">
  40. import * as THREE from '../build/three.module.js';
  41. var scene, camera, renderer, analyser, uniforms;
  42. var startButton = document.getElementById( 'startButton' );
  43. startButton.addEventListener( 'click', init );
  44. function init() {
  45. var fftSize = 128;
  46. //
  47. var overlay = document.getElementById( 'overlay' );
  48. overlay.remove();
  49. //
  50. var container = document.getElementById( 'container' );
  51. renderer = new THREE.WebGLRenderer( { antialias: true } );
  52. renderer.setSize( window.innerWidth, window.innerHeight );
  53. renderer.setClearColor( 0x000000 );
  54. renderer.setPixelRatio( window.devicePixelRatio );
  55. container.appendChild( renderer.domElement );
  56. scene = new THREE.Scene();
  57. camera = new THREE.Camera();
  58. //
  59. var listener = new THREE.AudioListener();
  60. var audio = new THREE.Audio( listener );
  61. var mediaElement = new Audio( 'sounds/376737_Skullbeatz___Bad_Cat_Maste.mp3' );
  62. mediaElement.loop = true;
  63. mediaElement.play();
  64. audio.setMediaElementSource( mediaElement );
  65. analyser = new THREE.AudioAnalyser( audio, fftSize );
  66. //
  67. uniforms = {
  68. tAudioData: { value: new THREE.DataTexture( analyser.data, fftSize / 2, 1, THREE.LuminanceFormat ) }
  69. };
  70. var material = new THREE.ShaderMaterial( {
  71. uniforms: uniforms,
  72. vertexShader: document.getElementById( 'vertexShader' ).textContent,
  73. fragmentShader: document.getElementById( 'fragmentShader' ).textContent
  74. } );
  75. var geometry = new THREE.PlaneBufferGeometry( 1, 1 );
  76. var mesh = new THREE.Mesh( geometry, material );
  77. scene.add( mesh );
  78. //
  79. window.addEventListener( 'resize', onResize, false );
  80. animate();
  81. }
  82. function onResize() {
  83. renderer.setSize( window.innerWidth, window.innerHeight );
  84. }
  85. function animate() {
  86. requestAnimationFrame( animate );
  87. render();
  88. }
  89. function render() {
  90. analyser.getFrequencyData();
  91. uniforms.tAudioData.value.needsUpdate = true;
  92. renderer.render( scene, camera );
  93. }
  94. </script>
  95. </body>
  96. </html>