Blending pure tones: Additive Synthesis from scratch with Python code

Chuck-jee Chau 周卓之
3 min readJul 11, 2022

--

There are a few basic waveforms that are commonly seen in analog synthesizers as building blocks for more complex sounds. But indeed, they can all be built from the purest sound — sine waves! You just need the correct recipe for the combination.

Although we hear the vibration of a single frequency as a single pitch, the harmonious sound from musical instruments all happen to have a collection of frequencies for every single pitch. With a different recipe of combination of these frequencies, the tone colour (timbre) differs.

Timbre /ˈtambə/: The character or quality of a musical or vocal sound (Oxford English Dictionary)

Photo by Nature Zen on Unsplash

The set of harmonics is an important base of the timbre. Technically, we need to look for an appropriate proportion of the fundamental frequency and its integer multiples.

A Violin Sound

For example, a violin sound of A440 has a fundamental frequency of 440 Hz. On top of it, the sound also has a mix of 880 Hz (440×2), 1320 Hz (440×3), 1760 Hz (440×4), and so on. Let’s use this table of secret recipe that we can find using some sound analysis tools. Ignore the vibrato for the moment…

The recipe of violin timbre, obtained with spectrum analysis
The analysis was made on this original violin sound

With a very simple synthesis, it sounds like this:

Violin sound from a very simple additive synthesis

Try comparing it with the pure sine tone. It sounds so different, isn’t it?

A simple sine wave

To mix pure sine tones into a new timbre is called additive synthesis. Let’s do this step by step with Python code!

Don’t worry. It’s not so addictive

Additive Synthesis with Python

We start with importing the necessary libraries— IPython for showing the sound player, matplotlib for plotting charts, numpy for arrays, and math for some mathematical functions. We also define the sampling rate sr.

Initialization

We have seen making a pure sine waveform before. We can put the code into a function makesine() to easily reuse it:

A function for making basic sine waves

Then, we can run makesine() several times to make a new waveform, with certain frequency freq and duration dur. We can put all of them together using numpy.concatenate().

Let’s try the makesine() function…

The basic sine wave sounds pretty good! Now let’s make more sine waves with frequencies at integer multiples using freq*i, where i is a counter from 1 and adds 1 every time. The sine waves need to softened into the predefined amplitudes in amplist. They are then stacked up in out.

Basic Waveforms with Additive Synthesis

Now, here are the common basic waveforms re-created with a simple additive synthesizer which only includes the first 10 harmonics: (I’m too lazy to obtain more harmonic amplitudes…)

The sine wave, with amplist having only the first harmonic and nothing more
The square wave has only odd harmonics
The sawtooth wave has attenuating harmonics
The triangle wave also has only odd harmonics, with harmonics attenuating faster

And here’s the violin again!

The simple violin sound (doesn’t it sound like a real one??)

While all of them have 440 Hz as the fundamental frequency, it sounds quite drastically different with a different combination of harmonics.

More Sophisticated Ways to Generate a Melody

Going further, here we prepare a function for the pitch-to-frequency calculator:

MIDI pitch numbers are more musician-friendly!

And a melody player to accept lists of notes and durations to play, with a pre-defined list of harmonic ratios:

More functions to make the process easier

Then… let’s play a melody! Here p is the notes to play, d is the durations of each note (made faster by ×4 times), and h is the list of harmonic amplitudes.

What is this tune?

I guess this may sound like the 80’s for you? (Indeed it’s the 90’s and beyond…)

Such an operation of additive synthesis can be quite tedious, even for computers (in older days when computation resource was less plentiful as today)! Just like mixing paints manually, such a synthesis gives the developer/musician the highest degree of control, but could be less favourable than a ready-mix that saves plenty of time.

Next time we can use easier tools such as programming libraries or software to get pre-mixture of tones for further manipulation.

--

--

Chuck-jee Chau 周卓之

Lecturer/Musician at Hong Kong, made an “Intro to Computer Music” course at CUHK, appears as a collaborative pianist/percussionist in shows 大學講師,以向學生推廣電腦音樂研究為己任