I ended up playing around with a couple mosfets, a LM393 comparator, NE555 timer, and other stuff and I made myself a decent amplifier, but the inputs I used for the comparator were a square wave running at about 1Mhz and the audio source itself via a capacitor. I also connected a resistor voltage divider to the same input and this divider made things work well. (example: I made the input at ultra low volume and can hear the same thing very loud at the output)
Now I'm curious. In my design, I used a square wave as an input to the comparator. On the web, the majority of the designs use a sawtooth or triangular wave input. I'm curious. Why is the triangular wave input preferred? What is the disadvantage with a square wave input?
With a triangular wave connected to one input of the comparator and the audio connected to the other input the output of the comparator will be a PWM signal whose duty cycle represents the instantanious level of the audio signal. I don't see how it could work with a square wave input.
Perhaps parasitics / bandwidth-limitation caused some low-pass filtering whereby the edges got knocked off the square-wave so that it became an approximation of a triangular wave. That would result in crude PWM.
I agree that Les provided the answer, but the other suggestions that the LM393 device is too slow are also good. I would definitely look at that because the slew rate could actually be defeating the purpose of creating a class D amp in the first place because the output can not be a clean PWM and thus the efficiency of the amp will not approach a true class D amp so all is for nothing.
A quick rough efficiency measurement would show some problems.
Correction to your correction
All designs use a triangle wave - a saw tooth would mean massive distortion - not the 100% of a square wave, but at least 50%.
Disagreement with your correction to his correction.
The only difference between saw and triangle waves is a faster leading edge. There still is a linear relationship between elapsed time into a wave cycle and the percentage of the total wave area under the curve. With a positive-going saw wave (narrow peak at the top) of 2 V p-p and incoming audio of 2 V p-p, both into a fast comparator, the range of output rectangular wave pulse widths still is 0% to 100% of the saw wave period (note - theoretically perfect comparator).
For low-distortion PWM, the pulse frequency should be much greater than the highest audio freq. In terms of instantaneous values, the comparator sees a fast periodic waveform (saw or tri) on one input, and a slowly varying DC level on the other. For an instantaneous audio waveform value of 1.5 V, the comparator output will be a 25/75 rectangular wave (or 75/25 depending on how the inverting and non-inverting inputs are assigned), for *either* carrier wave.
The only difference between saw and triangle waves is a faster leading edge. There still is a linear relationship between elapsed time into a wave cycle and the percentage of the total wave area under the curve.
And, just to be clear, we are talking about the internal modulator waveform, not the waveform out of the modulator (which is a PWM square-wave).
I think there may be some confusion on that point.
How is a sawtooth inferior?
A critical factor in the distortion generated by the PWM modulator is the linearity of the ramp of either the sawtooth or the triangle-wave.
If they both are equally linear, then they will produce equal output distortion.
To clarify, for waves of the same amplitude and frequency, a saw wave has a much faster leading edge. Mucho incredibly zoomy faster. But the area under both curves is the same, which is why the modulation noise is the same.
ak