2
0

Audio.html 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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]( value )</h3>
  127. <p>Return the current volume.</p>
  128. <h3>[method:this play]( delay )</h3>
  129. <p>
  130. If [page:Audio.hasPlaybackControl hasPlaybackControl] is true, starts
  131. playback.
  132. </p>
  133. <h3>[method:this pause]()</h3>
  134. <p>
  135. If [page:Audio.hasPlaybackControl hasPlaybackControl] is true, pauses
  136. playback.
  137. </p>
  138. <h3>[method:undefined onEnded]()</h3>
  139. <p>Called automatically when playback finished.</p>
  140. <h3>[method:this setBuffer]( audioBuffer )</h3>
  141. <p>
  142. Setup the [page:Audio.source source] to the audioBuffer, and sets
  143. [page:Audio.sourceType sourceType] to 'buffer'.<br />
  144. If [page:Audio.autoplay autoplay], also starts playback.
  145. </p>
  146. <h3>[method:this setDetune]( [param:Float value] )</h3>
  147. <p>Defines the detuning of oscillation in cents.</p>
  148. <h3>[method:this setFilter]( filter )</h3>
  149. <p>Applies a single filter node to the audio.</p>
  150. <h3>[method:this setFilters]( [param:Array value] )</h3>
  151. <p>
  152. value - arrays of filters.<br />
  153. Applies an array of filter nodes to the audio.
  154. </p>
  155. <h3>[method:this setLoop]( [param:Boolean value] )</h3>
  156. <p>
  157. Set
  158. [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/loop source.loop] to `value` (whether playback should loop).
  159. </p>
  160. <h3>[method:this setLoopStart]( [param:Float value] )</h3>
  161. <p>
  162. Set
  163. [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/loopStart source.loopStart] to `value`.
  164. </p>
  165. <h3>[method:this setLoopEnd]( [param:Float value] )</h3>
  166. <p>
  167. Set
  168. [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/loopEnd source.loopEnd] to `value`.
  169. </p>
  170. <h3>[method:this setMediaElementSource]( mediaElement )</h3>
  171. <p>
  172. Applies the given object of type
  173. [link:https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement HTMLMediaElement] as the source of this audio.<br />
  174. Also sets [page:Audio.hasPlaybackControl hasPlaybackControl] to false.
  175. </p>
  176. <h3>[method:this setMediaStreamSource]( mediaStream )</h3>
  177. <p>
  178. Applies the given object of type
  179. [link:https://developer.mozilla.org/en-US/docs/Web/API/MediaStream MediaStream] as the source of this audio.<br />
  180. Also sets [page:Audio.hasPlaybackControl hasPlaybackControl] to false.
  181. </p>
  182. <h3>[method:this setNodeSource]( audioNode )</h3>
  183. <p>
  184. Setup the [page:Audio.source source] to the audioBuffer, and sets
  185. [page:Audio.sourceType sourceType] to 'audioNode'.<br />
  186. Also sets [page:Audio.hasPlaybackControl hasPlaybackControl] to false.
  187. </p>
  188. <h3>[method:this setPlaybackRate]( [param:Float value] )</h3>
  189. <p>
  190. If [page:Audio.hasPlaybackControl hasPlaybackControl] is enabled, set the
  191. [page:Audio.playbackRate playbackRate] to `value`.
  192. </p>
  193. <h3>[method:this setVolume]( [param:Float value] )</h3>
  194. <p>Set the volume.</p>
  195. <h3>[method:this stop]()</h3>
  196. <p>
  197. If [page:Audio.hasPlaybackControl hasPlaybackControl] is enabled, stops
  198. playback.
  199. </p>
  200. <h2>Source</h2>
  201. <p>
  202. [link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]
  203. </p>
  204. </body>
  205. </html>