Part 2. Basic sound waves with C++ and JUCE

In this tutorial we’ll be creating a simple application with 4 different oscillators, which produce 4 basic sound wave types. Oscillators are function generators to produce sound waves with defined properties. Basic waveform types are on the image below.

Different types of sound waves

Sine wave was used in my previous tutorial. This application is based on application from previous tutorial. Therefore you should know how to create components and handle audio output in JUCE. I’ll just clone oscillator from previous tutorial and add support to generate different sound wave types. In this tutorial we will be generating other 3 wave types and mix them together.

Algorithms for generating different wave types

There are several ways to generate sound waves:

  • Formulas— a math formulas
  • Lookup table— a predefined table with input and output values.
  • Polynomial Approx — This will make an estimation of the sinusoid function using predefined mathematical expression.

In this project I’ll use 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 user
  • phase — phase of a signal set by user
  • double_Pi — Pi number with double precision

How to mix them together

This is very easy, just add them together:

y(t) = sin(t) + saw(t) + triangle(t) + square(t)

Oscilloscope and spectral views are just helpers to visualize result sound wave. Square, saw and triangle waves sound not so clear because of many harmonics and other issues, but you should get main idea how to generate them.

Waves Generator Application

Source code is hosted on GitHub.