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 "addGroup" feature to Group class #135

Open
wants to merge 3 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
139 changes: 127 additions & 12 deletions distr/Pizzicato.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
isAudioBufferSourceNode: function(audioNode) {
return (audioNode && audioNode.toString() === "[object AudioBufferSourceNode]");
},

isGroup: function(group) {
return group instanceof Pz.Group;
},

isSound: function(sound) {
return sound instanceof Pz.Sound;
Expand Down Expand Up @@ -106,16 +110,18 @@
return 1 - ((0.5 - mix) * 2);
}
};
/* In order to allow an AudioNode to connect to a Pizzicato
Effect object, we must shim its connect method */
var gainNode = Pizzicato.context.createGain();
var audioNode = Object.getPrototypeOf(Object.getPrototypeOf(gainNode));
var connect = audioNode.connect;

audioNode.connect = function(node) {
var endpoint = Pz.Util.isEffect(node) ? node.inputNode : node;
connect.call(this, endpoint);
return node;
/**
* In order to allow an AudioNode to connect to a Pizzicato
* Effect object, we must shim its connect method
* Ref => https://github.com/GoogleChromeLabs/web-audio-samples/wiki/CompositeAudioNode
*/
var _connect = AudioNode.prototype.connect;
AudioNode.prototype.connect = function () {
var args = Array.prototype.slice.call(arguments);
if (args[0]._isPizzicatoEffectNode)
args[0] = args[0].inputNode;

_connect.apply(this, args);
};

Object.defineProperty(Pizzicato, 'volume', {
Expand Down Expand Up @@ -484,6 +490,18 @@
}
},

seek: {
enumerable: true,

value: function(offset) {
var playing = this.playing;
if (playing) this.pause();
this.offsetTime = offset;
this.trigger('seek');
if (playing) this.play();
}
},


clone: {
enumerable: true,
Expand Down Expand Up @@ -836,13 +854,15 @@
}
});

Pizzicato.Group = function(sounds) {

Pizzicato.Group = function(sounds, groups) {
sounds = sounds || [];
groups = groups || [];

this.mergeGainNode = Pz.context.createGain();
this.masterVolume = Pz.context.createGain();
this.sounds = [];
this.groups = [];
this.effects = [];
this.effectConnectors = [];

Expand All @@ -851,6 +871,10 @@

for (var i = 0; i < sounds.length; i++)
this.addSound(sounds[i]);

for (var j = 0; j < groups.length; j++)
this.addGroup(groups[j]);

};

Pizzicato.Group.prototype = Object.create(Pz.Events, {
Expand All @@ -875,6 +899,64 @@
},


addGroup: {
enumerable: true,

value: function(group) {
if (!Pz.Util.isGroup(group)) {
console.error('You can only add Pizzicato.Group objects');
return;
}
if (this.groups.indexOf(group) > -1) {
console.warn('The Pizzicato.Group object was already added to this group');
return;
}
if (group.detached) {
console.warn('Groups do not support detached groups. You can manually create an audio graph to group detached groups together.');
return;
}
group.masterVolume.disconnect(Pz.masterGainNode);
group.masterVolume.connect(this.mergeGainNode);
this.groups.push(group);
}
},


removeGroup: {
enumerable: true,

value: function(group) {
var index = this.groups.indexOf(group);

if (index === -1) {
console.warn('Cannot remove a group that is not part of this group.');
return;
}

group.masterVolume.disconnect(this.mergeGainNode);
group.masterVolume.connect(Pz.masterGainNode);
this.groups.splice(index, 1);
}
},


connectSource: {
enumerable: true,

value: function(source) {
source.connect(this.mergeGainNode);
}
},

disconnectSource: {
enumerable: true,

value: function(source) {
source.disconnect(this.mergeGainNode);
}
},


addSound: {
enumerable: true,

Expand Down Expand Up @@ -939,6 +1021,9 @@
for (var i = 0; i < this.sounds.length; i++)
this.sounds[i].play();

for (var j = 0; j < this.groups.length; j++)
this.groups[j].play();

this.trigger('play');
}

Expand All @@ -952,6 +1037,9 @@
for (var i = 0; i < this.sounds.length; i++)
this.sounds[i].stop();

for (var j = 0; j < this.groups.length; j++)
this.groups[j].stop();

this.trigger('stop');
}

Expand All @@ -965,11 +1053,30 @@
for (var i = 0; i < this.sounds.length; i++)
this.sounds[i].pause();

for (var j = 0; j < this.groups.length; j++)
this.groups[j].pause();

this.trigger('pause');
}

},

seek: {
enumerable: true,

value: function(offset) {

for (var i = 0; i < this.sounds.length; i++)
this.sounds[i].seek(offset);

for (var j = 0; j < this.groups.length; j++)
this.groups[j].pause(offset);

this.trigger('seek');
}
},


/**
* 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 Expand Up @@ -1053,6 +1160,14 @@

var baseEffect = Object.create(null, {

_isPizzicatoEffectNode: {
enumerable: true,

get value () {
return true;
}
},

connect: {
enumerable: true,

Expand Down
4 changes: 2 additions & 2 deletions distr/Pizzicato.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "pizzicato",
"description": "A web-audio library to simplify using and manipulating sounds.",
"version": "0.6.4",
"version": "0.6.6",
"license": "MIT",
"homepage": "https://alemangui.github.io/pizzicato/",
"author": {
Expand Down
Loading