Linux Audio Driver Brief

Source: Internet
Author: User

First, digital audio

Audio signal is a continuous change of the analog signal, but the computer can only process and record binary digital signal, from the natural sound source audio signal must undergo a certain transformation, become a digital audio signal, can be sent to the computer for further processing.

The digital audio system realizes the reproduction of the original sound by converting the waveform of the sound wave into a series of binary data, which is often referred to as the analog-to-digital converter (A/D). The A/D converter samples The sound waves at a rate of tens of thousands of times per second, each of which records the state of the original analog sound at a given moment, often referred to as a sample, and the number of samples per second is called the sampling frequency, by connecting a series of successive samples. You can describe a sound in your computer. For each sample in the sampling process, the digital audio system allocates a certain storage bit to record the amplitude of the sound wave, commonly referred to as the sampling resolution or sampling accuracy, the higher the sampling accuracy, the more delicate the sound will be restored.

Digital audio involves a lot of concepts, and the most important thing for programmers who do audio programming under Linux is to understand the two key steps of sound digitization: sampling and quantification . Sampling is the amplitude of a sound signal at a certain time, while quantization is the conversion of the sampled sound signal amplitude to a digital value, in essence, the sampling is the digitization of time, and quantization is the magnitude of the digitization.

Here are a few of the technical indicators that are often needed for audio programming:

Sampling Frequency  

The sampling frequency is the number of times per second that the acoustic amplitude sample is sampled when the analog sound waveform is digitized. The selection of sampling frequency should follow the Nyquist (Harry Nyquist) Sampling theory: If an analog signal is sampled, the highest signal frequency that can be restored after sampling is only half of the sampling frequency, or the original signal can be reconstructed from the sampled signal series as long as the sampling frequency is twice times higher than the maximum frequency of the input signal. The frequency range of normal hearing is approximately between 20hz~20khz, according to the Nyquist sampling theory, in order to ensure that the sound is not distorted, the sampling frequency should be around 40kHz. Commonly used audio sampling frequency 8kHz, 11.025kHz, 22.05kHz, 16kHz, 37.8kHz, 44.1kHz, 48kHz, etc., if the use of higher sampling frequency, but also to achieve the sound quality of the DVD.

quantify the number of digits  

The quantization digit is digitized to the amplitude of the analog audio signal, it determines the dynamic range after the analog signal digitization, commonly has 8-bit, 12-bit and 16-bit. The higher the quantization bit, the greater the dynamic range of the signal, the more likely the digitized audio signal will be close to the original signal, but the more storage space is needed.

Number of channels  

The number of channels is another important factor that reflects the digital quality of audio, with mono and two-channel points. The two-channel, also known as stereo, has two lines in the hardware, with better sound quality and timbre than mono, but the amount of storage space occupied by digitizing is one-fold more than mono.

Second, sound card driver

For security reasons, applications under Linux cannot operate directly on a hardware device such as a sound card, but must pass through a kernel-supplied driver to complete it. The essence of audio programming on Linux is to use a driver to complete various operations on the sound card. The control of the hardware involves the operation of each bit in the register, usually this is directly related to the device and the requirements of the timing is very strict, if the work is left to the application staff, then the programming of the sound card will become extremely complex and difficult, the role of the driver is to shield the hardware of these underlying details, This simplifies the writing of applications.

There are two main types of sound card drivers commonly used under Linux:OSS and ALSA.

The biggest difference between ALSA and OSS is that ALSA is a free project maintained by volunteers, while OSS is a commercial product provided by the company, so the degree of adaptation to the hardware is better than ALSA, and it can support more types of sound cards. Alsa, although not as widely used as OSS, but has a more friendly programming interface, and fully compatible with OSS, is undoubtedly a better choice for application programmers.

Third, Linux OSS audio device driver

3.1 OSS-driven components

The OSS standard has 2 most basic audio Devices:mixer(mixer) and DSP(Digital signal processor).

In the hardware circuit of the sound card, mixer is a very important component, it is the function of the combination of multiple signals or stacked together, for different sound cards, the role of their mixer may vary. In OSS drivers, the/dev/mixer device file is the software interface through which the application operates on the mixer.

