Skip to content

Commit 06022c9

Browse files
jvntftherewasaguy
authored andcommitted
add p5.EQ (#183)
* started writing compressor.js * rough structure of compressor * draft of compressor * updated gruntfile, added restrictions and error logging to compressor * removed range error checking * compressor test * started draft of EQ * assigned bands to EQ * added band mod functions to eq * added to gruntfile * eq adjusting band levels based on filter.output.amp() * eq works based on p5.Filter.biquad.gain.value * added control points to eq example * spline curve in example, next: toggle band * using p5.Filter for bands makes EQ innacurate; handled by deleting output nodes of each filter * much better results using a raw biquad filter * added comments to eq.js * improved example * toggle works * added example to eq.js * fixed the example * reverted to pre-merge state * eq changed to have 2 sizes, 3 and 8 band * finished EQ with options of 3 or 8 bands * started secondary eq example * dj mixer working but EQ is connected wrong * dj mixer example complete with EQs * ToneJs is not getting imported correctly here * added tests for eq * finished both examples and cleaned up * added doc * refactored EQ to use a lightweight p5.Filter * added .gain() to p5.Filter, allows control of Biquad gain attribute * cleaned up * added return p5.EQ * fixed preoload() problem in eq_dj_mixer example * added a toggle function to p5.Filter() * reset this._untoggledType when calling .setType() * corrected some variable name inconsistencies * updated eq tests to use correct API, corrected some errors * added .addBand() and filter disconnection on dispose * changed .freq to .freq() * draw FFT logarithmically * added freq and gain labels to eq cntrl points * adjusted labels * added to index.html * added EQ.set() and fixed test * eslint --fix src/eq.js * update eq.js - create eqFilter class with a proper dispose method and overrides of invalid methods - comment out the set method - update comments * added more override methods tp EQFilter.js * added a few methods to eqFilter * added inline example to EQ
1 parent ba48038 commit 06022c9

File tree

16 files changed

+779
-10
lines changed

16 files changed

+779
-10
lines changed

Gruntfile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ module.exports = function(grunt) {
8181
'effect': 'src/effect',
8282
'filter': 'src/filter',
8383
'reverb': 'src/reverb',
84+
'eq': 'src/eq',
8485
'distortion': 'src/distortion',
8586
'compressor': 'src/compressor',
8687
'looper': 'src/looper',

examples/Gymnopedia.mp3

3.22 MB
Binary file not shown.

examples/graphical_eq/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/graphical_eq/sketch.js

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
/**
2+
* Example: Apply a p5.EQ filter to a p5.Noise.
3+
* Visualize the sound with FFT.
4+
* Use control points to change the spline that shapes the soundwave
5+
*/
6+
7+
var fft;
8+
9+
var eq, eqSize;
10+
11+
//Array to hold contrl points
12+
var cntrlPts = [];
13+
var ctrlPtRad, cntrlIndex;
14+
15+
//Array to hold the spline vertices
16+
var splineVerts = [];
17+
18+
var pressed;
19+
var description;
20+
21+
22+
var soundFile;
23+
24+
function preload() {
25+
soundFormats('mp3', 'ogg');
26+
soundFile = loadSound('../files/Soni_Ventorum_Wind_Quintet_-_08_-_Danzi_Wind_Quintet_Op_67_No_3_In_E-Flat_Major_4_Allegretto');
27+
}
28+
29+
30+
31+
32+
function setup() {
33+
createCanvas(710, 256);
34+
//sound wave color
35+
fill(255, 40, 255);
36+
ctrlPtRad = 15;
37+
38+
eqSize = 8;
39+
eq = new p5.EQ(eqSize);
40+
41+
// Disconnect soundFile from master output.
42+
// Then, connect it to the EQ, so that we only hear the EQ'd sound
43+
soundFile.loop();
44+
soundFile.disconnect();
45+
soundFile.connect(eq);
46+
47+
//use an fft to visualize the sound
48+
fft = new p5.FFT();
49+
50+
//Evenly spaced control points line up with logarithmically spaced
51+
//filter frequencies on the logarithmically drawn spectrum
52+
for (var i = 0; i < eqSize; i++) {
53+
cntrlPts[i] = new CntrlPt(i,
54+
//map(x, 0, Math.log(1024), 0, width), height/2);
55+
i + i*101, height/2);
56+
splineVerts[i] = [cntrlPts[i].x,cntrlPts[i].y];
57+
58+
}
59+
60+
61+
description = createDiv("p5.EQ:<br>"+
62+
"Use the p5.EQ to shape a sound spectrum. The p5.EQ is "+
63+
"built with Web Audio Biquad Filters (peaking mode) and can "+
64+
"be set to use 3 or 8 bands.");
65+
description.position(10,300);
66+
}
67+
68+
function draw() {
69+
background(30);
70+
71+
// Draw every value in the FFT spectrum analysis where
72+
// x = lowest (10Hz) to highest (22050Hz) frequencies,
73+
// h = energy / amplitude at that frequency
74+
var spectrum = fft.analyze();
75+
76+
noStroke();
77+
for (var i = 0; i< spectrum.length; i++){
78+
//var x = map(i, 0, spectrum.length, 0, width);
79+
var x = map(Math.log((i+1)/8), 0, Math.log(spectrum.length/8), 0, width);
80+
var h = -height + map(spectrum[i], 0, 255, height, 0);
81+
rect(x, height, width/spectrum.length, h) ;
82+
83+
}
84+
85+
86+
//When mouse is pressed, move relevant control point, then display all
87+
if (pressed) {cntrlPts[cntrlIndex].move();}
88+
89+
for (var i = 0; i < cntrlPts.length; i++) {
90+
cntrlPts[i].display();
91+
splineVerts[i] = [cntrlPts[i].x, cntrlPts[i].y];
92+
}
93+
94+
//Draw a spline to connect control points
95+
stroke(255,255,255);
96+
noFill();
97+
beginShape();
98+
curveVertex( splineVerts[0][0],splineVerts[0][1])
99+
for (var i = 0; i < splineVerts.length; i++) {
100+
curveVertex( splineVerts[i][0],splineVerts[i][1]);
101+
}
102+
curveVertex( splineVerts[eqSize-1][0],splineVerts[eqSize-1][1])
103+
endShape();
104+
}
105+
106+
//Class for each control point
107+
function CntrlPt(i,x,y){
108+
this.c = color(255);
109+
this.x = x;
110+
this.y = y;
111+
this.index = i;
112+
113+
this.display = function () {
114+
fill(this.c);
115+
ellipse(this.x,this.y,ctrlPtRad,ctrlPtRad);
116+
rectMode(CENTER);
117+
noStroke();
118+
119+
var upDown = this.index % 2 > 0 ? 1 : -1
120+
var textY;
121+
var textX;
122+
if (this.index === 0) {
123+
textX = this.x + 10;
124+
}
125+
else if (this.index === eq.bands.length - 1){
126+
textX = this.x - 70;
127+
}
128+
else{
129+
textX = this.x - 18;
130+
}
131+
if (upDown > 0) {
132+
text("freq: " + eq.bands[this.index].freq(),textX,this.y + upDown*40);
133+
text("gain: " + eq.bands[this.index].gain(),textX,this.y + upDown *25);
134+
} else {
135+
text("gain: " + eq.bands[this.index].gain(),textX,this.y + upDown *40);
136+
text("freq: " + eq.bands[this.index].freq(),textX,this.y + upDown*25);
137+
}
138+
139+
}
140+
141+
this.move = function () {
142+
if (mouseX < 1) { this.x = 1;}
143+
else if (mouseX > width) {this.x = width -1;}
144+
else if (mouseY < 1) {this.y = 1;}
145+
else if (mouseY > height) {this.y = height - 1;}
146+
else {
147+
this.y = mouseY;
148+
eq.bands[this.index].biquad.gain.value = map(this.y, 0, height, 40, -40);
149+
}
150+
}
151+
152+
//Checks to see if mouse is ontop of control point
153+
this.mouseOver = function () {
154+
if (mouseX > this.x - ctrlPtRad && mouseX < this.x + ctrlPtRad
155+
&& mouseY < this.y + ctrlPtRad && mouseY > this.y - ctrlPtRad){
156+
return true;
157+
} else {
158+
return false;
159+
}s
160+
}
161+
}
162+
163+
function mousePressed(){
164+
for (var i = 0; i < cntrlPts.length; i++) {
165+
if (cntrlPts[i].mouseOver()){
166+
pressed = true;
167+
cntrlIndex = i;
168+
break;
169+
}
170+
}
171+
}
172+
173+
function mouseReleased(){
174+
pressed = false;
175+
}

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ <h2>p5.sound
3131
<div><a href="examples/envelopeRamp">envelopeRamp</a></div>
3232
<div><a href="examples/envelope_exponential_play">envelope_exponential_play</a></div>
3333
<div><a href="examples/envelope_exponential_trig_rel">envelope_exponential_trig_rel</a></div>
34+
<div><a href="examples/graphical_eq">graphical_eq</a></div>
3435
<div><a href="examples/granular_sampler">granular_sampler</a></div>
3536
<div><a href="examples/loadSound_404ErrorHandling">loadSound_404ErrorHandling</a></div>
3637
<div><a href="examples/loadSound_callback">loadSound_callback</a></div>

0 commit comments

Comments
 (0)