Audio.html 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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:AudioNode 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:this 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:this 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:Float getDetune]()</h3>
  87. <p>
  88. Returns the detuning of oscillation in cents.
  89. </p>
  90. <h3>[method:BiquadFilterNode getFilter]()</h3>
  91. <p>
  92. Returns the first element of the [page:Audio.filters filters] array.
  93. </p>
  94. <h3>[method:Array getFilters]()</h3>
  95. <p>
  96. Returns the [page:Audio.filters filters] array.
  97. </p>
  98. <h3>[method:Boolean getLoop]()</h3>
  99. <p>
  100. Return the value of [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/loop source.loop]
  101. (whether playback should loop).
  102. </p>
  103. <h3>[method:GainNode getOutput]()</h3>
  104. <p>
  105. Return the [page:Audio.gain gainNode].
  106. </p>
  107. <h3>[method:Float getPlaybackRate]()</h3>
  108. <p>
  109. Return the value of [page:Audio.playbackRate playbackRate].
  110. </p>
  111. <h3>[method:Float getVolume]( value )</h3>
  112. <p>
  113. Return the current volume.
  114. </p>
  115. <h3>[method:this play]( delay )</h3>
  116. <p>
  117. If [page:Audio.hasPlaybackControl hasPlaybackControl] is true, starts playback.
  118. </p>
  119. <h3>[method:this pause]()</h3>
  120. <p>
  121. If [page:Audio.hasPlaybackControl hasPlaybackControl] is true, pauses playback.
  122. </p>
  123. <h3>[method:undefined onEnded]()</h3>
  124. <p>
  125. Called automatically when playback finished.
  126. </p>
  127. <h3>[method:this setBuffer]( audioBuffer )</h3>
  128. <p>
  129. Setup the [page:Audio.source source] to the audioBuffer, and sets [page:Audio.sourceType sourceType] to 'buffer'.<br />
  130. If [page:Audio.autoplay autoplay], also starts playback.
  131. </p>
  132. <h3>[method:this setDetune]( [param:Float value] )</h3>
  133. <p>
  134. Defines the detuning of oscillation in cents.
  135. </p>
  136. <h3>[method:this setFilter]( filter )</h3>
  137. <p>
  138. Applies a single filter node to the audio.
  139. </p>
  140. <h3>[method:this setFilters]( [param:Array value] )</h3>
  141. <p>
  142. value - arrays of filters.<br />
  143. Applies an array of filter nodes to the audio.
  144. </p>
  145. <h3>[method:this setLoop]( [param:Boolean value] )</h3>
  146. <p>
  147. Set [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/loop source.loop] to `value`
  148. (whether playback should loop).
  149. </p>
  150. <h3>[method:this setLoopStart]( [param:Float value] )</h3>
  151. <p>
  152. Set [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/loopStart source.loopStart] to `value`.
  153. </p>
  154. <h3>[method:this setLoopEnd]( [param:Float value] )</h3>
  155. <p>
  156. Set [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/loopEnd source.loopEnd] to `value`.
  157. </p>
  158. <h3>[method:this setMediaElementSource]( mediaElement )</h3>
  159. <p>
  160. 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 />
  161. Also sets [page:Audio.hasPlaybackControl hasPlaybackControl] to false.
  162. </p>
  163. <h3>[method:this setMediaStreamSource]( mediaStream )</h3>
  164. <p>
  165. 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 />
  166. Also sets [page:Audio.hasPlaybackControl hasPlaybackControl] to false.
  167. </p>
  168. <h3>[method:this setNodeSource]( audioNode )</h3>
  169. <p>
  170. Setup the [page:Audio.source source] to the audioBuffer, and sets [page:Audio.sourceType sourceType] to 'audioNode'.<br />
  171. Also sets [page:Audio.hasPlaybackControl hasPlaybackControl] to false.
  172. </p>
  173. <h3>[method:this setPlaybackRate]( [param:Float value] )</h3>
  174. <p>
  175. If [page:Audio.hasPlaybackControl hasPlaybackControl] is enabled, set the [page:Audio.playbackRate playbackRate] to `value`.
  176. </p>
  177. <h3>[method:this setVolume]( [param:Float value] )</h3>
  178. <p>
  179. Set the volume.
  180. </p>
  181. <h3>[method:this stop]()</h3>
  182. <p>
  183. If [page:Audio.hasPlaybackControl hasPlaybackControl] is enabled, stops playback.
  184. </p>
  185. <h2>Source</h2>
  186. <p>
  187. [link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]
  188. </p>
  189. </body>
  190. </html>