p5.sound Synth

Overview

Make some music using the p5 sound library.

Tools

p5.js

Qualities of our Synth

P5.sound has built-in monophonic and polyphonic synthesizers. They are a little underdocumented, so we’ll build our own using the p5.sound Oscillator and Env classes. Building our own is a good way to understand what is going on anyway.

The p5.sound Oscillator class generates a periodic signal with customizable frequency, amplitude, and waveform.

We can shape the amplitude of the Oscillator using the p5.sound Evn class. This class can control the amplitude of an oscillator using an ADSR envelope. An ADSR envelope controls the attack, decay, sustain and release of a sound and can be used to simulate these characteristics of acoustic instruments.

[whiteboard diagram ADSR Envelope]whiteboard diagram [Evn] -> (amp)[Oscillator]]

By combining a p5 Oscillator and Env we’ll have a synth with these parameters:

Our synth will be very basic. We won’t have control of amplitude at the note level. We also won’t be able to play more than one note at a time. Playing a note will cut off any other note currently playing and chords won’t work at all. This type of synth was pretty common on cheap 80’s musical keyboards.

::: js-lab /music/sketches/hello_env.js /::

SimpleSynth

The code above creates a small synthesizer system. SimpleSynth is a small, reusable class that encapsulates this system and gives it an easy to use programming interface.

SimpleSynth Source Try it with MIDI

::: js-lab /music/sketches/hello_simplesynth.js /::