| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- /**
- * @author mrdoob / http://mrdoob.com/
- */
- THREE.Audio = function ( listener ) {
- THREE.Object3D.call( this );
- this.type = 'Audio';
- this.context = listener.context;
- this.source = this.context.createBufferSource();
- this.source.onended = this.onEnded.bind( this );
- this.gain = this.context.createGain();
- this.gain.connect( listener.getOutputNode() );
- this.autoplay = false;
- this.startTime = 0;
- this.playbackRate = 1;
- this.isPlaying = false;
- this.hasPlaybackControl = true;
- this.sourceType = 'empty';
- this.filter = null;
- };
- THREE.Audio.prototype = Object.create( THREE.Object3D.prototype );
- THREE.Audio.prototype.constructor = THREE.Audio;
- THREE.Audio.prototype.getOutput = function () {
- return this.gain;
- };
- THREE.Audio.prototype.load = function ( file ) {
- var buffer = new THREE.AudioBuffer( this.context );
- buffer.load( file );
- this.setBuffer( buffer );
- return this;
- };
- THREE.Audio.prototype.setNodeSource = function ( audioNode ) {
- this.hasPlaybackControl = false;
- this.sourceType = 'audioNode';
- this.source = audioNode;
- this.connect();
- return this;
- };
- THREE.Audio.prototype.setBuffer = function ( audioBuffer ) {
- var scope = this;
- audioBuffer.onReady(function(buffer) {
- scope.source.buffer = buffer;
- scope.sourceType = 'buffer';
- if ( scope.autoplay ) scope.play();
- });
- return this;
- };
- THREE.Audio.prototype.play = function () {
- if ( this.isPlaying === true ) {
- console.warn( 'THREE.Audio: Audio is already playing.' );
- return;
- }
- if ( this.hasPlaybackControl === false ) {
- console.warn( 'THREE.Audio: this Audio has no playback control.' );
- return;
- }
- var source = this.context.createBufferSource();
- source.buffer = this.source.buffer;
- source.loop = this.source.loop;
- source.onended = this.source.onended;
- source.start( 0, this.startTime );
- source.playbackRate.value = this.playbackRate;
- this.isPlaying = true;
- this.source = source;
- this.connect();
- };
- THREE.Audio.prototype.pause = function () {
- if ( this.hasPlaybackControl === false ) {
- console.warn( 'THREE.Audio: this Audio has no playback control.' );
- return;
- }
- this.source.stop();
- this.startTime = this.context.currentTime;
- };
- THREE.Audio.prototype.stop = function () {
- if ( this.hasPlaybackControl === false ) {
- console.warn( 'THREE.Audio: this Audio has no playback control.' );
- return;
- }
- this.source.stop();
- this.startTime = 0;
- };
- THREE.Audio.prototype.connect = function () {
- if ( this.filter !== null ) {
- this.source.connect( this.filter );
- this.filter.connect( this.getOutput() );
- } else {
- this.source.connect( this.getOutput() );
- }
- };
- THREE.Audio.prototype.disconnect = function () {
- if ( this.filter !== null ) {
- this.source.disconnect( this.filter );
- this.filter.disconnect( this.getOutput() );
- } else {
- this.source.disconnect( this.getOutput() );
- }
- };
- THREE.Audio.prototype.setFilter = function ( value ) {
- if ( this.isPlaying === true ) {
- this.disconnect();
- this.filter = value;
- this.connect();
- } else {
- this.filter = value;
- }
- };
- THREE.Audio.prototype.getFilter = function () {
- return this.filter;
- };
- THREE.Audio.prototype.setPlaybackRate = function ( value ) {
- if ( this.hasPlaybackControl === false ) {
- console.warn( 'THREE.Audio: this Audio has no playback control.' );
- return;
- }
- this.playbackRate = value;
- if ( this.isPlaying === true ) {
- this.source.playbackRate.value = this.playbackRate;
- }
- };
- THREE.Audio.prototype.getPlaybackRate = function () {
- return this.playbackRate;
- };
- THREE.Audio.prototype.onEnded = function() {
- this.isPlaying = false;
- };
- THREE.Audio.prototype.setLoop = function ( value ) {
- if ( this.hasPlaybackControl === false ) {
- console.warn( 'THREE.Audio: this Audio has no playback control.' );
- return;
- }
- this.source.loop = value;
- };
- THREE.Audio.prototype.getLoop = function () {
- if ( this.hasPlaybackControl === false ) {
- console.warn( 'THREE.Audio: this Audio has no playback control.' );
- return false;
- }
- return this.source.loop;
- };
- THREE.Audio.prototype.setVolume = function ( value ) {
- this.gain.gain.value = value;
- };
- THREE.Audio.prototype.getVolume = function () {
- return this.gain.gain.value;
- };
|