Audio.js 6.1 KB

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