Audio.html 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <base href="../../../" />
  6. <script src="page.js"></script>
  7. <link type="text/css" rel="stylesheet" href="page.css" />
  8. </head>
  9. <body>
  10. [page:Object3D] &rarr;
  11. <h1>[name]</h1>
  12. <p class="desc">
  13. Create a non-positional ( global ) audio object.<br /><br />
  14. This uses the [link:https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API Web Audio API].
  15. </p>
  16. <h2>Code Example</h2>
  17. <code>
  18. // create an AudioListener and add it to the camera
  19. const listener = new THREE.AudioListener();
  20. camera.add( listener );
  21. // create a global audio source
  22. const sound = new THREE.Audio( listener );
  23. // load a sound and set it as the Audio object's buffer
  24. const audioLoader = new THREE.AudioLoader();
  25. audioLoader.load( 'sounds/ambient.ogg', function( buffer ) {
  26. sound.setBuffer( buffer );
  27. sound.setLoop( true );
  28. sound.setVolume( 0.5 );
  29. sound.play();
  30. });
  31. </code>
  32. <h2>Examples</h2>
  33. <p>
  34. [example:webaudio_sandbox webaudio / sandbox ]<br />
  35. [example:webaudio_visualizer webaudio / visualizer ]
  36. </p>
  37. <h2>Constructor</h2>
  38. <h3>[name]( [param:AudioListener listener] )</h3>
  39. <p>
  40. listener — (required) [page:AudioListener AudioListener] instance.
  41. </p>
  42. <h2>Properties</h2>
  43. <h3>[property:Boolean autoplay]</h3>
  44. <p>Whether to start playback automatically. Default is *false*.</p>
  45. <h3>[property:AudioContext context]</h3>
  46. <p>The [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioContext AudioContext] of the [page:AudioListener listener] given in the constructor.</p>
  47. <h3>[property:Number detune]</h3>
  48. <p>Modify pitch, measured in cents. +/- 100 is a semitone. +/- 1200 is an octave. Default is *0*.</p>
  49. <h3>[property:Array filters]</h3>
  50. <p>Represents an array of [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioNode AudioNodes]. Can be used to apply a variety of low-order filters to create more complex sound effects.
  51. In most cases, the array contains instances of [link:https://developer.mozilla.org/en-US/docs/Web/API/BiquadFilterNode BiquadFilterNodes]. Filters are set via [page:Audio.setFilter] or [page:Audio.setFilters].</p>
  52. <h3>[property:GainNode gain]</h3>
  53. <p>A [link:https://developer.mozilla.org/en-US/docs/Web/API/GainNode GainNode] created
  54. using [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/createGain AudioContext.createGain]().</p>
  55. <h3>[property:Boolean hasPlaybackControl]</h3>
  56. <p>Whether playback can be controlled using the [page:Audio.play play](),
  57. [page:Audio.pause pause]() etc. methods. Default is *true*.</p>
  58. <h3>[property:Boolean isPlaying]</h3>
  59. <p>Whether the audio is currently playing.</p>
  60. <h3>[property:AudioListener listener]</h3>
  61. <p>A reference to the listener object of this audio.</p>
  62. <h3>[property:Number playbackRate]</h3>
  63. <p>Speed of playback. Default is *1*.</p>
  64. <h3>[property:Number offset]</h3>
  65. <p>An offset to the time within the audio buffer that playback should begin. Same as the *offset* parameter of [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/start AudioBufferSourceNode.start](). Default is *0*.</p>
  66. <h3>[property:Number duration]</h3>
  67. <p>Overrides the duration of the audio. Same as the *duration* parameter of [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/start AudioBufferSourceNode.start](). Default is *undefined* to play the whole buffer.</p>
  68. <h3>[property:String source]</h3>
  69. <p>An [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode AudioBufferSourceNode] created
  70. using [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/createBufferSource AudioContext.createBufferSource]().</p>
  71. <h3>[property:String sourceType]</h3>
  72. <p>Type of the audio source. Default is string 'empty'.</p>
  73. <h3>[property:String type]</h3>
  74. <p>String denoting the type, set to 'Audio'.</p>
  75. <h2>Methods</h2>
  76. <h3>[method:Audio connect]()</h3>
  77. <p>
  78. Connect to the [page:Audio.source]. This is used internally on initialisation and when
  79. setting / removing filters.
  80. </p>
  81. <h3>[method:Audio disconnect]()</h3>
  82. <p>
  83. Disconnect from the [page:Audio.source]. This is used internally when
  84. setting / removing filters.
  85. </p>
  86. <h3>[method:BiquadFilterNode getFilter]()</h3>
  87. <p>
  88. Returns the first element of the [page:Audio.filters filters] array.
  89. </p>
  90. <h3>[method:Array getFilters]()</h3>
  91. <p>
  92. Returns the [page:Audio.filters filters] array.
  93. </p>
  94. <h3>[method:Boolean getLoop]()</h3>
  95. <p>
  96. Return the value of [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/loop source.loop]
  97. (whether playback should loop).
  98. </p>
  99. <h3>[method:GainNode getOutput]()</h3>
  100. <p>
  101. Return the [page:Audio.gain gainNode].
  102. </p>
  103. <h3>[method:Float getPlaybackRate]()</h3>
  104. <p>
  105. Return the value of [page:Audio.playbackRate playbackRate].
  106. </p>
  107. <h3>[method:Float getVolume]( value )</h3>
  108. <p>
  109. Return the current volume.
  110. </p>
  111. <h3>[method:Audio play]( delay )</h3>
  112. <p>
  113. If [page:Audio.hasPlaybackControl hasPlaybackControl] is true, starts playback.
  114. </p>
  115. <h3>[method:Audio pause]()</h3>
  116. <p>
  117. If [page:Audio.hasPlaybackControl hasPlaybackControl] is true, pauses playback.
  118. </p>
  119. <h3>[method:null onEnded]()</h3>
  120. <p>
  121. Called automatically when playback finished.
  122. </p>
  123. <h3>[method:Audio setBuffer]( audioBuffer )</h3>
  124. <p>
  125. Setup the [page:Audio.source source] to the audioBuffer, and sets [page:Audio.sourceType sourceType] to 'buffer'.<br />
  126. If [page:Audio.autoplay autoplay], also starts playback.
  127. </p>
  128. <h3>[method:Audio setFilter]( filter )</h3>
  129. <p>
  130. Applies a single filter node to the audio.
  131. </p>
  132. <h3>[method:Audio setFilters]( [param:Array value] )</h3>
  133. <p>
  134. value - arrays of filters.<br />
  135. Applies an array of filter nodes to the audio.
  136. </p>
  137. <h3>[method:Audio setLoop]( [param:Boolean value] )</h3>
  138. <p>
  139. Set [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/loop source.loop] to *value*
  140. (whether playback should loop).
  141. </p>
  142. <h3>[method:Audio setLoopStart]( [param:Float value] )</h3>
  143. <p>
  144. Set [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/loopStart source.loopStart] to *value*.
  145. </p>
  146. <h3>[method:Audio setLoopEnd]( [param:Float value] )</h3>
  147. <p>
  148. Set [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/loopEnd source.loopEnd] to *value*.
  149. </p>
  150. <h3>[method:Audio setMediaElementSource]( mediaElement )</h3>
  151. <p>
  152. Applies the given object of type [link:https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement HTMLMediaElement] as the source of this audio.<br />
  153. Also sets [page:Audio.hasPlaybackControl hasPlaybackControl] to false.
  154. </p>
  155. <h3>[method:Audio setMediaStreamSource]( mediaStream )</h3>
  156. <p>
  157. Applies the given object of type [link:https://developer.mozilla.org/en-US/docs/Web/API/MediaStream MediaStream] as the source of this audio.<br />
  158. Also sets [page:Audio.hasPlaybackControl hasPlaybackControl] to false.
  159. </p>
  160. <h3>[method:Audio setNodeSource]( audioNode )</h3>
  161. <p>
  162. Setup the [page:Audio.source source] to the audioBuffer, and sets [page:Audio.sourceType sourceType] to 'audioNode'.<br />
  163. Also sets [page:Audio.hasPlaybackControl hasPlaybackControl] to false.
  164. </p>
  165. <h3>[method:Audio setPlaybackRate]( [param:Float value] )</h3>
  166. <p>
  167. If [page:Audio.hasPlaybackControl hasPlaybackControl] is enabled, set the [page:Audio.playbackRate playbackRate] to *value*.
  168. </p>
  169. <h3>[method:Audio setVolume]( [param:Float value] )</h3>
  170. <p>
  171. Set the volume.
  172. </p>
  173. <h3>[method:Audio stop]()</h3>
  174. <p>
  175. If [page:Audio.hasPlaybackControl hasPlaybackControl] is enabled, stops playback.
  176. </p>
  177. <h2>Source</h2>
  178. <p>
  179. [link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]
  180. </p>
  181. </body>
  182. </html>