diff options
Diffstat (limited to 'doc/examples/SoundRecorder/src/main.c')
-rw-r--r-- | doc/examples/SoundRecorder/src/main.c | 296 |
1 files changed, 296 insertions, 0 deletions
diff --git a/doc/examples/SoundRecorder/src/main.c b/doc/examples/SoundRecorder/src/main.c new file mode 100644 index 00000000..f1967da6 --- /dev/null +++ b/doc/examples/SoundRecorder/src/main.c | |||
@@ -0,0 +1,296 @@ | |||
1 | /* by Luis Figueiredo (stdio@netc.pt) | ||
2 | * | ||
3 | * file: main.c | ||
4 | * | ||
5 | * description: Programa main | ||
6 | * | ||
7 | * date: 16:47,13-47-2002 | ||
8 | */ | ||
9 | |||
10 | |||
11 | #include "soundcard.h" | ||
12 | #include "soundbuffers.h" | ||
13 | #include "soundmixer.h" | ||
14 | #include "web_server.h" | ||
15 | #include <signal.h> | ||
16 | |||
17 | #define ADJUST_VOLUME(s, v) (s = (s*v)/128) | ||
18 | |||
19 | int PORT=80; | ||
20 | struct sound_buf *SoundBuf; | ||
21 | |||
22 | void outgif() { | ||
23 | char *nam; | ||
24 | struct sound_buf *sbuf=NULL; | ||
25 | char gif[640*40+1]; | ||
26 | int x=0,y=0; | ||
27 | int i,h=20; | ||
28 | short d; | ||
29 | nam=malloc(strlen(ClientInfo->request)-3); | ||
30 | memcpy(nam,ClientInfo->request+1,strlen(ClientInfo->request)-4); | ||
31 | nam[strlen(ClientInfo->request)-5]=0; | ||
32 | sbuf=sbuf_select(SoundBuf,nam); | ||
33 | if(sbuf!=NULL) { | ||
34 | if(sbuf->data_i>_SDMAX) { | ||
35 | if(gif!=NULL) { | ||
36 | memset(gif,0,640*40); | ||
37 | for(i=0;i<1280;i++) { | ||
38 | x=i/2; | ||
39 | //y=(*((unsigned short *)sbuf->data+((i*4)%640))%20)+10; | ||
40 | d=((sbuf->data[((i*((sbuf->data_i-1280)/2560))*2)+1]<<8)|sbuf->data[((i*((sbuf->data_i-1280)/2560))*2)]); | ||
41 | y=d/(32767/20)+20; | ||
42 | //fprintf(stderr,"y=%d\n",y); | ||
43 | //gif[x+y*640]=10; | ||
44 | if(h>y) { | ||
45 | for(h=20;h>y;h--) gif[x+h*640]=10; | ||
46 | } else { | ||
47 | for(h=20;h<=y;h++) gif[x+h*640]=10; | ||
48 | }; | ||
49 | |||
50 | }; | ||
51 | printf("Cache-control: no-cache\r\n"); | ||
52 | printf("Content-type: image/gif\r\n\r\n"); | ||
53 | web_client_gifoutput(gif,640,40,0); | ||
54 | }; | ||
55 | }; | ||
56 | }; | ||
57 | free(nam); | ||
58 | return; | ||
59 | }; | ||
60 | void dlbuf() { | ||
61 | char *nam; | ||
62 | struct sound_buf *sbuf=NULL; | ||
63 | nam=malloc(strlen(ClientInfo->request)-3); | ||
64 | memcpy(nam,ClientInfo->request+1,strlen(ClientInfo->request)-4); | ||
65 | nam[strlen(ClientInfo->request)-5]=0; | ||
66 | sbuf=sbuf_select(SoundBuf,nam); | ||
67 | printf("Content-type: x-form/application\r\n\r\n"); | ||
68 | if(sbuf!=NULL) { | ||
69 | fwrite(sbuf->data,sbuf->data_i,1,stdout); | ||
70 | }; | ||
71 | |||
72 | }; | ||
73 | |||
74 | void index_html() { | ||
75 | struct sound_buf *sbuf=NULL; | ||
76 | char *id; | ||
77 | char *tmp; | ||
78 | int i; | ||
79 | unsigned char *soundout=NULL; | ||
80 | short src1,src2; | ||
81 | int dst_sample; | ||
82 | const int max_audioval =((1<<(16-1))-1); | ||
83 | const int min_audioval =-(1<<(16-1)); | ||
84 | int bigger=0; | ||
85 | |||
86 | printf("Cache-control: no-cache\r\n"); | ||
87 | printf("Content-type: text/html\r\n\r\n"); | ||
88 | printf("<HTML>\n"); | ||
89 | printf("<BODY>\n"); | ||
90 | printf("<center>Sound Recorder (webgui)</center>\n"); | ||
91 | if(*ClientInfo->Post("sbufnew")!=0) { | ||
92 | if(*ClientInfo->Post("sbufname")!=0) { | ||
93 | if(!sbuf_select(SoundBuf,ClientInfo->Post("sbufname"))) { | ||
94 | sbuf=sbuf_add(SoundBuf,ClientInfo->Post("sbufname")); | ||
95 | if(ClientInfo->MultiPart("sbufdata").size>0) { | ||
96 | sbuf->data=malloc(ClientInfo->MultiPart("sbufdata").size); | ||
97 | memcpy(sbuf->data,ClientInfo->MultiPart("sbufdata").data,ClientInfo->MultiPart("sbufdata").size); | ||
98 | sbuf->data_i=ClientInfo->MultiPart("sbufdata").size; | ||
99 | }; | ||
100 | } else { | ||
101 | printf("<FONT color='FF0000'>Sound buffer exists</font>\n"); | ||
102 | }; | ||
103 | |||
104 | }else { | ||
105 | printf("<FONT color='FF0000'>Sound buffer name is empty</font>\n"); | ||
106 | }; | ||
107 | }; | ||
108 | if(*ClientInfo->Post("sbufvol")!=0) { | ||
109 | sbuf=SoundBuf->next; | ||
110 | while(sbuf!=NULL) { | ||
111 | tmp=malloc(strlen(sbuf->id)+5); | ||
112 | snprintf(tmp,strlen(sbuf->id)+5,"%s.vol",sbuf->id); | ||
113 | sbuf->volume=atoi(ClientInfo->Post(tmp))%129; | ||
114 | free(tmp); | ||
115 | sbuf=sbuf->next; | ||
116 | } | ||
117 | }; | ||
118 | if(*ClientInfo->Post("sbufch")!=0) { | ||
119 | sbuf=SoundBuf->next; | ||
120 | while(sbuf!=NULL) { | ||
121 | tmp=malloc(strlen(sbuf->id)+4); | ||
122 | snprintf(tmp,strlen(sbuf->id)+4,"%s.ch",sbuf->id); | ||
123 | sbuf->channels=atoi(ClientInfo->Post(tmp))%3; | ||
124 | free(tmp); | ||
125 | sbuf=sbuf->next; | ||
126 | } | ||
127 | }; | ||
128 | |||
129 | printf("<form method='POST' enctype='multipart/form-data'>\n"); | ||
130 | |||
131 | printf("Sound Buffer:<BR>\n"); | ||
132 | printf("Name:<input type=text name='sbufname' size=5 maxlength=10><BR>\n"); | ||
133 | printf("Data:<input type=file name='sbufdata'><BR>\n"); | ||
134 | printf("<input type=submit name='sbufnew' value=' New '><BR>\n"); | ||
135 | |||
136 | printf("<table width=100%% cellpadding=2 cellspacing=0 bgcolor='AAAAAA'>\n"); | ||
137 | printf("<TR><TD colspan=7 align=center><font face='Helvetica' size=6 color='FFFFFF'> Sound Buffers </font></TD></TR>\n"); | ||
138 | sbuf=SoundBuf->next; | ||
139 | i=0; | ||
140 | while(sbuf!=NULL) { | ||
141 | if(*ClientInfo->Post("sbufprocess")!=0) { | ||
142 | if(atoi(ClientInfo->Post(sbuf->id))==5) { | ||
143 | sbuf->mode=0; | ||
144 | sbuf->play_i=0; | ||
145 | if(sbuf->data_i>bigger) { | ||
146 | bigger=sbuf->data_i; | ||
147 | soundout=realloc(soundout,bigger); | ||
148 | }; | ||
149 | for(i=0;i<sbuf->data_i;i+=4) { | ||
150 | // LEFT | ||
151 | src1=((sbuf->data[i+1]<<8) | sbuf->data[i]); | ||
152 | ADJUST_VOLUME(src1,sbuf->volume); | ||
153 | src2=((soundout[i+1]<<8) | soundout[i]); | ||
154 | dst_sample=src1+src2; | ||
155 | if ( dst_sample > max_audioval ) { | ||
156 | dst_sample = max_audioval; | ||
157 | } else if ( dst_sample < min_audioval ) { | ||
158 | dst_sample = min_audioval; | ||
159 | } | ||
160 | soundout[i]=dst_sample &0xFF; | ||
161 | dst_sample>>=8; | ||
162 | soundout[i+1]=dst_sample & 0xFF; | ||
163 | // RIGHT | ||
164 | src1=((sbuf->data[i+1+2]<<8) | sbuf->data[i+2]); | ||
165 | ADJUST_VOLUME(src1,sbuf->volume); | ||
166 | if(sbuf->channels==2) { | ||
167 | src2=((soundout[i+1+2]<<8) | soundout[i+2]); // join left to right | ||
168 | } else { | ||
169 | src2=((soundout[i+1]<<8) | soundout[i]); // separate | ||
170 | }; | ||
171 | dst_sample=src1+src2; | ||
172 | if ( dst_sample > max_audioval ) { | ||
173 | dst_sample = max_audioval; | ||
174 | } else if ( dst_sample < min_audioval ) { | ||
175 | dst_sample = min_audioval; | ||
176 | } | ||
177 | soundout[i+2]=dst_sample &0xFF; | ||
178 | dst_sample>>=8; | ||
179 | soundout[i+2+1]=dst_sample & 0xFF; | ||
180 | }; | ||
181 | |||
182 | } else { | ||
183 | sbuf->mode=atoi(ClientInfo->Post(sbuf->id)); | ||
184 | tmp=malloc(strlen(sbuf->id)+5); | ||
185 | snprintf(tmp,strlen(sbuf->id)+5,"%s.vol",sbuf->id); | ||
186 | sbuf->volume=atoi(ClientInfo->Post(tmp))%129; | ||
187 | free(tmp); | ||
188 | sbuf->play_i=0; | ||
189 | }; | ||
190 | }; | ||
191 | if(sbuf->mode!=4) { | ||
192 | printf("<TR><TD bgcolor='EFEFEF'><font face='Verdana'>%s</font></TD><TD>",sbuf->id); | ||
193 | printf("<select name='%s'>",sbuf->id); | ||
194 | printf("<option value='0' %s>none</option>\n",(atoi(ClientInfo->Post(sbuf->id))==0)?"selected":""); | ||
195 | printf("<option value='1' %s>play</option>\n",(atoi(ClientInfo->Post(sbuf->id))==1)?"selected":""); | ||
196 | printf("<option value='2' %s>record</option>\n",(atoi(ClientInfo->Post(sbuf->id))==2)?"selected":""); | ||
197 | printf("<option value='3'>reset</option>\n"); | ||
198 | printf("<option value='4'>delete</option>\n"); | ||
199 | printf("<option value='5'>mixfrom</option>\n"); | ||
200 | printf("<option value='6'>mixto</option>\n"); | ||
201 | printf("</select>\n"); | ||
202 | printf("</TD><TD>%dm:%ds</TD>",(sbuf->data_i/44100/2/2)/60,(sbuf->data_i/44100/2/2%60)); | ||
203 | printf("</TD><TD>\n"); | ||
204 | printf("<select name='%s.ch'>\n",sbuf->id); | ||
205 | printf("<option value='1' %s>Mono</option>\n",(sbuf->channels==1)?"selected":""); | ||
206 | printf("<option value='2' %s>Stereo</option>\n",(sbuf->channels==2)?"selected":""); | ||
207 | printf("</select></TD>"); | ||
208 | |||
209 | printf("</TD><TD>Volume:<BR>\n"); | ||
210 | printf("<input type=text name=%s.vol size=3 maxlength=3 value='%d'></TD>",sbuf->id,sbuf->volume); | ||
211 | printf("<TD width=100%%> <input type=image width=100%% height=40 src='/%s.gif' border=0></TD>\n",sbuf->id); | ||
212 | printf("<TD><a href='/%s.raw'>Download</a></TD></TR>\n",sbuf->id); | ||
213 | } | ||
214 | if(sbuf->mode==3) { | ||
215 | sbuf->data_i=0; | ||
216 | sbuf->play_i=0; | ||
217 | free(sbuf->data); | ||
218 | sbuf->data=NULL; | ||
219 | sbuf->mode=0; | ||
220 | }; | ||
221 | |||
222 | id=sbuf->id; | ||
223 | sbuf=sbuf->next; | ||
224 | if(atoi(ClientInfo->Post(id))==4) { | ||
225 | sbuf_delete(SoundBuf,id); | ||
226 | } | ||
227 | i++; | ||
228 | }; | ||
229 | // mix to (6) | ||
230 | sbuf=SoundBuf->next; | ||
231 | while(sbuf!=NULL) { | ||
232 | if(atoi(ClientInfo->Post(sbuf->id))==6) { | ||
233 | free(sbuf->data); // free previous | ||
234 | sbuf->data=malloc(bigger+1); | ||
235 | memcpy(sbuf->data,soundout,bigger); | ||
236 | sbuf->data_i=bigger; | ||
237 | sbuf->mode=0; | ||
238 | sbuf->play_i=0; | ||
239 | sbuf->volume=128; | ||
240 | }; | ||
241 | sbuf=sbuf->next; | ||
242 | }; | ||
243 | if(i) printf("<TR><TD align=center valign=center>.</TD><TD><input type=submit name='sbufprocess' value=' Process '></TD><TD align=center valign=center>.</TD><TD><input type=submit name='sbufch' value='Set'></TD><TD><input type=submit name='sbufvol' value='Set'></TD><TD>.</TD><TD>.</TD></TR>\n"); | ||
244 | |||
245 | printf("</TABLE>\n"); | ||
246 | printf("</form>\n"); | ||
247 | |||
248 | |||
249 | |||
250 | |||
251 | |||
252 | |||
253 | }; | ||
254 | |||
255 | |||
256 | |||
257 | |||
258 | |||
259 | |||
260 | |||
261 | |||
262 | |||
263 | |||
264 | int main() { | ||
265 | struct web_server server; | ||
266 | int soundfd; | ||
267 | struct soundcard_setup SoundSetup; | ||
268 | signal(SIGPIPE,SIG_IGN); | ||
269 | |||
270 | SoundBuf=sbuf_init(); | ||
271 | |||
272 | |||
273 | SoundSetup.rate=44100; | ||
274 | SoundSetup.channels=2; | ||
275 | SoundSetup.fmt=16; | ||
276 | |||
277 | soundfd=soundcard_init("/dev/dsp",&SoundSetup); | ||
278 | if(soundfd<1) { | ||
279 | return 0; | ||
280 | }; | ||
281 | |||
282 | |||
283 | while(!web_server_init(&server,PORT,NULL,0))PORT++; | ||
284 | printf("http://localhost:%d\n",PORT); | ||
285 | |||
286 | web_server_addhandler(&server,"* /*.gif",outgif,WS_LOCAL); | ||
287 | web_server_addhandler(&server,"* /*.raw",dlbuf,WS_LOCAL); | ||
288 | web_server_addhandler(&server,"* /*",index_html,WS_LOCAL); | ||
289 | |||
290 | while(1) { | ||
291 | sound_process(soundfd,SoundBuf); | ||
292 | web_server_run(&server); | ||
293 | }; | ||
294 | |||
295 | |||
296 | }; | ||