Skip to content

Commit d893159

Browse files
authored
Merge pull request #214 from jvntf/synths
Synths
2 parents d94bdf5 + 6e3f153 commit d893159

File tree

16 files changed

+1055
-154
lines changed

16 files changed

+1055
-154
lines changed

Gruntfile.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,10 @@ module.exports = function(grunt) {
9292
'signal': 'src/signal',
9393
'metro': 'src/metro',
9494
'peakdetect': 'src/peakDetect',
95-
'gain': 'src/gain'
95+
'gain': 'src/gain',
96+
'audioVoice': 'src/audioVoice',
97+
'monosynth': 'src/monosynth',
98+
'polysynth': 'src/polysynth'
9699
},
97100
useStrict: true,
98101
wrap: {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<head>
2+
<script language="javascript" type="text/javascript" src="../../lib/p5.min.js"></script>
3+
4+
<script language="javascript" type="text/javascript" src="../../lib/addons/p5.dom.js"></script>
5+
6+
<script language="javascript" type="text/javascript" src="../../lib/p5.sound.js"></script>
7+
8+
<script language="javascript" type="text/javascript" src="sketch.js"></script>
9+
10+
</head>
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// mouseX = playback position
2+
// mouseY = playback rate
3+
// up arrow increase grain duration
4+
// down arrow decrease grain duration
5+
6+
var source_file; // sound file
7+
var src_length; // hold its duration
8+
var peaks; // an array of peaks for the visual
9+
var pg;
10+
11+
var psynth;
12+
var grainDur = 1; // length of the grain
13+
14+
function preload(){
15+
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
16+
}
17+
18+
function setup() {
19+
createCanvas(800, 250);
20+
frameRate(25);
21+
22+
psynth = new p5.PolySynth(25,GranularVoice);
23+
24+
src_length = source_file.duration(); // store the sound duration
25+
peaks = source_file.getPeaks(); // get an array of peaks
26+
// draw the waveform to an off-screen graphic
27+
pg = createGraphics(width,height);
28+
pg.background(180);
29+
pg.noFill();
30+
pg.stroke(0);
31+
for (var i = 0 ; i < peaks.length ; i++){
32+
var x = map(i,0,peaks.length,0,width);
33+
var y = map(peaks[i],0,1,0,height);
34+
pg.line(x,height/2,x,height/2+y);
35+
pg.line(x,height/2,x,height/2-y);
36+
}
37+
}
38+
39+
function draw() {
40+
background(180);
41+
42+
if (mouseIsPressed){
43+
var start_play = map(mouseX,0,width,0,src_length); // map mouseX to position in the source
44+
var pitch = map(mouseY,0,height,1.5,0.5); // map mouseY to the rate the sound will be played
45+
//console.log(psynth.poly_counter);
46+
psynth.setADSR(grainDur*2/5,0,0,grainDur*2/5);
47+
psynth.voices[psynth.poly_counter].playGrain(start_play, pitch,grainDur);
48+
psynth.play();
49+
}
50+
51+
image(pg,0,0); // display our waveform representation
52+
// draw playhead position
53+
fill(255,255,180,150);
54+
noStroke();
55+
rect(mouseX,0,map(grainDur,0,src_length,0,width),height);
56+
57+
fill(0);
58+
text('Grain Duration : ' + grainDur , 5,25);
59+
}
60+
61+
function keyPressed(){
62+
if (keyCode === DOWN_ARROW){
63+
grainDur -=0.05;
64+
}
65+
else if (keyCode === UP_ARROW){
66+
grainDur += 0.05;
67+
}
68+
grainDur = constrain(grainDur,0.1,25);
69+
}
70+
71+
72+
function GranularVoice(){
73+
74+
p5.AudioVoice.call(this);
75+
76+
this.amp = 0.05;
77+
78+
source_file.connect(this.synthOut);
79+
80+
this.playGrain = function(start,rate,grainDur){
81+
source_file.play(0,rate,this.amp,start,grainDur); // we need to play longer than grainDur because of the rate
82+
}
83+
84+
this.setParams = function(params){
85+
86+
}
87+
}
88+
GranularVoice.prototype = Object.create(p5.AudioVoice.prototype);
89+
GranularVoice.prototype.constructor = GranularVoice;

examples/monosynth_basic/index.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<head>
2+
<script language="javascript" type="text/javascript" src="../../lib/p5.js"></script>
3+
4+
<script language="javascript" type="text/javascript" src="../../lib/addons/p5.dom.js"></script>
5+
6+
<script language="javascript" type="text/javascript" src="../../lib/p5.sound.js"></script>
7+
8+
<script language="javascript" type="text/javascript" src="sketch.js"></script>
9+
10+
</head>

examples/monosynth_basic/sketch.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Play a random note
3+
* every time you press a key
4+
*/
5+
6+
var monoSynth;
7+
8+
function setup() {
9+
monoSynth = new p5.MonoSynth();
10+
11+
createCanvas(400, 400);
12+
text('press to play a random note at a random velocity', 20, 20);
13+
}
14+
15+
function mousePressed() {
16+
// pick a random midi note
17+
var midiVal = round( random(50,72) );
18+
monoSynth.triggerAttack(midiVal, random() );
19+
}
20+
21+
function mouseReleased() {
22+
monoSynth.triggerRelease();
23+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<head>
2+
<script language="javascript" type="text/javascript" src="../../lib/p5.js"></script>
3+
4+
<script language="javascript" type="text/javascript" src="../../lib/addons/p5.dom.js"></script>
5+
6+
<script language="javascript" type="text/javascript" src="../../lib/p5.sound.js"></script>
7+
<script language="javascript" type="text/javascript" src="sketch.js"></script>
8+
9+
</head>
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
var polySynth;
2+
var octave;
3+
4+
var keysDown={};
5+
var colors = {};
6+
var keys = {};
7+
var notes = {};
8+
9+
var compressor;
10+
11+
var description;
12+
function setup() {
13+
14+
polySynth = new p5.PolySynth(p5.MonoSynth, 8);
15+
stroke(0);
16+
for(var i = 0; i<17; i++) {
17+
if(i!==11 && i!==15){
18+
colors[i] = [color(255),false];
19+
}
20+
}
21+
22+
keys = {'A':'C0', 'W':'C#0', 'S':'D0', 'E':'Eb0', 'D':'E0', 'F':'F0', 'T':'F#0','G':'G0',
23+
'Y':'G#0', 'H':'A1', 'U':'Bb1', 'J':'B1', 'K':'C1', 'O':'C#1', 'L':'D1'};
24+
25+
notes = {'C0':0, 'D0':1 , 'E0':2 , 'F0':3, 'G0':4 , 'A1':5 , 'B1':6 , 'C1':7 , 'D1':8,
26+
'C#0': 9, 'Eb0':10, 'F#0':12, 'G#0':13, 'Bb1':14, 'C#1':16};
27+
octave = 3;
28+
29+
30+
description = createP('p5.PolySynth is a handler class for monophonic extensions '+
31+
'of the p5.AudioVoice class. Use the computer keyboard to play notes on '+
32+
'the piano roll. Use UP_ARROW and DOWN_ARROW to change octave');
33+
polySynth.disconnect();
34+
compressor = new p5.Compressor();
35+
polySynth.connect(compressor);
36+
37+
38+
}
39+
40+
function draw() {
41+
background(255);
42+
createCanvas(800,600);
43+
44+
//draw white keys
45+
for(var i = 0; i<9; i++) {
46+
fill(colors[i][0]);
47+
rect(50*i,0,50,230);
48+
}
49+
//draw black keys
50+
for(var i = 9; i<17; i++) {
51+
if(i!==11 && i!==15){
52+
fill(colors[i][0]);
53+
rect(50*(i - 8) - 12.5, 0, 25, 150);
54+
}
55+
}
56+
57+
}
58+
59+
function keyPressed() {
60+
//OCTAVEf
61+
if (keyCode === UP_ARROW) {
62+
octave +=1;
63+
} else if (keyCode === DOWN_ARROW) {
64+
octave -=1;
65+
}
66+
else if (keyToMidi() && keysDown[key] !== true){
67+
// var keyToMidi() = keyToMidi();
68+
var currentOctave = Number(keyToMidi()[keyToMidi().length-1]) + octave;
69+
var currentKey= keyToMidi().substr(0,keyToMidi().length -1) + currentOctave;
70+
71+
polySynth.noteAttack(currentKey);
72+
var index = notes[keyToMidi()];
73+
74+
colors[index][1] = !colors[index][1];
75+
colors[index][1] ? colors[index][0] = color(random(255),random(255),random(255)) : colors[index][0] = color(255);
76+
if (keysDown[key] === undefined) {
77+
keysDown[key] = true;
78+
}
79+
}
80+
}
81+
82+
function keyReleased() {
83+
Object.keys(keysDown).forEach(function(key) {
84+
85+
if(!keyIsDown(key.charCodeAt(0))){
86+
// var keyToMidi() = keyToMidi();
87+
88+
var currentOctave = Number(keyToMidi()[keyToMidi().length - 1]) + octave;
89+
currentKey = keyToMidi().substr(0,keyToMidi().length -1) + currentOctave;
90+
polySynth.noteRelease(currentKey);
91+
92+
var index = notes[keyToMidi(keyCodeToLetter(key))];
93+
colors[index][1] = !colors[index][1];
94+
colors[index][1] ? colors[index][0] = color(random(255),random(255),random(255)) : colors[index][0] = color(255);
95+
delete keysDown[key];
96+
}
97+
});
98+
}
99+
100+
function keyToMidi(keyboard) {
101+
var thisKey = typeof keyboard ==='undefined' ? key : keyboard
102+
return keys[thisKey];
103+
}
104+
105+
function keyCodeToLetter(code) {
106+
107+
}

0 commit comments

Comments
 (0)