Introduction
Twang is a Rust library for doing audio synthesis. The API of Twang uses a dependency tree (type of DAG or directed acyclic graph) of audio nodes rather than supporting arbitrary graphs. The tree makes it easier to visualize what's happening during synthesis. The root node of the tree is the final operation that produces the synthesized sound.
This book will teach not only how to use Twang, but also about how all the different methods of audio synthesis work. Each chapter will cover a synthesis method and provide an example of how to use it with Twang. You will find that the reason there are so many methods of synthesis is because they each make their own unique type of sound from each other. So if you want to synthesize a specific type of sound, you choose a specific type of synthesis. Here's a little guide to help (you might find a way to synthesize a type of sound with a different method than listed here, and that's ok - it's not an exact science):
- Additive Synthesis: Electronic reproduction of a sound with reduced quality, such as an "electric piano" sound
- Subtractive Synthesis: Electronic immitation of brass, saxaphone, or any other sound with rich harmonic content
- Frequency Modulation Synthesis: Electronic immitation of bells
Digital Audio
There's a great introduction to digital audio concepts on MDN that's worth checking out before reading this book.
Oscillators
Oscillators are at the core of audio synthesis. The basic concept is that a sound is composed of a repeating pattern. How many times this pattern repeats is the frequency of the sound. The higher the frequency (more times per second) the higher the pitch. The repeating pattern is often called a "waveform".
Sawtooth Wave
The sawtooth wave is the simplest waveform to generate, as it's just the identity function; so as the phase goes from -1 to 1, the amplitude goes from -1 to 1. It is the waveform that is returned from the phase accumulator (a variable that determines the phase from a frequency).
Sinusoidal Oscillators
A sinusoidal oscillator is a repeating sine/cosine waveform. It is the only primitive oscillator that isn't a modified sawtooth wave.
Property Parameters
None
Sound Theory
All sounds are composed of a number of sine waves at different frequencies and volumes. Each of these sine waves are multiples of the original frequency, so if the base frequency (the one you hear) is 440 HZ the next frequency is 880 HZ. These frequencies are called harmonics.
Triangle Oscillators
A triangle oscillator is a repeating waveform that oscillates between -1 and 1 using straight line segments.
Property Parameters
vertex
Moves the peak amplitude between the phase of -1 and 1.
- -1: downwards sloping sawtooth wave
- 0: symmetrical triangle wave
- 1: upwards sloping sawtooth wave
Sound Theory
A symmetrical triangle wave only contains odd harmonics and can be approximated
with sine waves with additive synthesis using amplitude -1 / (n * n)
, where
n
is the odd harmonic number, inverting every other.
Pulse Oscillators
A true pulse oscillator only contains the samples -1 and 1. The alias
amount
is used to determine how close it should be to a sawtooth wave.
Property Parameters
alias
Controls the distortion level by dividing the amplitude.
- -1: downwards sloping sawtooth wave
- 0: symmetrical square wave
- 1: upwards sloping sawtooth wave
Alternate Interpolation
Send a triangle wave as input into a pulse oscillator to interpolate between a triangle and pulse wave.
Sound Theory
The square wave, similar to the triangle wave only has odd harmonics.
Bezier Oscillators
A bezier oscillator is a wrapping symmetrical quadratic bezier waveform.
Property Parameters
curve
See how changing the parameter affects the waveform
- -1: slows middle phase, speeds outer phase
- 0: downwards sloping sawtooth wave
- 1: speeds middle phase, slows outer phase
Noise
Additive Synthesis
Additive synthesis is probably the simplest synthesis algorithm to understand. The fundamental idea is that all sounds are made up of an infinite number of sine waves at different volumes mixed together. Of course, when synthesizing you can only mix a finite amount of sine waves, otherwise you would be able to synthesize any sound with additive synthesis! That said, you can approximate any sound with additive synthesis, even to the point where it's indistiguishable!
But, additive synthesis isn't limited to just sine waves; You can use other kinds of waveforms as well. Interestingly, additive synthesis is actually the exact same thing as mixing! This means when you mix the two waveforms together, you are able to hear both of them playing together. Of course, the more waveforms you add together, the more difficult it becomes to pick them out, and the less it sounds like multiple "instruments" or "voices".
Fast Fourier Tranform
TODO
(Electric) Piano Example
TODO
Subtractive Synthesis
Subtractive synthesis is (you guessed it!) the opposite of additive synthesis. The fundamental idea is that you take a sound that naturally has an infinite number of sine waves added together (lots of harmonics), and start reducing ranges of frequencies with band-pass filters. A bandpass filter is made of two parts; a high-pass and a low-pass filter. High-pass filters reduce the lower frequencies, and let the higher frequencies pass through them unchanged. Low-pass filters are just the opposite; the higher frequencies are reduced and the low frequencies are left unchanged. The means a band-pass filter reduces any frequencies outside a band/range of frequencies.
Trumpet Example
TODO
Angular Modulation Synthesis
Angular modulation includes both frequency modulation and phase modulation synthesis. The two are very similar methods, and essentially do the same thing. The only difference is in the implementation; frequency modulation modulates the velocity of the waveform, while phase modulation modulates the offset position.
Frequency modulation synthesis is when the frequency variable of an oscillator is set to another waveform.
Phase modulation synthesis is when the phase variable of an oscillator is set to another waveform. Phase modulation synthesis was popular in 1980s synthesizers.
Phase Distortion
Phase distortion builds upon angular modulation synthesis.
- Do angular modulation of a waveform
- After each cycle of the lower frequency, reset the high frequency oscillator
- Multiply by decreasing sawtooth wave at the lower frequency to hide the "jump"
Waveshaping (Direct Phase Modulation)
Waveshaping builds upon phase modulation synthesis. In this type of modulation, the two oscillators used must have the same frequency.
Phase Limiting
Phase limiting is an improvement on the phase distortion synthesis "invented" by Casio (which as far as I know, was "invented" by me - Jeron Aldaron Lau). The idea is simple; take the first two steps of phase distortion synthesis as they are, then instead of multiplying by a decreasing sawtooth oscillator, multiply by a decreasing bezier oscillator. This reduces the amount of lost amplitude during phase distortion synthesis. Additionally, splice a plateau at the beginning of the bezier waveform to only diminish the amplitude after the phase has passed a specified point.