# Turning audio into a pattern

**URL:** <https://discourse.processing.org/t/turning-audio-into-a-pattern/25694>\
**Category:** Libraries\
**Created:** [November 25, 2020, 11:22am UTC](https://discourse.processing.org/t/turning-audio-into-a-pattern/25694 "2020-11-25T11:22:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![scooijmans](https://avatars.discourse-cdn.com/v4/letter/s/b487fb/32.png) [@scooijmans](https://discourse.processing.org/u/scooijmans)\
**Post date:** [November 25, 2020, 11:22am UTC](https://discourse.processing.org/t/turning-audio-into-a-pattern/25694/1 "2020-11-25T11:22:39Z")

</div>

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.

```auto
/**
 * 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;
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 25, 2020, 11:34am UTC](https://discourse.processing.org/t/turning-audio-into-a-pattern/25694/2 "2020-11-25T11:34:33Z")

</div>

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]
