Audio Device programming example for Linux operating system (1)

Source: Internet
Author: User

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 and use ioctl) the function sets some parameters and then writes the 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, use the system file I/O processing functions open, read, write, lseek, and close) to process these device files. Ioctl) may be the most complex function in Linux. It can control the attributes of various files. In the programming of Linux sound devices, 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:

Ioctl (fd, KIOCSOUND, (int) tone );

Fd indicates the file device number, and tone indicates the audio value. When tone is 0, the voice is terminated. It must be mentioned that the audio we understand is different from the audio we usually think. Because the clock frequency of the timer on the computer motherboard is 1.19 MHZ, sound should be made correctly, the following conversion is required: Speaker audio value = 1190000/our expected audio value.

The duration of speaker voice is controlled by usleepunsigned long usec. It is defined in the header file/usr/include/unistd. h to make the program sleep usec microseconds. The following is a complete list of programs that enable the speaker to sound at the specified length and audio:

# Include <fcntl. h>

# Include <stdio. h>

# Include <stdlib. h>

# Include <string. h>

# Include <unistd. h>

# Include <sys/ioctl. h>

# Include <sys/types. h>

# Include <linux/kd. h>

/* Set the default value */

# Define DEFAULT_FREQ 440/* set an appropriate frequency */

# Define DEFAULT_LENGTH 200/* 200 microseconds. The audible length is measured in microseconds */

# Define DEFAULT_REPS 1/* No repeated voices by default */

# Define DEFAULT_DELAY 100/* in microseconds */

/* Define a structure to store the required data */

Typedef struct {

Int freq;/* the expected output frequency, in Hz */

Int length;/* audible length, in microseconds */

Int reps;/* repeated times */

Int delay;/* Two audible intervals, in microseconds */

} Beep_parms_t;

/* Print the 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 running parameters, which have the following meanings:

* "-F <以hz为单位的频率值> "

* "-L <以毫秒为单位的发声时长> "

* "-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 */

Int length = atoi (* (++ argv ));

If (length <0 ){

Fprintf (stderr, "Bad parameter: length must be> = 0 \ n ");

Exit (1 );

} Else {

Result-> length = length;

Argv ++;

}

} Else if (! Strcmp (* argv, "-r") {/* repeated times */

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") {/* latency */

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;/* cyclic counter */

/* Set the voice parameter to the default value */

Beep_parms_t parms = {DEFAULT_FREQ, DEFAULT_LENGTH, DEFAULT_REPS,

DEFAULT_DELAY };

/* Analyze parameters. If possible, update the audible parameters */

Parse_command_line (argv, & parms );

/* Open the console and terminate the program if it fails */

If (console_fd = open ("/dev/console", O_WRONLY) =-1 ){

Fprintf (stderr, "Failed to open console. \ n ");

Perror ("open ");

Exit (1 );

}

/* Start to let the speaker speak */

For (I = 0; I <parms. reps; I ++ ){

/* Do not know where the number 1190000 comes from */

Int magical_fairy_number = 1190000/parms. freq;

Ioctl (console_fd, KIOCSOUND, magical_fairy_number);/* Start voice */

Usleep (1000 * parms. length);/* Wait ...*/

Ioctl (console_fd, KIOCSOUND, 0);/* Stop voice */

Usleep (1000 * parms. delay);/* Wait ...*/

}/* Replay */

Return EXIT_SUCCESS;

}


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.