Audio Device programming example in Linux

Source: Internet
Author: User
Tags usleep

Publisher: chinaitlab Date: 00:00:00

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:
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 usleep (unsigned 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 speaker length is measured in microseconds */# define default_reps 1/* No repeated voices by default */# define default_delay 100/* define a structure in microseconds * // *, store the required data */typedef struct {int freq;/* I 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 <frequency value in Hz>" * "-l <audible duration in milliseconds>" * "-r <repeated times> "*" -d <Interval Duration in milliseconds> "*/void parse_command_line (Cha R ** 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") {/* length */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") {/* repeat 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;/* loop counter * // set the voice parameter to the default value */beep_parms_t parms = {default_freq, default_length, default_reps, default_delay }; /* analysis parameters. If possible, update the voice parameter */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);}/* start to make the speaker speak */for (I = 0; I <parms. reps; I ++) {/* Where the number 1190000 comes from. It is unknown */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 ;}

The above example is slightly extended so that the user can make the speaker sing. You only need to find the corresponding relationship between the scale, sound length, cycle and frequency, sound length, and interval of the Five-line or simplified spectrum. I still remember the excitement when I wrote "only good mom in the world" under DOS. Finally, to mention some other things, this is actually a very simple program, but we have used a long space, I hope that the reader can understand some methods of the program from the above Code. Perhaps the most important thing is to add comments. There will never be too many comments for a program. Even if you think it is redundant at the time of writing, believe me and many excellent programmers who have told us this: develop the habit of writing many comments.
Sound Card Programming
As long as we do not do work such as driver device development, there is no essential difference between sound card programming and speaker programming. When you try to write complex programs such as CD players and MP3 players, your job is to obtain information like CDROM control and MP3 decoding, this step of reading and writing system devices is simple in Linux. For example, in Linux, the simplest program for playing wav only has one line: CP $ <>/dev/audio. Write it into a shell file, which is also a program (shell programming ).
First, we need to know whether there is a sound card on a machine. One way to check is to check the file/dev/sndstat. If the file is opened incorrectly and the error code is enodev, the sound card is not installed on this machine. In addition, try to open the file/dev/DSP to check whether the sound card is installed.
There are many files related to sound cards in Linux, such as the/dev/DSP file for digital samples, the/dev/mixer file for the mixer, and the/dev/sequencer file for the sequencer. File/dev/audio is a sound device file based on compatibility considerations. It is actually a ing to the above digital devices, its biggest feature is its direct support for file formats such as WAV. The following example uses this device file to implement a simple RECORDER: We read audio frequency data from the sound card device (of course, we need to use the microphone to save the audio to the file test.wav. To play the wav file, use the command CP test.wav as described earlier.
>/Dev/audio. Of course, you can use other multimedia software in Linux to play the file.
The following is a complete program list:

/* This file defines all the variables below, such as snd _ */# include # includemain () {/* ID: Read the audio file descriptor; FD: the written file descriptor. I, j is the temporary variable */int id, FD, I, j;/* buffer for storing audio data, which can be adjusted */Char testbuf [4096]; /* Open the sound card device. If it fails, exit */If (ID = open ("/dev/Audio", o_rdwr) <0) {fprintf (stderr, "Can't open sound device! \ N "); exit (-1);}/* Open the output file. If it fails, exit */If (FD = open (" test.wav ", o_rdwr) <0) {fprintf (stderr, "can't open output file! \ N "); exit (-1);}/* set the appropriate parameter, make the sound device work normally * // * For details, refer to the Linux documentation on sound card programming */I = 0; IOCTL (ID, sndctl_dsp_reset, (char *) & I ); IOCTL (ID, sndctl_dsp_sync, (char *) & I); I = 1; IOCTL (ID, sndctl_dsp_nonblock, (char *) & I); I = 8000; IOCTL (ID, sndctl_dsp_speed, (char *) & I); I = 1; IOCTL (ID, sndctl_dsp_channels, (char *) & I); I = 8; IOCTL (ID, sndctl_dsp_setfmt, (char *) & I); I = 3; IOCTL (ID, sndctl_dsp_settrigger, (char *) & I); I = 3; IOCTL (ID, sndctl_dsp_setfragment, (char *) & I); I = 1; IOCTL (ID, sndctl_dsp_profile, (char *) & I);/* reads a certain amount of audio data, write it to the output file */For (j = 0; j <10;) {I = read (ID, testbuf, 4096); if (I> 0) {write (FD, filebuf, I); j ++ ;}/ * close the input and output files */close (FD); close (ID );}

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.