Audio.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. THREE.Audio = function ( listener ) {
  5. THREE.Object3D.call( this );
  6. this.type = 'Audio';
  7. this.context = listener.context;
  8. this.source = this.context.createBufferSource();
  9. this.source.onended = this.onEnded.bind( this );
  10. this.gain = this.context.createGain();
  11. this.gain.connect( listener.getOutputNode() );
  12. this.autoplay = false;
  13. this.startTime = 0;
  14. this.playbackRate = 1;
  15. this.isPlaying = false;
  16. this.hasPlaybackControl = true;
  17. this.sourceType = 'empty';
  18. this.filter = null;
  19. };
  20. THREE.Audio.prototype = Object.create( THREE.Object3D.prototype );
  21. THREE.Audio.prototype.constructor = THREE.Audio;
  22. THREE.Audio.prototype.getOutput = function () {
  23. return this.gain;
  24. };
  25. THREE.Audio.prototype.load = function ( file ) {
  26. var buffer = new THREE.AudioBuffer( this.context );
  27. buffer.load( file );
  28. this.setBuffer( buffer );
  29. return this;
  30. };
  31. THREE.Audio.prototype.setNodeSource = function ( audioNode ) {
  32. this.hasPlaybackControl = false;
  33. this.sourceType = 'audioNode';
  34. this.source = audioNode;
  35. this.connect();
  36. return this;
  37. };
  38. THREE.Audio.prototype.setBuffer = function ( audioBuffer ) {
  39. var scope = this;
  40. audioBuffer.onReady(function(buffer) {
  41. scope.source.buffer = buffer;
  42. scope.sourceType = 'buffer';
  43. if ( scope.autoplay ) scope.play();
  44. });
  45. return this;
  46. };
  47. THREE.Audio.prototype.play = function () {
  48. if ( this.isPlaying === true ) {
  49. console.warn( 'THREE.Audio: Audio is already playing.' );
  50. return;
  51. }
  52. if ( this.hasPlaybackControl === false ) {
  53. console.warn( 'THREE.Audio: this Audio has no playback control.' );
  54. return;
  55. }
  56. var source = this.context.createBufferSource();
  57. source.buffer = this.source.buffer;
  58. source.loop = this.source.loop;
  59. source.onended = this.source.onended;
  60. source.start( 0, this.startTime );
  61. source.playbackRate.value = this.playbackRate;
  62. this.isPlaying = true;
  63. this.source = source;
  64. this.connect();
  65. };
  66. THREE.Audio.prototype.pause = function () {
  67. if ( this.hasPlaybackControl === false ) {
  68. console.warn( 'THREE.Audio: this Audio has no playback control.' );
  69. return;
  70. }
  71. this.source.stop();
  72. this.startTime = this.context.currentTime;
  73. };
  74. THREE.Audio.prototype.stop = function () {
  75. if ( this.hasPlaybackControl === false ) {
  76. console.warn( 'THREE.Audio: this Audio has no playback control.' );
  77. return;
  78. }
  79. this.source.stop();
  80. this.startTime = 0;
  81. };
  82. THREE.Audio.prototype.connect = function () {
  83. if ( this.filter !== null ) {
  84. this.source.connect( this.filter );
  85. this.filter.connect( this.getOutput() );
  86. } else {
  87. this.source.connect( this.getOutput() );
  88. }
  89. };
  90. THREE.Audio.prototype.disconnect = function () {
  91. if ( this.filter !== null ) {
  92. this.source.disconnect( this.filter );
  93. this.filter.disconnect( this.getOutput() );
  94. } else {
  95. this.source.disconnect( this.getOutput() );
  96. }
  97. };
  98. THREE.Audio.prototype.setFilter = function ( value ) {
  99. if ( this.isPlaying === true ) {
  100. this.disconnect();
  101. this.filter = value;
  102. this.connect();
  103. } else {
  104. this.filter = value;
  105. }
  106. };
  107. THREE.Audio.prototype.getFilter = function () {
  108. return this.filter;
  109. };
  110. THREE.Audio.prototype.setPlaybackRate = function ( value ) {
  111. if ( this.hasPlaybackControl === false ) {
  112. console.warn( 'THREE.Audio: this Audio has no playback control.' );
  113. return;
  114. }
  115. this.playbackRate = value;
  116. if ( this.isPlaying === true ) {
  117. this.source.playbackRate.value = this.playbackRate;
  118. }
  119. };
  120. THREE.Audio.prototype.getPlaybackRate = function () {
  121. return this.playbackRate;
  122. };
  123. THREE.Audio.prototype.onEnded = function() {
  124. this.isPlaying = false;
  125. };
  126. THREE.Audio.prototype.setLoop = function ( value ) {
  127. if ( this.hasPlaybackControl === false ) {
  128. console.warn( 'THREE.Audio: this Audio has no playback control.' );
  129. return;
  130. }
  131. this.source.loop = value;
  132. };
  133. THREE.Audio.prototype.getLoop = function () {
  134. if ( this.hasPlaybackControl === false ) {
  135. console.warn( 'THREE.Audio: this Audio has no playback control.' );
  136. return false;
  137. }
  138. return this.source.loop;
  139. };
  140. THREE.Audio.prototype.setVolume = function ( value ) {
  141. this.gain.gain.value = value;
  142. };
  143. THREE.Audio.prototype.getVolume = function () {
  144. return this.gain.gain.value;
  145. };
粤ICP备19079148号