Draw waveform between 2 spots using beads.
in
Share your Work
•
11 months ago
This is a piece of code that uses Minim's way of forming a waveform but using bead library.
The truth is that i lack experience of programming and am new at processing so i made it by changing continously the data till it work as i wanted it. I don't know exactly how buffers and bead works yet and in the end how really the waveform is formed so any suggestions for improvements are for the best, and please correct anything you want and feedback..
The patch uses 2 points ( p1 & p2 ) and receives sound data from the AudioContext's buffer, so it probably can draw any waveform.
- import beads.*;
- AudioContext ac;
- void setup() {
- size(300,300);
- ac = new AudioContext();
- Envelope freqEnv = new Envelope(ac, 500);
- WavePlayer wp = new WavePlayer(ac, freqEnv, Buffer.SINE);
- freqEnv.addSegment(1000, 1000);
- Gain g = new Gain(ac, 1, 0.1);
- g.addInput(wp);
- ac.out.addInput(g);
- ac.start();
- }
- void draw() {
- int[] p1 = {height/2,width/2}; // Set 1st point p1.
- int[] p2 = {mouseX,mouseY}; // Set 2nd point p2.
- int amp = 500; // Set the amplitube of the wave, in pixels.
- // Factors for the placement of the waveform ("polar" placement):
- float fi = atan2(p2[1]-p1[1],p2[0]-p1[0]); // fi sets the rotation angle.
- int yp = (int) sqrt(pow((p2[0]-p1[0]),2)+pow((p2[1]-p1[1]),2)); // yp sets the lenght.
- // Set the colors
- background(0);
- stroke(255);
- // Set points so can see the points...
- ellipse(p1[0],p1[1],10,10);
- ellipse(p2[0],p2[1],10,10);
- // Apply the transformation
- translate(p1[0],p1[1]);
- rotate(fi);
- for(int i = 0 ; i < yp; i++) { // In case it "throws exception out of bounds for sth" for this line, set yp-1 instead of yp.
- int buffIndex = i * ac.getBufferSize() / width;
- int buffIndex2= (i+1) * ac.getBufferSize() / width;
- int vOffset = (int)((1+ ac.out.getValue(0, buffIndex)) * amp);
- int vOffset2 = (int)((1+ ac.out.getValue(0, buffIndex2)) * amp);
- line(i,vOffset-amp,i+1,vOffset2-amp);
- // or point(i,vOffset-amp);
- /* Just for fun, try play by changing the prices at fact1 and fact2, see what happens.
- Sths are not good, thers are usefool...
- float fact1 = 1.1; // or float fact1 = 1.1;
- float fact2 = 0.9; // float fact2 = 1.1;
- line(i,vOffset-amp*fact1,i+1,vOffset2-amp*fact2);
- */
- }
- }