Skip to content

Instantly share code, notes, and snippets.

Last active June 29, 2017 22:41
What would you like to do?
Additive Synthesizer
license: mit

I originally tried building the sound wave manually by pulling values form the oscillators to calculate each sample and pushing it to the audio buffer as one compound wave, but timing the updates is problematic. This is in part because it is a lot of data to process on the fly! While I had some luck using turbo.js to offload the processing to the gpu, I found it to be virtually impossible to sync the waves on each cycle.

The original solution I came up with was to give each oscillator its own Web Audio OscillatorNode, but that wouldn't allow for proper phasing of the waves. It also kinda fealt like cheating since the goal in my mind was to create a synthesizer that outputs one compound tone rather than one that creates multiple tones and mixes them together on output.

What I ended up doing was using Web Audio's createPeriodicWave() method. First, it creates two blank arrays (real and imag) that will function like a Fourier Series and then populating them with the values from the oscillators. There was still the tricky part of handling the phase offsets, but I found a nice solution where you simply apply a 2D rotation to the value in the Fourier Series!

This result is pretty much exactly what I was hoping for. The one catch is that there is a clipping that can happen when adjusting the phase or the frequency. This is because due a timing issue regarding the position of the wave when it gets changed. That is, if the end of old wave and start of the new wave don't align, they can cause an unwanted sound. The only way I could find for resolving that was to add a very fast fade in/out effect to the sound whenever a value is changed, but this was also unreliable and would usually resulting in a stuttering effect that was even more distracting than the clipping.

This is part of a series exploring the visualization of audio that was presented at a d3.oakland meetup. A big list of the demos from this series can be found here.

