From f952d6a2feb3ccabe62cf439c7a5cb2f0f8671a2 Mon Sep 17 00:00:00 2001 From: Alberto Congiu <4lbertoc@gmail.com> Date: Tue, 1 Jan 2013 18:40:40 +0100 Subject: [PATCH] Added playback modes for groups: blend (normal), random and chain --- buzz.js | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/buzz.js b/buzz.js index 7aad28d..7ee2a6c 100644 --- a/buzz.js +++ b/buzz.js @@ -36,6 +36,11 @@ var buzz = { preload: 'metadata', volume: 80 }, + validPlaybackModes: [ + 'blend', + 'chain', + 'random' + ], types: { 'mp3': 'audio/mpeg', 'ogg': 'audio/ogg', @@ -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; }; @@ -671,7 +682,9 @@ var buzz = { }; this.play = function() { - fn( 'play' ); + fn( 'play', { + mode: playbackMode + } ); return this; }; @@ -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; @@ -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 ); }