An Introduction to Digital Audio and Audio Devices

Sound waves are analog by nature, and as we know, analog signals aren't easily stored or manipulated by a computer system that stores and processes information digitally. Other devices, such as CD, DVD, and Blu-Ray players, also operate with digital audio.

Digital audio information is mainly derived from an analog audio wave by a process known as Pulse Code Modulation (PCM). PCM works by sampling or taking a measurement of the analog audio wave at fixed intervals. The number of measurements taken per second is known as the sample rate. Audio on a CD is sampled at 44.1 kHz. Other sources, such as HD video, may use 48 kHz, which means there are 48000 measurements of the audio wave performed per second. Each measurement is known as a sample.

The sample is a measurement of the analog signal's amplitude at the time of the measurement, which is then quantized to a digital scale. The range of this scale is known as the bit depth or sample depth. For CD audio and many other applications, the bit depth is 16 bits, which gives each sample a possible value between −32768 and 32767. When a sample is taken, the value is converted into this scale by rounding it to the nearest integer value. The higher the sample rate and sample width, the more accurate the representation of the original audio wave.

As computer programmers, we rarely need to care about the conversion of a signal from analog to digital or vice versa, as that is handled by circuitry on the audio hardware (ADC/DAC). However, a programmer needs to be aware of what digital PCM samples represent and understand the significance of the sample rate and depth. PCM samples are typically stored with channels interleaved in memory, as shown in Figure 12-1.

images

Figure 12-1. Buffer of interleaved 16-bit signed PCM samples

As you can see in Figure 12-1, the data corresponding to each channel are not stored sequentially but are interleaved in the buffer. The buffer above uses 16 bits per sample, which means that each sample occupies two bytes. A pair of left/right samples is referred to as a sample frame (or sample group). If there were more than two channels, for example, eight (as used by HDMI), a sample group would instead consist of channels 1–8. Most digital audio systems expect audio data in this format and usually this is how audio is stored in a file on a computer, assuming the audio is uncompressed. File formats such as MP3 compress the audio data; however, they have to decompress the audio back to interleaved PCM samples before it can be played back by the audio hardware. Audio at 44.1 kHz will give us 44100 sample frames per second. If the sample depth is 16 bits and there are two channels, we need 176.4 KB (44100 Hz * (16/8 bits) * 2 channels = 176400 bytes) of data to store a single second of audio. PCM samples aren't necessarily always 16 bits wide, however. The sample depth can also be 8, 20, 24, or 32 bits. Furthermore, samples can be stored as unsigned or signed, or even in floating point, which is the preferred audio format of Core Audio. Table 12-1 shows some commonly used PCM formats.

images

An audio device and driver usually revolve around the concept of a sample buffer. The sample buffer usually contains interleaved PCM samples (assuming there is more than one channel). The sample buffer is a circular buffer allocated by the driver. For audio playback, the hardware device usually continuously reads the buffer. The device will access the memory of the buffer directly via direct memory access (DMA) without involving the CPU and issue an interrupt at some fixed interval to let the driver know the current location the device is reading from. This is necessary so that whatever is producing the audio data can write to the correct location without interfering with the device. After a period has elapsed, we know that a certain number of samples have been played by the device. In this case, it is common for the driver to erase the played samples. This ensures that silence will be outputted rather than repeating previous data should the buffer wrap around to the start without any new audio being inserted into the buffer. In the case of audio input, the process is simply reversed. Instead of reading from the buffer, the device will be writing audio samples into the sample buffer. It will also issue an interrupt, letting you know when/where audio samples can be read.

Some audio devices may have multiple independent inputs and outputs. In this case, each input and output may have its own sample buffer. Mac OS X comes with a USB audio device driver so no third party driver is generally needed for devices that conform to the USB audio interface.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.183.234