Linux
The sound equipment programming is much simpler than most people think. Generally speaking, our common sound equipment is internal speakers and sound cards, they all correspond
/ dev
One or more device files in the directory, we open them like open ordinary files, use
IOCTL
() The function sets some parameters, buts to use these open special files.
These files are not ordinary files, so we can't use
ANSI C
(standard
C
)of
Fopen
,
Fclose
Wait to operate file, but should use system files
I / O
Processing function
Open
,
reta
,
Write
,
Lseek
with
Close
) To process these device files.
IOCTL
()perhaps
Linux
The most common function, it can control the properties of various files,
Linux
In the voice device programming, the most important thing is to use this function to set the necessary parameters correctly.
Let's take two actual examples to explain how to implement
Linux
The sound programming is programmed. Such programming involves the reading and writing of the system equipment, so many times you need you
root
Permissions, if you use the following example compile can't be executed correctly, then you first check if it is because there is no permission to manipulate a device.
1.
Interior speaker programming
The internal speaker is part of the console, so its corresponding device file is
/ dev / console
. variable
Kiocsound
Head file
/ usr / include / linux / kd.h
Declaration,
IOCTL
The function uses it to control the sound of the speaker, and the rules are:
IOCTL (FD, KiocSound, (int) TONE);
FD
For the file device number,
Tone
Is the audio value. when
Tone
for
0
Time to terminate the sound. It must be mentioned that the audio it is understood and the audio we usually think is different, and the clock frequency of the calculator motherboard timer is
1.19MHz
Therefore, the correct vocal must be performed, and the following conversion must be performed:
Speaker audio value
= 1190000 /
We expect the audio value.
The length of the speaker vocational time We pass functions
Usleep
(
Unsigned long
) To control. It is in the header file
/ usr / include /unistd.h
Defined in order to sleep
USEC
Microseconds. Below is a complete list of programs that make the speaker press the specified length and audio:
#include
/ * Set the default * / # define default_freq 440 / * Set a suitable frequency * / # define default_length 200 / * 200 microseconds, the length of the sound is in microseconds * / # Define default_reps 1 / * By default, no repetition * / # define default_delay 100 / * equally in microseconds * /
/ * Define a structure, store the required data * / typef struct {int freq; / * We expect the frequency of output, unit is Hz * / int LENGTH; / * Vocal length, in microseconds * / int REPS; / * Repeat the number of times * / int custom; / * Two vocal spacing, in microseconds * /} beep_parms_t; / * Print Help information and exit * / void usage_bail (const char * executable_name) {Printf ("Usage: / N / T% s [-f frequency] [-l length] [-r reps] [-d delay] / n ", executable_name); exit (1);}
/ * Analyze the operation parameters, all the significance is as follows: * "-f
int main (int argc, char ** argv) {int console_fd; int i; / * // * * loop counter provided utterance default parameters * / beep_parms_t parms = {DEFAULT_FREQ, DEFAULT_LENGTH, DEFAULT_REPS, DEFAULT_DELAY}; / * Analysis Parameters, possible update Voice Parameters * / PARSE_COMMAND_LINE (Argv, & Parms); / * Open the console, end the program * / if ((console_fd = open ("/ dev / console", o_wronly)) == -1) {FPRINTF (stderr, "failed to open console./n" "; perror ("open" ";exit( 1 );/* Really let the speaker vocal * / for (i = 0; i IOCTL (console_fd, kiocsound, magical_fairy_number); / * Start vocal * / usleep (1000 * parms.length); / * Wait ... * / ioctl (console_fd, kiocsound, 0); / * Stop voice * / usleep (1000 * Parms.delay); / * Wait ... * /} / * Repeat the play * / return exit_suCcess;} Take the above example slightly, the user can sing the speaker. As long as you find a five-wire or notes, the pitch, beats, and frequencies, the vocality, the corresponding relationship between the interval is OK. I still remember the excitement when I have written out "I have only mother in the world". Most, say some extraordinary words, this is actually a very simple program, but we used a long space, I hope that readers can experience some of the methods of writing from the above code, maybe the most important thing is to add Comment. The annotation of a program will never be too much, even when you write, I feel that it is more than, but I believe in me, I believe many of the excellent programmers who have to tell us: Develop a lot of comments. 2. Programming for sound card As long as we are not working, such as driven equipment development, the programming of the sound card and the programming of the speakers on the speaker. When you try to write a complex program such as a CD player, MP3 player, your job is to get information about the CDROM control, MP3 decoding, and reading and writing system devices under Linux. It is easy to imagine. For example, the simplest playback of WAV under Linux is only one line: CP $ <> / dev / audio. Write it into a shell file, which is also a program (shell programming). We first need to know if there is a sound card on a machine, an inspection method is to check the file / dev / sndstat file, if this file is incorrect, and the error number is ENODEV, then this machine does not install the sound card. In addition, try to open the file / dev / dsp or check if the sound card is installed. There are many files related to linux and sound cards, such as acquiring digital samples / dev / dsp files, for the mixer / dev / mixer file, and / dev / sequencer with sequencer / dev / sequencer. File / dev / audio is a sound device file that is based on based compatibility. It is actually an image of the above digital devices. Its biggest feature is perhaps a direct support for file formats such as WAV. The following example uses this device file to implement a simple recorder: We use the sound card device (of course to use the microphone) to read audio data, and store it to the file Test.wav. To play this WAV file, just as mentioned earlier, use the command cp test.wav> / dev / audio, of course, you can also play this file with other multimedia software under Linux. Below is a complete program list: / * This file defines all the variables such as SND_ * / # include