The 1-Bit Instrument
online.ucpress.edu
online.ucpress.edu
This is also how high quality electronic motor drives and servos generally work.
Switched power supplies have always seemed to be, to me, kin to class D amplifiers, though the block diagrams don't highlight the similarities.
module pdm(clk, level, O);
parameter N = 16;
input wire clk;
input wire [N-1:0] level;
output wire O;
reg [N+1:0] sigma = 0;
assign O = ~sigma[N+1];
always @(posedge clk) sigma <= sigma + {O,O,level};
endmodule
I’ll grant you that PWM is a bit more intuitive. I bet there is a more thorough comparison between them but I haven’t seen it.My (effectively one-line) implementation above was derived from first-principle:
Given a desired target level 0 <= T <= 1, control the output O in {1,0}, such that O on on average is T. Do this by integrating the error T - O over time and switching O such that the sum of (T - O) is finite.
S = O = 0
loop:
S = S + (T - O)
O = (S >= 0)
In fixed point arithmetic this becomes even simpler (assume N-bit arith) S = Sf * 2^N = Sf << N. As |S| <= 1, N+2 bits is sufficient S = O = 0
loop:
D = T + (~O + 1) << N === T + (O << N) + (O << (N+1))
S = S + D
O = 1 & ~(S >> (N+1))
and that's the Verilog belowAnother example is Bresenham's algorithm for drawing straight lines on raster displays. The quantity being approximated there is the slope of the line, which is approximated with minimal diffused error as the line is being drawn with only integer adds and subtracts. No divisions and no floating point needed.
These are some of the most subtly beautiful algorithms in computing.
Well, they also put a low-pass filter after.
By the way, while the principle behind class D amplifiers was already long known, it's the gallium nitride MOSFET technology used for switching that makes them really of sufficient quality.
There are some fun techniques in the article, though! I was surprised to see my friend Norm Hardy mentioned — I had never realized he was a pioneer of computer music :)
The program came with a tiny card plugged into the S-100 bus consisting of nothing more than an RC filter. The Sol didn't use interrupts. The program worked by toggling EI/DI (enable and disable interrupt) to cause one of the pins on the s-100 bus to toggle.
The intonation of especially suffers because those notes are more affected by the fixed 8080 clock cycle.
Here is a clip of it. This clip is from an emulator of the Sol-20, and it uses a higher order filter than the first order RC used by the original hardware. The real hardware is harsher sounding than this. The ten seconds of the 25 second clip is unimpressive, but the 2nd half shows how much can be done with such limited hardware.
https://open.spotify.com/album/6Ie1GtBI9ZCfmcT1OsK8a0?si=Aov...
If you're into chiptunes, this hits the spot.
There were other 1-bit machines out there, like the Apple ][. Paul Lutus' Electric Duet managed to get two-voices out of the speaker and he describes the process here:
Tim Follins original ZX Spectrum tune: https://www.youtube.com/watch?v=Iz46pCROkjM