So for example, one might think drawing a basic rectangle in time would create a pulse waveform... but in the digital domain a square wave has ripples in it. Recall that in the spectral domain, a rectangle is approximated by ever-higher and ever-smaller sinusoids to infinity... in sampled signals, though, only frequencies below Nyquist are available. So the sampled version of the waveform will have ripples that the highest frequency components would have completed. The requirement then is to work with a higher Nyquist/sampling frequency than audible so that audible part of the square is correct.
From 0 to 1, 1 to to, 127 to 128, 254 to 255, nothing surprising happens, the output voltage slowly rises. When the counter rolls over from 255 to 0, something surprising *does* happen though, we get a massive and instant negative-going spike.
Because this jump from 255 to 0 is instant, it has to have a lot of harmonics, up into high frequencies. If you plot the spectrum of a sawtooth wave you'll see that there's a sine wave at every harmonic, reducing in amplitude as 1/f - that is, the first one at full level, the second harmonic, twice the frequency, and at half level, 3rd at 1/3, 4th at 1/4 and so on.
Now here's your problem - some of those harmonics, which extend off to infinity, are going to be beyond the Nyquist frequency. As you increase the frequency of the sawtooth relative to the sample rate, you'll start to creep into an area where those harmonics are actually still quite loud compared to the lower ones, and that gives you a problem.
Aliasing occurs because those harmonics "reflect" off the Nyquist frequency. Think about watching a Western, where the wagon wheels appear to spin backwards at certain speeds - they are aliasing. If they were turning slowly they'd move a little before the next frame, but at some point - where one spoke moves exactly into the space left by the previous spoke - they will appear to stand perfectly still! With a bit of thought, you'll see that the fastest the wheel can go puts the spoke exactly in the middle of the gap between spokes in the previous frame, and any faster will make it look like it's going backwards.
This is exactly what's happening with aliasing. The sinewave that's just above half your sampling rate is coming back down towards you, backwards. What goes up must come down.
In a real-life analogue sawtooth oscillator this is of course a huge problem because that big negative-going spike has infinite energy, and we're lucky that it's also infinitely short because anything that requires infinite energy is going to have terrible battery life. In practice, the maximum frequency of the harmonics in the sawtooth are limited by other parameters in the circuit (mostly how quickly the capacitor discharges through the reset transistor). And herein lies our major clue.
In order to generate a sawtooth that doesn't alias, we must calculate that step so it is no longer infinitely fast. There are a bunch of ways we can do this, but a nice simple way is to calculate an amount to "correct" the sample just before and just after the step so that you've got a rough approximation of passing the signal spectrally through your sinc window. This is crude but effective, and can be done with a couple of adds and multiplies making it ideal for real-time computation on even fairly crappy chips.
You can actually generate a sinc wavelet and "paste" that over the step, and when you get that right it sounds perfect. If you've got a fairly chunky DSP or general-purpose CPU with good math support to play with, this will give best results.
It's important to note that if you take a sinewave and clip it, then sample it, you'll still get aliasing because you'll still have a big infinitely fast step somewhere, which is why audio equipment is so big on having steep lowpass filters before and after digital (or, sampled, at least) bits. A great example is in the resolutely analogue Roland Juno 106, where the chorus board has three 24dB/octave Butterworth filters at around 10kHz, realised as cascaded pairs of Sallen-Key filters. The first rips off any signal above about 12kHz so it cannot be passed to the chorus chip, the second two pairs are "reconstruction filters" to remove fizzy-sounding sampled steps which would be apparent as a swooshing sound as the chorus went through the "long" end of its delay.
The reason they need this is that the analogue "Bucket Brigade Delay" chips are a kind of analogue dynamic RAM, where on every clock pulse a capacitor charges up from the input to its left, and on the next discharges into the output on its right. Stick a few hundred of these capacitors together and it will "pass buckets of signal" down the line, delaying it by whatever the clock rate is divided by the number of buckets. It's all analogue, but it's still sampling!
Ah-ha. Gotcha. So my intuition was more or less correct. It's not that clipping in digital is especially susceptible to aliasing, it's that when people clip in analog they brickwall the signal to get rid of the harmonics.
You can't create a naive sawtooth (for example) and *then* filter it because the damage has already been done. That being said, the "supersaw" oscillator in the Roland JP8000 generated a bunch of naive saws and *highpass* filtered them just below their fundamental to remove the gurgly "beat note" from aliased partials.