Mixer circuits typically consist of two parts: the input mixer (inputs mixer) and the output mixer. The input mixer is responsible for receiving analog signals from a number of different sources, sometimes referred to as mixing channels or mixing devices. After the analog signal is modulated by the gain controller and the volume regulator controlled by the software, the level modulation is performed in different mixing channels and then sent to the input mixer for sound synthesis. The electronic switch on the mixer controls which channels have a signal connected to the mixer, and some sound cards allow only one mix channel to be used as the sound source for the recording, while some sound cards allow any connection to the mix channel. The signals processed by the input mixer are still analog, and they are sent to A/D converter for digitizing.

The output mixer works like an input mixer, and there are multiple sources connected to the mixer, which are previously gain-adjusted. When the output mixer mixes all analog signals, there is usually a master gain adjuster to control the output sound size, and there are some tone controllers to adjust the tone of the output sound. The signals that are processed by the output mixer are also analog signals, which are eventually sent to the speakers or other analog output devices. The programming of the mixer includes how to set the level of the gain controller and how to switch between different sources, which are usually discontinuous and do not require a lot of computer resources like recording or playback. Because the mixer operation does not conform to the typical read/write operation pattern, most operations are done through the IOCTL () system call, in addition to the open () and close () two system calls. Unlike/DEV/DSP,/dev/mixer allows multiple applications to be accessed at the same time, and the mixer's settings persist until the corresponding device file is closed.

DSP is also known as a codec, to achieve recording (recording) and playback (play), the corresponding device file is/DEV/DSP or/DEV/SOUND/DSP. The/DEV/DSP provided by the OSS sound driver is a device file for digital sampling and digital recording, which means that the D/a converter on the sound card is activated for playback, and reading the data to the device means that the A/D converter on the sound card is activated for recording.

When reading data from a DSP device, the analog signal entered from the sound card is converted into a digital sample after A/d converter, stored in the kernel buffer of the sound card driver, and when the application reads the data from the sound card through the read () system call. The numeric sample results that are saved in the kernel buffer are copied to the user buffer specified by the application. It should be noted that the sound card sampling frequency is determined by the driver in the kernel, not the speed at which the application reads the data from the sound card. If the application reads data too slowly to lower the sample frequency of the sound card, the excess data is discarded (that is, overflow), and if the data is read faster than the sound card's sample frequency, the sound card driver blocks the application that requests the data until the new data arrives.

When writing data to a DSP device, the digital signal goes through a D/a converter into an analog signal and generates a sound. The speed at which the application writes data should be at least equal to the sample frequency of the sound card, which can cause sound pauses or pauses (that is, underflow). If the user writes too fast, it will be blocked by the sound card driver in the kernel until the hardware has the ability to process the new data.

Unlike other devices, sound cards typically do not require support for non-blocking (non-blocking) I/O operations. Even if the kernel OSS driver provides non-blocking I/O support, user space is not recommended.

Whether you are reading data from a sound card or writing data to a sound card, in fact you have a specific format (format), such as unsigned 8-bit, mono, 8KHz sample rate, which can be changed by the IOCTL () system call if the default values fail to meet the requirements. In general, after you open the device file/DEV/DSP in your application, you should set it up in the appropriate format before you can read or write data from the sound card.

3.2 Mixer Interface

int Register_sound_mixer (structfile_operations *fops, int dev);

The above function is used to register 1 mixer, the 1th parameter fops is the file operation interface, the 2nd parameter dev is the device number, if 1 is filled, the system automatically assigns a device number. Mixer is a typical 1 character device, so the main work of coding is to implement functions such as open (), IOCTL () in File_operations.

The most important function in the mixer interface file_operations is the ioctl (), which implements the different IO control commands of the mixer.

3.3 DSP Interface

int REGISTER_SOUND_DSP (structfile_operations *fops, int dev);

