1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <math.h>
6 #include <math.h>
7 #include <sys/ioctl.h>
8 #include <fcntl.h>
9 #include <sys/soundcard.h>
10 #include "../other/signal_processing_library.h"
11
12 #include "../filter_audio.h"
13
14
15 #define DEVICE_NAME "/dev/dsp"
16
17
18
main(int argc,char * argv[])19 int main(int argc, char *argv[])
20 {
21 unsigned int sample_rate = 16000; //16khz
22 unsigned int format = 16;
23 unsigned int channels = 1;
24 unsigned int status;
25 unsigned int audio_fd; //����
26 unsigned int recoder_fid;
27
28 unsigned int samples_perframe = sample_rate/50;
29 _Bool filter = 1;
30
31 FILE *fp_in,*fp_out;
32 char *read_mic_file = "read_from_mic.pcm";
33 char *result_pcm_file = "result_out_agc.pcm";
34 fp_in=fopen(read_mic_file,"wb");
35 fp_out=fopen(result_pcm_file,"wb");
36
37
38 recoder_fid = open(DEVICE_NAME, O_RDONLY,0777);
39 if (recoder_fid < 0)
40 {
41 perror("Cannot open /dev/dsp device");
42 return 1;
43 }
44
45 status = ioctl(recoder_fid, SOUND_PCM_WRITE_BITS, &format);//���������
46 if(status == -1)
47 {
48 perror("Cannot set SOUND_PCM_WRITE_BITS ");
49 return 1;
50 }
51
52 status = ioctl(recoder_fid, SOUND_PCM_WRITE_CHANNELS, &channels);//����������
53 if (status == -1)
54 {
55 perror("Cannot set SOUND_PCM_WRITE_CHANNELS");
56 return 1;
57 }
58
59 status = ioctl(recoder_fid, SOUND_PCM_WRITE_RATE, &sample_rate);//������
60 if (status == -1)
61 {
62 perror("Cannot set SOUND_PCM_WRITE_WRITE");
63 return 1;
64 }
65
66
67 if ((audio_fd = open(DEVICE_NAME, O_WRONLY,0777)) == -1)
68 {
69 printf("open error\n");
70 return -1;
71 }
72
73
74 if (ioctl(audio_fd, SOUND_PCM_WRITE_BITS, &format) == -1)
75 {
76 /* fatal error */
77 printf("SNDCTL_DSP_SETFMT error\n");
78 return -1;
79 }
80
81 int param;
82 param = ( 0x0002 << 16) + 0x0006; //����param����������ɣ���16λΪfragment�Ĵ�С���˴�0x0007��ʾfragment��СΪ2^7����128�ֽڣ�
83 //��16λΪfragment���������˴�Ϊ0x0002����2��fragement��
84
85 if (ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, ¶m) == -1) {
86 printf("SNDCTL_DSP_SETFRAGMENT error\n");
87 }
88
89
90 if (ioctl(audio_fd, SOUND_PCM_WRITE_CHANNELS, &channels) == -1)
91 {
92 /* Fatal error */
93 printf("SOUND_PCM_WRITE_CHANNELS error");
94 return -1;
95 }
96
97 if (ioctl(audio_fd, SOUND_PCM_WRITE_RATE, &sample_rate)==-1)
98 {
99 /* Fatal error */
100 printf("SOUND_PCM_WRITE_RATE error\n");
101 return -1;
102 }
103
104 int16_t *SigBuf;
105 float *Sigout;
106
107 Filter_Audio *f_a = new_filter_audio(sample_rate);
108
109 SigBuf = calloc(1, sizeof(int16_t) * samples_perframe);
110 Sigout = calloc(1, sizeof(float) * samples_perframe);
111
112 int delay_ms = 0;
113 sscanf(argv[1],"%d", &delay_ms);
114 set_echo_delay_ms(f_a, delay_ms);
115
116
117
118 while (1)
119 {
120
121
122 read(recoder_fid, SigBuf, sizeof(SigBuf[0])*samples_perframe);
123 fwrite(SigBuf,sizeof(SigBuf[0]),samples_perframe,fp_in);
124 if (filter && filter_audio(f_a, SigBuf, samples_perframe) == -1)
125 {
126 printf("filter_audio fail\n");
127 return 0;
128 }
129
130
131 write(audio_fd,SigBuf,sizeof(int16_t)*samples_perframe);
132
133
134 fwrite(SigBuf,sizeof(SigBuf[0]),samples_perframe,fp_out);
135 }
136
137
138 return 0;
139 }
140
141