#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <stdio.h>
#include <linux/soundcard.h>
#include <termios.h>
#define LENGTH 10//Recording time, sec
#define RATE 88200//Sampling frequency
#define SIZE 16//quantization number of digits
#define CHANNELS 2//channel number
#define the size of the Rsize 8//buf,
/******** The following is the file header format description for the wave format file ******/
/*------------------------------------------------
| RIFF WAVE Chunk |
| ID = ' RIFF ' |
| Rifftype = ' WAVE ' |
------------------------------------------------
| Format Chunk |
| ID = ' FMT ' |
------------------------------------------------
| Fact Chunk (optional) |
| ID = ' fact ' |
------------------------------------------------
| Data Chunk |
| ID = ' data ' |
------------------------------------------------*/
/********** above is the wave file format header format Description ***********/
/*wave file A total of four chunk composition, where the third chunk can be omitted, each chunk has a mark (ID),
Size, which is the content of this chunk part of the length of the contents of the three components of the * *
struct Fhead
{
/****riff WAVE chunk*/
unsigned char a[4];//four bytes storing ' R ', ' I ', ' f ', ' F '
long int b; The length of the entire file-8; The size field for each chunk represents the length except for the ID and size field of the chunk;
unsigned char c[4];//four bytes storing ' W ', ' A ', ' V ', ' E '
/****riff WAVE chunk*/
/****format chunk*/
unsigned char d[4];//four bytes storing ' f ', ' m ', ' t ', '
long int e; After 16 there is no additional message, 18 is followed by an additional message; generally 16, and the other format is 18.
short int F; Encoding method, generally for 0x0001;
short int g; Number of channels, 1 mono, 2 double channels;
long int h; sampling frequency;
long int i; The number of bytes required per second;
short Int J; The number of bytes per sample, if the channel is double, two are considered together;
short int k; That is, the number of digits
/****format chunk*/
/***data chunk**/
unsigned char p[4];//four bytes to store ' d ', ' a ', ' t ', ' a '
long int q; Voice data part length, excluding any part of the file header
}wavehead;//the file header structure that defines the wave file
int main (void)
{
/* The following wave file header assignment * *
wavehead.a[0]= ' R ';
wavehead.a[1]= ' I ';
wavehead.a[2]= ' F ';
wavehead.a[3]= ' F ';
wavehead.b=length*rate*channels*size/8-8;
wavehead.c[0]= ' W ';
wavehead.c[1]= ' A ';
wavehead.c[2]= ' V ';
wavehead.c[3]= ' E ';
Wavehead.d[0]= ' F ';
wavehead.d[1]= ' m ';
wavehead.d[2]= ' t ';
Wavehead.d[3]= ';
wavehead.e=16;
wavehead.f=1;
Wavehead.g=channels;
Wavehead.h=rate;
WAVEHEAD.I=RATE*CHANNELS*SIZE/8;
WAVEHEAD.J=CHANNELS*SIZE/8;
Wavehead.k=size;
wavehead.p[0]= ' d ';
Wavehead.p[1]= ' a ';
wavehead.p[2]= ' t ';
Wavehead.p[3]= ' a ';
WAVEHEAD.Q=LENGTH*RATE*CHANNELS*SIZE/8;
* * Above Wave file header assignment/
int i;
unsigned char buf[rsize]; Each loop gets the size of the rsize, puts the buf in, and then writes the file, and the playback is reversed.
Turn on the sound card device, read-only, and set the sound card
int fd_dev_r= Open ("/DEV/DSP", o_rdonly,0777);
if (Fd_dev_r < 0)
{
Perror ("Cannot open/dev/dsp device");
return 1;
}
int arg = SIZE;
if (IOCTL (Fd_dev_r, sound_pcm_write_bits, &arg) = = 1)//Set the number of quantization digits
{
Perror ("Cannot set sound_pcm_write_bits");
return 1;
}
arg = channels;
if (IOCTL (Fd_dev_r, sound_pcm_write_channels, &arg) = = 1)//Set Number of channels
{
Perror ("Cannot set sound_pcm_write_channels");
return 1;
}
arg = RATE;
if (IOCTL (Fd_dev_r, sound_pcm_write_rate, &arg) = = 1)//Set sample rate
{
Perror ("Cannot set Sound_pcm_write_write");
return 1;
}
Turn on the sound card device, write only, and set the sound card
int fd_dev_w = open ("/DEV/DSP", o_wronly,0777);
if (Fd_dev_w < 0)
{
Perror ("Cannot open/dev/dsp device");
return 1;
}
arg = SIZE;
if (IOCTL (Fd_dev_w, sound_pcm_write_bits, &arg) = = 1)//Set the number of quantization digits
{
Perror ("Cannot set sound_pcm_write_bits");
return 1;
}
arg = channels;
if (IOCTL (Fd_dev_w, sound_pcm_write_channels, &arg) = = 1)//Set Number of channels
{
Perror ("Cannot set sound_pcm_write_channels");
return 1;
}
arg = RATE;
if (IOCTL (Fd_dev_w, sound_pcm_write_rate, &arg) = = 1)//Set sample rate
{
Perror ("Cannot set Sound_pcm_write_write");
return 1;
}
/** The following start recording **/
System ("Rm-f sound.wav");//Erase existing WAV audio files
int fd_f;
if (Fd_f = Open ("./sound.wav", o_creat| o_rdwr,0777) ==-1)//Create a Wave format voice file
{
Perror ("Cannot creat the sound file");
}
if (Write (Fd_f, &wavehead, sizeof (Wavehead)) ==-1)/file header written to wave file
{
Perror ("Write to Sound ' head wrong!!");
}
For (i=0;i< (LENGTH*RATE*SIZE*CHANNELS/8)/rsize;i++)//each time from the sound card to obtain rsize size of the data, the total circulation of the voice length/rsize times
{
if (read (Fd_dev_r, buf, sizeof (BUF))!= sizeof (BUF))
{
Perror ("read wrong number of bytes");
}
if (Write (Fd_f, buf, sizeof (BUF)) ==-1)
{
Perror ("Write to Sound wrong!!");
}
}
Close (fd_dev_r);//Turn off read-only sound card
Close (fd_f);//Turn off wave file
printf ("Play your sound now??") \ n y OR n\n ");
/*** The following is the play WAV voice file **/
if (getchar () = = ' Y ')
{
printf ("play...:\n");
if ((Fd_f = Open ("./sound.wav", o_rdonly,0777)) ==-1)
{
Perror ("Cannot creat the sound file");
}
Lseek (Fd_f,44,seek_set);
For (i=0;i< (LENGTH*RATE*SIZE*CHANNELS/8)/rsize;i++)
{
if (read (Fd_f, buf, sizeof (BUF))!= sizeof (BUF))//Reading speech files
Perror ("write Wrong number of bytes");
if (Write (Fd_dev_w, buf, sizeof (BUF))!= sizeof (BUF))//Send sound card playback
Perror ("wrote2 Wrong number of bytes");
}
Close (Fd_f);
Close (FD_DEV_W);
return 0;
}
Else
{
printf ("bye!\n");
return 0;
}
}