Audio.js 4.2 KB

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