How to get rid of click at the beginning of an Audiosample?

Hi. I noticed that audiosamples I play always have a click in the beginning, which is not a good quality for use. To demonstrate it, I play an inaudible 1hz sinewave below. Press d to play the wave and you hear the click on every press. Why does this happen and is there something I can do to remedy it?

import processing.sound.*;
AudioSample sample;

void setup(){
float[] r = new float[44100];
for ( int i = 0; i < 44100; i++){
  r[i] = sin((TWO_PI/(44100/1)) * i); //1Hz frequency
}
sample = new AudioSample(this, r, false, 44100);
}

void draw(){}

void keyPressed(){
  if (key == 'd' && !sample.isPlaying()){
    sample.play();
  }
}

void keyReleased(){
  if (key == 'd'){
    sample.stop();
  }
}

try my example with a little bit more show:

import processing.sound.*;
AudioSample sample;
FFT fft;
Waveform waveform;

int fftBands = 128;
int showfftBands = 10;
// Define how many samples of the Waveform you want to be able to read at once
int samples = 300;

int resolution = 50;   // vals to make one sinus ( minimal 4 for triangle
float[] sinewave = new float[resolution];
int note = 440; //Hz

void setup() {
  size(300, 300);
  for (int i = 0; i < resolution; i++)  sinewave[i] = sin(TWO_PI*i/resolution);
  sample = new AudioSample(this, sinewave, note * resolution);
  sample.amp(0.2);
  //sample.loop();

  fft = new FFT(this, fftBands);
  fft.input(sample);

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

  println("SFSampleRate= " + sample.sampleRate() + " Hz");
  println("SFSamples= " + sample.frames() + " samples");
  println("SFDuration= " + sample.duration() + " seconds");

  println("use: key [d] sound start / stop");
}

void draw() {
  background(200, 200, 0);
  fft.analyze();
  stroke(0, 200, 0);
  fill(0, 0, 200);
  float r_width = width/float(showfftBands);
  for (int i = 0; i < showfftBands; i++) rect( i*r_width, height, r_width, -fft.spectrum[i]*height);

  // Perform the analysis
  waveform.analyze();

  beginShape();
  noFill();
  stroke(200, 0, 0);
  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, 0, samples, 0, width), 
      map(waveform.data[i], -1, 1, 0, height)
      );
  }
  endShape();
}

void keyPressed() {
  if (key == 'd' && !sample.isPlaying() ) {
    sample.loop();
    sample.amp(0.2);
  }
}

void keyReleased() {
  if (key == 'd') {
    sample.amp(0.001);
    sample.stop();
  }
}

Hi kll. I get “The class “Waveform” does not exist” unfortunately. Am I supposed to replace this with something else?

Edit: For now I just tried removing waveform and playing the sample and the click is still there.

this is processing 3.5.3 on a win 7 PC
with the default processing SOUND lib
and the waveform thing is from a ready example, also the FFT…

the rest i changed to stay close to your code

Could you point me in the direction of the example for Waveform? Having trouble finding it.

could you first confirm that you use Processing 3.5.3 and its IDE?
and what computer / OS you use?

then try:
/ File / Examples / Libraries / Sound / Analysis / AudioWaveform /