Audio programming in Linux

Source: Internet
Author: User
Tags usleep

In fact, the programming of 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.

1. Programming internal speakers
The internal speaker is part of the console, so its 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 conversions are required:
Speaker audio = 1190000/the 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 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 <frequency value in Hz>"
* "-L <voice duration in milliseconds>"
* "-R <repeated times>"
* "-D <interval in milliseconds>"
*/
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;
}
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.

2. 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 the audio data from the sound card device (of course, use a microphone) and store it in test.wav. To play the wav file, use the command CP test.wav>/dev/audio as described earlier. Of course, you can also use other multimedia software in Linux to play the file.
The following is a complete program list:

/* This file defines all the following variables, such as snd */
# Include <sys/Soundcard. h>
# Include <stdio. h>
# Include <sys/types. h>
# Include <sys/STAT. h>
# Include <fcntl. h

Main ()
{
/* ID: Read audio file descriptor; FD: written file descriptor. I, J is a temporary variable */
Int ID, FD, I, j;
/* Buffer for storing audio data, which can be adjusted */
Char testbuf [4096];
/* Enable the sound card device. If the device fails, exit */
If (ID = open ("/dev/Audio", o_rdwr) <0 ){
Fprintf (stderr, "can't open sound device! /N ");
Exit (-1 );
}
/* Open the output file and exit if the file fails */
If (FD = open ("test.wav", o_rdwr) <0 ){
Fprintf (stderr, "can't open output file! /N ");
Exit (-1 );
}
/* Set appropriate parameters to make the sound device work properly */
/* 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 );
/* Read a certain amount of audio data and 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.