webaudio_sandbox.html 8.2 KB

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