├── .gitignore ├── LICENSE ├── README.md ├── assets ├── AlphaDance.mp3 └── AlphaDance.ogg ├── build ├── pixi-audio.js └── pixi-audio.js.map ├── index.html ├── package.json ├── src ├── Audio.js ├── AudioManager.js ├── audioParser.js ├── index.js └── utils.js └── webpack.config.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | npm-debug.log 3 | .DS_STORE 4 | .idea 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Celsius online 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | pixi-audio 2 | ====================== 3 | 4 | pixi-audio is a plugin for Pixi.js v3.0.8 or higher to add Audio support using the pixi resource-loader. This plugin use WebAudio by default , and fallbacks to HTMLAudio if it's needed. 5 | 6 | ## Installation 7 | ``` 8 | npm install pixi-audio 9 | ``` 10 | 11 | ## Usage 12 | ### Browserify - webpack 13 | if you use Browserify or Webpack you can use pixi-audio like this: 14 | 15 | ```js 16 | var PIXI = require('pixi'); 17 | var audio = require('pixi-audio'); //pixi-audio is added automatically to the PIXI namespace 18 | ``` 19 | 20 | ### Prebuilt files 21 | Just add [pixi-audio.js](https://raw.githubusercontent.com/Nazariglez/pixi-audio/master/build/pixi-audio.js) under `pixi.js` in your HTML file. 22 | 23 | ### Loading and using Audio 24 | To load audio files you need to use the resource-loader built in pixi. 25 | 26 | ```js 27 | PIXI.loader.add([ 28 | {name:"AwesomeMusic", url:"./assets/AwesomeMusic.ogg"} 29 | ]).load(function(){ 30 | var awesomeMusic = PIXI.audioManager.getAudio('AwesomeMusic'); 31 | awesomeMusic.play(); 32 | }); 33 | ``` 34 | 35 | ### Audio formats support 36 | This lib support mp3, ogg, wav and m4a formats. Unfortunately all [browsers doesn't support all this Audio formats](http://www.w3schools.com/html/html5_audio.asp), to solve this you can pass as an array some urls with different formats for the same file. So, the loader can load the correct format depending of the browser: 37 | 38 | ```js 39 | var files = ["./assets/AwesomeMusic.ogg", "./assets/AwesomeMusic.mp3"]; 40 | 41 | PIXI.loader.add([ 42 | {name:"AwesomeMusic", url: files} 43 | ]).load(function(){ 44 | var awesomeMusic = PIXI.audioManager.getAudio('AwesomeMusic'); 45 | awesomeMusic.play(); 46 | }); 47 | ``` 48 | 49 | In the above example, the audio loaded in chrome will be the __ogg__ and in safari will be the __mp3__. But, you don't need care about this, just need to call getAudio using the audio name as param. 50 | 51 | ### How it works 52 | This plugin add a new namespace named `audio` to the PIXI namespace. This namespace has some audio utils under the name `utils`, the `audioParser` for the resource-loader, and two new classes: `Audio` and `AudioManager`. Also, an instance of `AudioManager` is added to `PIXI` with the `audioManager` name. So, you don't need do anything, just use PIXI.audioManager if you want. 53 | 54 | It's important to know when you call `PIXI.audioManager.getAudio('myAudio')` a new instance is created, so if you try again to get the same audio, it will be the same sound, but in a different instance. 55 | 56 | ### Events 57 | The Audio class extends from [PIXI.utils.EventEmitter](https://github.com/primus/eventemitter3), and emit some events: play, stop, end, pause and resume. This events name are so clear about what they doing. More info: [Node.js Events](https://nodejs.org/api/events.html#events_emitter_emit_event_arg1_arg2) 58 | 59 | ### Some examples 60 | Listening the basic events 61 | ```js 62 | var myAudio = PIXI.audioManager.getAudio('myAudio'); 63 | myAudio.on('start', function(){ console.log('Start!!'); }); 64 | myAudio.on('end', function(){ console.log('End!!'); }); 65 | 66 | myAudio.play(); 67 | ``` 68 | 69 | Adding custom categories to our audios. 70 | ```js 71 | var myFX = PIXI.audioManager.getAudio('myAudio1'); 72 | myFX.fx = true; 73 | var myFX2 = PIXI.audioManager.getAudio('myAudio2'); 74 | myFX2.fx = true; 75 | 76 | //filter 77 | var myFXAudios = PIXI.audioManager.filterAudios('fx'); 78 | for(var i = 0; i < myFXAudios.length; i++)myFXAudios[i].stop(); 79 | ``` 80 | 81 | ## api 82 | ### AudioManager 83 | #### constructor() 84 | The constructor 85 | #### .getAudio( name ) 86 | Return a new instance of your sound (previously loaded). 87 | #### .removeAudio( audio ) 88 | Remove the audio (instance) from the .sounds array. 89 | #### .filterAudios( tag [, value] ) 90 | Return an array with all the audios matched with the tag and value passed. 91 | #### .mute() 92 | Mute all the sounds in the sounds array. 93 | #### .unmute() 94 | Unmute all the sounds in the sounds array. 95 | #### .pause() 96 | Pause all the sounds in the sounds array. 97 | #### .resume() 98 | Resume all the sounds paused in the sounds array. 99 | 100 | ### Audio 101 | #### constructor( data, manager ) 102 | The constructor, usually you don't create an Audio, just call audioManager.getAudio(name). 103 | #### .manager 104 | The AudioManager instance who manage this audio. 105 | #### .data 106 | The audio source 107 | #### .playing 108 | Read only, it's the state of this sound. 109 | #### .paused 110 | Set true to pause the sound and false to resume. 111 | #### .loop 112 | Set true if you want to play the sound in a loop. 113 | #### .volume 114 | The sound volume. 115 | #### .muted 116 | Set true if you want to mute the sound, or false if you want unmute. 117 | #### .play() 118 | Start playing the audio. 119 | #### .stop() 120 | Stop playing the audio. 121 | #### .remove() 122 | Remove this audio instance from the AudioManager 123 | -------------------------------------------------------------------------------- /assets/AlphaDance.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Nazariglez/pixi-audio/7f6c42a303b15f29246c89b3d3984529b742e02f/assets/AlphaDance.mp3 -------------------------------------------------------------------------------- /assets/AlphaDance.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Nazariglez/pixi-audio/7f6c42a303b15f29246c89b3d3984529b742e02f/assets/AlphaDance.ogg -------------------------------------------------------------------------------- /build/pixi-audio.js: -------------------------------------------------------------------------------- 1 | !function(e){function t(o){if(i[o])return i[o].exports;var u=i[o]={exports:{},id:o,loaded:!1};return e[o].call(u.exports,u,u.exports,t),u.loaded=!0,u.exports}var i={};return t.m=e,t.c=i,t.p="",t(0)}([function(e,t,i){e.exports=i(6)},function(e,t,i){"use strict";function o(e){s?u.loaders.Resource.setExtensionXhrType(e,u.loaders.Resource.XHR_RESPONSE_TYPE.BUFFER):u.loaders.Resource.setExtensionLoadType(e,u.loaders.Resource.LOAD_TYPE.AUDIO)}Object.defineProperty(t,"__esModule",{value:!0});var u=i(2),a=!!window.Audio,n=window.AudioContext||window.webkitAudioContext,s=!!n,r=s||a,d=!1,l=!1,f=!1,p=!1,c=null,h=s?new n:null;if(r){var m=document.createElement("audio");d=""!==m.canPlayType("audio/mpeg;"),l=""!==m.canPlayType('audio/ogg; codecs="vorbis"'),f=""!==m.canPlayType("audio/wav"),p=""!==m.canPlayType('audio/mp4; codecs="mp4a.40.5"'),d&&o("mp3"),l&&o("ogg"),f&&o("wav"),p&&o("m4a"),s&&(c=function(e){return e.createGain?e.createGain():e.createGainNode()})}t["default"]={isHTMLAudioSupported:a,webAudioContext:n,isWebAudioSupported:s,isAudioSupported:r,isMp3Supported:d,isOggSupported:l,isWavSupported:f,isM4aSupported:p,globalWebAudioContext:h,createGainNode:c}},function(e,t){e.exports=PIXI},function(e,t,i){"use strict";function o(e){return e&&e.__esModule?e:{"default":e}}function u(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var a=function(){function e(e,t){for(var i=0;ia;a++)(u&&this.sounds[a][e]||!u&&this.sounds[a][e]===t)&&i.push(this.sounds[a]);return i}},{key:"mute",value:function(e){e=e!==!1;for(var t=this.sounds.length,i=0;t>i;i++)this.sounds[i].muted=e}},{key:"unmute",value:function(){return this.mute(!1)}},{key:"pause",value:function(e){e=e!==!1;for(var t=this.sounds.length,i=0;t>i;i++)this.sounds[i].paused=e}},{key:"resume",value:function(){return this.pause(!1)}}]),e}();l.audios={},t["default"]=l},function(e,t,i){"use strict";function o(e){return e&&e.__esModule?e:{"default":e}}function u(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function a(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function n(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}var s=function(){function e(e,t){for(var i=0;i 2 | 3 | 4 | 5 | Pixi.js Test 6 | 7 | 8 | 17 | 18 | 19 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "pixi-audio", 3 | "version": "0.1.0", 4 | "description": "pixi-audio is a plugin for Pixi.js v3.0.8 or higher to create ...", 5 | "main": "./build/pixi-audio.js", 6 | "author": "Nazariglez (http://www.nazariglez.com)", 7 | "license": "MIT", 8 | "keywords": [ 9 | "pixi", 10 | "audiomanager", 11 | "audio", 12 | "music", 13 | "sounds", 14 | "mp3", 15 | "ogg", 16 | "m4a", 17 | "wav" 18 | ], 19 | "homepage": "https://github.com/Nazariglez/pixi-audio", 20 | "repository": { 21 | "type": "git", 22 | "url": "https://github.com/Nazariglez/pixi-audio.git" 23 | }, 24 | "scripts": { 25 | "prepublish": "npm run build", 26 | "dev": "webpack --config webpack.config.js --progress --inline --colors --watch", 27 | "build": "NODE_ENV=production webpack --config webpack.config.js --progress --colors -p" 28 | }, 29 | "devDependencies": { 30 | "babel-core": "^6.3.17", 31 | "babel-loader": "^6.2.0", 32 | "babel-preset-es2015": "^6.3.13", 33 | "babel-preset-stage-0": "^6.3.13", 34 | "babel-runtime": "^6.3.13", 35 | "brfs": "^1.4.1", 36 | "json-loader": "^0.5.4", 37 | "pixi.js": "^3.0.8", 38 | "transform-loader": "^0.2.3", 39 | "webpack": "^1.12.9" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/Audio.js: -------------------------------------------------------------------------------- 1 | import 'pixi.js'; 2 | import utils from './utils'; 3 | 4 | export default class Audio extends PIXI.utils.EventEmitter{ 5 | _loop = false; 6 | _paused = false; 7 | _muted = false; 8 | _volume = 1; 9 | 10 | _startTime = 0; 11 | _lastPauseTime = 0; 12 | _offsetTime = 0; 13 | 14 | playing = false; 15 | 16 | constructor(data, manager){ 17 | super(); 18 | this.manager = manager; 19 | this.data = data; 20 | 21 | if(!utils.isWebAudioSupported){ 22 | this.audio = new window.Audio(); 23 | this.audio.addEventListener('ended', this._onEnd.bind(this)); 24 | } 25 | } 26 | 27 | play(pause){ 28 | if((!pause && this.paused) || (!pause && this.playing)) return this; 29 | this.playing = true; 30 | this.emit('play'); 31 | 32 | if(utils.isWebAudioSupported){ 33 | this.audio = this.manager.context.createBufferSource(); 34 | this.audio.start = this.audio.start || this.audio.noteOn; 35 | this.audio.stop = this.audio.stop || this.audio.noteOff; 36 | 37 | this.audio.buffer = this.data; 38 | this.audio.loop = this.loop; 39 | this._startTime = this.manager.context.currentTime; 40 | 41 | this.audio.onended = this._onEnd.bind(this); 42 | this.audio.gainNode = utils.createGainNode(this.manager.context); 43 | this.audio.gainNode.gain.value = this.muted ? 0 : this.volume; 44 | this.audio.gainNode.connect(this.manager.gainNode); 45 | 46 | this.audio.connect(this.audio.gainNode); 47 | this.audio.start(0, pause ? this._lastPauseTime : null); 48 | }else{ 49 | this.audio.src = this.data.children[0].src; 50 | this.audio.preload = "auto"; 51 | this.audio.volume = this.muted ? 0 : this.volume; 52 | this.audio.load(); 53 | this.audio.play(); 54 | } 55 | 56 | return this; 57 | } 58 | 59 | stop(){ 60 | if(!this.playing)return this; 61 | 62 | if(utils.isWebAudioSupported){ 63 | this.audio.stop(0); 64 | }else{ 65 | this.audio.pause(); 66 | this.audio.currentTime = 0; 67 | } 68 | this.playing = false; 69 | this.emit('stop'); 70 | 71 | return this; 72 | } 73 | 74 | reset(){ 75 | this._startTime = 0; 76 | this._lastPauseTime = 0; 77 | this._offsetTime = 0; 78 | 79 | this.playing = false; 80 | //if(utils.isWebAudioSupported)this.audio = null; 81 | } 82 | 83 | remove(){ 84 | this.manager.removeAudio(this); 85 | } 86 | 87 | _onEnd(){ 88 | if(!utils.isWebAudioSupported){ 89 | if(this.loop){ 90 | this.audio.currentTime = 0; 91 | this.audio.play(); 92 | }else{ 93 | this.reset(); 94 | this.emit('end'); 95 | } 96 | }else{ 97 | if(!this.paused){ 98 | this.reset(); 99 | this.emit('end'); 100 | } 101 | } 102 | } 103 | 104 | get paused(){return this._paused} 105 | set paused(value){ 106 | if(value === this._paused)return; 107 | if(value){ 108 | if(utils.isWebAudioSupported){ 109 | this._offsetTime += this.manager.context.currentTime - this._startTime; 110 | this._lastPauseTime = this._offsetTime%this.audio.buffer.duration; 111 | if(this.audio)this.audio.stop(0); 112 | }else{ 113 | if(this.audio)this.audio.pause(); 114 | } 115 | this.emit('pause'); 116 | }else{ 117 | if(utils.isWebAudioSupported){ 118 | this.play(true); 119 | }else{ 120 | if(this.audio)this.audio.play(); 121 | } 122 | this.emit('resume'); 123 | } 124 | this._paused = value; 125 | } 126 | 127 | get loop(){return this._loop} 128 | set loop(value){ 129 | if(value === this._loop)return; 130 | this._loop = value; 131 | if(utils.isWebAudioSupported&&this.audio){ 132 | this.audio.loop = value; 133 | } 134 | } 135 | 136 | get volume(){return this._volume} 137 | set volume(value){ 138 | if(value === this._volume)return; 139 | if(utils.isWebAudioSupported){ 140 | if(this.audio)this.audio.gainNode.gain.value = this.muted ? 0 : this.volume; 141 | }else{ 142 | if(this.audio)this.audio.volume = this.muted ? 0 : this.volume; 143 | } 144 | this._volume = value; 145 | } 146 | 147 | get muted(){return this._muted}; 148 | set muted(value){ 149 | if(value === this._muted)return; 150 | this._muted = value; 151 | if(utils.isWebAudioSupported){ 152 | if(this.audio)this.audio.gainNode.gain.value = this._muted ? 0 : this.volume; 153 | }else{ 154 | if(this.audio)this.audio.volume = this._muted ? 0 : this.volume; 155 | } 156 | } 157 | } 158 | -------------------------------------------------------------------------------- /src/AudioManager.js: -------------------------------------------------------------------------------- 1 | import utils from './utils'; 2 | import Audio from './Audio'; 3 | 4 | export default class AudioManager{ 5 | static audios = {}; 6 | 7 | constructor(){ 8 | this.enabled = utils.isAudioSupported; 9 | this.sounds = []; 10 | 11 | if(utils.isWebAudioSupported){ 12 | this.context = utils.globalWebAudioContext; 13 | this.gainNode = utils.createGainNode(this.context); 14 | this.gainNode.connect(this.context.destination); 15 | } 16 | } 17 | 18 | getAudio(name){ 19 | let audio = new Audio(AudioManager.audios[name], this); 20 | this.sounds.push(audio); 21 | return audio; 22 | } 23 | 24 | removeAudio(audio){ 25 | let index = this.sounds.indexOf(audio); 26 | if(index !== -1){ 27 | this.sounds.splice(index, 1); 28 | } 29 | } 30 | 31 | filterAudios(id, value){ 32 | let audios = []; 33 | let len = this.sounds.length; 34 | let emptyValue = typeof value === "undefined"; 35 | 36 | for(let i = 0; i < len; i++){ 37 | if((emptyValue && !!this.sounds[i][id])||(!emptyValue && this.sounds[i][id] === value)){ 38 | audios.push(this.sounds[i]); 39 | } 40 | } 41 | 42 | return audios; 43 | } 44 | 45 | mute(value){ 46 | value = (value !== false); 47 | let len = this.sounds.length; 48 | for(let i = 0; i < len; i++)this.sounds[i].muted = value; 49 | } 50 | 51 | unmute(){ 52 | return this.mute(false); 53 | } 54 | 55 | pause(value){ 56 | value = (value !== false); 57 | let len = this.sounds.length; 58 | for(let i = 0; i < len; i++)this.sounds[i].paused = value; 59 | } 60 | 61 | resume(){ 62 | return this.pause(false); 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /src/audioParser.js: -------------------------------------------------------------------------------- 1 | import utils from './utils'; 2 | import AudioManager from './AudioManager'; 3 | 4 | const _allowedExt = ["m4a", "ogg", "mp3", "wav", "aac"]; 5 | 6 | export function audioParser(){ 7 | return function(resource, next){ 8 | if(!utils.isAudioSupported || !resource.data)return next(); 9 | 10 | let ext = _getExt(resource.url); 11 | if(_allowedExt.indexOf(ext) === -1 || !_canPlay(ext))return next(); 12 | 13 | let name = resource.name || resource.url; 14 | if(utils.isWebAudioSupported){ 15 | utils.globalWebAudioContext.decodeAudioData(resource.data, (buffer)=>{ 16 | AudioManager.audios[name] = buffer; 17 | next(); 18 | }); 19 | }else{ 20 | AudioManager.audios[name] = resource.data; 21 | return next(); 22 | } 23 | } 24 | } 25 | 26 | export function audioUrlParser(resourceUrl){ 27 | let url; 28 | for(let i = 0; i < resourceUrl.length; i++){ 29 | let ext = _getExt(resourceUrl[i]); 30 | if(_allowedExt.indexOf(ext) === -1)break; 31 | if(_canPlay(ext)){ 32 | url = resourceUrl[i]; 33 | break; 34 | } 35 | } 36 | 37 | return url; 38 | } 39 | 40 | function _getExt(url){ 41 | return url.split('?').shift().split('.').pop().toLowerCase(); 42 | } 43 | 44 | function _canPlay(ext){ 45 | let canPlay = false; 46 | switch(ext){ 47 | case "m4a": canPlay = utils.isM4aSupported; break; 48 | case "mp3": canPlay = utils.isMp3Supported; break; 49 | case "ogg": canPlay = utils.isOggSupported; break; 50 | case "wav": canPlay = utils.isWavSupported; break; 51 | case "aac": canPlay = utils.isAacSupported; break; 52 | } 53 | 54 | return canPlay; 55 | } 56 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import 'pixi.js'; 2 | import utils from './utils'; 3 | import AudioManager from './AudioManager'; 4 | import {audioParser, audioUrlParser} from './audioParser'; 5 | 6 | let audio = { 7 | utils : utils, 8 | AudioManager : AudioManager, 9 | Audio : Audio, 10 | audioParser : audioParser 11 | }; 12 | 13 | if(!PIXI.AudioManager){ 14 | let Loader = PIXI.loaders.Loader; 15 | Loader.addPixiMiddleware(audioParser); 16 | 17 | let baseAdd = Loader.prototype.add; 18 | Loader.prototype.add = function(name, url, options, cb){ 19 | if(typeof name === 'object'){ 20 | if(Object.prototype.toString.call(name.url) === "[object Array]"){ 21 | name.url = audioUrlParser(name.url); 22 | } 23 | } 24 | 25 | if(Object.prototype.toString.call(url) === "[object Array]"){ 26 | url = audioUrlParser(url); 27 | } 28 | 29 | return baseAdd.call(this, name, url, options, cb); 30 | }; 31 | 32 | PIXI.audio = audio; 33 | PIXI.loader = new PIXI.loaders.Loader(); 34 | PIXI.loaders.audioParser = audioParser; 35 | PIXI.audioManager = new AudioManager(); 36 | } 37 | 38 | export default audio; 39 | -------------------------------------------------------------------------------- /src/utils.js: -------------------------------------------------------------------------------- 1 | import {loaders} from 'pixi.js'; 2 | 3 | let isHTMLAudioSupported = !!window.Audio, 4 | webAudioContext = window.AudioContext || window.webkitAudioContext, 5 | isWebAudioSupported = !!webAudioContext, 6 | isAudioSupported = isWebAudioSupported || isHTMLAudioSupported, 7 | isMp3Supported = false, 8 | isOggSupported = false, 9 | isWavSupported = false, 10 | isM4aSupported = false, 11 | isAacSupported = false, 12 | createGainNode = null, 13 | globalWebAudioContext = isWebAudioSupported ? new webAudioContext() : null; 14 | 15 | if(isAudioSupported){ 16 | let audio = document.createElement("audio"); 17 | isMp3Supported = audio.canPlayType('audio/mpeg;') !== ""; 18 | isOggSupported = audio.canPlayType('audio/ogg; codecs="vorbis"') !== ""; 19 | isWavSupported = audio.canPlayType('audio/wav') !== ""; 20 | isM4aSupported = audio.canPlayType('audio/mp4; codecs="mp4a.40.5"') !== ""; 21 | isAacSupported = audio.canPlayType('audio/aac') !== ""; 22 | 23 | //Add some config to the pixi loader 24 | if(isMp3Supported)_setAudioExt("mp3"); 25 | if(isOggSupported)_setAudioExt("ogg"); 26 | if(isWavSupported)_setAudioExt("wav"); 27 | if(isM4aSupported)_setAudioExt("m4a"); 28 | if(isAacSupported)_setAudioExt("aac"); 29 | 30 | if(isWebAudioSupported){ 31 | createGainNode = function createGainNode(ctx){ 32 | return ctx.createGain ? ctx.createGain() : ctx.createGainNode(); 33 | } 34 | } 35 | } 36 | 37 | function _setAudioExt(ext){ 38 | if(isWebAudioSupported){ 39 | loaders.Resource.setExtensionXhrType(ext, loaders.Resource.XHR_RESPONSE_TYPE.BUFFER); 40 | }else{ 41 | loaders.Resource.setExtensionLoadType(ext, loaders.Resource.LOAD_TYPE.AUDIO); 42 | } 43 | } 44 | 45 | export default { 46 | isHTMLAudioSupported : isHTMLAudioSupported, 47 | webAudioContext : webAudioContext, 48 | isWebAudioSupported : isWebAudioSupported, 49 | isAudioSupported : isAudioSupported, 50 | isMp3Supported : isMp3Supported, 51 | isOggSupported : isOggSupported, 52 | isWavSupported : isWavSupported, 53 | isM4aSupported : isM4aSupported, 54 | isAacSupported : isAacSupported, 55 | globalWebAudioContext : globalWebAudioContext, 56 | createGainNode: createGainNode 57 | }; 58 | -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var webpack = require('webpack'); 4 | var path = require('path'); 5 | 6 | var PLUGIN_NAME = require('./package.json').name; 7 | var DEV = process.env.NODE_ENV !== "production"; 8 | var ENTRY = ['./src/index.js']; 9 | var EXTERNALS = {}; 10 | 11 | if(DEV){ 12 | ENTRY.unshift('pixi.js'); 13 | }else{ 14 | EXTERNALS['pixi.js'] = "PIXI"; 15 | } 16 | 17 | module.exports = { 18 | devtool: 'source-map', 19 | entry: ENTRY, 20 | output: { 21 | filename: 'build/' + PLUGIN_NAME + '.js' 22 | }, 23 | resolve: { 24 | extensions: ["", ".js"] 25 | }, 26 | externals : EXTERNALS, 27 | module: { 28 | postLoaders: [ 29 | { 30 | loader: "transform?brfs" 31 | } 32 | ], 33 | loaders: [ 34 | { 35 | test: /\.json$/, 36 | include: path.join(__dirname, 'node_modules', 'pixi.js'), 37 | loader: 'json', 38 | }, 39 | { 40 | test: /\.js$/, 41 | exclude: path.join(__dirname, 'node_modules'), 42 | loader: 'babel-loader', 43 | query: { 44 | presets: ['es2015','stage-0'] 45 | } 46 | } 47 | ] 48 | } 49 | }; 50 | --------------------------------------------------------------------------------