Audio.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author Reece Aaron Lecrivain / http://reecenotes.com/
  4. */
  5. import { Object3D } from '../core/Object3D.js';
  6. function Audio( listener ) {
  7. Object3D.call( this );
  8. this.type = 'Audio';
  9. this.listener = listener;
  10. this.context = listener.context;
  11. this.gain = this.context.createGain();
  12. this.gain.connect( listener.getInput() );
  13. this.autoplay = false;
  14. this.buffer = null;
  15. this.detune = 0;
  16. this.loop = false;
  17. this.loopStart = 0;
  18. this.loopEnd = 0;
  19. this.offset = 0;
  20. this.duration = undefined;
  21. this.playbackRate = 1;
  22. this.isPlaying = false;
  23. this.hasPlaybackControl = true;
  24. this.sourceType = 'empty';
  25. this._startedAt = 0;
  26. this._progress = 0;
  27. this.filters = [];
  28. }
  29. Audio.prototype = Object.assign( Object.create( Object3D.prototype ), {
  30. constructor: Audio,
  31. getOutput: function () {
  32. return this.gain;
  33. },
  34. setNodeSource: function ( audioNode ) {
  35. this.hasPlaybackControl = false;
  36. this.sourceType = 'audioNode';
  37. this.source = audioNode;
  38. this.connect();
  39. return this;
  40. },
  41. setMediaElementSource: function ( mediaElement ) {
  42. this.hasPlaybackControl = false;
  43. this.sourceType = 'mediaNode';
  44. this.source = this.context.createMediaElementSource( mediaElement );
  45. this.connect();
  46. return this;
  47. },
  48. setMediaStreamSource: function ( mediaStream ) {
  49. this.hasPlaybackControl = false;
  50. this.sourceType = 'mediaStreamNode';
  51. this.source = this.context.createMediaStreamSource( mediaStream );
  52. this.connect();
  53. return this;
  54. },
  55. setBuffer: function ( audioBuffer ) {
  56. this.buffer = audioBuffer;
  57. this.sourceType = 'buffer';
  58. if ( this.autoplay ) this.play();
  59. return this;
  60. },
  61. play: function ( delay ) {
  62. if ( delay === undefined ) delay = 0;
  63. if ( this.isPlaying === true ) {
  64. console.warn( 'THREE.Audio: Audio is already playing.' );
  65. return;
  66. }
  67. if ( this.hasPlaybackControl === false ) {
  68. console.warn( 'THREE.Audio: this Audio has no playback control.' );
  69. return;
  70. }
  71. this._startedAt = this.context.currentTime + delay;
  72. var source = this.context.createBufferSource();
  73. source.buffer = this.buffer;
  74. source.loop = this.loop;
  75. source.loopStart = this.loopStart;
  76. source.loopEnd = this.loopEnd;
  77. source.onended = this.onEnded.bind( this );
  78. source.start( this._startedAt, this._progress + this.offset, this.duration );
  79. this.isPlaying = true;
  80. this.source = source;
  81. this.setDetune( this.detune );
  82. this.setPlaybackRate( this.playbackRate );
  83. return this.connect();
  84. },
  85. pause: function () {
  86. if ( this.hasPlaybackControl === false ) {
  87. console.warn( 'THREE.Audio: this Audio has no playback control.' );
  88. return;
  89. }
  90. if ( this.isPlaying === true ) {
  91. // update current progress
  92. this._progress += Math.max( this.context.currentTime - this._startedAt, 0 ) * this.playbackRate;
  93. if ( this.loop === true ) {
  94. // ensure _progress does not exceed duration with looped audios
  95. this._progress = this._progress % ( this.duration || this.buffer.duration );
  96. }
  97. this.source.stop();
  98. this.source.onended = null;
  99. this.isPlaying = false;
  100. }
  101. return this;
  102. },
  103. stop: function () {
  104. if ( this.hasPlaybackControl === false ) {
  105. console.warn( 'THREE.Audio: this Audio has no playback control.' );
  106. return;
  107. }
  108. this._progress = 0;
  109. this.source.stop();
  110. this.source.onended = null;
  111. this.isPlaying = false;
  112. return this;
  113. },
  114. connect: function () {
  115. if ( this.filters.length > 0 ) {
  116. this.source.connect( this.filters[ 0 ] );
  117. for ( var i = 1, l = this.filters.length; i < l; i ++ ) {
  118. this.filters[ i - 1 ].connect( this.filters[ i ] );
  119. }
  120. this.filters[ this.filters.length - 1 ].connect( this.getOutput() );
  121. } else {
  122. this.source.connect( this.getOutput() );
  123. }
  124. return this;
  125. },
  126. disconnect: function () {
  127. if ( this.filters.length > 0 ) {
  128. this.source.disconnect( this.filters[ 0 ] );
  129. for ( var i = 1, l = this.filters.length; i < l; i ++ ) {
  130. this.filters[ i - 1 ].disconnect( this.filters[ i ] );
  131. }
  132. this.filters[ this.filters.length - 1 ].disconnect( this.getOutput() );
  133. } else {
  134. this.source.disconnect( this.getOutput() );
  135. }
  136. return this;
  137. },
  138. getFilters: function () {
  139. return this.filters;
  140. },
  141. setFilters: function ( value ) {
  142. if ( ! value ) value = [];
  143. if ( this.isPlaying === true ) {
  144. this.disconnect();
  145. this.filters = value;
  146. this.connect();
  147. } else {
  148. this.filters = value;
  149. }
  150. return this;
  151. },
  152. setDetune: function ( value ) {
  153. this.detune = value;
  154. if ( this.source.detune === undefined ) return; // only set detune when available
  155. if ( this.isPlaying === true ) {
  156. this.source.detune.setTargetAtTime( this.detune, this.context.currentTime, 0.01 );
  157. }
  158. return this;
  159. },
  160. getDetune: function () {
  161. return this.detune;
  162. },
  163. getFilter: function () {
  164. return this.getFilters()[ 0 ];
  165. },
  166. setFilter: function ( filter ) {
  167. return this.setFilters( filter ? [ filter ] : [] );
  168. },
  169. setPlaybackRate: function ( value ) {
  170. if ( this.hasPlaybackControl === false ) {
  171. console.warn( 'THREE.Audio: this Audio has no playback control.' );
  172. return;
  173. }
  174. this.playbackRate = value;
  175. if ( this.isPlaying === true ) {
  176. this.source.playbackRate.setTargetAtTime( this.playbackRate, this.context.currentTime, 0.01 );
  177. }
  178. return this;
  179. },
  180. getPlaybackRate: function () {
  181. return this.playbackRate;
  182. },
  183. onEnded: function () {
  184. this.isPlaying = false;
  185. },
  186. getLoop: function () {
  187. if ( this.hasPlaybackControl === false ) {
  188. console.warn( 'THREE.Audio: this Audio has no playback control.' );
  189. return false;
  190. }
  191. return this.loop;
  192. },
  193. setLoop: function ( value ) {
  194. if ( this.hasPlaybackControl === false ) {
  195. console.warn( 'THREE.Audio: this Audio has no playback control.' );
  196. return;
  197. }
  198. this.loop = value;
  199. if ( this.isPlaying === true ) {
  200. this.source.loop = this.loop;
  201. }
  202. return this;
  203. },
  204. setLoopStart: function ( value ) {
  205. this.loopStart = value;
  206. return this;
  207. },
  208. setLoopEnd: function ( value ) {
  209. this.loopEnd = value;
  210. return this;
  211. },
  212. getVolume: function () {
  213. return this.gain.gain.value;
  214. },
  215. setVolume: function ( value ) {
  216. this.gain.gain.setTargetAtTime( value, this.context.currentTime, 0.01 );
  217. return this;
  218. }
  219. } );
  220. export { Audio };
粤ICP备19079148号