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

**URL:** https://discourse.processing.org/t/draw-a-waveform-from-an-oscillator-e-g-sawosc/32525
**Category:** Libraries
**Created:** [September 30, 2021, 1:23pm UTC](https://discourse.processing.org/t/draw-a-waveform-from-an-oscillator-e-g-sawosc/32525 "2021-09-30T13:23:45Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![matteo](https://avatars.discourse-cdn.com/v4/letter/m/f9ae1b/32.png) [@matteo](https://discourse.processing.org/u/matteo)
#### Post date: [September 30, 2021, 1:23pm UTC](https://discourse.processing.org/t/draw-a-waveform-from-an-oscillator-e-g-sawosc/32525/1 "2021-09-30T13:23:45Z")

</div>

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](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](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

---

<div class="post-metadata">

### Author: ![mcintyre](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mcintyre/32/14561_2.png) [@mcintyre](https://discourse.processing.org/u/mcintyre)
#### Post date: [September 30, 2021, 2:45pm UTC](https://discourse.processing.org/t/draw-a-waveform-from-an-oscillator-e-g-sawosc/32525/2 "2021-09-30T14:45:59Z")

</div>

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

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

```

---

<div class="post-metadata">

### Author: ![matteo](https://avatars.discourse-cdn.com/v4/letter/m/f9ae1b/32.png) [@matteo](https://discourse.processing.org/u/matteo)
#### Post date: [September 30, 2021, 4:01pm UTC](https://discourse.processing.org/t/draw-a-waveform-from-an-oscillator-e-g-sawosc/32525/3 "2021-09-30T16:01:02Z")

</div>

> [@mcintyre](#):
>
> ```auto
> 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();
> }
> 
> ```

Excellet, thanks a lot