<!DOCTYPE html>
<title>Additive Synthesizer</title>
body {
margin: 0px;
display: flex;
path {
fill: none;
stroke: black;
stroke-width: 1;
svg {
border: 1px solid steelblue;
margin: 10px;
margin-left: 5px;
flex-grow: 2;
button {
margin: 5px;
.controls {
margin: 10px;
margin-right: 5px;
padding-left: 5px;
padding-right: 5px;
overflow-y: scroll;
border: 1px solid steelblue;
.title {
margin-right: 30px;
font-weight: bold;
<script src="//"></script>
<template class="oscillator-template">
<div class="oscillator">
<text class="title"></text>
<label><input class="mute" type="checkbox" checked>mute</label>
<div class="frequency">
<input type="range" min="20" max="999" value="220" name="freq"><text></text>
<div class="amplitude">
<input type="range" min="0" max="100" value="100" name="amp"><text></text>
<div class="phase">
<input type="range" min="0" max="100" value="0" name="phase"><text></text>
<div class="offset">
<input type="range" min="0" max="99" value="0" name="offset"><text></text>
<div class="controls">
<button onclick="randomize()">Random Wave</button>
<button onclick="randomize(true)">Random Wave (lower frequency)</button>
const audioCtx = new (window.AudioContext || window.webkitAudioContext)();
const sampleRate = 22100;
const svgWaveData = d3.range(sampleRate / 40);
const real = new Float32Array(1000);
const imag = new Float32Array(1000);
const mainOsc = audioCtx.createOscillator();
const gainNode = audioCtx.createGain();
gainNode.gain.value = -.8;
mainOsc.frequency.value = 1;
window.addEventListener('resize', onWindowResize, false);
function onWindowResize(){
width ="clientWidth");
x.range([0, width]);
mainWave.attr("d", wave);
const height = innerHeight - 20;".controls").style("height", height + "px");
const svg ="svg")
.attr("height", height);
let width ="clientWidth");
// scales
const x = d3.scaleLinear()
.range([0, width])
.domain([0, sampleRate / 40]);
const y = d3.scaleLinear()
.range([height - 60, 60])
.domain([-1, 1]);
const wave = d3.line()
.x(function (d, i) {return x(i)})
.y(function (d, i) {return y(d3.sum(oscs, function(d) {return getVal(i, d)}))});
const mainWave = svg.append("g")
.attr("class", "mainWave")
.style("stroke", "red")
.style("stroke-width", 2);
const indivWaves = svg.append("g")
.attr("class", "indivWaves");
const oscs = []
for (let i=0; i<10; i++) oscs.push(Oscillator(i))
mainWave.attr("d", wave);
function generatePeriodicWave() {
for (let osc of oscs) {
if (!osc.mute && osc.amp !== 0) {
const shift = 2 * Math.PI * osc.offset/100;
real[osc.freq] += -Math.sin(shift) * osc.amp/100;
imag[osc.freq] += Math.cos(shift) * osc.amp/100;
mainOsc.setPeriodicWave(audioCtx.createPeriodicWave(real, imag,{disableNormalization: true}));
function Oscillator(i) {
const self = {}
const template = document.querySelector(".oscillator-template")
const newOsc = document.importNode(template, true);
newOsc.querySelector(".title").innerHTML = "Wave " + (i + 1)
// create the individual controls
const mInput = newOsc.querySelector(".mute");
const [fInput, fText] = newOsc.querySelector(".frequency").children;
const [aInput, aText] = newOsc.querySelector(".amplitude").children;
const [pInput, pText] = newOsc.querySelector(".phase").children;
const [oInput, oText] = newOsc.querySelector(".offset").children;
self.mute = true;
self.freq = Number(fInput.value);
self.amp = Number(aInput.value);
self.phase = Number(pInput.value);
self.offset = Number(oInput.value);
mInput.onchange = updateInput;
fInput.oninput = updateInput;
aInput.oninput = updateInput;
oInput.oninput = updateInput;
pInput.oninput = function() {
const active = self.phase > 0;
const newPhase = Number(this.value);
self.phase = newPhase;
if (!active) phaseLoop();
const thisWaveFunc = d3.line()
.x(function (d, i) {return x(i)})
.y(function(d, i) {return y(self.mute ? 0 : getVal(i, self))});
const thisWave = indivWaves.append("path")
.style("stroke-dasharray", [5, 5])
.style("stroke", "#e9e9e9")
.style("stroke-width", 1);
newOsc.onmouseover = function() {"stroke", "steelblue")};
newOsc.onmouseout = function() {"stroke", "#e9e9e9")};
self.randomize = function(lowFreqs = null) {
mInput.checked = Math.random() > .5;
self.freq = Math.floor(Math.random() * (lowFreqs ? 200 : 900)) + 100;
self.amp = Math.floor(Math.random() * 100);
self.offset = Math.floor(Math.random() * 100);
const activePhase = self.phase > 0;
self.phase = Math.floor(Math.random() * 100)
if (!activePhase) phaseLoop();
return self;
function phaseLoop() {
self.offset = (self.offset + self.phase/100) % 100;
if (self.phase > 0) requestAnimationFrame(phaseLoop);
mainWave.attr("d", wave);
function updateOsc() {
self.mute = mInput.checked;
fText.innerHTML = "Frequency: " + self.freq + "Hz";
aText.innerHTML = "Amplitude: " + self.amp + "%";
pText.innerHTML = "Phase Rate: " + self.phase + "%";
oText.innerHTML = "Offset: " + Math.round(self.offset) + "%";
fInput.value = self.freq;
aInput.value = self.amp;
pInput.value = self.phase;
oInput.value = self.offset;
if (self.mute || self.amp <= 0) {"visibility", "hidden").attr("d", null);
} else {"visibility", "visible").attr("d", thisWaveFunc);
function updateInput() {
if ( self[] = Number(this.value);
mainWave.attr("d", wave);
function randomize(range = false) {
for (let osc of oscs) osc.randomize(range);
mainWave.attr("d", wave);
function getVal(i, osc) {
if (osc.mute) return 0;
return (Math.sin(2*Math.PI * ((i+osc.offset) / (sampleRate/osc.freq))) / 2) * osc.amp / 500;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment