# About the Audio Waveform

**URL:** <https://discourse.processing.org/t/about-the-audio-waveform/29780>\
**Category:** Libraries\
**Tags:** homework\
**Created:** [May 1, 2021, 8:12pm UTC](https://discourse.processing.org/t/about-the-audio-waveform/29780 "2021-05-01T20:12:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marga](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@Marga](https://discourse.processing.org/u/Marga)\
**Post date:** [May 1, 2021, 8:12pm UTC](https://discourse.processing.org/t/about-the-audio-waveform/29780/1 "2021-05-01T20:12:15Z")

</div>

Hello, good evening,  
In my code, I would like to have a single line that moves with my music and not that new lines are formed, what can I do to make this not happen? Thanks

```auto
import processing.sound.*;

SoundFile sample;
Waveform waveform;

int samples = 100;
float r;
float g;
float b;
float a;
float diam;
float x;
float y; 

void setup(){
size(1000, 1000);
background(0);
smooth();

sample = new SoundFile(this, "Maroon 5 - Nobody's Love.wav");
sample.loop();
  
  waveform = new Waveform(this, samples);
  waveform.input(sample);
  
  

}

void draw() {
 r = random(255);
 g = random(255);
 b = random(255);
 
 a = random(255);
 diam = random(15);
 x = random(width);
 y = random(height);
 
 noStroke();
 fill(r,g,b,a);
 ellipse(x,y,diam,diam);
 
 

  strokeWeight(1);
 
 

 
waveform.analyze(); 

beginShape();
  for(int i = 0; i < samples; i++){
    
    // Draw current data of the waveform
    // Each sample in the data array is between -1 and +1 
    vertex(
      map(i, 1, samples, 0, width),
      map(waveform.data[i], -1, 1, 0, height)
    );
  }
  endShape();
}

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [May 1, 2021, 8:37pm UTC](https://discourse.processing.org/t/about-the-audio-waveform/29780/2 "2021-05-01T20:37:53Z")

</div>

Hello,

> [@Marga](#):
>
> what can I do to make this not happen?

I always look to see if there are examples that are already available that can help.

> [@What is the code for my wave to move at the same time as the song?](https://discourse.processing.org/t/what-is-the-code-for-my-wave-to-move-at-the-same-time-as-the-song/29728/3):
>
> The sound libraries have examples that come with them. Processing sound library: [https://processing.org/tutorial/](https://processing.org/tutorial/) There is one for Sound [Sound \ Libraries \ Processing.org](https://processing.org/reference/libraries/sound/) From the Processing PDE in the file menu: [image] The Minim library also has resources and examples. slight_smile

Look through the examples ( File \> Examples…) that come with Processing:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/8/80931cbf1e7ece8b2ec75144a1ba7b365bf7147e.png)

And there are examples there for you that will help.

`:)`

---

<div class="post-metadata">

**Author:** ![Marga](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@Marga](https://discourse.processing.org/u/Marga)\
**Post date:** [May 1, 2021, 10:22pm UTC](https://discourse.processing.org/t/about-the-audio-waveform/29780/3 "2021-05-01T22:22:19Z")

</div>

Thanks for the tip. I’ve already added a few more lines of code to declare a smoothing factor to smooth out sudden changes in amplitude, just like in this example of Peak Amplitude, but nothing has changed

```auto
import processing.sound.*;

SoundFile sample;
Waveform waveform;
Amplitude rms;
float smoothingFactor = 0.25;
float sum;

int samples = 100;
float r;
float g;
float b;
float a;
float diam;
float x;
float y; 

void setup(){
size(1000, 1000);
background(0);
smooth();

sample = new SoundFile(this, "Maroon 5 - Nobody's Love.wav");
sample.loop();

 rms = new Amplitude(this);
  rms.input(sample);
  
  waveform = new Waveform(this, samples);
  waveform.input(sample);
  
  

}

void draw() {
 r = random(255);
 g = random(255);
 b = random(255);
 
 a = random(255);
 diam = random(15);
 x = random(width);
 y = random(height);
 
 noStroke();
 fill(r,g,b,a);
 ellipse(x,y,diam,diam);
 
 
strokeWeight(1);
  
 
waveform.analyze(); 

 sum += (rms.analyze() - sum) * smoothingFactor;
  float rms_scaled = sum * (height/2) * 5;

beginShape();
  for(int i = 0; i < samples; i++){
    
    // Draw current data of the waveform
    // Each sample in the data array is between -1 and +1 
    vertex(
      map(i, 1, samples, 0, width),
      map(waveform.data[i], -1, 1, 0, height)
    );
  }
  endShape();
}

```

---

<div class="post-metadata">

**Author:** ![Marga](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@Marga](https://discourse.processing.org/u/Marga)\
**Post date:** [May 2, 2021, 12:08pm UTC](https://discourse.processing.org/t/about-the-audio-waveform/29780/4 "2021-05-02T12:08:02Z")

</div>

Good afternoon,  
I am very confused with this question of mine and I cannot solve it, can someone help me? I am really upset, because I have to deliver this work tomorrow. I was really grateful 🙂

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [May 2, 2021, 2:48pm UTC](https://discourse.processing.org/t/about-the-audio-waveform/29780/5 "2021-05-02T14:48:44Z")

</div>

> [@Marga](#):
>
> I am really upset, because I have to deliver this work tomorrow

I also have to meet a deadline tomorrow morning for work.  
That is life.  
Don’t waste time (yours or ours) with negative emotions that will impact your progress.

```auto
 sum += (rms.analyze() - sum) * smoothingFactor;
 float rms_scaled = sum * (height/2) * 5;

```

This is smoothing your peak amplitude over time and you are NOT using _rms\_scaled_ any where in the code.

You could use this to move a straight line up and down in place of the ellipse in the example.

If you want to smooth the data in waveform.data you should do it in the loop on that data.  
I was able to adapt the smoothing code to do this in the loop on the waveform.data and then plot smoothed\_data vs x.

Not smoothed:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/9/9feb9d31b7c91a175d8abbe5dd6b658039dbfefd.png)

Smoothed:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/ceacf9dad95692434de8b6c37c1b073301957e96.png)

`:)`
