Audio.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author Reece Aaron Lecrivain / http://reecenotes.com/
  4. */
  5. import { Object3D } from '../core/Object3D';
  6. function Audio( listener ) {
  7. Object3D.call( this );
  8. this.type = 'Audio';
  9. this.context = listener.context;
  10. this.gain = this.context.createGain();
  11. this.gain.connect( listener.getInput() );
  12. this.autoplay = false;
  13. this.buffer = null;
  14. this.loop = false;
  15. this.startTime = 0;
  16. this.playbackRate = 1;
  17. this.isPlaying = false;
  18. this.hasPlaybackControl = true;
  19. this.sourceType = 'empty';
  20. this.filters = [];
  21. }
  22. Audio.prototype = Object.assign( Object.create( Object3D.prototype ), {
  23. constructor: Audio,
  24. getOutput: function () {
  25. return this.gain;
  26. },
  27. setNodeSource: function ( audioNode ) {
  28. this.hasPlaybackControl = false;
  29. this.sourceType = 'audioNode';
  30. this.source = audioNode;
  31. this.connect();
  32. return this;
  33. },
  34. setBuffer: function ( audioBuffer ) {
  35. this.buffer = audioBuffer;
  36. this.sourceType = 'buffer';
  37. if ( this.autoplay ) this.play();
  38. return this;
  39. },
  40. play: function () {
  41. if ( this.isPlaying === true ) {
  42. console.warn( 'THREE.Audio: Audio is already playing.' );
  43. return;
  44. }
  45. if ( this.hasPlaybackControl === false ) {
  46. console.warn( 'THREE.Audio: this Audio has no playback control.' );
  47. return;
  48. }
  49. var source = this.context.createBufferSource();
  50. source.buffer = this.buffer;
  51. source.loop = this.loop;
  52. source.onended = this.onEnded.bind( this );
  53. source.playbackRate.setValueAtTime( this.playbackRate, this.startTime );
  54. source.start( 0, this.startTime );
  55. this.isPlaying = true;
  56. this.source = source;
  57. return this.connect();
  58. },
  59. pause: function () {
  60. if ( this.hasPlaybackControl === false ) {
  61. console.warn( 'THREE.Audio: this Audio has no playback control.' );
  62. return;
  63. }
  64. this.source.stop();
  65. this.startTime = this.context.currentTime;
  66. this.isPlaying = false;
  67. return this;
  68. },
  69. stop: function () {
  70. if ( this.hasPlaybackControl === false ) {
  71. console.warn( 'THREE.Audio: this Audio has no playback control.' );
  72. return;
  73. }
  74. this.source.stop();
  75. this.startTime = 0;
  76. this.isPlaying = false;
  77. return this;
  78. },
  79. connect: function () {
  80. if ( this.filters.length > 0 ) {
  81. this.source.connect( this.filters[ 0 ] );
  82. for ( var i = 1, l = this.filters.length; i < l; i ++ ) {
  83. this.filters[ i - 1 ].connect( this.filters[ i ] );
  84. }
  85. this.filters[ this.filters.length - 1 ].connect( this.getOutput() );
  86. } else {
  87. this.source.connect( this.getOutput() );
  88. }
  89. return this;
  90. },
  91. disconnect: function () {
  92. if ( this.filters.length > 0 ) {
  93. this.source.disconnect( this.filters[ 0 ] );
  94. for ( var i = 1, l = this.filters.length; i < l; i ++ ) {
  95. this.filters[ i - 1 ].disconnect( this.filters[ i ] );
  96. }
  97. this.filters[ this.filters.length - 1 ].disconnect( this.getOutput() );
  98. } else {
  99. this.source.disconnect( this.getOutput() );
  100. }
  101. return this;
  102. },
  103. getFilters: function () {
  104. return this.filters;
  105. },
  106. setFilters: function ( value ) {
  107. if ( ! value ) value = [];
  108. if ( this.isPlaying === true ) {
  109. this.disconnect();
  110. this.filters = value;
  111. this.connect();
  112. } else {
  113. this.filters = value;
  114. }
  115. return this;
  116. },
  117. getFilter: function () {
  118. return this.getFilters()[ 0 ];
  119. },
  120. setFilter: function ( filter ) {
  121. return this.setFilters( filter ? [ filter ] : [] );
  122. },
  123. setPlaybackRate: function ( value ) {
  124. if ( this.hasPlaybackControl === false ) {
  125. console.warn( 'THREE.Audio: this Audio has no playback control.' );
  126. return;
  127. }
  128. this.playbackRate = value;
  129. if ( this.isPlaying === true ) {
  130. this.source.playbackRate.setValueAtTime( this.playbackRate, this.context.currentTime );
  131. }
  132. return this;
  133. },
  134. getPlaybackRate: function () {
  135. return this.playbackRate;
  136. },
  137. onEnded: function () {
  138. this.isPlaying = false;
  139. },
  140. getLoop: function () {
  141. if ( this.hasPlaybackControl === false ) {
  142. console.warn( 'THREE.Audio: this Audio has no playback control.' );
  143. return false;
  144. }
  145. return this.loop;
  146. },
  147. setLoop: function ( value ) {
  148. if ( this.hasPlaybackControl === false ) {
  149. console.warn( 'THREE.Audio: this Audio has no playback control.' );
  150. return;
  151. }
  152. this.loop = value;
  153. if ( this.isPlaying === true ) {
  154. this.source.loop = this.loop;
  155. }
  156. return this;
  157. },
  158. getVolume: function () {
  159. return this.gain.gain.value;
  160. },
  161. setVolume: function ( value ) {
  162. this.gain.gain.value = value;
  163. return this;
  164. }
  165. } );
  166. export { Audio };