Draw a waveform from an oscillator (e.g. SawOsc)

Hi,

My goal is to create a sketch which displays the waveform of the sounds it’s generating.

To make it easier, i would like that the example here: https://processing.org/reference/libraries/sound/SawOsc.html

would also display the waveform (the one we can see on the right) and not only to create the sound.

I assume i need the https://processing.org/reference/libraries/sound/Waveform.html class to do it but i am not getting how i can let it display the waveform of the sound generated by the sketch (without having to read this from outside).

Any suggestions? thanks

Welcome! I tried stitching a couple of examples together and came up with the following.

import processing.sound.*;

SawOsc saw;
Waveform waveform;
int samples = 512;

void setup() {
  size(640, 360);
  background(255);
  
  saw = new SawOsc(this);
  saw.play();
  
  waveform = new Waveform(this, samples);
  waveform.input(saw);
}

void draw() {
  background(0);
  stroke(255);
  strokeWeight(2);
  noFill();


  waveform.analyze();

  beginShape();
  for(int i = 0; i < samples; i++) {
    vertex(
      map(i, 0, samples, 0, width),
      map(waveform.data[i], -1, 1, 0, height)
    );
  }
  endShape();
}
2 Likes

Excellet, thanks a lot