In the Linux operating system, program the audio device instance-Linux general technology-Linux programming and kernel information. The following is a detailed description. Programming for sound devices in Linux is much simpler than most people think. Generally, our commonly used sound devices are internal speakers and sound cards. They all correspond to one or more device files in the/dev directory. We open them like opening common files, use the ioctl () function to set some parameters and then perform write operations on these opened special files.
Because these files are not common files, we cannot use ansi c (Standard C) fopen, fclose, and so on to operate files, instead, you should use the system file I/O processing functions (open, read, write, lseek, and close) to process these device files. Ioctl () is perhaps the most complex function in Linux. It can control the attributes of various files. In Linux sound device programming, the most important thing is to use this function to correctly set necessary parameters.
The following two examples illustrate how to implement voice programming in Linux. Because this type of programming involves the reading and writing of system devices, you often need to have the root permission. If you fail to correctly execute the following example after compiling, first, check whether you are not authorized to manipulate a device.
Programming the internal speaker is part of the console, so the corresponding device file is/dev/console. The variable KIOCSOUND is declared in the header file/usr/include/linux/kd. h. The ioctl function can be used to control the speaker's voice. The usage rules are as follows: