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