1
0

Audio.html 8.3 KB

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