Skip to content

Commit

Permalink
adding gain node class
Browse files Browse the repository at this point in the history
  • Loading branch information
b2renger committed May 31, 2015
1 parent 85d5eab commit cf9428f
Show file tree
Hide file tree
Showing 7 changed files with 338 additions and 5 deletions.
3 changes: 2 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ module.exports = function(grunt) {
'soundRecorder': 'src/soundRecorder',
'signal': 'src/signal',
'metro': 'src/metro',
'peakdetect': 'src/peakDetect'
'peakdetect': 'src/peakDetect',
'gain': 'src/gain'
},
useStrict: true,
wrap: {
Expand Down
10 changes: 10 additions & 0 deletions examples/mixingSounds/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<head>
<script language="javascript" type="text/javascript" src="../../lib/p5.js"></script>

<script language="javascript" type="text/javascript" src="../../lib/addons/p5.dom.js"></script>

<script language="javascript" type="text/javascript" src="../../lib/p5.sound.min.js"></script>

<script language="javascript" type="text/javascript" src="sketch.js"></script>

</head>
57 changes: 57 additions & 0 deletions examples/mixingSounds/sketch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

// load two soundfile and crossfade beetween them
var sound1,sound2;
var gain1, gain2, gain3;

function preload(){
soundFormats('ogg', 'mp3');
sound1 = loadSound('../_files/Damscray_-_Dancing_Tiger_01');
sound2 = loadSound('../_files/beat.mp3');
}

function setup() {
createCanvas(400,200);

// create a 'master' gain to which we will connect both soundfiles
gain3 = new p5.Gain();
gain3.connect();

// setup first sound for playing
sound1.rate(1);
sound1.loop();
sound1.disconnect(); // diconnect from p5 output

gain1 = new p5.Gain(); // setup a gain node
gain1.connectSource(sound1); // connect the first sound to its input
gain1.connect(gain3); // connect its output to the 'master'

sound2.rate(1);
sound2.disconnect();
sound2.loop();

gain2 = new p5.Gain();
gain2.connectSource(sound2);
gain2.connect(gain3);

}


function draw(){
background(180);

// calculate the horizontal distance beetween the mouse and the right of the screen
var d = dist(mouseX,0,width,0);

// map the horizontal position of the mouse to values useable for volume control of sound1
var vol1 = map(mouseX,0,width,0,1);
var vol2 = 1-vol1; // when sound1 is loud, sound2 is quiet and vice versa

gain1.amp(vol1,0.5,0);
gain2.amp(vol2,0.5,0);

// map the vertical position of the mouse to values useable for 'master volume control'
var vol3 = map(mouseY,0,height,0,1);
gain3.amp(vol3,0.5,0);
}


128 changes: 126 additions & 2 deletions lib/p5.sound.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! p5.sound.js v0.2.1 2015-05-30 */
/*! p5.sound.js v0.2.1 2015-05-31 */
(function (root, factory) {
if (typeof define === 'function' && define.amd)
define('p5.sound', ['p5'], function (p5) { (factory(p5));});
Expand Down Expand Up @@ -6851,10 +6851,134 @@ peakdetect = function () {
};
};
}(master);
var gain;
gain = function () {
'use strict';
var p5sound = master;
/**
* A gain node is usefull to set the relative volume of sound.
* It's typically used to build mixers.
*
* @class p5.Gain
* @constructor
* @example
* <div><code>
*
* // load two soundfile and crossfade beetween them
* var sound1,sound2;
* var gain1, gain2, gain3;
*
* function preload(){
* soundFormats('ogg', 'mp3');
* sound1 = loadSound('../_files/Damscray_-_Dancing_Tiger_01');
* sound2 = loadSound('../_files/beat.mp3');
* }
*
* function setup() {
* createCanvas(400,200);
*
* // create a 'master' gain to which we will connect both soundfiles
* gain3 = new p5.Gain();
* gain3.connect();
*
* // setup first sound for playing
* sound1.rate(1);
* sound1.loop();
* sound1.disconnect(); // diconnect from p5 output
*
* gain1 = new p5.Gain(); // setup a gain node
* gain1.connectSource(sound1); // connect the first sound to its input
* gain1.connect(gain3); // connect its output to the 'master'
*
* sound2.rate(1);
* sound2.disconnect();
* sound2.loop();
*
* gain2 = new p5.Gain();
* gain2.connectSource(sound2);
* gain2.connect(gain3);
*
* }
*
* function draw(){
* background(180);
*
* // calculate the horizontal distance beetween the mouse and the right of the screen
* var d = dist(mouseX,0,width,0);
*
* // map the horizontal position of the mouse to values useable for volume control of sound1
* var vol1 = map(mouseX,0,width,0,1);
* var vol2 = 1-vol1; // when sound1 is loud, sound2 is quiet and vice versa
*
* gain1.amp(vol1,0.5,0);
* gain2.amp(vol2,0.5,0);
*
* // map the vertical position of the mouse to values useable for 'master volume control'
* var vol3 = map(mouseY,0,height,0,1);
* gain3.amp(vol3,0.5,0);
* }
*</code></div>
*
*/
p5.Gain = function () {
this.ac = p5sound.audiocontext;
this.input = this.ac.createGain();
this.output = this.ac.createGain();
// otherwise, Safari distorts
this.input.gain.value = 0.5;
this.input.connect(this.output);
};
/**
* Connect a source to the gain node.
*
* @method connectSource
* @param {Object} src p5.sound / Web Audio object with a sound
* output.
*/
p5.Gain.prototype.connectSource = function (src) {
src.connect(this.input);
};
/**
* Send output to a p5.sound or web audio object
*
* @method connect
* @param {Object} unit
*/
p5.Gain.prototype.connect = function (unit) {
var u = unit || p5.soundOut.input;
this.output.connect(u.input ? u.input : u);
};
/**
* Disconnect all output.
*
* @method disconnect
*/
p5.Gain.prototype.disconnect = function () {
this.output.disconnect();
};
/**
* Set the output level of the gain node.
*
* @method amp
* @param {Number} volume amplitude between 0 and 1.0
* @param {Number} [rampTime] create a fade that lasts rampTime
* @param {Number} [timeFromNow] schedule this event to happen
* seconds from now
*/
p5.Gain.prototype.amp = function (vol, rampTime, tFromNow) {
var rampTime = rampTime || 0;
var tFromNow = tFromNow || 0;
var now = p5sound.audiocontext.currentTime;
var currentVol = this.output.gain.value;
this.output.gain.cancelScheduledValues(now);
this.output.gain.linearRampToValueAtTime(currentVol, now + tFromNow + 0.001);
this.output.gain.linearRampToValueAtTime(vol, now + tFromNow + rampTime + 0.001);
};
}(master, sndcore);
var src_app;
src_app = function () {
'use strict';
var p5SOUND = sndcore;
return p5SOUND;
}(sndcore, master, helpers, panner, soundfile, amplitude, fft, signal, oscillator, env, pulse, noise, audioin, filter, delay, reverb, metro, looper, soundRecorder, peakdetect);
}(sndcore, master, helpers, panner, soundfile, amplitude, fft, signal, oscillator, env, pulse, noise, audioin, filter, delay, reverb, metro, looper, soundRecorder, peakdetect, gain);
}));
4 changes: 2 additions & 2 deletions lib/p5.sound.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ define(function (require) {
require('looper');
require('soundRecorder');
require('peakdetect');
require('gain');

return p5SOUND;

Expand Down
140 changes: 140 additions & 0 deletions src/gain.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
define(function (require) {
'use strict';

var p5sound = require('master');
require('sndcore');


/**
* A gain node is usefull to set the relative volume of sound.
* It's typically used to build mixers.
*
* @class p5.Gain
* @constructor
* @example
* <div><code>
*
* // load two soundfile and crossfade beetween them
* var sound1,sound2;
* var gain1, gain2, gain3;
*
* function preload(){
* soundFormats('ogg', 'mp3');
* sound1 = loadSound('../_files/Damscray_-_Dancing_Tiger_01');
* sound2 = loadSound('../_files/beat.mp3');
* }
*
* function setup() {
* createCanvas(400,200);
*
* // create a 'master' gain to which we will connect both soundfiles
* gain3 = new p5.Gain();
* gain3.connect();
*
* // setup first sound for playing
* sound1.rate(1);
* sound1.loop();
* sound1.disconnect(); // diconnect from p5 output
*
* gain1 = new p5.Gain(); // setup a gain node
* gain1.connectSource(sound1); // connect the first sound to its input
* gain1.connect(gain3); // connect its output to the 'master'
*
* sound2.rate(1);
* sound2.disconnect();
* sound2.loop();
*
* gain2 = new p5.Gain();
* gain2.connectSource(sound2);
* gain2.connect(gain3);
*
* }
*
* function draw(){
* background(180);
*
* // calculate the horizontal distance beetween the mouse and the right of the screen
* var d = dist(mouseX,0,width,0);
*
* // map the horizontal position of the mouse to values useable for volume control of sound1
* var vol1 = map(mouseX,0,width,0,1);
* var vol2 = 1-vol1; // when sound1 is loud, sound2 is quiet and vice versa
*
* gain1.amp(vol1,0.5,0);
* gain2.amp(vol2,0.5,0);
*
* // map the vertical position of the mouse to values useable for 'master volume control'
* var vol3 = map(mouseY,0,height,0,1);
* gain3.amp(vol3,0.5,0);
* }
*</code></div>
*
*/

p5.Gain = function() {
this.ac = p5sound.audiocontext;

this.input = this.ac.createGain();
this.output = this.ac.createGain();

// otherwise, Safari distorts
this.input.gain.value = 0.5;
this.input.connect(this.output);
}

/**
* Connect a source to the gain node.
*
* @method connectSource
* @param {Object} src p5.sound / Web Audio object with a sound
* output.
*/


p5.Gain.prototype.connectSource = function(src) {
src.connect(this.input);
}

/**
* Send output to a p5.sound or web audio object
*
* @method connect
* @param {Object} unit
*/
p5.Gain.prototype.connect = function(unit) {
var u = unit || p5.soundOut.input;
this.output.connect(u.input ? u.input : u);
};

/**
* Disconnect all output.
*
* @method disconnect
*/
p5.Gain.prototype.disconnect = function() {
this.output.disconnect();
};

/**
* Set the output level of the gain node.
*
* @method amp
* @param {Number} volume amplitude between 0 and 1.0
* @param {Number} [rampTime] create a fade that lasts rampTime
* @param {Number} [timeFromNow] schedule this event to happen
* seconds from now
*/
p5.Gain.prototype.amp = function(vol, rampTime, tFromNow) {
var rampTime = rampTime || 0;
var tFromNow = tFromNow || 0;
var now = p5sound.audiocontext.currentTime;
var currentVol = this.output.gain.value;
this.output.gain.cancelScheduledValues(now);
this.output.gain.linearRampToValueAtTime(currentVol, now + tFromNow + .001);
this.output.gain.linearRampToValueAtTime(vol, now + tFromNow + rampTime + .001);
};

});



0 comments on commit cf9428f

Please sign in to comment.