| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 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> |
| 70 |
#include <speex/speex_bits.h> |
| 71 |
|
| 72 |
#include "m_pd.h" |
| 73 |
|
| 74 |
#define IN_BUFFER_SIZE 65536 |
| 75 |
#define OUT_BUFFER_SIZE 8192 |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
#define SPEEX_NB_MODE 0 |
| 80 |
#define SPEEX_WB_MODE 1 |
| 81 |
|
| 82 |
#define SPEEX_DEFAULT_QUALITY 5 |
| 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; |
| 92 |
|
| 93 |
|
| 94 |
SpeexBits x_bits; |
| 95 |
void *x_encstate; |
| 96 |
t_int x_framesize; |
| 97 |
t_int x_mode; |
| 98 |
int x_quality; |
| 99 |
|
| 100 |
|
| 101 |
unsigned short x_inp; |
| 102 |
unsigned short x_outp; |
| 103 |
t_int x_encsize; |
| 104 |
t_float *x_inbuf; |
| 105 |
char *x_outbuf; |
| 106 |
int x_bytesbuffered; |
| 107 |
int x_bytesemitted; |
| 108 |
int x_start; |
| 109 |
t_float *x_encchunk; |
| 110 |
|
| 111 |
|
| 112 |
int x_fd; |
| 113 |
int x_outpackets; |
| 114 |
|
| 115 |
t_float x_f; |
| 116 |
|
| 117 |
} t_speexout; |
| 118 |
|
| 119 |
|
| 120 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 194 |
if ( x->x_outpackets%100 == 0 ) |
| 195 |
{ |
| 196 |
|
| 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 |
|
| 209 |
static t_int *speexout_perform(t_int *w) |
| 210 |
{ |
| 211 |
t_float *in = (t_float *)(w[1]); |
| 212 |
t_speexout *x = (t_speexout *)(w[2]); |
| 213 |
int n = (int)(w[3]); |
| 214 |
unsigned short i,wp; |
| 215 |
t_float accum = 0.; |
| 216 |
int sratio; |
| 217 |
|
| 218 |
|
| 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 |
|
| 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; |
| 237 |
|
| 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 |
|
| 247 |
speexout_encode(x); |
| 248 |
speexout_stream(x); |
| 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 |
|
| 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 |
|
| 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; |
| 294 |
|
| 295 |
|
| 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 |
|
| 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 |
|
| 335 |
csocket.sin_port = htons((unsigned short)portno); |
| 336 |
|
| 337 |
|
| 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 |
|
| 359 |
static void speexout_disconnect(t_speexout *x) |
| 360 |
{ |
| 361 |
|
| 362 |
int err = -1; |
| 363 |
|
| 364 |
if(x->x_fd >= 0) |
| 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 |
|
| 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 |
|
| 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)); |
| 422 |
x->x_outbuf = getbytes(OUT_BUFFER_SIZE); |
| 423 |
if ((!x->x_inbuf)||(!x->x_outbuf)) |
| 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) |
| 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 |
|