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

[WIP] - Refactor SoundJS lib with good practices #303

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1,855 changes: 97 additions & 1,758 deletions src/soundjs/Sound.js

Large diffs are not rendered by default.

1,117 changes: 1,117 additions & 0 deletions src/soundjs/docs/Sound.js

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions src/soundjs/metadata/SoundMetadata.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// namespace:
this.createjs = this.createjs || {};

createjs.metadata.sound = (function () {

var InterruptMode = {
INTERRUPT_ANY: "any",
INTERRUPT_EARLY: "early",
INTERRUPT_LATE: "late",
INTERRUPT_NONE: "none"
};

var PlayState = {
PLAY_INITED: "playInited",
PLAY_SUCCEEDED: "playSucceeded",
PLAY_INTERRUPTED: "playInterrupted",
PLAY_FINISHED: "playFinished",
PLAY_FAILED: "playFailed"
};

return {
InterruptMode: InterruptMode,
PlayState: PlayState
};
})();
190 changes: 190 additions & 0 deletions src/soundjs/sound-utils/SoundChannel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
// TODO remove SoundChannel from namespace
// namespace:
this.createjs = this.createjs || {};
createjs.soundUtils = createjs.soundUtils || {};

createjs.soundUtils.SoundChannel = (function () {

var SoundChannel = function (source, max) {
this.init(source, max);
};

var prototype = SoundChannel.prototype;
prototype.constructor = SoundChannel;

prototype.init = function (source, max) {
this.src = source;
this.max = max || this.maxDefault;

if (this.max == -1) {
this.max = this.maxDefault;
}

this._instances = [];
this.src = null;
this.max = null;
this.maxDefault = 100;
this.length = 0;
};

prototype.toString = function () {
return "[Sound SoundChannel]";
};

prototype._get = _get;
prototype._add = _add;
prototype._remove = _remove;
prototype._removeAll = _removeAll;
prototype._getSlot = _getSlot;

SoundChannel.channels = {};

SoundChannel.create = create;
SoundChannel.removeSrc = removeSrc;
SoundChannel.removeAll = removeAll;
SoundChannel.add = add;
SoundChannel.remove = remove;
SoundChannel.maxPerChannel = maxPerChannel;
SoundChannel.get = get;

return SoundChannel;

function _get(index) {
return this._instances[index];
}

function _add(instance, interrupt) {
if (!this._getSlot(interrupt, instance)) {
return false;
}

this._instances.push(instance);
this.length++;

return true;
}

function _remove(instance) {
var index = createjs.indexOf(this._instances, instance);

if (index == -1) {
return false;
}

this._instances.splice(index, 1);
this.length--;

return true;
}

function _removeAll() {
// Note that stop() removes the item from the list
for (var i=this.length-1; i>=0; i--) {
this._instances[i].stop();
}
}

function _getSlot(interrupt, instance) {
var target, replacement;

if (interrupt != Sound.INTERRUPT_NONE) {
// First replacement candidate
replacement = this._get(0);
if (replacement == null) {
return true;
}
}

for (var i = 0, l = this.max; i < l; i++) {
target = this._get(i);

// Available Space
if (target == null) {
return true;
}

// Audio is complete or not playing
if (target.playState == Sound.PLAY_FINISHED ||
target.playState == Sound.PLAY_INTERRUPTED ||
target.playState == Sound.PLAY_FAILED) {
replacement = target;
break;
}

if (interrupt == Sound.INTERRUPT_NONE) {
continue;
}

// Audio is a better candidate than the current target, according to playhead
if ((interrupt == Sound.INTERRUPT_EARLY && target.position < replacement.position) ||
(interrupt == Sound.INTERRUPT_LATE && target.position > replacement.position)) {
replacement = target;
}
}

if (replacement != null) {
replacement._interrupt();
this._remove(replacement);
return true;
}

return false;
}

function create(source, max) {
var channel = SoundChannel.get(source);
if (channel == null) {
SoundChannel.channels[source] = new SoundChannel(source, max);
return true;
}

return false;
}

function removeSrc(source) {
var channel = SoundChannel.get(source);
if (channel == null) {
return false;
}

channel._removeAll(); // this stops and removes all active instances
delete(SoundChannel.channels[source]);

return true;
}

function removeAll() {
for(var channel in SoundChannel.channels) {
SoundChannel.channels[channel]._removeAll(); // this stops and removes all active instances
}
SoundChannel.channels = {};
}

function add(instance, interrupt) {
var channel = SoundChannel.get(instance.src);
if (channel == null) {
return false;
}

return channel._add(instance, interrupt);
}

function remove(instance) {
var channel = SoundChannel.get(instance.src);
if (channel == null) {
return false;
}

channel._remove(instance);
return true;
}

function maxPerChannel() {
return p.maxDefault;
}

function get(source) {
return SoundChannel.channels[source];
}

})();
73 changes: 73 additions & 0 deletions src/soundjs/sound-utils/SoundEventHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// TODO remove SoundEventHandler from namespace
// namespace:
this.createjs = this.createjs || {};
createjs.soundUtils = createjs.soundUtils || {};

