Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Playback modes for groups #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions buzz.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ var buzz = {
preload: 'metadata',
volume: 80
},
validPlaybackModes: [
'blend',
'chain',
'random'
],
types: {
'mp3': 'audio/mpeg',
'ogg': 'audio/ogg',
Expand Down Expand Up @@ -641,7 +646,13 @@ var buzz = {
group: function( sounds ) {
sounds = argsToArray( sounds, arguments );

var playbackMode = 'blend';

// publics
this.getPlaybackMode = function() {
return playbackMode;
};

this.getSounds = function() {
return sounds;
};
Expand Down Expand Up @@ -671,7 +682,9 @@ var buzz = {
};

this.play = function() {
fn( 'play' );
fn( 'play', {
mode: playbackMode
} );
return this;
};

Expand All @@ -685,6 +698,13 @@ var buzz = {
return this;
};

this.setPlaybackMode = function( mode ) {
if(buzz.validPlaybackModes.indexOf(mode) != -1) {
playbackMode = mode;
}
return this;
};

this.stop = function() {
fn( 'stop' );
return this;
Expand Down Expand Up @@ -778,8 +798,26 @@ var buzz = {
// privates
function fn() {
var args = argsToArray( null, arguments ),
func = args.shift();

func = args.shift(),
opt = args.shift();

if ( opt && typeof opt.mode === 'string' ) {
if ( opt.mode === 'random' ) {
var rnd = Math.floor( Math.random() * sounds.length );
sounds[ rnd ][ func ].apply( sounds[ rnd ], args );
return;
}
else if ( opt.mode === 'chain' ) {
var i = 0;
var handler = function(e) {
if ( ++i < sounds.length ) {
sounds[ i ][ func ].apply( sounds[ i ], args ).bindOnce('ended', handler);
}
};
sounds[ 0 ][ func ].apply( sounds[ 0 ], args ).bindOnce( 'ended', handler);
return;
}
}
for( var i = 0; i < sounds.length; i++ ) {
sounds[ i ][ func ].apply( sounds[ i ], args );
}
Expand Down