Linux under audio programming

xiaoxiao2021-03-06  18

In fact, the sound equipment under Linux is much simpler than most people think. Generally speaking, our commonly used sound devices are internal speakers and sound cards, they all correspond to one or more devices files in / dev directory, and we open them like open ordinary files, set some parameters with the ioctl () function, and then These open special files. Since these files are not ordinary files, we cannot operate files with ANSI C (standard C), FCLOSE, etc., and should use the system file I / O processing function (Open, Read, Write, Lseek and Close). These device files. IOCTL () may be the most common function under Linux, which controls the properties of various files. In Linux sound device programming, the most important thing is to use this function to properly set the necessary parameters. Let's take two actual examples to explain how to implement sound programming under Linux. Since such programming involves reading and writing of system devices, many times you need to have root privileges, if you use the following example compile, you can't execute correctly, then you first check if it is because there is no device. . 1. Programming the internal speaker is part of the console, so it corresponds to the device file as / dev / console. Variable KiocSound declaration in header file / usr / include / linux / kD.h, the IOCTL function uses it to control the sound of the speaker, the rules are: ioctl (fd, kiocsound, (int) Tone); fd is the file device number TONE is the audio value. When Tone is 0, the end is terminated. It must be mentioned that the audio it is understood and the audio we usually think is different. Since the clock frequency of the computer motherboard timer is 1.19MHz, the correct vocal must be performed, the following conversion must be performed: speaker audio value = 1190000 / We expect the audio value. The length of the speaker vocalization time We control it through the function USEP (unsigned long usec). It is defined in header file / usr / include /unistd.h, let the program sleep USEC microseconds.

Below is a complete list of programs that make the speakers to vocalize the specified length and audio: #include #include #include #include #include #include #include #include / * setting default * / #define default_freq 440 / * Set a suitable frequency * / #define default_length 200 / * 200 microseconds, the length of the sound is * / #Define default_reps 1 / * default non-repetition * / * in microsecond * / * 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);} / * Analysis running parameters, all the significance is as follows: * "-f " * "-L " * "-r " * "-d 10000)) {fprintf (stderr, "bad parameter: frequency must be from 1..10000 / n"); exit (1);} else { Result-> freq = freq; argv ;}} else if (! strcmp (* argv, "-l")) {/ * duration * / int length = ATOI (* ( argv)); if (Length <0 ) {fprintf (stderr, "bad parameter: length); exit (1);} else {result-> length = length; argv ;}} else if (! strcmp (* argv," -r ")) {/ * repetition number * / int REPS =

ATOI (* ( Argv)); if (REPS <0) {fprintf (stderr, "bad parameter: reps must be> = 0 / n"); EXIT (1);} else {result-> REPS = REPS Argv ;}} else if (! strcmp (* argv, "-d")) {/ * delay * / int delay = ATOI (* ( argv)); if (delay <0) {fprintf (stderr , "Bad Parameter: DELAY MUST BE> = 0 / N"); exit (1);} else {result-> delay = delay; argv ;}} else {fprintf (stderr, "bad parameter:% s / n" , * argv); usage_bail (arg0);}}}} INT main (int Argc, char ** argv) {int console_fd; int i; / * loop counter * / / * set the sound parameter is the default value * / 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 started to make speakers * / for (i = 0; I

When you try to prepare a complex program such as a CD player, MP3 player, your job is to get information on the CDROM control, MP3 decoding, and reading and writing system devices in Linux Mutual thinking is simple. 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. LINUX and sound card related files have many files, such as acquiring digital samples / dev / dsp files, for the mixer / dev / mixer file, and / dev / sequencer for the sequencer, etc. File / dev / audio is a voice device file based on compatibility. It is actually a mapping of the above digital devices. Its maximum feature may be direct support for file format 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 of the following shapes such as SND_ * / #include #include #include # Include #include

转载请注明原文地址:https://www.9cbs.com/read-41064.html

New Post(0)