Debian Codegraph @

speexout~.c

raw symbols ◀ ▶ SCIP
2 Debian patches touch this file
1 /* ------------------------ speexout~ ----------------------------------------- */
2 /* */
3 /* Tilde object to send speex encoded data to 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 codec which can */
25 /* be found at http://speex.sourceforge.net */
26 /* */
27 /* "Western values mean nothing to us" */
28 /* "She is beyond good and evil" */
29 /* Pop Group -- */
30 /* ---------------------------------------------------------------------------- */
31
32
33
34 #ifdef _MSC_VER
35 #pragma warning( disable : 4244 )
36 #pragma warning( disable : 4305 )
37 #endif
38
39 #include <sys/types.h>
40 #include <string.h>
41 #include <stdio.h>
42 #include <errno.h>
43 #include <stdlib.h>
44 #include <fcntl.h>
45 #include <sys/stat.h>
46 #ifndef __APPLE__
47 #include <malloc.h>
48 #endif
49 #include <ctype.h>
50 #ifdef _WIN32
51 #include <io.h>
52 #include <windows.h>
53 #include <winsock.h>
54 #include <windef.h>
55 #else
56 #include <unistd.h>
57 #include <sys/socket.h>
58 #include <netinet/in.h>
59 #include <netdb.h>
60 #include <time.h>
61 #include <sys/time.h>
62 #define SOCKET_ERROR -1
63 #endif
64
65 #if defined(__APPLE__) || defined(_WIN32)
66 #define MSG_NOSIGNAL 0
67 #endif
68
69 #include <speex/speex.h> /* speex codec stuff */
70 #include <speex/speex_bits.h> /* speex codec stuff */
71
72 #include "m_pd.h" /* standard pd stuff */
73
74 #define IN_BUFFER_SIZE 65536
75 #define OUT_BUFFER_SIZE 8192
76
77 // #define DATADEBUG
78
79 #define SPEEX_NB_MODE 0 /* audio data must be 8kHz */
80 #define SPEEX_WB_MODE 1 /* audio data must be 16kHz */
81
82 #define SPEEX_DEFAULT_QUALITY 5 /* default quality */
83
84 static char *speexout_version = "speexout~: speex voice quality streamer version 0.2, written by [email protected]";
85
86 static t_class *speexout_class;
87
88 typedef struct _speexout
89 {
90 t_object x_obj;
91 int x_samplerate; /* pd sampling rate */
92
93 /* Speex stuff */
94 SpeexBits x_bits; /* bits packing structure */
95 void *x_encstate; /* encoder state */
96 t_int x_framesize; /* frame size */
97 t_int x_mode; /* Narrow or Wide Band */
98 int x_quality; /* encoding quality ( 0 to 10 ) */
99
100 /* buffer stuff */
101 unsigned short x_inp; /* in position for buffer */
102 unsigned short x_outp; /* out position for buffer */
103 t_int x_encsize; /* size of encoded data */
104 t_float *x_inbuf; /* data to be coded by Speex */
105 char *x_outbuf; /* data returned by Speex -> our speex stream */
106 int x_bytesbuffered; /* number of unprocessed bytes in buffer */
107 int x_bytesemitted; /* number of encoded bytes emitted */
108 int x_start;
109 t_float *x_encchunk;
110
111 /* connection data */
112 int x_fd; /* info about connection status */
113 int x_outpackets; /* speex packets sent */
114
115 t_float x_f; /* float needed for signal input */
116
117 } t_speexout;
118
119
120 /* encode PCM data to speex frames */
121 static void speexout_encode(t_speexout *x)
122 {
123 if ( x->x_bytesbuffered > x->x_framesize )
124 {
125 speex_bits_reset(&x->x_bits);
126
127 {
128 t_int sp=0, rp=0;
129
130 while( sp < x->x_framesize )
131 {
132 rp=(x->x_outp+sp)%IN_BUFFER_SIZE;
133 // post( "speexout~ : sp=%d : rp=%d", sp, rp );
134 x->x_encchunk[ sp++ ] = *(x->x_inbuf+rp);
135 }
136 speex_encode(x->x_encstate, x->x_encchunk, &x->x_bits);
137 }
138
139 x->x_outp = (x->x_outp+x->x_framesize)%IN_BUFFER_SIZE;
140 x->x_bytesbuffered -= x->x_framesize;
141 x->x_encsize = speex_bits_write(&x->x_bits, x->x_outbuf+1, OUT_BUFFER_SIZE );
142 if ( x->x_encsize < 127 )
143 {
144 *(x->x_outbuf) = (char)x->x_encsize;
145 }
146 else
147 {
148 post( "speexout~ : encoding error : frame is more than 127 bytes" );
149 x->x_encsize = -1;
150 }
151 x->x_bytesemitted += x->x_encsize;
152 #ifdef DATADEBUG
153 {
154 t_int si;
155
156 printf( "speexout~ : encoded : " );
157 for ( si=0; si<x->x_encsize; si++ )
158 {
159 printf( "%d ", *(x->x_outbuf+si) );
160 }
161 printf( "\n" );
162 }
163 #endif
164 }
165 else
166 {
167 x->x_encsize = -1;
168 }
169 }
170
171 /* stream data to the peer */
172 static void speexout_stream(t_speexout *x)
173 {
174 int count = -1, i;
175
176 if ( x->x_encsize > 0 )
177 {
178 count = send(x->x_fd, x->x_outbuf, x->x_encsize+1, MSG_NOSIGNAL);
179 if(count < 0)
180 {
•181 pd_error(x, "speexout~: could not send encoded data to the peer (%d)", count);
182 #ifdef _WIN32
183 closesocket(x->x_fd);
184 #else
185 close(x->x_fd);
186 #endif
187 x->x_fd = -1;
188 outlet_float(x->x_obj.ob_outlet, 0);
189 }
190 else
191 {
192 x->x_outpackets++;
193 // post( "speexout~ : emitted %d bytes (packets = %d)", count, x->x_outpackets );
194 if ( x->x_outpackets%100 == 0 )
195 {
196 // post( "speexout~ : emitted %d bytes (packets = %d)", x->x_bytesemitted, x->x_outpackets );
197 }
198 if(count != x->x_encsize+1)
199 {
•200 pd_error(x, "speexout~: %d bytes skipped", x->x_encsize - count);
201 }
202 }
203 x->x_encsize = -1;
204 }
205 }
206
207
208 /* buffer and downsample the data */
209 static t_int *speexout_perform(t_int *w)
210 {
211 t_float *in = (t_float *)(w[1]); /* audio inlet */
212 t_speexout *x = (t_speexout *)(w[2]);
213 int n = (int)(w[3]); /* number of samples */
214 unsigned short i,wp;
215 t_float accum = 0.;
216 int sratio;
217
218 /* samplerate is supposed to be > 16kHz, thus sratio > 1 */
219 if ( x->x_mode == SPEEX_NB_MODE )
220 {
221 sratio = x->x_samplerate / 8000;
222 }
223 else
224 {
225 sratio = x->x_samplerate / 16000;
226 }
227
228 /* copy the data into the buffer and resample audio data */
229
230 accum=0;
231 for(wp = 0; wp < n; wp++)
232 {
233 accum += *(in+wp);
234 if ( wp % sratio == sratio - 1 )
235 {
236 x->x_inbuf[x->x_inp] = ( accum / sratio ) * 8000; // scale the input for speex best efficiency
237 // post( "x->x_inp : %d", x->x_inp );
238 x->x_inp = (x->x_inp+1)%IN_BUFFER_SIZE;
239 x->x_bytesbuffered ++;
240 accum = 0;
241 }
242 }
243
244 if( ( x->x_fd >= 0 ) && ( x->x_bytesbuffered > x->x_framesize ) )
245 {
246 /* encode and send to the peer */
247 speexout_encode(x); /* speex encoding */
248 speexout_stream(x); /* stream mp3 to the peer */
249 }
250 else
251 {
252 x->x_start = -1;
253 }
254 return (w+4);
255 }
256
257 static void speexout_dsp(t_speexout *x, t_signal **sp)
258 {
259 dsp_add(speexout_perform, 3, sp[0]->s_vec, x, sp[0]->s_n);
260 }
261
262 /* initialize the speex library */
263 static void speexout_tilde_speex_init(t_speexout *x)
264 {
265
266 speex_bits_init(&x->x_bits);
267
268 switch ( x->x_mode )
269 {
270 case SPEEX_NB_MODE :
271 x->x_encstate = speex_encoder_init(&speex_nb_mode);
272 break;
273
274 case SPEEX_WB_MODE :
275 x->x_encstate = speex_encoder_init(&speex_wb_mode);
276 break;
277
278 default :
•279 pd_error(x, "speexout~ : severe error : encoding scheme is unknown" );
280 break;
281 }
282
283 speex_encoder_ctl(x->x_encstate, SPEEX_GET_FRAME_SIZE, (void*)&x->x_framesize);
284 post( "speexout~ : frame size : %d", x->x_framesize );
285
286 }
287
288 /* connect to the peer */
289 static void speexout_connect(t_speexout *x, t_symbol *hostname, t_floatarg fportno)
290 {
291 struct sockaddr_in csocket;
292 struct hostent *hp;
293 int portno = fportno; /* get port from message box */
294
295 /* variables used for communication with the peer */
296 const char *buf = 0;
297 unsigned int len;
298 int sockfd;
299
300 #ifdef _WIN32
301 unsigned int ret;
302 #else
303 int ret;
304 #endif
305
306 if (x->x_fd >= 0)
307 {
•308 pd_error(x, "speexout~: already connected");
309 return;
310 }
311
312 sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
313 if (sockfd < 0)
314 {
•315 pd_error(x, "speexout~: internal error while attempting to open socket");
316 return;
317 }
318
319 /* connect socket using hostname provided in command line */
320 csocket.sin_family = AF_INET;
321 hp = gethostbyname(hostname->s_name);
322 if (hp == 0)
323 {
324 post("speexout~: bad host?");
325 #ifdef _WIN32
326 closesocket(sockfd);
327 #else
328 close(sockfd);
329 #endif
330 return;
331 }
332 memcpy((char *)&csocket.sin_addr, (char *)hp->h_addr, hp->h_length);
333
334 /* assign client port number */
335 csocket.sin_port = htons((unsigned short)portno);
336
337 /* try to connect. */
338 post("speexout~: connecting to port %d", portno);
339 if (connect(sockfd, (struct sockaddr *) &csocket, sizeof (csocket)) < 0)
340 {
•341 pd_error(x, "speexout~: connection failed!\n");
342 #ifdef _WIN32
343 closesocket(sockfd);
344 #else
345 close(sockfd);
346 #endif
347 return;
348 }
349
350 x->x_fd = sockfd;
351 x->x_outpackets = 0;
352 outlet_float( x->x_obj.ob_outlet, 1 );
353 post( "speexout~ : connected to peer" );
354
355
356 }
357
358 /* close connection to the peer */
359 static void speexout_disconnect(t_speexout *x)
360 {
361
362 int err = -1;
363
364 if(x->x_fd >= 0) /* close socket */
365 {
366 #ifdef _WIN32
367 closesocket(x->x_fd);
368 #else
369 close(x->x_fd);
370 #endif
371 x->x_fd = -1;
372 outlet_float( x->x_obj.ob_outlet, 0 );
373 post("speexout~: connection closed");
374 }
375 }
376
377 /* settings for encoding quality */
378 static void speexout_quality(t_speexout *x, t_floatarg fquality )
379 {
380 if ( fquality < 0 || fquality > 10 )
381 {
382 post( "speexout~ : wrong quality." );
383 return;
384 }
385 x->x_quality = fquality;
386 post("speexout~: setting quality to : %d", x->x_quality);
387 speex_encoder_ctl(x->x_encstate, SPEEX_SET_QUALITY, &x->x_quality);
388 }
389
390 /* clean up */
391 static void speexout_free(t_speexout *x)
392 {
393
394 speex_bits_destroy(&x->x_bits);
395
396 speex_encoder_destroy(x->x_encstate);
397
398 post("speexout~: encoder destroyed");
399
400 if(x->x_fd >= 0)
401 #ifdef _WIN32
402 closesocket(x->x_fd);
403 #else
404 close(x->x_fd);
405 #endif
406 freebytes(x->x_inbuf, IN_BUFFER_SIZE*sizeof(t_float));
407 freebytes(x->x_outbuf, OUT_BUFFER_SIZE);
408 freebytes(x->x_encchunk, x->x_framesize*sizeof(t_float));
409 }
410
411 static void *speexout_new(t_symbol *s, int argc, t_atom *argv)
412 {
413 t_speexout *x = (t_speexout *)pd_new(speexout_class);
414 outlet_new( &x->x_obj, &s_float );
415
416 x->x_mode = SPEEX_NB_MODE;
417 x->x_quality = SPEEX_DEFAULT_QUALITY;
418 x->x_fd = -1;
419 x->x_outpackets = 0;
420 x->x_samplerate = sys_getsr();
421 x->x_inbuf = getbytes(IN_BUFFER_SIZE*sizeof(t_float)); /* buffer for encoder input */
422 x->x_outbuf = getbytes(OUT_BUFFER_SIZE); /* our mp3 stream */
423 if ((!x->x_inbuf)||(!x->x_outbuf)) /* check buffers... */
424 {
•425 pd_error(x, "speexout~ : cannot allocate buffers");
426 return NULL;
427 }
428 x->x_bytesbuffered = 0;
429 x->x_bytesemitted = 0;
430 x->x_inp = 0;
431 x->x_outp = 0;
432 x->x_encsize = 0;
433 x->x_start = -1;
434 speexout_tilde_speex_init(x);
435
436 x->x_encchunk = (t_float*)getbytes(x->x_framesize*sizeof(t_float));
437 if (!x->x_encchunk) /* check allocation... */
438 {
•439 pd_error(x, "speexout~ : cannot allocate chunk");
440 return NULL;
441 }
442 return(x);
443 }
444
445 void speexout_tilde_setup(void)
446 {
•447 logpost(NULL, 4, "%s", speexout_version);
448 speexout_class = class_new(gensym("speexout~"), (t_newmethod)speexout_new, (t_method)speexout_free,
449 sizeof(t_speexout), 0, A_GIMME, 0);
450 CLASS_MAINSIGNALIN(speexout_class, t_speexout, x_f );
451 class_addmethod(speexout_class, (t_method)speexout_dsp, gensym("dsp"), 0);
452 class_addmethod(speexout_class, (t_method)speexout_connect, gensym("connect"), A_SYMBOL, A_FLOAT, 0);
453 class_addmethod(speexout_class, (t_method)speexout_disconnect, gensym("disconnect"), 0);
454 class_addmethod(speexout_class, (t_method)speexout_quality, gensym("quality"), A_FLOAT, 0);
455 }
456