webaudio_sandbox.html 7.4 KB

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