Audio.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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.startTime = 0;
  18. this.offset = 0;
  19. this.playbackRate = 1;
  20. this.isPlaying = false;
  21. this.hasPlaybackControl = true;
  22. this.sourceType = 'empty';
  23. this.filters = [];
  24. }
  25. Audio.prototype = Object.assign( Object.create( Object3D.prototype ), {
  26. constructor: Audio,
  27. getOutput: function () {
  28. return this.gain;
  29. },
  30. setNodeSource: function ( audioNode ) {
  31. this.hasPlaybackControl = false;
  32. this.sourceType = 'audioNode';
  33. this.source = audioNode;
  34. this.connect();
  35. return this;
  36. },
  37. setMediaElementSource: function ( mediaElement ) {
  38. this.hasPlaybackControl = false;
  39. this.sourceType = 'mediaNode';
  40. this.source = this.context.createMediaElementSource( mediaElement );
  41. this.connect();
  42. return this;
  43. },
  44. setBuffer: function ( audioBuffer ) {
  45. this.buffer = audioBuffer;
  46. this.sourceType = 'buffer';
  47. if ( this.autoplay ) this.play();
  48. return this;
  49. },
  50. play: function () {
  51. if ( this.isPlaying === true ) {
  52. console.warn( 'THREE.Audio: Audio is already playing.' );
  53. return;
  54. }
  55. if ( this.hasPlaybackControl === false ) {
  56. console.warn( 'THREE.Audio: this Audio has no playback control.' );
  57. return;
  58. }
  59. var source = this.context.createBufferSource();
  60. source.buffer = this.buffer;
  61. source.loop = this.loop;
  62. source.onended = this.onEnded.bind( this );
  63. this.startTime = this.context.currentTime;
  64. source.start( this.startTime, this.offset );
  65. this.isPlaying = true;
  66. this.source = source;
  67. this.setDetune( this.detune );
  68. this.setPlaybackRate( this.playbackRate );
  69. return this.connect();
  70. },
  71. pause: function () {
  72. if ( this.hasPlaybackControl === false ) {
  73. console.warn( 'THREE.Audio: this Audio has no playback control.' );
  74. return;
  75. }
  76. if ( this.isPlaying === true ) {
  77. this.source.stop();
  78. this.source.onended = null;
  79. this.offset += ( this.context.currentTime - this.startTime ) * this.playbackRate;
  80. this.isPlaying = false;
  81. }
  82. return this;
  83. },
  84. stop: function () {
  85. if ( this.hasPlaybackControl === false ) {
  86. console.warn( 'THREE.Audio: this Audio has no playback control.' );
  87. return;
  88. }
  89. this.source.stop();
  90. this.source.onended = null;
  91. this.offset = 0;
  92. this.isPlaying = false;
  93. return this;
  94. },
  95. connect: function () {
  96. if ( this.filters.length > 0 ) {
  97. this.source.connect( this.filters[ 0 ] );
  98. for ( var i = 1, l = this.filters.length; i < l; i ++ ) {
  99. this.filters[ i - 1 ].connect( this.filters[ i ] );
  100. }
  101. this.filters[ this.filters.length - 1 ].connect( this.getOutput() );
  102. } else {
  103. this.source.connect( this.getOutput() );
  104. }
  105. return this;
  106. },
  107. disconnect: function () {
  108. if ( this.filters.length > 0 ) {
  109. this.source.disconnect( this.filters[ 0 ] );
  110. for ( var i = 1, l = this.filters.length; i < l; i ++ ) {
  111. this.filters[ i - 1 ].disconnect( this.filters[ i ] );
  112. }
  113. this.filters[ this.filters.length - 1 ].disconnect( this.getOutput() );
  114. } else {
  115. this.source.disconnect( this.getOutput() );
  116. }
  117. return this;
  118. },
  119. getFilters: function () {
  120. return this.filters;
  121. },
  122. setFilters: function ( value ) {
  123. if ( ! value ) value = [];
  124. if ( this.isPlaying === true ) {
  125. this.disconnect();
  126. this.filters = value;
  127. this.connect();
  128. } else {
  129. this.filters = value;
  130. }
  131. return this;
  132. },
  133. setDetune: function ( value ) {
  134. this.detune = value;
  135. if ( this.source.detune === undefined ) return; // only set detune when available
  136. if ( this.isPlaying === true ) {
  137. this.source.detune.setTargetAtTime( this.detune, this.context.currentTime, 0.01 );
  138. }
  139. return this;
  140. },
  141. getDetune: function () {
  142. return this.detune;
  143. },
  144. getFilter: function () {
  145. return this.getFilters()[ 0 ];
  146. },
  147. setFilter: function ( filter ) {
  148. return this.setFilters( filter ? [ filter ] : [] );
  149. },
  150. setPlaybackRate: function ( value ) {
  151. if ( this.hasPlaybackControl === false ) {
  152. console.warn( 'THREE.Audio: this Audio has no playback control.' );
  153. return;
  154. }
  155. this.playbackRate = value;
  156. if ( this.isPlaying === true ) {
  157. this.source.playbackRate.setTargetAtTime( this.playbackRate, this.context.currentTime, 0.01 );
  158. }
  159. return this;
  160. },
  161. getPlaybackRate: function () {
  162. return this.playbackRate;
  163. },
  164. onEnded: function () {
  165. this.isPlaying = false;
  166. },
  167. getLoop: function () {
  168. if ( this.hasPlaybackControl === false ) {
  169. console.warn( 'THREE.Audio: this Audio has no playback control.' );
  170. return false;
  171. }
  172. return this.loop;
  173. },
  174. setLoop: function ( value ) {
  175. if ( this.hasPlaybackControl === false ) {
  176. console.warn( 'THREE.Audio: this Audio has no playback control.' );
  177. return;
  178. }
  179. this.loop = value;
  180. if ( this.isPlaying === true ) {
  181. this.source.loop = this.loop;
  182. }
  183. return this;
  184. },
  185. getVolume: function () {
  186. return this.gain.gain.value;
  187. },
  188. setVolume: function ( value ) {
  189. this.gain.gain.setTargetAtTime( value, this.context.currentTime, 0.01 );
  190. return this;
  191. }
  192. } );
  193. export { Audio };