Hacker News new | ask | show | jobs
by meindnoch 443 days ago
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

2 comments

Aliasing seems like a potentially desirable feature for a chiptune / retro sound, though.
This is the nice thing about constraining myself to a rather old and crude sound style, the rough edges can remain a little rough
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/