2
0

webaudio_sandbox.html 7.3 KB

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