Monday, July 21, 2008

Pulse-width modulation

The PC speaker is normally meant to reproduce a square wave via only 2 levels of output, driven by channel 2 of the Intel 8253 (PC, XT) or 8254 (AT and later) Programmable Interval Timer operating in mode three (square wave signal). The speaker hardware itself is directly accessible via PC I/O port 61H (61 hexadecimal) via bit 1 and can be physically manipulated for 2 levels of output (i.e. 1-bit sound). However, by carefully timing a short pulse (i.e. going from one output level to the other and then back to the first), it is possible to drive the speaker to various output levels in between the two defined levels. This effectively allows the speaker to function as a crude DAC, thereby enabling approximate playback of PCM audio. This technique is called pulse-width modulation (PWM) and is notably used in class D audio amplifiers.

Getting a high fidelity sound output out of this technique requires the switching frequency between the minimum and maximum sound levels to be much higher than the audio frequencies meant to be reproduced (typically with a ratio of 10:1 or more), and the output voltage to be bipolar in order to make better use the output devices' dynamic range and power (e.g. by making a loudspeaker vibrate in both directions). On the PC speaker, however, the output voltage is either zero or TTL level (unipolar). As a result, the precision of this technique when used on the PC speaker is comparable at best to a 6-bit PCM DAC, while the final audio results will depend on precise timing and the nature of the reproduced sound.

No comments: