Skip to content

Commit

Permalink
Implement get/setVolume methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jsantell committed Aug 13, 2012
1 parent fa103bb commit b96bb99
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ All controls return `this`. If provided an audio element as the source, one can

* `play()` plays the audio and begins the dance.
* `pause()` pauses the madness.
* `setVolume()` sets the player's current volume.

### Getters

* `getVolume()` returns a normalized value (0 to 1) of the current volume.
* `getTime()` returns the current time.
* `getProgress()` returns the downloading progress as a float from 0 to 1.
* `getWaveform()` returns the waveform data array (Float32Array(1024))
Expand Down
11 changes: 11 additions & 0 deletions spec/dancer.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ describe('Dancer', function () {
expect(playReturn).toBe(dancer);
expect(pauseReturn).toBe(dancer);
});

it("The volume should default to 1", function () {
expect(dancer.getVolume()).toEqual(1);
});
it("Should change the volume with setVolume() and return `this`", function () {
expect(dancer.setVolume(0.5)).toBe(dancer);
expect(dancer.getVolume()).toBeWithin(0.5, 0.0001);
dancer.setVolume(0.1);
expect(dancer.getVolume()).toBeWithin(0.1, 0.0001);
dancer.setVolume(1);
});
});

describe('createBeat()', function () {
Expand Down
8 changes: 8 additions & 0 deletions src/adapterFlash.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,18 @@
this.isPlaying = false;
},

setVolume : function ( volume ) {
this.audio.setVolume( volume * 100 );
},

_updateProgress : function ( _this ) {
_this.progress = this.bytesLoaded / this.bytesTotal;
},

getVolume : function () {
return this.audio.volume / 100;
},

getProgress : function () {
return this.progress;
},
Expand Down
8 changes: 8 additions & 0 deletions src/adapterMoz.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,20 @@
this.isPlaying = false;
},

setVolume : function ( volume ) {
this.audio.volume = volume;
},

_updateProgress : function ( e ) {
if ( e.currentTarget.duration ) {
this.progress = e.currentTarget.seekable.end( 0 ) / e.currentTarget.duration;
}
},

getVolume : function () {
return this.audio.volume;
},

getProgress : function () {
return this.progress;
},
Expand Down
12 changes: 11 additions & 1 deletion src/adapterWebkit.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
this.proc.onaudioprocess = function ( e ) {
_this.update.call( _this, e );
};
this.gain = this.context.createGainNode();

this.fft = new FFT( SAMPLE_SIZE / 2, SAMPLE_RATE );
this.signal = new Float32Array( SAMPLE_SIZE / 2 );
Expand Down Expand Up @@ -53,12 +54,20 @@
this.isPlaying = false;
},

setVolume : function ( volume ) {
this.gain.gain.value = volume;
},

_updateProgress : function ( e ) {
if ( e.currentTarget.duration ) {
this.progress = e.currentTarget.seekable.end( 0 ) / e.currentTarget.duration;
}
},

getVolume : function () {
return this.gain.gain.value;
},

getProgress : function() {
return this.progress;
},
Expand Down Expand Up @@ -105,7 +114,8 @@
function connectContext () {
this.source = this.context.createMediaElementSource( this.audio );
this.source.connect( this.proc );
this.source.connect( this.context.destination );
this.source.connect( this.gain );
this.gain.connect( this.context.destination );
this.proc.connect( this.context.destination );

this.isLoaded = true;
Expand Down
9 changes: 9 additions & 0 deletions src/dancer.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
return this;
},

setVolume : function ( volume ) {
this.audioAdapter.setVolume( volume );
return this;
},


/* Actions */

Expand Down Expand Up @@ -86,6 +91,10 @@

/* Getters */

getVolume : function () {
return this.audioAdapter.getVolume();
},

getProgress : function () {
return this.audioAdapter.getProgress();
},
Expand Down

0 comments on commit b96bb99

Please sign in to comment.