Skip to content

Commit

Permalink
Merge pull request #214 from jvntf/synths
Browse files Browse the repository at this point in the history
Synths
  • Loading branch information
therewasaguy authored Aug 29, 2017
2 parents d94bdf5 + 6e3f153 commit d893159
Show file tree
Hide file tree
Showing 16 changed files with 1,055 additions and 154 deletions.
5 changes: 4 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ module.exports = function(grunt) {
'signal': 'src/signal',
'metro': 'src/metro',
'peakdetect': 'src/peakDetect',
'gain': 'src/gain'
'gain': 'src/gain',
'audioVoice': 'src/audioVoice',
'monosynth': 'src/monosynth',
'polysynth': 'src/polysynth'
},
useStrict: true,
wrap: {
Expand Down
10 changes: 10 additions & 0 deletions examples/granular_sampler_psynth/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.min.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.js"></script>

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

</head>
89 changes: 89 additions & 0 deletions examples/granular_sampler_psynth/sketch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// mouseX = playback position
// mouseY = playback rate
// up arrow increase grain duration
// down arrow decrease grain duration

var source_file; // sound file
var src_length; // hold its duration
var peaks; // an array of peaks for the visual
var pg;

var psynth;
var grainDur = 1; // length of the grain

function preload(){
source_file = loadSound('../files/Soni_Ventorum_Wind_Quintet_-_08_-_Danzi_Wind_Quintet_Op_67_No_3_In_E-Flat_Major_4_Allegretto.mp3'); // preload the sound
}

function setup() {
createCanvas(800, 250);
frameRate(25);

psynth = new p5.PolySynth(25,GranularVoice);

src_length = source_file.duration(); // store the sound duration
peaks = source_file.getPeaks(); // get an array of peaks
// draw the waveform to an off-screen graphic
pg = createGraphics(width,height);
pg.background(180);
pg.noFill();
pg.stroke(0);
for (var i = 0 ; i < peaks.length ; i++){
var x = map(i,0,peaks.length,0,width);
var y = map(peaks[i],0,1,0,height);
pg.line(x,height/2,x,height/2+y);
pg.line(x,height/2,x,height/2-y);
}
}

function draw() {
background(180);

if (mouseIsPressed){
var start_play = map(mouseX,0,width,0,src_length); // map mouseX to position in the source
var pitch = map(mouseY,0,height,1.5,0.5); // map mouseY to the rate the sound will be played
//console.log(psynth.poly_counter);
psynth.setADSR(grainDur*2/5,0,0,grainDur*2/5);
psynth.voices[psynth.poly_counter].playGrain(start_play, pitch,grainDur);
psynth.play();
}

image(pg,0,0); // display our waveform representation
// draw playhead position
fill(255,255,180,150);
noStroke();
rect(mouseX,0,map(grainDur,0,src_length,0,width),height);

fill(0);
text('Grain Duration : ' + grainDur , 5,25);
}

function keyPressed(){
if (keyCode === DOWN_ARROW){
grainDur -=0.05;
}
else if (keyCode === UP_ARROW){
grainDur += 0.05;
}
grainDur = constrain(grainDur,0.1,25);
}


function GranularVoice(){

p5.AudioVoice.call(this);

this.amp = 0.05;

source_file.connect(this.synthOut);

this.playGrain = function(start,rate,grainDur){
source_file.play(0,rate,this.amp,start,grainDur); // we need to play longer than grainDur because of the rate
}

this.setParams = function(params){

}
}
GranularVoice.prototype = Object.create(p5.AudioVoice.prototype);
GranularVoice.prototype.constructor = GranularVoice;
10 changes: 10 additions & 0 deletions examples/monosynth_basic/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.js"></script>

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

</head>
23 changes: 23 additions & 0 deletions examples/monosynth_basic/sketch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Play a random note
* every time you press a key
*/

var monoSynth;

function setup() {
monoSynth = new p5.MonoSynth();

createCanvas(400, 400);
text('press to play a random note at a random velocity', 20, 20);
}

function mousePressed() {
// pick a random midi note
var midiVal = round( random(50,72) );
monoSynth.triggerAttack(midiVal, random() );
}

function mouseReleased() {
monoSynth.triggerRelease();
}
9 changes: 9 additions & 0 deletions examples/polyphonicSynth-Keyboard/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<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.js"></script>
<script language="javascript" type="text/javascript" src="sketch.js"></script>

</head>
107 changes: 107 additions & 0 deletions examples/polyphonicSynth-Keyboard/sketch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
var polySynth;
var octave;

var keysDown={};
var colors = {};
var keys = {};
var notes = {};

var compressor;

var description;
function setup() {

polySynth = new p5.PolySynth(p5.MonoSynth, 8);
stroke(0);
for(var i = 0; i<17; i++) {
if(i!==11 && i!==15){
colors[i] = [color(255),false];
}
}

keys = {'A':'C0', 'W':'C#0', 'S':'D0', 'E':'Eb0', 'D':'E0', 'F':'F0', 'T':'F#0','G':'G0',
'Y':'G#0', 'H':'A1', 'U':'Bb1', 'J':'B1', 'K':'C1', 'O':'C#1', 'L':'D1'};

notes = {'C0':0, 'D0':1 , 'E0':2 , 'F0':3, 'G0':4 , 'A1':5 , 'B1':6 , 'C1':7 , 'D1':8,
'C#0': 9, 'Eb0':10, 'F#0':12, 'G#0':13, 'Bb1':14, 'C#1':16};
octave = 3;


description = createP('p5.PolySynth is a handler class for monophonic extensions '+
'of the p5.AudioVoice class. Use the computer keyboard to play notes on '+
'the piano roll. Use UP_ARROW and DOWN_ARROW to change octave');
polySynth.disconnect();
compressor = new p5.Compressor();
polySynth.connect(compressor);


}

function draw() {
background(255);
createCanvas(800,600);

//draw white keys
for(var i = 0; i<9; i++) {
fill(colors[i][0]);
rect(50*i,0,50,230);
}
//draw black keys
for(var i = 9; i<17; i++) {
if(i!==11 && i!==15){
fill(colors[i][0]);
rect(50*(i - 8) - 12.5, 0, 25, 150);
}
}

}

function keyPressed() {
//OCTAVEf
if (keyCode === UP_ARROW) {
octave +=1;
} else if (keyCode === DOWN_ARROW) {
octave -=1;
}
else if (keyToMidi() && keysDown[key] !== true){
// var keyToMidi() = keyToMidi();
var currentOctave = Number(keyToMidi()[keyToMidi().length-1]) + octave;
var currentKey= keyToMidi().substr(0,keyToMidi().length -1) + currentOctave;

polySynth.noteAttack(currentKey);
var index = notes[keyToMidi()];

colors[index][1] = !colors[index][1];
colors[index][1] ? colors[index][0] = color(random(255),random(255),random(255)) : colors[index][0] = color(255);
if (keysDown[key] === undefined) {
keysDown[key] = true;
}
}
}

function keyReleased() {
Object.keys(keysDown).forEach(function(key) {

if(!keyIsDown(key.charCodeAt(0))){
// var keyToMidi() = keyToMidi();

var currentOctave = Number(keyToMidi()[keyToMidi().length - 1]) + octave;
currentKey = keyToMidi().substr(0,keyToMidi().length -1) + currentOctave;
polySynth.noteRelease(currentKey);

var index = notes[keyToMidi(keyCodeToLetter(key))];
colors[index][1] = !colors[index][1];
colors[index][1] ? colors[index][0] = color(random(255),random(255),random(255)) : colors[index][0] = color(255);
delete keysDown[key];
}
});
}

function keyToMidi(keyboard) {
var thisKey = typeof keyboard ==='undefined' ? key : keyboard
return keys[thisKey];
}

function keyCodeToLetter(code) {

}
Loading

0 comments on commit d893159

Please sign in to comment.