Skip to content

Instantly share code, notes, and snippets.

@drart
Forked from colinbdclark/gist:9179042
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drart/9573502 to your computer and use it in GitHub Desktop.
Save drart/9573502 to your computer and use it in GitHub Desktop.
An example of combining flocking.js with the compressor in Web Audio.
flock.init();
var as = flock.enviro.shared.audioStrategy;
// Create the new gain node and set some parameters on it.
var compressor = as.context.createDynamicsCompressor();
// Connect the Flocking jsNode up to it.
as.jsNode.connect(compressor);
// Monkey patch the Audio Strategy's start and stop methods.
// Cut and pasted from flocking-webaudio.js
as.startGeneratingSamples = function () {
as.jsNode.onaudioprocess = as.writeSamples; // TODO: When Firefox ships, is this still necessary?
compressor.connect(as.context.destination);
// Work around a bug in iOS Safari where it now requires a noteOn()
// message to be invoked before sound will work at all. Just connecting a
// ScriptProcessorNode inside a user event handler isn't sufficient.
if (as.model.shouldInitIOS) {
var s = as.source;
(s.start || s.noteOn).call(as.source, 0);
(s.stop || s.noteOff).call(as.source, 0);
as.model.shouldInitIOS = false;
}
};
// Cut and pasted from flocking-webaudio.js
as.stopGeneratingSamples = function () {
compressor.disconnect(0);
as.jsNode.onaudioprocess = undefined;
};
var synth = flock.synth({
synthDef: {
ugen: "flock.ugen.filter.biquad.lp",
freq: {
ugen: "flock.ugen.sin",
rate: "control",
freq: 5,
phase: 0,
mul: 3600,
add: 4000
},
source: {
ugen: "flock.ugen.impulse",
freq: 4,
mul: 10,
}
}
});
var synth2 = flock.synth({
synthDef: [
{
id: "leftSine",
ugen: "flock.ugen.sinOsc",
freq: 741,
mul: 0.05
},
{
id: "rightSine",
ugen: "flock.ugen.sinOsc",
freq: 740,
mul: 0.05
}
]
});
var synth3 = flock.synth({
synthDef: [
{
id: "leftSine",
ugen: "flock.ugen.sinOsc",
freq: 440,
mul: 0.05
},
{
id: "rightSine",
ugen: "flock.ugen.sinOsc",
freq: {
ugen: "flock.ugen.sinOsc",
freq: 0.125,
mul: 2,
add: 442,
},
mul: 0.05
}
]
});
var synth4 = flock.synth({
synthDef: [
{
id: "leftSine",
ugen: "flock.ugen.sinOsc",
freq: 43.125,
mul: 0.1
},
{
id: "rightSine",
ugen: "flock.ugen.sinOsc",
freq: 44,
mul: 0.1
}
]
});
compressor.threshold = -9;
compressor.ratio = 5;
compressor.attack = 0;
compressor.release = 200;
flock.enviro.shared.play();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment