Turning audio into a pattern

Hi, I’m a beginner in programming and got this code, it draws a line of the audio input as time goes by. Is it possible that when the line is formed till the end of the screen it stays still and then starts again underneath this line. so it will be a pattern? I hope my question is clear. Thanks in advance for the help.

/**
 * Grab audio from the microphone input and draw a circle whose size
 * is determined by how loud the audio input is.
 */

import processing.sound.*;
import processing.svg.*;

boolean record; 

AudioIn input;
Amplitude loudness;

int streamSize = 1500;
float[] sensorHist = new float[streamSize]; //history data to show


void setup() {
  size(1500, 500);
  background(255);

  // Create an Audio input and grab the 1st channel
  input = new AudioIn(this, 0);

  // Begin capturing the audio input
  input.start();
  // start() activates audio capture so that you can use it as
  // the input to live sound analysis, but it does NOT cause the
  // captured audio to be played back to you. if you also want the
  // microphone input to be played back to you, call
  //    input.play();
  // instead (be careful with your speaker volume, you might produce
  // painful audio feedback. best to first try it out wearing headphones!)

  // Create a new Amplitude analyzer
  loudness = new Amplitude(this);

  // Patch the input to the volume analyzer
  loudness.input(input);
}


void draw() {
  if (record) {
    beginRecord (SVG, "frame-####.svg");
  }
  // Adjust the volume of the audio input based on mouse position
  float inputLevel = map(mouseY, 0, height, 1.0, 0.0);
  input.amp(inputLevel);

  // loudness.analyze() return a value between 0 and 1. To adjust
  // the scaling and mapping of an ellipse we scale from 0 to 0.5
  float volume = loudness.analyze();
  appendArray(sensorHist, map(volume, 0, 0.5, 0, 500)); //store the data to history (for visualization)
  
  background(255);
  noFill();
  stroke(0);
  beginShape();
  for (int n=0; n<sensorHist.length; n++) {
    float h = sensorHist[n];
    vertex(n, h);
  }
  if (record) {
    endRecord();
    record = false;
  }
  endShape();
}

void mousePressed() {
  record = true;
}

//Append a value to a float[] array.
float[] appendArray (float[] _array, float _val) {
  float[] array = _array;
  float[] tempArray = new float[_array.length-1];
  arrayCopy(array, tempArray, tempArray.length);
  array[0] = _val;
  arrayCopy(tempArray, 0, array, 1, tempArray.length);
  return array;
}

It’s possible.

Idea 1 a :

when the line is complete (n>width-20) start a new line: You have an y-value yAdd you add to h. Increase yAdd by 40 or so.

Since your are not drawing points but vertexes within beginShape(); and endShape(); this would look not very good.

You could say endShape(); when line is complete (n>width-20) and start a new line with beginShape(); and x is 10 and yAdd=yAdd+40;

Tricky.

It might work when you don’t use background in draw().

Idea 1 b :

store the x-position (your n in vertex(n, h);) together with the value of the sound bit.
proceed like in 1 a).

Idea 2 :

Advanced approach when you are good at programming.

Make a class OneLine
Store your line float[] sensorHist and yAdd etc. inside it.

Make an ArrayList of OneLine before setup.

When a line is finished, add it to the ArrayList

Chrisir

[EDITED]

1 Like