rc-web@66: // ---------------------------------------------------------------------------- rc-web@66: // Buzz, a Javascript HTML5 Audio library rc-web@66: // v 1.0.4 beta rc-web@66: // Licensed under the MIT license. rc-web@66: // http://buzz.jaysalvat.com/ rc-web@66: // ---------------------------------------------------------------------------- rc-web@66: // Copyright (C) 2011 Jay Salvat rc-web@66: // http://jaysalvat.com/ rc-web@66: // ---------------------------------------------------------------------------- rc-web@66: // Permission is hereby granted, free of charge, to any person obtaining a copy rc-web@66: // of this software and associated documentation files ( the "Software" ), to deal rc-web@66: // in the Software without restriction, including without limitation the rights rc-web@66: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell rc-web@66: // copies of the Software, and to permit persons to whom the Software is rc-web@66: // furnished to do so, subject to the following conditions: rc-web@66: // rc-web@66: // The above copyright notice and this permission notice shall be included in rc-web@66: // all copies or substantial portions of the Software. rc-web@66: // rc-web@66: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR rc-web@66: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, rc-web@66: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE rc-web@66: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER rc-web@66: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, rc-web@66: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN rc-web@66: // THE SOFTWARE. rc-web@66: // ---------------------------------------------------------------------------- rc-web@66: rc-web@66: var buzz = { rc-web@66: defaults: { rc-web@66: autoplay: false, rc-web@66: duration: 5000, rc-web@66: formats: [], rc-web@66: loop: false, rc-web@66: placeholder: '--', rc-web@66: preload: 'metadata', rc-web@66: volume: 80 rc-web@66: }, rc-web@66: types: { rc-web@66: 'mp3': 'audio/mpeg', rc-web@66: 'ogg': 'audio/ogg', rc-web@66: 'wav': 'audio/wav', rc-web@66: 'aac': 'audio/aac', rc-web@66: 'm4a': 'audio/x-m4a' rc-web@66: }, rc-web@66: sounds: [], rc-web@66: el: document.createElement( 'audio' ), rc-web@66: rc-web@66: sound: function( src, options ) { rc-web@66: options = options || {}; rc-web@66: rc-web@66: var pid = 0, rc-web@66: events = [], rc-web@66: eventsOnce = {}, rc-web@66: supported = buzz.isSupported(); rc-web@66: rc-web@66: // publics rc-web@66: this.load = function() { rc-web@66: if ( !supported ) { rc-web@66: return this; rc-web@66: } rc-web@66: rc-web@66: this.sound.load(); rc-web@66: return this; rc-web@66: }; rc-web@66: rc-web@66: this.play = function() { rc-web@66: if ( !supported ) { rc-web@66: return this; rc-web@66: } rc-web@66: rc-web@66: this.sound.play(); rc-web@66: return this; rc-web@66: }; rc-web@66: rc-web@66: this.togglePlay = function() { rc-web@66: if ( !supported ) { rc-web@66: return this; rc-web@66: } rc-web@66: rc-web@66: if ( this.sound.paused ) { rc-web@66: this.sound.play(); rc-web@66: } else { rc-web@66: this.sound.pause(); rc-web@66: } rc-web@66: return this; rc-web@66: }; rc-web@66: rc-web@66: this.pause = function() { rc-web@66: if ( !supported ) { rc-web@66: return this; rc-web@66: } rc-web@66: rc-web@66: this.sound.pause(); rc-web@66: return this; rc-web@66: }; rc-web@66: rc-web@66: this.isPaused = function() { rc-web@66: if ( !supported ) { rc-web@66: return null; rc-web@66: } rc-web@66: rc-web@66: return this.sound.paused; rc-web@66: }; rc-web@66: rc-web@66: this.stop = function() { rc-web@66: if ( !supported ) { rc-web@66: return this; rc-web@66: } rc-web@66: rc-web@66: this.setTime( this.getDuration() ); rc-web@66: this.sound.pause(); rc-web@66: return this; rc-web@66: }; rc-web@66: rc-web@66: this.isEnded = function() { rc-web@66: if ( !supported ) { rc-web@66: return null; rc-web@66: } rc-web@66: rc-web@66: return this.sound.ended; rc-web@66: }; rc-web@66: rc-web@66: this.loop = function() { rc-web@66: if ( !supported ) { rc-web@66: return this; rc-web@66: } rc-web@66: rc-web@66: this.sound.loop = 'loop'; rc-web@66: this.bind( 'ended.buzzloop', function() { rc-web@66: this.currentTime = 0; rc-web@66: this.play(); rc-web@66: }); rc-web@66: return this; rc-web@66: }; rc-web@66: rc-web@66: this.unloop = function() { rc-web@66: if ( !supported ) { rc-web@66: return this; rc-web@66: } rc-web@66: rc-web@66: this.sound.removeAttribute( 'loop' ); rc-web@66: this.unbind( 'ended.buzzloop' ); rc-web@66: return this; rc-web@66: }; rc-web@66: rc-web@66: this.mute = function() { rc-web@66: if ( !supported ) { rc-web@66: return this; rc-web@66: } rc-web@66: rc-web@66: this.sound.muted = true; rc-web@66: return this; rc-web@66: }; rc-web@66: rc-web@66: this.unmute = function() { rc-web@66: if ( !supported ) { rc-web@66: return this; rc-web@66: } rc-web@66: rc-web@66: this.sound.muted = false; rc-web@66: return this; rc-web@66: }; rc-web@66: rc-web@66: this.toggleMute = function() { rc-web@66: if ( !supported ) { rc-web@66: return this; rc-web@66: } rc-web@66: rc-web@66: this.sound.muted = !this.sound.muted; rc-web@66: return this; rc-web@66: }; rc-web@66: rc-web@66: this.isMuted = function() { rc-web@66: if ( !supported ) { rc-web@66: return null; rc-web@66: } rc-web@66: rc-web@66: return this.sound.muted; rc-web@66: }; rc-web@66: rc-web@66: this.setVolume = function( volume ) { rc-web@66: if ( !supported ) { rc-web@66: return this; rc-web@66: } rc-web@66: rc-web@66: if ( volume < 0 ) { rc-web@66: volume = 0; rc-web@66: } rc-web@66: if ( volume > 100 ) { rc-web@66: volume = 100; rc-web@66: } rc-web@66: rc-web@66: this.volume = volume; rc-web@66: this.sound.volume = volume / 100; rc-web@66: return this; rc-web@66: }; rc-web@66: rc-web@66: this.getVolume = function() { rc-web@66: if ( !supported ) { rc-web@66: return this; rc-web@66: } rc-web@66: rc-web@66: return this.volume; rc-web@66: }; rc-web@66: rc-web@66: this.increaseVolume = function( value ) { rc-web@66: return this.setVolume( this.volume + ( value || 1 ) ); rc-web@66: }; rc-web@66: rc-web@66: this.decreaseVolume = function( value ) { rc-web@66: return this.setVolume( this.volume - ( value || 1 ) ); rc-web@66: }; rc-web@66: rc-web@66: this.setTime = function( time ) { rc-web@66: if ( !supported ) { rc-web@66: return this; rc-web@66: } rc-web@66: rc-web@66: this.whenReady( function() { rc-web@66: this.sound.currentTime = time; rc-web@66: }); rc-web@66: return this; rc-web@66: }; rc-web@66: rc-web@66: this.getTime = function() { rc-web@66: if ( !supported ) { rc-web@66: return null; rc-web@66: } rc-web@66: rc-web@66: var time = Math.round( this.sound.currentTime * 100 ) / 100; rc-web@66: return isNaN( time ) ? buzz.defaults.placeholder : time; rc-web@66: }; rc-web@66: rc-web@66: this.setPercent = function( percent ) { rc-web@66: if ( !supported ) { rc-web@66: return this; rc-web@66: } rc-web@66: rc-web@66: return this.setTime( buzz.fromPercent( percent, this.sound.duration ) ); rc-web@66: }; rc-web@66: rc-web@66: this.getPercent = function() { rc-web@66: if ( !supported ) { rc-web@66: return null; rc-web@66: } rc-web@66: rc-web@66: var percent = Math.round( buzz.toPercent( this.sound.currentTime, this.sound.duration ) ); rc-web@66: return isNaN( percent ) ? buzz.defaults.placeholder : percent; rc-web@66: }; rc-web@66: rc-web@66: this.setSpeed = function( duration ) { rc-web@66: if ( !supported ) { rc-web@66: return this; rc-web@66: } rc-web@66: rc-web@66: this.sound.playbackRate = duration; rc-web@66: }; rc-web@66: rc-web@66: this.getSpeed = function() { rc-web@66: if ( !supported ) { rc-web@66: return null; rc-web@66: } rc-web@66: rc-web@66: return this.sound.playbackRate; rc-web@66: }; rc-web@66: rc-web@66: this.getDuration = function() { rc-web@66: if ( !supported ) { rc-web@66: return null; rc-web@66: } rc-web@66: rc-web@66: var duration = Math.round( this.sound.duration * 100 ) / 100; rc-web@66: return isNaN( duration ) ? buzz.defaults.placeholder : duration; rc-web@66: }; rc-web@66: rc-web@66: this.getPlayed = function() { rc-web@66: if ( !supported ) { rc-web@66: return null; rc-web@66: } rc-web@66: rc-web@66: return timerangeToArray( this.sound.played ); rc-web@66: }; rc-web@66: rc-web@66: this.getBuffered = function() { rc-web@66: if ( !supported ) { rc-web@66: return null; rc-web@66: } rc-web@66: rc-web@66: return timerangeToArray( this.sound.buffered ); rc-web@66: }; rc-web@66: rc-web@66: this.getSeekable = function() { rc-web@66: if ( !supported ) { rc-web@66: return null; rc-web@66: } rc-web@66: rc-web@66: return timerangeToArray( this.sound.seekable ); rc-web@66: }; rc-web@66: rc-web@66: this.getErrorCode = function() { rc-web@66: if ( supported && this.sound.error ) { rc-web@66: return this.sound.error.code; rc-web@66: } rc-web@66: return 0; rc-web@66: }; rc-web@66: rc-web@66: this.getErrorMessage = function() { rc-web@66: if ( !supported ) { rc-web@66: return null; rc-web@66: } rc-web@66: rc-web@66: switch( this.getErrorCode() ) { rc-web@66: case 1: rc-web@66: return 'MEDIA_ERR_ABORTED'; rc-web@66: case 2: rc-web@66: return 'MEDIA_ERR_NETWORK'; rc-web@66: case 3: rc-web@66: return 'MEDIA_ERR_DECODE'; rc-web@66: case 4: rc-web@66: return 'MEDIA_ERR_SRC_NOT_SUPPORTED'; rc-web@66: default: rc-web@66: return null; rc-web@66: } rc-web@66: }; rc-web@66: rc-web@66: this.getStateCode = function() { rc-web@66: if ( !supported ) { rc-web@66: return null; rc-web@66: } rc-web@66: rc-web@66: return this.sound.readyState; rc-web@66: }; rc-web@66: rc-web@66: this.getStateMessage = function() { rc-web@66: if ( !supported ) { rc-web@66: return null; rc-web@66: } rc-web@66: rc-web@66: switch( this.getStateCode() ) { rc-web@66: case 0: rc-web@66: return 'HAVE_NOTHING'; rc-web@66: case 1: rc-web@66: return 'HAVE_METADATA'; rc-web@66: case 2: rc-web@66: return 'HAVE_CURRENT_DATA'; rc-web@66: case 3: rc-web@66: return 'HAVE_FUTURE_DATA'; rc-web@66: case 4: rc-web@66: return 'HAVE_ENOUGH_DATA'; rc-web@66: default: rc-web@66: return null; rc-web@66: } rc-web@66: }; rc-web@66: rc-web@66: this.getNetworkStateCode = function() { rc-web@66: if ( !supported ) { rc-web@66: return null; rc-web@66: } rc-web@66: rc-web@66: return this.sound.networkState; rc-web@66: }; rc-web@66: rc-web@66: this.getNetworkStateMessage = function() { rc-web@66: if ( !supported ) { rc-web@66: return null; rc-web@66: } rc-web@66: rc-web@66: switch( this.getNetworkStateCode() ) { rc-web@66: case 0: rc-web@66: return 'NETWORK_EMPTY'; rc-web@66: case 1: rc-web@66: return 'NETWORK_IDLE'; rc-web@66: case 2: rc-web@66: return 'NETWORK_LOADING'; rc-web@66: case 3: rc-web@66: return 'NETWORK_NO_SOURCE'; rc-web@66: default: rc-web@66: return null; rc-web@66: } rc-web@66: }; rc-web@66: rc-web@66: this.set = function( key, value ) { rc-web@66: if ( !supported ) { rc-web@66: return this; rc-web@66: } rc-web@66: rc-web@66: this.sound[ key ] = value; rc-web@66: return this; rc-web@66: }; rc-web@66: rc-web@66: this.get = function( key ) { rc-web@66: if ( !supported ) { rc-web@66: return null; rc-web@66: } rc-web@66: rc-web@66: return key ? this.sound[ key ] : this.sound; rc-web@66: }; rc-web@66: rc-web@66: this.bind = function( types, func ) { rc-web@66: if ( !supported ) { rc-web@66: return this; rc-web@66: } rc-web@66: rc-web@66: types = types.split( ' ' ); rc-web@66: rc-web@66: var that = this, rc-web@66: efunc = function( e ) { func.call( that, e ); }; rc-web@66: rc-web@66: for( var t = 0; t < types.length; t++ ) { rc-web@66: var type = types[ t ], rc-web@66: idx = type; rc-web@66: type = idx.split( '.' )[ 0 ]; rc-web@66: rc-web@66: events.push( { idx: idx, func: efunc } ); rc-web@66: this.sound.addEventListener( type, efunc, true ); rc-web@66: } rc-web@66: return this; rc-web@66: }; rc-web@66: rc-web@66: this.unbind = function( types ) { rc-web@66: if ( !supported ) { rc-web@66: return this; rc-web@66: } rc-web@66: rc-web@66: types = types.split( ' ' ); rc-web@66: rc-web@66: for( var t = 0; t < types.length; t++ ) { rc-web@66: var idx = types[ t ], rc-web@66: type = idx.split( '.' )[ 0 ]; rc-web@66: rc-web@66: for( var i = 0; i < events.length; i++ ) { rc-web@66: var namespace = events[ i ].idx.split( '.' ); rc-web@66: if ( events[ i ].idx == idx || ( namespace[ 1 ] && namespace[ 1 ] == idx.replace( '.', '' ) ) ) { rc-web@66: this.sound.removeEventListener( type, events[ i ].func, true ); rc-web@66: delete events[ i ]; rc-web@66: } rc-web@66: } rc-web@66: } rc-web@66: return this; rc-web@66: }; rc-web@66: rc-web@66: this.bindOnce = function( type, func ) { rc-web@66: if ( !supported ) { rc-web@66: return this; rc-web@66: } rc-web@66: rc-web@66: var that = this; rc-web@66: rc-web@66: eventsOnce[ pid++ ] = false; rc-web@66: this.bind( pid + type, function() { rc-web@66: if ( !eventsOnce[ pid ] ) { rc-web@66: eventsOnce[ pid ] = true; rc-web@66: func.call( that ); rc-web@66: } rc-web@66: that.unbind( pid + type ); rc-web@66: }); rc-web@66: }; rc-web@66: rc-web@66: this.trigger = function( types ) { rc-web@66: if ( !supported ) { rc-web@66: return this; rc-web@66: } rc-web@66: rc-web@66: types = types.split( ' ' ); rc-web@66: rc-web@66: for( var t = 0; t < types.length; t++ ) { rc-web@66: var idx = types[ t ]; rc-web@66: rc-web@66: for( var i = 0; i < events.length; i++ ) { rc-web@66: var eventType = events[ i ].idx.split( '.' ); rc-web@66: if ( events[ i ].idx == idx || ( eventType[ 0 ] && eventType[ 0 ] == idx.replace( '.', '' ) ) ) { rc-web@66: var evt = document.createEvent('HTMLEvents'); rc-web@66: evt.initEvent( eventType[ 0 ], false, true ); rc-web@66: this.sound.dispatchEvent( evt ); rc-web@66: } rc-web@66: } rc-web@66: } rc-web@66: return this; rc-web@66: }; rc-web@66: rc-web@66: this.fadeTo = function( to, duration, callback ) { rc-web@66: if ( !supported ) { rc-web@66: return this; rc-web@66: } rc-web@66: rc-web@66: if ( duration instanceof Function ) { rc-web@66: callback = duration; rc-web@66: duration = buzz.defaults.duration; rc-web@66: } else { rc-web@66: duration = duration || buzz.defaults.duration; rc-web@66: } rc-web@66: rc-web@66: var from = this.volume, rc-web@66: delay = duration / Math.abs( from - to ), rc-web@66: that = this; rc-web@66: this.play(); rc-web@66: rc-web@66: function doFade() { rc-web@66: setTimeout( function() { rc-web@66: if ( from < to && that.volume < to ) { rc-web@66: that.setVolume( that.volume += 1 ); rc-web@66: doFade(); rc-web@66: } else if ( from > to && that.volume > to ) { rc-web@66: that.setVolume( that.volume -= 1 ); rc-web@66: doFade(); rc-web@66: } else if ( callback instanceof Function ) { rc-web@66: callback.apply( that ); rc-web@66: } rc-web@66: }, delay ); rc-web@66: } rc-web@66: this.whenReady( function() { rc-web@66: doFade(); rc-web@66: }); rc-web@66: rc-web@66: return this; rc-web@66: }; rc-web@66: rc-web@66: this.fadeIn = function( duration, callback ) { rc-web@66: if ( !supported ) { rc-web@66: return this; rc-web@66: } rc-web@66: rc-web@66: return this.setVolume(0).fadeTo( 100, duration, callback ); rc-web@66: }; rc-web@66: rc-web@66: this.fadeOut = function( duration, callback ) { rc-web@66: if ( !supported ) { rc-web@66: return this; rc-web@66: } rc-web@66: rc-web@66: return this.fadeTo( 0, duration, callback ); rc-web@66: }; rc-web@66: rc-web@66: this.fadeWith = function( sound, duration ) { rc-web@66: if ( !supported ) { rc-web@66: return this; rc-web@66: } rc-web@66: rc-web@66: this.fadeOut( duration, function() { rc-web@66: this.stop(); rc-web@66: }); rc-web@66: rc-web@66: sound.play().fadeIn( duration ); rc-web@66: rc-web@66: return this; rc-web@66: }; rc-web@66: rc-web@66: this.whenReady = function( func ) { rc-web@66: if ( !supported ) { rc-web@66: return null; rc-web@66: } rc-web@66: rc-web@66: var that = this; rc-web@66: if ( this.sound.readyState === 0 ) { rc-web@66: this.bind( 'canplay.buzzwhenready', function() { rc-web@66: func.call( that ); rc-web@66: }); rc-web@66: } else { rc-web@66: func.call( that ); rc-web@66: } rc-web@66: }; rc-web@66: rc-web@66: // privates rc-web@66: function timerangeToArray( timeRange ) { rc-web@66: var array = [], rc-web@66: length = timeRange.length - 1; rc-web@66: rc-web@66: for( var i = 0; i <= length; i++ ) { rc-web@66: array.push({ rc-web@66: start: timeRange.start( length ), rc-web@66: end: timeRange.end( length ) rc-web@66: }); rc-web@66: } rc-web@66: return array; rc-web@66: } rc-web@66: rc-web@66: function getExt( filename ) { rc-web@66: return filename.split('.').pop(); rc-web@66: } rc-web@66: rc-web@66: function addSource( sound, src ) { rc-web@66: var source = document.createElement( 'source' ); rc-web@66: source.src = src; rc-web@66: if ( buzz.types[ getExt( src ) ] ) { rc-web@66: source.type = buzz.types[ getExt( src ) ]; rc-web@66: } rc-web@66: sound.appendChild( source ); rc-web@66: } rc-web@66: rc-web@66: // init rc-web@66: if ( supported ) { rc-web@66: rc-web@66: for(var i in buzz.defaults ) { rc-web@66: if(buzz.defaults.hasOwnProperty(i)) { rc-web@66: options[ i ] = options[ i ] || buzz.defaults[ i ]; rc-web@66: } rc-web@66: } rc-web@66: rc-web@66: this.sound = document.createElement( 'audio' ); rc-web@66: rc-web@66: if ( src instanceof Array ) { rc-web@66: for( var j in src ) { rc-web@66: if(src.hasOwnProperty(j)) { rc-web@66: addSource( this.sound, src[ j ] ); rc-web@66: } rc-web@66: } rc-web@66: } else if ( options.formats.length ) { rc-web@66: for( var k in options.formats ) { rc-web@66: if(options.formats.hasOwnProperty(k)) { rc-web@66: addSource( this.sound, src + '.' + options.formats[ k ] ); rc-web@66: } rc-web@66: } rc-web@66: } else { rc-web@66: addSource( this.sound, src ); rc-web@66: } rc-web@66: rc-web@66: if ( options.loop ) { rc-web@66: this.loop(); rc-web@66: } rc-web@66: rc-web@66: if ( options.autoplay ) { rc-web@66: this.sound.autoplay = 'autoplay'; rc-web@66: } rc-web@66: rc-web@66: if ( options.preload === true ) { rc-web@66: this.sound.preload = 'auto'; rc-web@66: } else if ( options.preload === false ) { rc-web@66: this.sound.preload = 'none'; rc-web@66: } else { rc-web@66: this.sound.preload = options.preload; rc-web@66: } rc-web@66: rc-web@66: this.setVolume( options.volume ); rc-web@66: rc-web@66: buzz.sounds.push( this ); rc-web@66: } rc-web@66: }, rc-web@66: rc-web@66: group: function( sounds ) { rc-web@66: sounds = argsToArray( sounds, arguments ); rc-web@66: rc-web@66: // publics rc-web@66: this.getSounds = function() { rc-web@66: return sounds; rc-web@66: }; rc-web@66: rc-web@66: this.add = function( soundArray ) { rc-web@66: soundArray = argsToArray( soundArray, arguments ); rc-web@66: for( var a = 0; a < soundArray.length; a++ ) { rc-web@66: sounds.push( soundArray[ a ] ); rc-web@66: } rc-web@66: }; rc-web@66: rc-web@66: this.remove = function( soundArray ) { rc-web@66: soundArray = argsToArray( soundArray, arguments ); rc-web@66: for( var a = 0; a < soundArray.length; a++ ) { rc-web@66: for( var i = 0; i < sounds.length; i++ ) { rc-web@66: if ( sounds[ i ] == soundArray[ a ] ) { rc-web@66: delete sounds[ i ]; rc-web@66: break; rc-web@66: } rc-web@66: } rc-web@66: } rc-web@66: }; rc-web@66: rc-web@66: this.load = function() { rc-web@66: fn( 'load' ); rc-web@66: return this; rc-web@66: }; rc-web@66: rc-web@66: this.play = function() { rc-web@66: fn( 'play' ); rc-web@66: return this; rc-web@66: }; rc-web@66: rc-web@66: this.togglePlay = function( ) { rc-web@66: fn( 'togglePlay' ); rc-web@66: return this; rc-web@66: }; rc-web@66: rc-web@66: this.pause = function( time ) { rc-web@66: fn( 'pause', time ); rc-web@66: return this; rc-web@66: }; rc-web@66: rc-web@66: this.stop = function() { rc-web@66: fn( 'stop' ); rc-web@66: return this; rc-web@66: }; rc-web@66: rc-web@66: this.mute = function() { rc-web@66: fn( 'mute' ); rc-web@66: return this; rc-web@66: }; rc-web@66: rc-web@66: this.unmute = function() { rc-web@66: fn( 'unmute' ); rc-web@66: return this; rc-web@66: }; rc-web@66: rc-web@66: this.toggleMute = function() { rc-web@66: fn( 'toggleMute' ); rc-web@66: return this; rc-web@66: }; rc-web@66: rc-web@66: this.setVolume = function( volume ) { rc-web@66: fn( 'setVolume', volume ); rc-web@66: return this; rc-web@66: }; rc-web@66: rc-web@66: this.increaseVolume = function( value ) { rc-web@66: fn( 'increaseVolume', value ); rc-web@66: return this; rc-web@66: }; rc-web@66: rc-web@66: this.decreaseVolume = function( value ) { rc-web@66: fn( 'decreaseVolume', value ); rc-web@66: return this; rc-web@66: }; rc-web@66: rc-web@66: this.loop = function() { rc-web@66: fn( 'loop' ); rc-web@66: return this; rc-web@66: }; rc-web@66: rc-web@66: this.unloop = function() { rc-web@66: fn( 'unloop' ); rc-web@66: return this; rc-web@66: }; rc-web@66: rc-web@66: this.setTime = function( time ) { rc-web@66: fn( 'setTime', time ); rc-web@66: return this; rc-web@66: }; rc-web@66: rc-web@66: this.setduration = function( duration ) { rc-web@66: fn( 'setduration', duration ); rc-web@66: return this; rc-web@66: }; rc-web@66: rc-web@66: this.set = function( key, value ) { rc-web@66: fn( 'set', key, value ); rc-web@66: return this; rc-web@66: }; rc-web@66: rc-web@66: this.bind = function( type, func ) { rc-web@66: fn( 'bind', type, func ); rc-web@66: return this; rc-web@66: }; rc-web@66: rc-web@66: this.unbind = function( type ) { rc-web@66: fn( 'unbind', type ); rc-web@66: return this; rc-web@66: }; rc-web@66: rc-web@66: this.bindOnce = function( type, func ) { rc-web@66: fn( 'bindOnce', type, func ); rc-web@66: return this; rc-web@66: }; rc-web@66: rc-web@66: this.trigger = function( type ) { rc-web@66: fn( 'trigger', type ); rc-web@66: return this; rc-web@66: }; rc-web@66: rc-web@66: this.fade = function( from, to, duration, callback ) { rc-web@66: fn( 'fade', from, to, duration, callback ); rc-web@66: return this; rc-web@66: }; rc-web@66: rc-web@66: this.fadeIn = function( duration, callback ) { rc-web@66: fn( 'fadeIn', duration, callback ); rc-web@66: return this; rc-web@66: }; rc-web@66: rc-web@66: this.fadeOut = function( duration, callback ) { rc-web@66: fn( 'fadeOut', duration, callback ); rc-web@66: return this; rc-web@66: }; rc-web@66: rc-web@66: // privates rc-web@66: function fn() { rc-web@66: var args = argsToArray( null, arguments ), rc-web@66: func = args.shift(); rc-web@66: rc-web@66: for( var i = 0; i < sounds.length; i++ ) { rc-web@66: sounds[ i ][ func ].apply( sounds[ i ], args ); rc-web@66: } rc-web@66: } rc-web@66: rc-web@66: function argsToArray( array, args ) { rc-web@66: return ( array instanceof Array ) ? array : Array.prototype.slice.call( args ); rc-web@66: } rc-web@66: }, rc-web@66: rc-web@66: all: function() { rc-web@66: return new buzz.group( buzz.sounds ); rc-web@66: }, rc-web@66: rc-web@66: isSupported: function() { rc-web@66: return !!buzz.el.canPlayType; rc-web@66: }, rc-web@66: rc-web@66: isOGGSupported: function() { rc-web@66: return !!buzz.el.canPlayType && buzz.el.canPlayType( 'audio/ogg; codecs="vorbis"' ); rc-web@66: }, rc-web@66: rc-web@66: isWAVSupported: function() { rc-web@66: return !!buzz.el.canPlayType && buzz.el.canPlayType( 'audio/wav; codecs="1"' ); rc-web@66: }, rc-web@66: rc-web@66: isMP3Supported: function() { rc-web@66: return !!buzz.el.canPlayType && buzz.el.canPlayType( 'audio/mpeg;' ); rc-web@66: }, rc-web@66: rc-web@66: isAACSupported: function() { rc-web@66: return !!buzz.el.canPlayType && ( buzz.el.canPlayType( 'audio/x-m4a;' ) || buzz.el.canPlayType( 'audio/aac;' ) ); rc-web@66: }, rc-web@66: rc-web@66: toTimer: function( time, withHours ) { rc-web@66: var h, m, s; rc-web@66: h = Math.floor( time / 3600 ); rc-web@66: h = isNaN( h ) ? '--' : ( h >= 10 ) ? h : '0' + h; rc-web@66: m = withHours ? Math.floor( time / 60 % 60 ) : Math.floor( time / 60 ); rc-web@66: m = isNaN( m ) ? '--' : ( m >= 10 ) ? m : '0' + m; rc-web@66: s = Math.floor( time % 60 ); rc-web@66: s = isNaN( s ) ? '--' : ( s >= 10 ) ? s : '0' + s; rc-web@66: return withHours ? h + ':' + m + ':' + s : m + ':' + s; rc-web@66: }, rc-web@66: rc-web@66: fromTimer: function( time ) { rc-web@66: var splits = time.toString().split( ':' ); rc-web@66: if ( splits && splits.length == 3 ) { rc-web@66: time = ( parseInt( splits[ 0 ], 10 ) * 3600 ) + ( parseInt(splits[ 1 ], 10 ) * 60 ) + parseInt( splits[ 2 ], 10 ); rc-web@66: } rc-web@66: if ( splits && splits.length == 2 ) { rc-web@66: time = ( parseInt( splits[ 0 ], 10 ) * 60 ) + parseInt( splits[ 1 ], 10 ); rc-web@66: } rc-web@66: return time; rc-web@66: }, rc-web@66: rc-web@66: toPercent: function( value, total, decimal ) { rc-web@66: var r = Math.pow( 10, decimal || 0 ); rc-web@66: rc-web@66: return Math.round( ( ( value * 100 ) / total ) * r ) / r; rc-web@66: }, rc-web@66: rc-web@66: fromPercent: function( percent, total, decimal ) { rc-web@66: var r = Math.pow( 10, decimal || 0 ); rc-web@66: rc-web@66: return Math.round( ( ( total / 100 ) * percent ) * r ) / r; rc-web@66: } rc-web@66: };