1

A while ago, a friend of mine and I wrote a little command line program in python which can take .csv data as an input and create sounds.

We recently discovered p5.js Sound and tone.js and were amazed by its capabilities. I'm especially in favour of p5.js Sound because it gives as the opportunity to easily visualize our sounds as well.

What we want

We want to create custom waveforms. p5.js provides standard waveforms like sine, triangle and so on, but we want to use our own waveforms.

What I tried

I had a look into the p5 sound source code to figure out how the function setType works. This is what I found.

p5.Oscillator.prototype.setType = function (type) {
    this.oscillator.type = type;
  };

And this:

  /**
   *  Constructor: <code>new p5.SinOsc()</code>.
   *  This creates a Sine Wave Oscillator and is
   *  equivalent to <code> new p5.Oscillator('sine')
   *  </code> or creating a p5.Oscillator and then calling
   *  its method <code>setType('sine')</code>.
   *  See p5.Oscillator for methods.
   *
   *  @class  p5.SinOsc
   *  @constructor
   *  @extends p5.Oscillator
   *  @param {Number} [freq] Set the frequency
   */
  p5.SinOsc = function (freq) {
    p5.Oscillator.call(this, freq, 'sine');
  };
  p5.SinOsc.prototype = Object.create(p5.Oscillator.prototype);

I thought there might be something like a switch case

case 'sine'
        //something

So I searched for something like this in the source code but couldn't find anything.

So now I am completely lost and cannot find any example on custom wave forms. Any ideas or hints on this?

Moglash
  • 259
  • 1
  • 5
  • 23
  • 1
    I did a little digging, and down in the [p5.sound source](https://github.com/processing/p5.js/blob/0.9.0/lib/addons/p5.sound.js#L5488) we see call to an AudioContext, which is a WebAPI object. A little more digging led to this [oscillatorNode](https://webaudio.github.io/web-audio-api/#oscillatornode). At this point, I don't know if you'll be finding any source to dig through because it's up to the browser to implement this stuff. This was just a long winded way of saying "I don't know", sorry – Tony Aug 22 '19 at 18:19
  • Thanks for your comment! I will have a look at [PeriodicWave](https://developer.mozilla.org/en-US/docs/Web/API/PeriodicWave) which can we used to shape the output of an oscillatorNode. – Moglash Aug 26 '19 at 09:59

0 Answers0