Anitaliasing?
in
Contributed Library Questions
•
2 years ago
Hi,
I'm currently building an interface that allows a user to draw a waveform into a box in realtime which will be used MAX/Msp over Osc to generate sound. I've got the waveform drawing bit working more or less how I want, but I'd like to make it look much prettier than it does (which is the whole point of using Processing rather than just the standard Max interface!). At the moment the waveform edge is covered with jaggies, and I want it to be antialiased so it looks smoother. I'm no graphics programmer and I'm slightly at a loss as to how to achieve this.
At the moment I'm drawing the curve using a quad for each position of the waveform which gives some antialiasing from the smooth() function, but, since there are so many data points it's still very jaggy; I'm sure there must be a better way to do it (line 22 2nd tab). I need to store the data for the waveform in the array in this way as this is the format I need for the audio, but it could be displayed in any way. The important thing is that the waveform display changes in realtime.
Any suggestions are welcome - I'm new to Processing so I may well be missing something really obvious!
Code posted below - I've removed a lot of extraneous stuff to keep it clear.
Many thanks,
Tim
Main:
- //Opens a wave drawing window that interfaces with a MAX buffer over OSC
- //general variables
- int drawWidth;
- int drawHeight;
- WaveBox[] waves = new WaveBox[1];
- void setup ()
- {
- size(480, 240, P2D);
- drawWidth = 440;
- drawHeight = 200;
- strokeWeight(1);
- fill(255);
- smooth();
- waves[0] = new WaveBox(20,20,drawWidth, drawHeight);
- }
- void draw()
- {
- background(255, 250, 229);
- waves[0].drawWave();
- }
- void mousePressed()
- {
- waves[0].writeMouse(false);
- }
- void mouseDragged()
- {
- waves[0].writeMouse(true);
- }
- void mouseReleased()
- {
- waves[0].mouseEnd();
- }
WaveBox tab:
- public class WaveBox {
- private int x, y, bHeight, bWidth;
- private float[] wave = new float[441];
- private boolean inBounds = false;
- WaveBox(int xPos,int yPos,int boxWidth,int boxHeight){
- x = xPos;
- y = yPos;
- bHeight = boxHeight;
- bWidth = boxWidth;
- }
- public void drawWave (){
- rect(x, y, bWidth, bHeight);
- pushMatrix();
- translate(x, y + (bHeight/2));
- line(0, 0, bWidth, 0);
- fill(0);
- //draw quad for each wave position to use antialiasing
- for (int i=0;i<440;i++){
- quad(i, (wave[i]*(bHeight/2)), i, 0, i+1, 0, i+1, (wave[i+1]*(bHeight/2)));
- }
- popMatrix();
- fill(255);
- }
- public void writeMouse(boolean drag)
- {
- int mouseDiff;
- int absMouseDiff;
- float mouseValDiff;
- int inc, count = 0;
- int adjMouseX, adjPMouseX, adjMouseY, adjPMouseY;
- float yVal;
- adjMouseX = mouseX - x;
- adjPMouseX = pmouseX - x;
- adjMouseY = mouseY - y;
- adjPMouseY = pmouseY - y;
- //if it is mousepress then write value if within bounds
- if ((drag == false) && (withinBounds(mouseX, mouseY))){
- inBounds = true;
- yVal = float((adjMouseY)-(drawHeight/2))/(drawHeight/2);
- wave[adjMouseX] = yVal;
- //if it is a mousedrag then write however many values are needed
- } else if ((drag) && (adjPMouseX != adjMouseX) && (inBounds)){
- //calc whether mouse has moved more then one pixel
- mouseDiff = adjMouseX - adjPMouseX;
- absMouseDiff = abs(mouseDiff);
- //determines whether the increment value is positive or negative
- //i.e. whether the mouse is moving right or left
- inc = mouseDiff / absMouseDiff;
- mouseValDiff = float(adjMouseY - adjPMouseY) / absMouseDiff;
- // loop through each position between the two mouse positions and
- //interpolate values between them so every pixel has a value
- while(count != mouseDiff){
- count = count + inc;
- if (((adjPMouseX + count) >= 0) && ((adjPMouseX + count) < (drawWidth))){
- yVal = ((adjPMouseY + (mouseValDiff * abs(count))-(drawHeight/2))/(drawHeight/2));
- //limit values between -1 and 1
- if (yVal > 1){
- yVal = 1;
- } else if (yVal< -1) {
- yVal = -1;
- }
- wave[adjPMouseX + count] = yVal;
- }
- }
- }
- wave[440] = wave[439];
- }
- public void mouseEnd(){
- inBounds = false;
- }
- //test if mouse position is within the box
- private boolean withinBounds(int posX, int posY){
- if ((posX >= x) && (posX < (x + bWidth)) && (posY >= y) && (posY < (y + bHeight))){
- return true;
- } else {
- return false;
- }
- }
- }
1