The above function is similar to Register_sound_mixer (), it is used to register 1 DSP devices, the 1th parameter fops is the file operation interface, the 2nd parameter dev is the device number, if filled 1, the system automatically assigns a device number. DSP is also a 1 typical character device, so the main work of coding is to implement read (),write (),IOCTL (file_operations) in the ) and other functions.

DSP interface file _operations read () and write () Span style= "COLOR: #4F81BD" >read () function gets the recording data from the audio controller to the buffer and copies it to the user space, write ()

DSP Interface file_operations in the IOCTL () function processing for sample rate, quantization accuracy, DMA parameter settings such as buffer block size IO controls the handling of commands.

In the process of copying data from buffers to audio controllers, it is often important to use DMA,DMA for sound cards. For example, when the driver sets up the DMA controller's source data address (in-memory DMA buffers), the destination address (the audio controller FIFO), and the DMA's data length when the sound is being placed, the DMA controller automatically sends the buffer's data fill FIFO until the corresponding data length is sent.

In OSS drivers, it is often a recommended method to establish a ring buffer for storing audio data. In addition, in the OSS driver, 1 large DMA buffers are generally divided into several blocks of the same size (also known as "segment", or fragment), and the driver uses DMA to move a fragment between the sound buffer and the sound card each time. In user space, you can use the IOCTL () system call to adjust the size and number of blocks.

In addition to read (), write (), and IOCTL (), the poll () function of the DSP interface usually needs to be implemented to give feedback to the user whether the DMA buffer is currently read or written.

During the OSS driver initialization process, REGISTER_SOUND_DSP () and Register_sound_mixer () are called to register the DSP and mixer devices, and UNREGISTER_SOUND_DSP is called when the module is unloaded ( AUDIO_DEV_DSP) and Unregister_sound_mixer (Audio_dev_mixer).

The Linux OSS driver structure is as follows:

3.4 OSS User space programming

1. DSP Programming

The operation of the DSP interface generally consists of the following steps:

① Open the device file /DEV/DSP

What mode to operate the sound card must also be specified when the device is turned on, for a sound card that does not support full duplex, should be opened in a read-only or write-only way, only those who support full-duplex sound card can be opened in a read-write manner, which also depends on the specific implementation of the driver. Linux allows applications to turn on or off the device files corresponding to the sound card multiple times, making it easy to switch between the playback state and the recording state.

② if necessary, set the buffer size

The sound card driver running in the Linux kernel is dedicated to maintaining a buffer whose size affects the playback and recording effects, and the IOCTL () system call can be used to properly set its size. It is not necessary to adjust the buffer size in the driver, and if there is no special requirement, the default buffer size will be used. If you want to set the size of the buffer, it should usually be immediately after the device file is opened, because other actions on the sound card can cause the driver to no longer be able to modify its buffer size.

③ set the channel ( Channel ) Quantity

Depending on the hardware device and the driver, it can be set to mono or stereo.

④ set sampling format and sampling frequency

Sampling formats include AFMT_U8 (unsigned 8-bit), AFMT_S8 (signed 8-bit), Afmt_u16_le (small-ended mode, unsigned 16-bit), Afmt_u16_be (big-endian, unsigned 16-bit), Afmt_mpeg, AFMT_AC3, and so on. Use the SNDCTL_DSP_SETFMT IO Control command to set the sampling format.
For most sound cards, the sampling frequency range is typically 5kHz to 44.1kHz or 48kHz, but does not mean that all successive frequencies within the range are hardware-supported, and that the most common sampling frequencies for audio programming under Linux are 11025Hz, 16000Hz, 22050Hz, 32000Hz and 44100Hz. Use the Sndctl_dsp_speed IO Control command to set the sampling frequency.

⑤ Read/write /DEV/DSP implementing playback or recording

2. Mixer Programming

The mixer on the sound card consists of multiple mixing channels, which can be programmed by the device file/dev/mixer provided by the driver.

Adjusting the input gain and output gain of the sound card is one of the main functions of the mixer, and most of the current sound cards use a 8-bit or 16-bit gain controller, and the sound card driver converts them into percentages, meaning that both the input gain and the output gain range from 0~100.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.