Part 2. Basic sound waves with C++ and JUCE
In this tutorial, we’ll create a simple application with 4 different oscillators, which produce 4 basic sound wave types. Oscillators are function generators that produce sound waves with defined properties. Basic waveform types are in the image below.
Sine wave was used in my previous tutorial. This application is based on an application from the previous tutorial. Therefore, you should know how to create components and handle audio output in JUCE. I’ll clone the oscillator from the previous tutorial and add support to generate different sound wave types. In this tutorial, we will generate other 3 wave types and mix them.
Algorithms for generating different wave types
There are several ways to generate sound waves:
- Formulas— math formulas
- Lookup table— a predefined table with input and output values.
- Polynomial Approx — This will estimate the sinusoid function using a predefined mathematical expression.
In this project, I’ll use the first approach, formulas. Below are samples of code with implementation. Notes:
- t — is a current time value
- level — amplitude (or volume of a signal)
- frequency — signal frequency set by the user
- phase — phase of a signal set by the user
- double_Pi — Pi number with double precision
How to mix them
This is very easy. Let’s mix them:
y(t) = sin(t) + saw(t) + triangle(t) + square(t)
Oscilloscope and spectral views help visualize the sound. Square, saw, and triangle waves sound unclear because of many harmonics and other issues, but you should know how to generate them.
The source code is hosted on GitHub.