PCM audio on AVR

After playing a while with DDS (Direct Digital Synthesis), let’s see what we can do with PCM (Pulse-Code Modulation). Simply said, PCM is just a way to digitally encode analog signal (in our case, sound). PCM has two parameters, sampling rate and bit depth (resolution).
So what is the difference between DDS and PCM when it comes to generate sound with an AVR ? Basically nothing, as in both cases the signal is represented digitally and then a DAC (Digital Analog Converter) is used to generate the analog signal itself (for example, a sound).
The difference is mostly in the nature of the sound to be generated. PCM is used to represent any analog signal digitally. The longer the signal the more samples are necessary. In the case of AVRs, it means that the length of the signal that can be represented with PCM is rather limited as the memory is relatively small.
DDS is particularly adapted to periodic signals. That is why it is mostly used to generate waveforms. So if one wants to play sound samples (voice for example), PCM would be more adapted. If one wants to do a synthesizer, DDS is the way to go.
The other difference is that PCM has a sampling rate that can be changed to vary the pitch. While in the case of DDS , the sampling rate is basically constant. To change the frequency of a waveform, more or less samples are read but at a constant sampling rate. This technic is not adapted for PCM as for example, to read only one sample every two samples (increased frequency) means a loss of information that can make the original signal unrecognizable (especially in the case of voice).
In the case of DDS, the sampling rate is usually high (higher than the largest frequency in a typical sound signal) and thus, a single timer to generate both the sampling rate and the PWM (Pulse-Width Modulation) can be used to generate the analog signal (one cycle of PWM for one sample, the sampling rate and the PWM frequency are the same). In the case of PCM the sampling rate is usually low (a lower sampling rate means less samples per second, that is less samples in memory, but a lower sound quality) so using PWM at the same frequency that the sampling rate to generate the analog signal is not possible as one would “hear” the PWM signal. In this case two timers are necessary, one to generate the sampling rate and a second one to generate the PWM signal at a much higher frequency. That is for one sample, there will be significantly more PWM cycles. To play a PCM sound sample, you just have to encode it at a low sampling rate (8kHz for example) and with 8 bits resolution (this is the resolution of the PWM on attiny85 and a lot of other AVRs). Next time I’ll post an example of PCM code for the attiny85. In the meantime, you can check this example or this tutorial.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Spam Protection by WP-SpamFree