createjs.soundUtils.SoundEventHandler = (function (){
var SoundEventHandler = function () {
this.init();
};

var prototype = SoundEventHandler.prototype;
prototype.constructor = SoundEventHandler;

prototype.init = function () {
this.addEventListener = null;
this.removeEventListener = null;
this.removeAllEventListeners = null;
this.dispatchEvent = null;
this.hasEventListener = null;
this._listeners = null;

createjs.EventDispatcher.initialize(this);
};

prototype.handleLoadComplete = _handleLoadComplete;
prototype.handleLoadError = _handleLoadError;

return SoundEventHandler;

function _handleLoadComplete(event, preloadHash) {
var source = event.target.getItem().src;
if (!preloadHash || !preloadHash[source]) {
return;
}

for (var i = 0, l = preloadHash[source].length; i < l; i++) {
var preloadItem = preloadHash[source][i];
preloadHash[source][i] = true;

if (!this.hasEventListener("fileload")) { continue; }

var event = new createjs.Event("fileload");
event.src = preloadItem.src;
event.id = preloadItem.id;
event.data = preloadItem.data;
event.sprite = preloadItem.sprite;

this.dispatchEvent(event);
}
}

function _handleLoadError(event, preloadHash) {
var source = event.target.getItem().src;
if (!preloadHash || !preloadHash[source]) {
return;
}

for (var i = 0, l = preloadHash[source].length; i < l; i++) {
var preloadItem = preloadHash[source][i];
preloadHash[source][i] = false;

if (!this.hasEventListener("fileerror")) { continue; }

var event = new createjs.Event("fileerror");
event.src = preloadItem.src;
event.id = preloadItem.id;
event.data = preloadItem.data;
event.sprite = preloadItem.sprite;

this.dispatchEvent(event);
}
}
})();
82 changes: 82 additions & 0 deletions src/soundjs/sound-utils/SoundFactory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// TODO remove SoundFactory from namespace
// namespace:
this.createjs = this.createjs || {};
createjs.soundUtils = createjs.soundUtils || {};

createjs.soundUtils.SoundFactory = (function () {
var SoundEventHandler = createjs.soundUtils.SoundEventHandler,
SoundInstance = createjs.soundUtils.SoundInstance,
SoundParser = createjs.soundUtils.SoundParser,
SoundPlugin = createjs.soundUtils.SoundPlugin,
SoundRegister = createjs.soundUtils.SoundRegister,
SoundVolume = createjs.soundUtils.SoundVolume;

var _soundEventHandler = null,
_soundInstance = null,
_soundParser = null,
_soundPlugin = null,
_soundRegister = null,
_soundVolume = null;

return {
getSoundEventHandler: getSoundEventHandler,
getSoundInstance: getSoundInstance,
getSoundParser: getSoundParser,
getSoundPlugin: getSoundPlugin,
getSoundRegister: getSoundRegister,
getSoundVolume: getSoundVolume
}

function getSoundEventHandler() {
if (!_soundEventHandler)
_soundEventHandler = new SoundEventHandler();

return _soundEventHandler;
}
function getSoundInstance() {
if (!_soundInstance) {
var soundPlugin = getSoundPlugin(),
soundParser = getSoundParser(),
soundRegister = getSoundRegister();

_soundInstance = new SoundInstance(soundPlugin, soundParser, soundRegister);
}

return _soundInstance;
}
function getSoundParser() {
if (!_soundParser) {
var soundVolume = getSoundVolume();
_soundParser = new SoundParser(soundVolume);
}

return _soundParser;
}
function getSoundPlugin() {
if (!_soundPlugin)
_soundPlugin = new SoundPlugin();

return _soundPlugin;
}
function getSoundRegister() {
if (!_soundRegister) {
var soundEventHandler = getSoundEventHandler(),
soundPlugin = getSoundPlugin(),
soundParser = getSoundParser();

_soundRegister = new SoundRegister(soundEventHandler, soundPlugin, soundParser);
}

return _soundRegister;
}
function getSoundVolume() {
if (!_soundVolume) {
var soundPlugin = getSoundPlugin(),
soundInstance = getSoundInstance();

_soundVolume = new SoundVolume(soundPlugin, soundInstance);
}

return _soundVolume;
}
})();
Loading