Thursday, September 27, 2007

Computer Audio

Sound cards

A sound card could be a separate add on card in your PC or could be part of the motherboard itself. Sound card is capable of interfacing the computer to the outside world for the input/output of audio. It has various input /output ports (or jacks)

Line in (input)
Microphone (input)
Line out (or headphones) (Output)

You need to connect external speakers or headphone in the lineout jack to listen to the audio played on your PC.


Software Applications and Sound Card

The software applications on your PC can not directly talk to the sound card or use its capabilities for audio input/output. There are multiple layers involved in it.

Applications
Operating System
Device driver
Sound card

At the bottom level there is device driver layer. Sound cards usually come with their own device drivers. Device drivers are installed by the operating system in order to provide support to the applications who want to use the services of the device.

Sound card device drivers are low level software programs which interface with hardware directly (for programming the hardware) and provide APIs to operating system to send and receive digital data from the sound card. The device driver may also supply other low level APIs for device initialization and various controls which may not be available directly to applications. Operating System controls the sound card using these primitive low level APIs

Audio applications (such as audio recorders, winamp etc) use APIs provided by operating system to play or record digital audio. Few applications may bypass operating system APIs for performance reasons or for better control of the underlying sound card.


Digital Audio files
To play digital audio in your PC you need files which store the digital audio. The digital audio is stored in various formats (and so different file extensions).
There are two major groups of audio file formats

Uncompressed formats
There are many uncompressed data formats. The most popular of them is WAV. It is a flexible file format designed to store more or less any combination of sampling rates or bit rates. This makes it an adequate file format for storing and archiving an original recording. There are other uncompressed formats such as .AU, .AIFF which are not very popular now.

Compressed formats
Compressed file formats are based on principle that leave out sounds that humans cannot or can hardly hear, e.g. a low volume sound after a big volume sound. MP3 is such an example. Compressed file formats optimize on the size rather than the quality of audio. Compressed formats require less space on disk compared to uncompressed one but lack the same quality as of the uncompressed format. MP3 is a popular compressed audio format.

Wave file format
A WAVE file is a collection of a number of different types of chunks. There is a mandatory Format ("fmt ") chunk which contains important parameters describing the waveform, such as its sample rate. The Data chunk, which contains the actual waveform data, is also required. All other chunks are optional.
There are no restrictions upon the order of the chunks within a WAVE file, with the exception that the Format chunk must precede the Data chunk.


The figure below shows the format of the wave file


A Note about data segment

The data segment contains the actual audio samples. Based on “bits per sample” field you can decide how to read the data. If bits per sample field is 8 that means samples are organized as bytes and you can read the samples byte by byte.
If bits per sample field is 16 that means samples are organized as 16 bit words and you should read one 16 bit word at a time.

For multi-channel sounds (for example, a stereo waveform), single sample points from each channel are interleaved. For example, assume a stereo (ie, 2 channel) waveform. Instead of storing all of the sample points for the left channel first, and then storing all of the sample points for the right channel next, you "mix" the two channels' sample points together. You would store the first sample point of the left channel. Next, you would store the first sample point of the right channel. Next, you would store the second sample point of the left channel. Next, you would store the second sample point of the right channel, and so on, alternating between storing the next sample point of each channel. This is what is meant by interleaved data; you store the next sample point of each of the channels in turn, so that the sample points that are meant to be "played" simultaneously are stored contiguously.
The figure below shows the bytes of a actual wave file



No comments: