Aeggy
May 3, 2019, 2:44am
1
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();
}
}
kll
May 3, 2019, 6:14am
2
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();
}
}
Aeggy
May 3, 2019, 12:59pm
3
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.
kll
May 3, 2019, 2:08pm
4
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
Aeggy
May 3, 2019, 3:30pm
5
Could you point me in the direction of the example for Waveform? Having trouble finding it.
kll
May 4, 2019, 12:05am
6
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 /