News for hackers. Stuff that matters.
Re:Variable duty cycle square waves with the Web Audio API by meindnoch on Monday April 07, 2025 @03:46PM (#43615163)
As others noted, merely switching back and forth between -1 and 1 will result in heavy aliasing. Also, since you can only switch from -1 to 1 at integer sample points, you won't be able to accurately generate frequencies that don't divide the sample rate evenly. E.g. if your sample rate is 48 kHz, you won't be able to generate a 50% duty cycle 11 kHz square wave. Either the duty cycle, or the frequency will be different. Like, audibly different.

The proper way is either via the Fourier series; or look into BLIT [1] synthesis.

[1] https://ccrma.stanford.edu/~stilti/papers/blit.pdf

Re:Variable duty cycle square waves with the Web Audio API by nkozyra on Monday April 07, 2025 @04:03PM (#43615347)
Aliasing seems like a potentially desirable feature for a chiptune / retro sound, though.
Re:Variable duty cycle square waves with the Web Audio API by iamdan on Monday April 07, 2025 @04:12PM (#43615458)
This is the nice thing about constraining myself to a rather old and crude sound style, the rough edges can remain a little rough
Re:Variable duty cycle square waves with the Web Audio API by montag on Monday April 07, 2025 @06:10PM (#43616505)
Blargg's Blip Buffer library is a widely-used implementation (especially in chip music synthesis), explained in detail here: https://www.slack.net/~ant/bl-synth/