From: http://www.linuxidc.com/Linux/2009-08/21427.htm
Reference material: <Linux Programming Technology details> edited by Du Hua
Page number: p186
You can write an audio file into the device file of the sound card to play the audio file. Using the READ function to read the content in the sound card device file can enable the recording function. The program code below realizes the recording function of the sound card device in Linux.
The Code is as follows:
// P6.8.c sound card recording function # include <unistd. h> # include <fcntl. h> # include <sys/types. h> # include <sys/IOCTL. h> # include <stdlib. h> # include <stdio. h> # include <Linux/Soundcard. h> // recording time # define length 3 // sampling frequency # define rate 8000 // quantize bits # define size 16 // number of audio channels http://ubuntuone.cn/#define channels 2 // Save the recording audio data unsigned char Buf [length * rate * size * channels/8]; int main (void) {// file descriptor of the sound device int FD; int ARG; // used to save the int status returned by IOCTL; // enable the sound Device FD = open ("/dev/DSP", o_rdwr); If (FD <0) {perror ("cannot open/dev/DSP device"); return 1 ;} // set the following sound card parameters // set the quantified bits for sampling: Arg = size; status = IOCTL (FD, sound_pcm_write_bits, & Arg); If (status =-1) {perror ("cannot set sound_pcm_write_bits"); return 1;} // set the number of sample channels Arg = channels; status = IOCTL (FD, sound_pcm_write_channels, & Arg ); if (status =-1) {perror ("cannot set sound_pcm_write_channels"); return 1 ;}// set the sampling frequency ar G = rate; status = IOCTL (FD, sound_pcm_write_rate, & Arg); If (status =-1) {perror ("cannot set sound_pcm_write_rate"); return 1 ;} // keep recording until you press "control-c" to stop while (1) {printf ("recording...: \ n "); status = read (FD, Buf, sizeof (BUF); If (status =-1) {perror ("read wrong number of bytes");} printf ("play...: \ n "); status = write (FD, Buf, sizeof (BUF); If (status! = Sizeof (BUF) perror ("wrote wrong number of bytes"); // wait until the playback ends before resuming the recording. Status = IOCTL (FD, sound_pcm_sync, 0 ); if (status =-1) perror ("cannot set sound_pcm_sync");} return 0 ;}