Debian Codegraph @

speexin~.c

raw symbols ◀ ▶
Source reconstructed at patch state 1 of 3 — not the installed (fully patched) source.
3 Debian patches touch this file
1 /* ------------------------ speexin~ ------------------------------------------ */
2 /* */
3 /* Object to receive a speex encoded stream sent by a peer using speexin~. */
4 /* Written by Yves Degoyon ([email protected]). */
5 /* Tarballs and updates @ http://ydegoyon.free.fr */
6 /* */
7 /* This program is free software; you can redistribute it and/or */
8 /* modify it under the terms of the GNU General Public License */
9 /* as published by the Free Software Foundation; either version 2 */
10 /* of the License, or (at your option) any later version. */
11 /* */
12 /* See file LICENSE for further informations on licensing terms. */
13 /* */
14 /* This program is distributed in the hope that it will be useful, */
15 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
16 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
17 /* GNU General Public License for more details. */
18 /* */
19 /* You should have received a copy of the GNU General Public License */
20 /* along with this program; if not, write to the Free Software */
21 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 /* */
23 /* Based on PureData by Miller Puckette and others. */
24 /* Uses the Speex voice quality encoding library which can */
25 /* be found at http://speex.sourceforge.net. */
26 /* */
27 /* ---------------------------------------------------------------------------- */
28
29
30 #include <m_pd.h>
31 #include <g_canvas.h>
32
33 #include <sys/types.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #ifdef _WIN32
37 #include <io.h>
38 #include <winsock.h>
39 #else
40 #include <sys/socket.h>
41 #include <errno.h>
42 #include <netinet/in.h>
43 #include <netinet/tcp.h>
44 #include <arpa/inet.h>
45 #include <netdb.h>
46 #include <sys/time.h>
47 #include <unistd.h>
48 #include <fcntl.h>
49 #include <stdio.h>
50 #define SOCKET_ERROR -1
51 #endif
52
53 #if defined(__APPLE__) || defined(_WIN32)
54 #define MSG_NOSIGNAL 0
55 #define SOL_TCP IPPROTO_TCP
56 #endif
57
58 #include <speex/speex.h> /* speex decoder stuff */
59 #include <speex/speex_bits.h> /* speex decoder stuff */
60
61 #ifdef _MSC_VER
62 #pragma warning( disable : 4244 )
63 #pragma warning( disable : 4305 )
64 #endif
65
66 #define MIN_AUDIO_INPUT 1024 // we must a least have 8 chunks to play a correct sound
67 #define INPUT_BUFFER_SIZE 32768
68 #define OUTPUT_BUFFER_SIZE 32768 /* 32k */
69 #define BARHEIGHT 10
70
71 #define SPEEX_NB_MODE 0 /* audio data must be 8kHz */
72 #define SPEEX_WB_MODE 1 /* audio data must be 16kHz */
73
74 //#define DATADEBUG
75
76 typedef void (*t_fdpollfn)(void *ptr, int fd);
77 extern void sys_rmpollfn(int fd);
78 extern void sys_addpollfn(int fd, t_fdpollfn fn, void *ptr);
79
80 /* time-out used for select() call */
81 static struct timeval ztout;
82
83 static char *speexin_version = "speexin~: speex voice quality streamer version 0.2, written by [email protected]";
84
85 extern void sys_sockerror(char *s);
86
87 void speexin_closesocket(int fd)
88 {
89 #ifndef _WIN32
90 if ( close(fd) < 0 )
91 {
92 perror( "close" );
93 }
94 else
95 {
96 post( "speexin~ : closed socket : %d", fd );
97 }
98 #endif
99 #ifdef _WIN32
100 closesocket(fd);
101 #endif
102 sys_rmpollfn(fd);
103 }
104
105 int setsocketoptions(int sockfd)
106 {
107 int sockopt = 1;
108 if (setsockopt(sockfd, SOL_TCP, TCP_NODELAY, (const char*) &sockopt, sizeof(int)) < 0)
109 {
110 post("speexin~ : setsockopt TCP_NODELAY failed");
111 perror( "setsockopt" );
112 return -1;
113 }
114 else
115 {
116 post("speexin~ : TCP_NODELAY set");
117 }
118
119 #ifdef _WIN32
120 sockopt = 1;
121 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof(int)) < 0)
122 {
123 post("speexin~ : setsockopt SO_REUSEADDR failed");
124 perror( "setsockopt" );
125 return -1;
126 }
127 else
128 {
129 post("speexin~ : setsockopt SO_REUSEADDR done.");
130 }
131 #endif
132 return 0;
133 }
134
135
136 /* ------------------------ speexin~ ----------------------------- */
137
138 static t_class *speexin_class;
139
140 typedef struct _speexin
141 {
142 t_object x_obj;
143 t_int x_socket;
144 t_outlet *x_connectionip;
145 t_int x_serversocket;
146 t_int x_samplerate;
147
148 /* Speex stuff */
149 SpeexBits x_bits; /* bits packing structure */
150 void *x_decstate; /* decoder state */
151 t_int x_framesize; /* frame size */
152 t_int x_mode; /* Narrow or Wide Band */
153 int x_quality; /* encoding quality ( 0 to 10 ) */
154
155 t_int x_inpackets; /* number of packets received */
156 t_int x_dpacket; /* displayed packet in status bar */
157 t_int x_packetsize; /* size of the packets */
158 t_int x_pblocks; /* processed blocks */
159 t_int x_graphic; /* indicates if we show a graphic bar */
160
161 void *x_inbuffer; /* accumulation buffer for incoming speex frames */
162 t_int x_inwritepos; /* accumulation buffer for incoming speex frames */
163 t_int x_encsize;
164 t_int x_inbuffersize;
165
166 t_float *x_outbuffer; /* buffer to store audio decoded data */
167 t_int x_oinp;
168 t_int x_ooutp;
169 t_int x_outunread;
170 t_int x_outbuffersize;
171 t_float *x_decchunk;
172
173 t_canvas *x_canvas;
174
175 t_int x_stream; /* indicates if a stream is connected ( meaning correct input flow ) */
176 t_int x_newstream; /* at first, the stream must provide enough data to start */
177
178 } t_speexin;
179
180 void speexin_tilde_speex_init(t_speexin *x)
181 {
182 int ret;
183 int pf=1;
184
185 speex_bits_init(&x->x_bits);
186
187 switch ( x->x_mode )
188 {
189 case SPEEX_NB_MODE :
190 x->x_decstate = speex_decoder_init(&speex_nb_mode);
191 break;
192
193 case SPEEX_WB_MODE :
194 x->x_decstate = speex_decoder_init(&speex_wb_mode);
195 break;
196
197 default :
198 error( "speexin~ : severe error : decoding scheme is unknown" );
199 break;
200 }
201
202 speex_decoder_ctl(x->x_decstate, SPEEX_GET_FRAME_SIZE, (void*)&x->x_framesize);
203
204 speex_decoder_ctl(x->x_decstate, SPEEX_SET_PF, &pf);
205
206 post( "speexin~ : frame size : %d", x->x_framesize );
207
208 }
209
210 static void speexin_decode_input(t_speexin *x)
211 {
212 int i;
213 int alength = 0;
214 static char out[8192];
215 signed short int *p = (signed short int *) out;
216 int pbytes;
217 int ret;
218 int flength = 0;
219
220 if ( x->x_encsize > 0 )
221 {
222
223 while ( x->x_encsize > *(char *)(x->x_inbuffer) )
224 {
225
226 flength = *(char *)(x->x_inbuffer );
227
228 // post( "speexin~ : reading bits from 1 to : %d", flength+1 );
229 speex_bits_read_from(&x->x_bits, x->x_inbuffer+1, flength);
230
231 #ifdef DATADEBUG
232 {
233 t_int si;
234
235 printf( "speexin~ : decoding : " );
236 for ( si=0; si<flength; si++ )
237 {
238 printf( "%d ", *(char *)(x->x_inbuffer+1+si) );
239 }
240 printf( "\n" );
241 }
242 #endif
243
244 {
245 t_int sp=0, rp=0;
246
247 speex_decode(x->x_decstate, &x->x_bits, x->x_decchunk);
248
249 while( sp < x->x_framesize )
250 {
251 rp=(x->x_oinp+sp)%x->x_outbuffersize;
252 // if ( rp == x->x_outbuffersize - 1 ) post( "speexin~ : write at the end of audio buffer" );
253 // post( "speexin~ : sp=%d : rp=%d", sp, rp );
254 x->x_outbuffer[ rp ] = x->x_decchunk[sp++];
255 }
256 x->x_oinp = rp+1;
257 }
258 x->x_outunread += x->x_framesize;
259 memcpy( x->x_inbuffer, x->x_inbuffer+flength+1, x->x_inbuffersize-flength-1 );
260 x->x_encsize -= flength+1;
261 x->x_inwritepos -= flength+1;
262
263 }
264
265 }
266
267 if ( x->x_graphic && glist_isvisible( x->x_canvas ) )
268 {
269 /* update graphical read status */
270 if ( x->x_inpackets != x->x_dpacket )
271 {
272 char color[32];
273 int minpackets = ( MIN_AUDIO_INPUT/x->x_framesize)-2; // audio loop has eaten some already
274
275
276 sys_vgui(".x%lx.c delete rectangle %xSTATUS\n", x->x_canvas, x );
277 sys_vgui(".x%lx.c delete line %xTHRESHOLD\n", x->x_canvas, x );
278 if ( x->x_outunread > 0 )
279 {
280 t_int width;
281
282 if ( x->x_inpackets < (MIN_AUDIO_INPUT/x->x_framesize)/2 )
283 {
284 strcpy( color, "red" );
285 }
286 else
287 {
288 strcpy( color, "lightgreen" );
289 }
290 width = rtext_width( glist_findrtext( (t_glist*)x->x_canvas, (t_text *)x ) );
291 sys_vgui(".x%lx.c create rectangle %d %d %d %d -fill %s -tags %xSTATUS\n",
292 x->x_canvas, x->x_obj.te_xpix, x->x_obj.te_ypix-BARHEIGHT-1,
293 x->x_obj.te_xpix+(x->x_inpackets*x->x_packetsize*width)/INPUT_BUFFER_SIZE,
294 x->x_obj.te_ypix - 1, color, x );
295 sys_vgui(".x%lx.c create line %d %d %d %d -fill red -tags %xTHRESHOLD\n",
296 x->x_canvas, x->x_obj.te_xpix+(minpackets*x->x_packetsize*width)/INPUT_BUFFER_SIZE,
297 x->x_obj.te_ypix-BARHEIGHT-1,
298 x->x_obj.te_xpix+(minpackets*x->x_packetsize*width)/INPUT_BUFFER_SIZE,
299 x->x_obj.te_ypix-1, x );
300 x->x_dpacket = x->x_inpackets;
301 }
302 }
303
304 }
305
306 }
307
308 static void speexin_recv(t_speexin *x)
309 {
310 int ret;
311
312 if ( x->x_inwritepos > x->x_inbuffersize - 1024 )
313 {
314 post( "speexin~ : input buffer is full" );
315 return;
316 }
317 if ( ( ret = recv(x->x_socket, (void*) x->x_inbuffer + x->x_inwritepos,
318 (size_t)x->x_inbuffersize,
319 MSG_NOSIGNAL) ) < 0 )
320 {
321 post( "speexin~ : receive error" );
322 perror( "recv" );
323 return;
324 }
325 else
326 {
327 // post( "speexin~ : received %d bytes at %d on %d ( up to %d)",
328 // ret, x->x_inwritepos, x->x_socket,
329 // x->x_inbuffersize );
330
331 if ( ret == 0 )
332 {
333 post( "speexin~ : closing connection ( s=%d )", x->x_socket );
334 speexin_closesocket(x->x_socket);
335 x->x_socket = -1;
336 sys_vgui(".x%lx.c delete rectangle %xPBAR\n", x->x_canvas, x );
337 sys_vgui(".x%lx.c delete line %xTHRESHOLD\n", x->x_canvas, x );
338 sys_vgui(".x%lx.c delete rectangle %xSTATUS\n", x->x_canvas, x );
339 outlet_symbol( x->x_connectionip, gensym("") );
340 }
341 else
342 {
343 x->x_inpackets++;
344 }
345
346 x->x_encsize += ret;
347 x->x_inwritepos += ret;
348
349 speexin_decode_input(x);
350 }
351 }
352
353 static void speexin_acceptconnection(t_speexin *x)
354 {
355 struct sockaddr_in incomer_address;
356 int sockaddrl = (int) sizeof( struct sockaddr );
357
358 int fd = accept(x->x_serversocket, (struct sockaddr*)&incomer_address, &sockaddrl );
359 post("speexin~: accepted incomer : %d.", fd );
360
361 if (fd < 0)
362 {
363 post("speexin~: accept failed");
364 return;
365 }
366
367 if (x->x_socket > 0)
368 {
369 post("speexin~: the source has changed to %s ( new socket = %d ).",
370 inet_ntoa( incomer_address.sin_addr ), fd );
371 speexin_closesocket(x->x_socket);
372 }
373
374 x->x_socket = fd;
375 sys_addpollfn(x->x_socket, (t_fdpollfn)speexin_recv, x);
376 outlet_symbol( x->x_connectionip, gensym( inet_ntoa( incomer_address.sin_addr) ) );
377
378 if ( x->x_graphic && glist_isvisible( x->x_canvas ) )
379 {
380 t_int width;
381
382 width = rtext_width( glist_findrtext( (t_glist*)x->x_canvas, (t_text *)x ) );
383 sys_vgui(".x%lx.c create rectangle %d %d %d %d -fill lightblue -tags %xPBAR\n",
384 x->x_canvas, x->x_obj.te_xpix, x->x_obj.te_ypix-BARHEIGHT-1,
385 x->x_obj.te_xpix + width, x->x_obj.te_ypix - 1, x );
386 }
387 x->x_stream = 0;
388 x->x_newstream = 1;
389
390 }
391
392
393 static int speexin_startservice(t_speexin* x, int portno)
394 {
395 struct sockaddr_in server;
396 int sockfd;
397
398 /* create a socket */
399 sockfd = socket(AF_INET, SOCK_STREAM, 0);
400
401 if (sockfd < 0)
402 {
403 sys_sockerror("socket");
404 return (0);
405 }
406 server.sin_family = AF_INET;
407 server.sin_addr.s_addr = INADDR_ANY;
408
409 /* assign server port number */
410 server.sin_port = htons((u_short)portno);
411 post("listening to port number %d", portno);
412
413 setsocketoptions(sockfd);
414
415 /* name the socket */
416 if (bind(sockfd, (struct sockaddr *)&server, sizeof(server)) < 0)
417 {
418 sys_sockerror("bind");
419 speexin_closesocket(sockfd);
420 return (0);
421 }
422
423 if (listen(sockfd, 5) < 0)
424 {
425 sys_sockerror("listen");
426 speexin_closesocket(sockfd);
427 }
428 else
429 {
430 x->x_serversocket = sockfd;
431 sys_addpollfn(x->x_serversocket, (t_fdpollfn)speexin_acceptconnection, x);
432 }
433
434 return 1;
435 }
436
437 static void speexin_free(t_speexin *x)
438 {
439 post( "speexin~ : free %x", x );
440 if (x->x_serversocket > 0)
441 {
442 post( "speexin~ : closing server socket" );
443 speexin_closesocket(x->x_serversocket);
444 x->x_serversocket = -1;
445 }
446 if (x->x_socket > 0)
447 {
448 post( "speexin~ : closing socket" );
449 speexin_closesocket(x->x_socket);
450 x->x_socket = -1;
451 }
452 if ( x->x_inbuffer ) freebytes( x->x_inbuffer, x->x_inbuffersize );
453 if ( x->x_outbuffer ) freebytes( x->x_outbuffer, x->x_outbuffersize*sizeof(t_float) );
454 if ( x->x_decchunk ) freebytes(x->x_decchunk, x->x_framesize*sizeof(t_float));
455 }
456
457 static t_int *speexin_perform(t_int *w)
458 {
459 t_speexin *x = (t_speexin*) (w[1]);
460 t_float *out = (t_float *)(w[2]);
461 t_int n = (int)(w[3]);
462 t_int bsize = n;
463 t_int ret;
464 t_int i = 0;
465 t_int j = 0;
466 t_int sp = 0;
467 t_int sratio;
468
469 // samplerate is supposed to be above 16kHz, thus sratio>1
470 if ( x->x_mode == SPEEX_NB_MODE )
471 {
472 sratio = x->x_samplerate / 8000;
473 }
474 else
475 {
476 sratio = x->x_samplerate / 16000;
477 }
478 // post( "speexin~ : ratio : %d", sratio );
479
480 memset( out, 0x0, n*sizeof(t_float ) );
481
482 sp = 0;
483 while( sp < n )
484 {
485 if ( ( ( x->x_outunread > MIN_AUDIO_INPUT ) && x->x_newstream ) || // wait the buffer to load
486 ( x->x_stream ) // check that the stream provides enough data
487 )
488 {
489 if ( x->x_newstream )
490 {
491 x->x_newstream = 0;
492 x->x_stream = 1;
493 }
494 /* resampling */
495 for ( j=0; j<sratio; j++ )
496 {
497 *(out+sp)=*(x->x_outbuffer+x->x_ooutp)/8000; // input has been scaled
498 //*(x->x_outbuffer+x->x_ooutp)=0.0; // data read, now zeroed
499 sp++;
500 if ( sp >= n ) break;
501 }
502 x->x_ooutp = (x->x_ooutp + 1)%x->x_outbuffersize;
503 // if ( x->x_ooutp == x->x_outbuffersize - 1 ) post( "speexin~ : end of audio buffer" );
504 x->x_outunread-=1;
505 }
506 else
507 {
508 for ( j=0; j<sratio; j++ )
509 {
510 *(out+sp)=0.0;
511 sp++;
512 if ( sp >= n ) break;
513 }
514 }
515 }
516 x->x_pblocks++;
517
518 if ( ( x->x_outunread <= MIN_AUDIO_INPUT/10 ) && ( x->x_stream ) )
519 {
520 // post( "speexin~ : stream lost (too little input)" );
521 x->x_stream = 0;
522 x->x_newstream = 1; // waiting for a new stream
523 }
524
525 if ( x->x_pblocks == x->x_framesize/bsize )
526 {
527 x->x_inpackets--;
528 x->x_pblocks = 0;
529 }
530
531 return (w+4);
532 }
533
534 static void speexin_dsp(t_speexin *x, t_signal **sp)
535 {
536 dsp_add(speexin_perform, 3, x, sp[0]->s_vec, sp[0]->s_n);
537 }
538
539
540 static void *speexin_new(t_floatarg fportno, t_floatarg fdographics)
541 {
542 t_speexin *x;
543 int i;
544
545 if ( fportno < 0 || fportno > 65535 )
546 {
547 post( "speexin~ : error : wrong portnumber : %d", (int)fportno );
548 return NULL;
549 }
550 if ( ((int)fdographics != 0) && ((int)fdographics != 1.) )
551 {
552 post( "speexin~ : error : constructor : speexin~ <portnumber> [graphic flag = 0 | 1 ] ( got = %f)", fdographics );
553 return NULL;
554 }
555
556 x = (t_speexin *)pd_new(speexin_class);
557 outlet_new(&x->x_obj, &s_signal);
558 x->x_connectionip = outlet_new(&x->x_obj, &s_symbol);
559
560 x->x_serversocket = -1;
561 x->x_socket = -1;
562 x->x_inpackets = 0;
563 x->x_inwritepos = 0;
564 x->x_dpacket = -1;
565 x->x_mode = SPEEX_NB_MODE;
566 x->x_samplerate = sys_getsr();
567
568 x->x_canvas = canvas_getcurrent();
569
570 x->x_inbuffersize = INPUT_BUFFER_SIZE;
571 x->x_outbuffersize = OUTPUT_BUFFER_SIZE;
572 x->x_inbuffer = (char*) getbytes( x->x_inbuffersize );
573 memset( x->x_inbuffer, 0x0, INPUT_BUFFER_SIZE );
574 x->x_outbuffer = (t_float*) getbytes( x->x_outbuffersize*sizeof(t_float) );
575 memset( x->x_outbuffer, 0x0, OUTPUT_BUFFER_SIZE*sizeof(t_float) );
576
577 if ( !x->x_inbuffer || !x->x_outbuffer )
578 {
579 post( "speexin~ : could not allocate buffers." );
580 return NULL;
581 }
582
583 x->x_encsize = 0;
584 x->x_oinp = 0;
585 x->x_ooutp = 0;
586
587 ztout.tv_sec = 0;
588 ztout.tv_usec = 0;
589
590 x->x_graphic = (int)fdographics;
591
592 post( "speexin~ : starting service on port %d", (int)fportno );
593 speexin_startservice(x, (int)fportno);
594
595 // init speex decoder
596 speexin_tilde_speex_init(x);
597
598 x->x_decchunk = (t_float*)getbytes(x->x_framesize*sizeof(t_float));
599 if (!x->x_decchunk) /* check allocation... */
600 {
601 error("speexin~ : cannot allocate chunk");
602 return NULL;
603 }
604
605 return (x);
606 }
607
608
609 void speexin_tilde_setup(void)
610 {
611 logpost(NULL, 4, "%s", speexin_version );
612 speexin_class = class_new(gensym("speexin~"),
613 (t_newmethod) speexin_new, (t_method) speexin_free,
614 sizeof(t_speexin), CLASS_NOINLET, A_DEFFLOAT, A_DEFFLOAT, A_NULL);
615
616 class_addmethod(speexin_class, nullfn, gensym("signal"), 0);
617 class_addmethod(speexin_class, (t_method) speexin_dsp, gensym("dsp"), 0);
618 }