Linux audio programming instance

zhaozj2021-02-16  37

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 #include #include #include #include #include #include #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 " * "-r " * " -d "* / void parse_command_line (char ** argv, beep_parms_t * result) {char * arg0 = * (argv ); while (* argv) {if (! strcmp (* argv, "-f")) {/ * frequency * / int freq = ATOI (* ( argv)); if ((FREQ <= 0) | | (FREQ> 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 * / intleth = ATOI (* ( Argv)); if (Length <0) {fprintf (stderr, "bad parameter: length); exit (1);} else { Result-> length = length; argv ;}} else if (! strcmp (* argv, "-r")) {/ * repetition * / 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 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 #include #include #include #include #include

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

New Post(0)