Variable duty cycle square waves with the Web Audio API
Published on: 2025-05-11 10:23:52
Creating variable duty cycle square waves with the Web Audio API
Created on 4/2/2025 Updated on 4/2/2025
Lately, I've been playing around with the Web Audio API for a side project I'm working on. I am building a web-based music tracker software for creating audio in the original style of the Gameboy. To faithfully recreate the sounds of the Gameboy, I need variable duty cycle square waves.
The iconic 8-bit, chiptune style of music is heavily reliant on square waves. The Web Audio API allows you to create OscillatorNode s that represent periodic waveforms like a sine, sawtooth, triangle, or square waves.
const ctx = new AudioContext ( ) ; const osc = new OscillatorNode ( ctx , { type : "square" , } ) ;
Here is what an A4 sounds like with a 50% duty cycle square wave: Volume: 50 %
Not the most pleasant musical thing you've ever heard but you get the gist.
Seems like we have our square wave, pretty easy right? Unfortunately no, not quite. Oscillators with the type "square" only all
... Read full article.