1

I need some help with a code for an exam in my university.

What I'm trying to do here is a visual representation of a speech between two people. So the code starts when you press "L" and then works a bit like walkie talkie so when the other person speaks need to push "A", when the word goes back to the first person he needs to press "L" again and so on.

I like the result of the code so far but my professor told me to try something and I'm not able to do it.

He would like to see the coloured lines covering all the screen in vertical and not just a portion of it and when they reach the end of the screen on the right split in two so that the first row that just got created becomes half of the screen and the new one creating in the other half. When the second row finishes and the third row is created the screen must split in 3 and so on.

I tried to achieve this but I messed up the code so I will post here the last version of it working.

I hope you can help in any way, all kind of suggestion are appreciated, thank you!

import processing.sound.*;

AudioIn input;
Amplitude amp;

int   y;
int   x;
int   incY = 141;

color bg   = color(255, 0);
color high;
color low;
color mid;

void setup() {
  size(1440, 846);
  background(bg);
  pixelDensity(displayDensity());

  input = new AudioIn(this, 0);
  input.start();

  amp = new Amplitude(this);
  amp.input(input);
}

void draw() {
  textSize(40);
  fill(0);
  float volume  = amp.analyze();
  int lncolor = int(map(volume, 0, 0.05, 0, 3));

  noFill();
  strokeCap(SQUARE);
  //strokeWeight(10);

  if (lncolor==0) {
    stroke(bg);
  }

  if (lncolor==1) {
    stroke(low);
  }

  if (lncolor==2) {
    stroke(mid);
  }

  if (lncolor==3) {
    stroke(high);
  }

  if (key == 'a') {
    x++;
    if (x==width) {
      x = 0;
      y = y + incY;
    }
    line(x, y, x, y+incY);
    high=color(72, 16, 255);
    low= color(179, 155, 255);
    mid  = lerpColor(low, high, .5);
  }

  if (key == 'l') {
    x++;
    if (x==width) {
      x = 0;
      y = y + incY;
    }
    line(x, y, x, y+incY);
    high=color(255, 128, 16);
    low= color(255, 203, 156);
    mid  = lerpColor(low, high, .5);
  }
}

1 Answers1

0

The following code won't solve all of your problems, but does show how to keep splitting the screen into proportionate rectangles based on the number of times the graph exceeds the width of the screen. The advancing green bar at the top is where your current signal would be plotted. I'll leave it to you to figure out how to get all the old signal into its respective rectangle. I was unable to run the code that you posted; error message was "Audio Input not configured in start() method". All that I saw was a blank screen.

int x = 0;
int counter = 1;

void rectGrid(int t, int w, int h) {
  int top;
  for (int k = 0; k < counter; k++) {
    top = t + k*h;
    stroke(0);
    strokeWeight(1);
    fill(random(255));
    rect( 0, top, w, h);
  }
}

void setup() {
  size(400, 400);
  background(209);
}

void draw() {
  fill(0, 255, 0);
  rect(0, 0, x++, height/counter);
  if (x == width) {
    counter++;
    println("count = ", counter + " : " + "height = ", height/counter);
    x = 0;
    background(209);
    rectGrid(0, width, height/counter);
  }
}
apodidae
  • 1,988
  • 2
  • 5
  • 9