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

Added 'end' event for groups. Fixes #69 #95

Open
wants to merge 1 commit 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
80 changes: 70 additions & 10 deletions distr/Pizzicato.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,18 @@
}
});
Pizzicato.Events = {

/**
* Adds an event handler that will be treated upon
* the triggering of that event.
*/
on: function(name, callback, context) {
if (!name || !callback)
return;

this._events = this._events || {};
var _event = this._events[name] || (this._events[name] = []);

_event.push({
callback: callback,
context: context || this,
Expand All @@ -167,7 +167,12 @@
* is linked to that event, the handler will be
* executed.
*/
trigger: function(name) {
trigger: function(name) {
this._observers = this._observers || [];
for (i = 0; i < this._observers.length; i++) {
this._observers[i].notify(this, name);
}

if (!name)
return;

Expand All @@ -186,7 +191,7 @@
args[i] = arguments[i + 1];

for (i = 0; i < _event.length; i++)
_event[i].callback.apply(_event[i].context, args);
_event[i].callback.apply(_event[i].context, args);
},

/**
Expand All @@ -199,8 +204,27 @@

else
this._events = {};
}
},

addObserver: function(observer) {
this._observers = this._observers || [];

if (observer == null){
console.error('Error adding observer, a null object was given.');
}

this._observers.push(observer);
},

removeObserver: function(observer) {
var index = this._observers.indexOf(observer);

if (index == -1) {
console.error('Error removing observer, not found.');
}

this._observers.splice(index, 1);
}
};
Pizzicato.Sound = function(description, callback) {
var self = this;
Expand Down Expand Up @@ -521,8 +545,9 @@

if (this.playing)
this.stop();
if (!this.paused)
if (!this.paused) {
this.trigger('end');
}
};
}
},
Expand Down Expand Up @@ -891,9 +916,15 @@
console.warn('Groups do not support detached sounds. You can manually create an audio graph to group detached sounds together.');
return;
}

sound.disconnect(Pz.masterGainNode);
sound.connect(this.mergeGainNode);

//Switches the observer of the last element
if (this.sounds.length > 0)
this.sounds[this.sounds.length-1].removeObserver(this);
sound.addObserver(this);

this.sounds.push(sound);
}
},
Expand All @@ -909,9 +940,16 @@
console.warn('Cannot remove a sound that is not part of this group.');
return;
}

sound.disconnect(this.mergeGainNode);
sound.connect(Pz.masterGainNode);

// Switch the observer of the last sound
if (index == this.sounds.length){
sound.removeObserver(this);
this.sounds[this.sounds.length-2].addObserver(this);
}

this.sounds.splice(index, 1);
}
},
Expand Down Expand Up @@ -960,7 +998,7 @@

pause: {
enumerable: true,

value: function() {
for (var i = 0; i < this.sounds.length; i++)
this.sounds[i].pause();
Expand All @@ -970,6 +1008,28 @@

},

onEnded: {
enumerable: true,

value: function() {
this.trigger('end');
}

},

notify: {
enumerable: true,

value: function(observable, name) {
if (observable == null){
console.error("Error, observable was null when calling notify on its observers.");
}
if (name == 'end')
this.onEnded();
}

},

/**
* Similarly to Sound objects, adding effects will create a graph in which there will be a
* gain node (effectConnector) in between every effect added. For example:
Expand Down
4 changes: 2 additions & 2 deletions distr/Pizzicato.min.js

Large diffs are not rendered by default.

80 changes: 70 additions & 10 deletions site/Pizzicato.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,18 @@
}
});
Pizzicato.Events = {

/**
* Adds an event handler that will be treated upon
* the triggering of that event.
*/
on: function(name, callback, context) {
if (!name || !callback)
return;

this._events = this._events || {};
var _event = this._events[name] || (this._events[name] = []);

_event.push({
callback: callback,
context: context || this,
Expand All @@ -167,7 +167,12 @@
* is linked to that event, the handler will be
* executed.
*/
trigger: function(name) {
trigger: function(name) {
this._observers = this._observers || [];
for (i = 0; i < this._observers.length; i++) {
this._observers[i].notify(this, name);
}

if (!name)
return;

Expand All @@ -186,7 +191,7 @@
args[i] = arguments[i + 1];

for (i = 0; i < _event.length; i++)
_event[i].callback.apply(_event[i].context, args);
_event[i].callback.apply(_event[i].context, args);
},

/**
Expand All @@ -199,8 +204,27 @@

else
this._events = {};
}
},

addObserver: function(observer) {
this._observers = this._observers || [];

if (observer == null){
console.error('Error adding observer, a null object was given.');
}

this._observers.push(observer);
},

removeObserver: function(observer) {
var index = this._observers.indexOf(observer);

if (index == -1) {
console.error('Error removing observer, not found.');
}

this._observers.splice(index, 1);
}
};
Pizzicato.Sound = function(description, callback) {
var self = this;
Expand Down Expand Up @@ -521,8 +545,9 @@

if (this.playing)
this.stop();
if (!this.paused)
if (!this.paused) {
this.trigger('end');
}
};
}
},
Expand Down Expand Up @@ -891,9 +916,15 @@
console.warn('Groups do not support detached sounds. You can manually create an audio graph to group detached sounds together.');
return;
}

sound.disconnect(Pz.masterGainNode);
sound.connect(this.mergeGainNode);

//Switches the observer of the last element
if (this.sounds.length > 0)
this.sounds[this.sounds.length-1].removeObserver(this);
sound.addObserver(this);

this.sounds.push(sound);
}
},
Expand All @@ -909,9 +940,16 @@
console.warn('Cannot remove a sound that is not part of this group.');
return;
}

sound.disconnect(this.mergeGainNode);
sound.connect(Pz.masterGainNode);

// Switch the observer of the last sound
if (index == this.sounds.length){
sound.removeObserver(this);
this.sounds[this.sounds.length-2].addObserver(this);
}

this.sounds.splice(index, 1);
}
},
Expand Down Expand Up @@ -960,7 +998,7 @@

pause: {
enumerable: true,

value: function() {
for (var i = 0; i < this.sounds.length; i++)
this.sounds[i].pause();
Expand All @@ -970,6 +1008,28 @@

},

onEnded: {
enumerable: true,

value: function() {
this.trigger('end');
}

},

notify: {
enumerable: true,

value: function(observable, name) {
if (observable == null){
console.error("Error, observable was null when calling notify on its observers.");
}
if (name == 'end')
this.onEnded();
}

},

/**
* Similarly to Sound objects, adding effects will create a graph in which there will be a
* gain node (effectConnector) in between every effect added. For example:
Expand Down
36 changes: 30 additions & 6 deletions src/Events.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
Pizzicato.Events = {

/**
* Adds an event handler that will be treated upon
* the triggering of that event.
*/
on: function(name, callback, context) {
if (!name || !callback)
return;

this._events = this._events || {};
var _event = this._events[name] || (this._events[name] = []);

_event.push({
callback: callback,
context: context || this,
Expand All @@ -23,7 +23,12 @@ Pizzicato.Events = {
* is linked to that event, the handler will be
* executed.
*/
trigger: function(name) {
trigger: function(name) {
this._observers = this._observers || [];
for (i = 0; i < this._observers.length; i++) {
this._observers[i].notify(this, name);
}

if (!name)
return;

Expand All @@ -42,7 +47,7 @@ Pizzicato.Events = {
args[i] = arguments[i + 1];

for (i = 0; i < _event.length; i++)
_event[i].callback.apply(_event[i].context, args);
_event[i].callback.apply(_event[i].context, args);
},

/**
Expand All @@ -55,6 +60,25 @@ Pizzicato.Events = {

else
this._events = {};
}
},

addObserver: function(observer) {
this._observers = this._observers || [];

if (observer == null){
console.error('Error adding observer, a null object was given.');
}

this._observers.push(observer);
},

removeObserver: function(observer) {
var index = this._observers.indexOf(observer);

if (index == -1) {
console.error('Error removing observer, not found.');
}

this._observers.splice(index, 1);
}
};
Loading