forked from processing/p5.js-sound
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
338 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
|
||
}); | ||
|
||
|
||
|