About the Audio Waveform

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

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();
}

Hello,

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

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

And there are examples there for you that will help.

:)

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

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();
}

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 :slight_smile:

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.

 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:

Smoothed:

:)