webaudio_sandbox.html 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webaudio - sandbox</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. </head>
  9. <body>
  10. <audio id="song" preload="auto" style="display: none">
  11. <source src="sounds/358232_j_s_song.ogg" type="audio/ogg">
  12. <source src="sounds/358232_j_s_song.mp3" type="audio/mpeg">
  13. </audio>
  14. <audio id="skullbeatz" preload="auto" style="display: none">
  15. <source src="sounds/376737_Skullbeatz___Bad_Cat_Maste.ogg" type="audio/ogg">
  16. <source src="sounds/376737_Skullbeatz___Bad_Cat_Maste.mp3" type="audio/mpeg">
  17. </audio>
  18. <audio id="utopia" loop preload="auto" style="display: none">
  19. <source src="sounds/Project_Utopia.ogg" type="audio/ogg">
  20. <source src="sounds/Project_Utopia.mp3" type="audio/mpeg">
  21. </audio>
  22. <div id="overlay">
  23. <button id="startButton">Play</button>
  24. </div>
  25. <div id="info">
  26. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webaudio - sandbox<br/>
  27. music by <a href="http://www.newgrounds.com/audio/listen/358232" target="_blank" rel="noopener">larrylarrybb</a>,
  28. <a href="http://www.newgrounds.com/audio/listen/376737" target="_blank" rel="noopener">skullbeatz</a> and
  29. <a href="http://opengameart.org/content/project-utopia-seamless-loop" target="_blank" rel="noopener">congusbongus</a><br/><br/>
  30. navigate with WASD / arrows / mouse
  31. </div>
  32. <!-- Import maps polyfill -->
  33. <!-- Remove this when import maps will be widely supported -->
  34. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  35. <script type="importmap">
  36. {
  37. "imports": {
  38. "three": "../build/three.module.js",
  39. "three/addons/": "./jsm/"
  40. }
  41. }
  42. </script>
  43. <script type="module">
  44. import * as THREE from 'three';
  45. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  46. import { FirstPersonControls } from 'three/addons/controls/FirstPersonControls.js';
  47. let camera, controls, scene, renderer, light;
  48. let material1, material2, material3;
  49. let analyser1, analyser2, analyser3;
  50. const clock = new THREE.Clock();
  51. const startButton = document.getElementById( 'startButton' );
  52. startButton.addEventListener( 'click', init );
  53. function init() {
  54. const overlay = document.getElementById( 'overlay' );
  55. overlay.remove();
  56. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 10000 );
  57. camera.position.set( 0, 25, 0 );
  58. const listener = new THREE.AudioListener();
  59. camera.add( listener );
  60. scene = new THREE.Scene();
  61. scene.fog = new THREE.FogExp2( 0x000000, 0.0025 );
  62. light = new THREE.DirectionalLight( 0xffffff, 3 );
  63. light.position.set( 0, 0.5, 1 ).normalize();
  64. scene.add( light );
  65. const sphere = new THREE.SphereGeometry( 20, 32, 16 );
  66. material1 = new THREE.MeshPhongMaterial( { color: 0xffaa00, flatShading: true, shininess: 0 } );
  67. material2 = new THREE.MeshPhongMaterial( { color: 0xff2200, flatShading: true, shininess: 0 } );
  68. material3 = new THREE.MeshPhongMaterial( { color: 0x6622aa, flatShading: true, shininess: 0 } );
  69. // sound spheres
  70. const mesh1 = new THREE.Mesh( sphere, material1 );
  71. mesh1.position.set( - 250, 30, 0 );
  72. scene.add( mesh1 );
  73. const sound1 = new THREE.PositionalAudio( listener );
  74. const songElement = document.getElementById( 'song' );
  75. sound1.setMediaElementSource( songElement );
  76. sound1.setRefDistance( 20 );
  77. songElement.play();
  78. mesh1.add( sound1 );
  79. //
  80. const mesh2 = new THREE.Mesh( sphere, material2 );
  81. mesh2.position.set( 250, 30, 0 );
  82. scene.add( mesh2 );
  83. const sound2 = new THREE.PositionalAudio( listener );
  84. const skullbeatzElement = document.getElementById( 'skullbeatz' );
  85. sound2.setMediaElementSource( skullbeatzElement );
  86. sound2.setRefDistance( 20 );
  87. skullbeatzElement.play();
  88. mesh2.add( sound2 );
  89. //
  90. const mesh3 = new THREE.Mesh( sphere, material3 );
  91. mesh3.position.set( 0, 30, - 250 );
  92. scene.add( mesh3 );
  93. const sound3 = new THREE.PositionalAudio( listener );
  94. const oscillator = listener.context.createOscillator();
  95. oscillator.type = 'sine';
  96. oscillator.frequency.setValueAtTime( 144, sound3.context.currentTime );
  97. oscillator.start( 0 );
  98. sound3.setNodeSource( oscillator );
  99. sound3.setRefDistance( 20 );
  100. sound3.setVolume( 0.5 );
  101. mesh3.add( sound3 );
  102. // analysers
  103. analyser1 = new THREE.AudioAnalyser( sound1, 32 );
  104. analyser2 = new THREE.AudioAnalyser( sound2, 32 );
  105. analyser3 = new THREE.AudioAnalyser( sound3, 32 );
  106. // global ambient audio
  107. const sound4 = new THREE.Audio( listener );
  108. const utopiaElement = document.getElementById( 'utopia' );
  109. sound4.setMediaElementSource( utopiaElement );
  110. sound4.setVolume( 0.5 );
  111. utopiaElement.play();
  112. // ground
  113. const helper = new THREE.GridHelper( 1000, 10, 0x444444, 0x444444 );
  114. helper.position.y = 0.1;
  115. scene.add( helper );
  116. //
  117. const SoundControls = function () {
  118. this.master = listener.getMasterVolume();
  119. this.firstSphere = sound1.getVolume();
  120. this.secondSphere = sound2.getVolume();
  121. this.thirdSphere = sound3.getVolume();
  122. this.Ambient = sound4.getVolume();
  123. };
  124. const GeneratorControls = function () {
  125. this.frequency = oscillator.frequency.value;
  126. this.wavetype = oscillator.type;
  127. };
  128. const gui = new GUI();
  129. const soundControls = new SoundControls();
  130. const generatorControls = new GeneratorControls();
  131. const volumeFolder = gui.addFolder( 'sound volume' );
  132. const generatorFolder = gui.addFolder( 'sound generator' );
  133. volumeFolder.add( soundControls, 'master' ).min( 0.0 ).max( 1.0 ).step( 0.01 ).onChange( function () {
  134. listener.setMasterVolume( soundControls.master );
  135. } );
  136. volumeFolder.add( soundControls, 'firstSphere' ).min( 0.0 ).max( 1.0 ).step( 0.01 ).onChange( function () {
  137. sound1.setVolume( soundControls.firstSphere );
  138. } );
  139. volumeFolder.add( soundControls, 'secondSphere' ).min( 0.0 ).max( 1.0 ).step( 0.01 ).onChange( function () {
  140. sound2.setVolume( soundControls.secondSphere );
  141. } );
  142. volumeFolder.add( soundControls, 'thirdSphere' ).min( 0.0 ).max( 1.0 ).step( 0.01 ).onChange( function () {
  143. sound3.setVolume( soundControls.thirdSphere );
  144. } );
  145. volumeFolder.add( soundControls, 'Ambient' ).min( 0.0 ).max( 1.0 ).step( 0.01 ).onChange( function () {
  146. sound4.setVolume( soundControls.Ambient );
  147. } );
  148. volumeFolder.open();
  149. generatorFolder.add( generatorControls, 'frequency' ).min( 50.0 ).max( 5000.0 ).step( 1.0 ).onChange( function () {
  150. oscillator.frequency.setValueAtTime( generatorControls.frequency, listener.context.currentTime );
  151. } );
  152. generatorFolder.add( generatorControls, 'wavetype', [ 'sine', 'square', 'sawtooth', 'triangle' ] ).onChange( function () {
  153. oscillator.type = generatorControls.wavetype;
  154. } );
  155. generatorFolder.open();
  156. //
  157. renderer = new THREE.WebGLRenderer( { antialias: true } );
  158. renderer.setPixelRatio( window.devicePixelRatio );
  159. renderer.setSize( window.innerWidth, window.innerHeight );
  160. renderer.useLegacyLights = false;
  161. document.body.appendChild( renderer.domElement );
  162. //
  163. controls = new FirstPersonControls( camera, renderer.domElement );
  164. controls.movementSpeed = 70;
  165. controls.lookSpeed = 0.05;
  166. controls.noFly = true;
  167. controls.lookVertical = false;
  168. //
  169. window.addEventListener( 'resize', onWindowResize );
  170. animate();
  171. }
  172. function onWindowResize() {
  173. camera.aspect = window.innerWidth / window.innerHeight;
  174. camera.updateProjectionMatrix();
  175. renderer.setSize( window.innerWidth, window.innerHeight );
  176. controls.handleResize();
  177. }
  178. function animate() {
  179. requestAnimationFrame( animate );
  180. render();
  181. }
  182. function render() {
  183. const delta = clock.getDelta();
  184. controls.update( delta );
  185. material1.emissive.b = analyser1.getAverageFrequency() / 256;
  186. material2.emissive.b = analyser2.getAverageFrequency() / 256;
  187. material3.emissive.b = analyser3.getAverageFrequency() / 256;
  188. renderer.render( scene, camera );
  189. }
  190. </script>
  191. </body>
  192. </html>