Debian Codegraph @

mp3cast~.c

raw symbols ◀ ▶ SCIP
2 Debian patches touch this file
1 /* -------------------------- mp3cast~ ---------------------------------------- */
2 /* */
3 /* Tilde object to send mp3-stream to shoutcast/icecast server. */
4 /* Written by Olaf Matthes ([email protected]). */
5 /* Get source at http://www.akustische-kunst.de/puredata/ */
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 LAME MPEG 1 Layer 3 encoding library (lame_enc.dll) which can */
25 /* be found at http://www.cdex.n3.net. */
26 /* */
27 /* ---------------------------------------------------------------------------- */
28
29
30
31 #ifdef _MSC_VER
32 #pragma warning( disable : 4244 )
33 #pragma warning( disable : 4305 )
34 #endif
35
36 #include "m_pd.h" /* standard pd stuff */
37
38 #include <sys/types.h>
39 #include <string.h>
40 #include <stdio.h>
41 #include <errno.h>
42 #include <stdlib.h>
43 #include <fcntl.h>
44 #include <sys/stat.h>
45 #include <ctype.h>
46
47 #ifndef __APPLE__
48 #include <malloc.h>
49 #endif
50
51 #ifdef _WIN32
52 #include <io.h>
53 #include <windows.h>
54 #include <winsock.h>
55 #include <windef.h>
56 #else
57 #include <unistd.h>
58 #include <sys/socket.h>
59 #include <netinet/in.h>
60 #include <netdb.h>
61 #include <time.h>
62 #include <sys/time.h>
63 #define SOCKET_ERROR -1
64 #endif
65
66 #include <lame/lame.h> /* lame encoder stuff */
67 #include "mpg123.h"
68
69
70 #define MY_MP3_MALLOC_IN_SIZE 65536
71 /* max size taken from lame readme */
72 #define MY_MP3_MALLOC_OUT_SIZE 1.25*MY_MP3_MALLOC_IN_SIZE+7200
73
74 #define MAXDATARATE 320 /* maximum mp3 data rate is 320kbit/s */
75 #define STRBUF_SIZE 1024
76
77 static char *mp3cast_version = "mp3cast~: mp3 streamer version 0.5, written by Yves Degoyon";
78
79 static char base64table[65] =
80 {
81 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
82 'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
83 'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
84 'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/',
85 };
86
87 static t_class *mp3cast_class;
88
89 typedef struct _mp3cast
90 {
91 t_object x_obj;
92
93 /* LAME stuff */
94 int x_lame; /* info about encoder status */
95 int x_lamechunk; /* chunk size for LAME encoder */
96 int x_mp3size; /* number of returned mp3 samples */
97
98 /* buffer stuff */
99 unsigned short x_inp; /* in position for buffer */
100 unsigned short x_outp; /* out position for buffer*/
101 short *x_mp3inbuf; /* data to be sent to LAME */
102 char *x_mp3outbuf; /* data returned by LAME -> our mp3 stream */
103 short *x_buffer; /* data to be buffered */
104 int x_bytesbuffered; /* number of unprocessed bytes in buffer */
105 int x_start;
106
107 /* mp3 format stuff */
108 int x_samplerate;
109 int x_bitrate; /* bitrate of mp3 stream */
110 int x_mp3mode; /* mode (mono, joint stereo, stereo, dual mono) */
111 int x_mp3quality; /* quality of encoding */
112
113 /* SHOUTcast server stuff */
114 int x_fd; /* info about connection status */
115 char* x_passwd; /* password for server */
116 int x_icecast; /* tells if we use a IceCast server or SHOUTcast */
117 /* special IceCast server stuff */
118 char* x_mountpoint;
119 char* x_name;
120 char* x_url;
121 char* x_genre;
122 char* x_description;
123 int x_isPublic;
124
125 t_float x_f; /* float needed for signal input */
126
127 lame_global_flags *lgfp; /* lame encoder configuration */
128
129 } t_mp3cast;
130
131
132 /* encode PCM data to mp3 stream */
133 static void mp3cast_encode(t_mp3cast *x)
134 {
135 unsigned short i, wp;
136 int err = -1;
137 int n = x->x_lamechunk;
138
139 #ifdef _WIN32
140 if(x->x_lamechunk < sizeof(x->x_mp3inbuf))
141 #else
142 if(x->x_lamechunk < (int)sizeof(x->x_mp3inbuf))
143 #endif
144 {
•145 pd_error(x, "not enough memory!");
146 return;
147 }
148
149 /* on start/reconnect set outpoint that it not interferes with inpoint */
150 if(x->x_start == -1)
151 {
152 post("mp3cast~: initialising buffers");
153 /* we try to keep 2.5 times the data the encoder needs in the buffer */
154 if(x->x_inp > (2 * x->x_lamechunk))
155 {
156 x->x_outp = (short) x->x_inp - (2.5 * x->x_lamechunk);
157 }
158 else if(x->x_inp < (2 * x->x_lamechunk))
159 {
160 x->x_outp = (short) MY_MP3_MALLOC_IN_SIZE - (2.5 * x->x_lamechunk);
161 }
162 x->x_start = 1;
163 }
•164 if((unsigned short)(x->x_outp - x->x_inp) < x->x_lamechunk)pd_error(x, "mp3cast~: buffers overlap!");
165
166 i = MY_MP3_MALLOC_IN_SIZE - x->x_outp;
167
168 /* read from buffer */
169 if(x->x_lamechunk <= i)
170 {
171 /* enough data until end of buffer */
172 for(n = 0; n < x->x_lamechunk; n++) /* fill encode buffer */
173 {
174 x->x_mp3inbuf[n] = x->x_buffer[n + x->x_outp];
175 }
176 x->x_outp += x->x_lamechunk;
177 }
178 else /* split data */
179 {
180 for(wp = 0; wp < i; wp++) /* data at end of buffer */
181 {
182 x->x_mp3inbuf[wp] = x->x_buffer[wp + x->x_outp];
183 }
184
185 for(wp = i; wp < x->x_lamechunk; wp++) /* write rest of data at beginning of buffer */
186 {
187 x->x_mp3inbuf[wp] = x->x_buffer[wp - i];
188 }
189 x->x_outp = x->x_lamechunk - i;
190 }
191
192 /* encode mp3 data */
193 x->x_mp3size = lame_encode_buffer_interleaved(x->lgfp, x->x_mp3inbuf,
194 x->x_lamechunk/lame_get_num_channels(x->lgfp),
195 x->x_mp3outbuf, MY_MP3_MALLOC_OUT_SIZE);
196 // post( "mp3cast~ : encoding returned %d frames", x->x_mp3size );
197
198 /* check result */
199 if(x->x_mp3size<0)
200 {
201 lame_close( x->lgfp );
•202 pd_error(x, "mp3cast~: lame_encode_buffer_interleaved failed (%d)", x->x_mp3size);
203 x->x_lame = -1;
204 }
205 }
206
207
208 /* stream mp3 to SHOUTcast server */
209 static void mp3cast_stream(t_mp3cast *x)
210 {
211 int err = -1, i; /* error return code */
212 struct frame hframe;
213
214 err = send(x->x_fd, x->x_mp3outbuf, x->x_mp3size, 0);
215 if(err < 0)
216 {
•217 pd_error(x, "mp3cast~: could not send encoded data to server (%d)", err);
218 lame_close( x->lgfp );
219 x->x_lame = -1;
220 #ifdef _WIN32
221 closesocket(x->x_fd);
222 #else
223 close(x->x_fd);
224 #endif /* _WIN32 */
225 x->x_fd = -1;
226 outlet_float(x->x_obj.ob_outlet, 0);
227 }
•228 if((err > 0)&&(err != x->x_mp3size))pd_error(x, "mp3cast~: %d bytes skipped", x->x_mp3size - err);
229 }
230
231
232 /* buffer data as channel interleaved PCM */
233 static t_int *mp3cast_perform(t_int *w)
234 {
235 t_float *in1 = (t_float *)(w[1]); /* left audio inlet */
236 t_float *in2 = (t_float *)(w[2]); /* right audio inlet */
237 t_mp3cast *x = (t_mp3cast *)(w[3]);
238 int n = (int)(w[4]); /* number of samples */
239 unsigned short i,wp;
240 float in;
241
242 /* copy the data into the buffer */
243 i = MY_MP3_MALLOC_IN_SIZE - x->x_inp; /* space left at the end of buffer */
244
245 n *= 2; /* two channels go into one buffer */
246
247 if( n <= i )
248 {
249 /* the place between inp and MY_MP3_MALLOC_IN_SIZE */
250 /* is big enough to hold the data */
251
252 for(wp = 0; wp < n; wp++)
253 {
254 if(wp%2)
255 {
256 in = *(in2++); /* right channel / inlet */
257 }
258 else
259 {
260 in = *(in1++); /* left channel / inlet */
261 }
262 if (in > 1.0)
263 {
264 in = 1.0;
265 }
266 if (in < -1.0)
267 {
268 in = -1.0;
269 }
270 x->x_buffer[wp + x->x_inp] = (short) (32767.0 * in);
271 }
272 x->x_inp += n; /* n more samples written to buffer */
273 }
274 else
275 {
276 /* the place between inp and MY_MP3_MALLOC_IN_SIZE is not */
277 /* big enough to hold the data */
278 /* writing will take place in two turns, one from */
279 /* x->x_inp -> MY_MP3_MALLOC_IN_SIZE, then from 0 on */
280
281 for(wp = 0; wp < i; wp++) /* fill up to end of buffer */
282 {
283 if(wp%2)
284 {
285 in = *(in2++);
286 }
287 else
288 {
289 in = *(in1++);
290 }
291 if (in > 1.0)
292 {
293 in = 1.0;
294 }
295 if (in < -1.0)
296 {
297 in = -1.0;
298 }
299 x->x_buffer[wp + x->x_inp] = (short) (32767.0 * in);
300 }
301 for(wp = i; wp < n; wp++) /* write rest at start of buffer */
302 {
303 if(wp%2)
304 {
305 in = *(in2++);
306 }
307 else
308 {
309 in = *(in1++);
310 }
311 if (in > 1.0)
312 {
313 in = 1.0;
314 }
315 if (in < -1.0)
316 {
317 in = -1.0;
318 }
319 x->x_buffer[wp - i] = (short) (32767.0 * in);
320 }
321 x->x_inp = n - i; /* new writeposition in buffer */
322 }
323
324 if((x->x_fd >= 0)&&(x->x_lame >= 0))
325 {
326 /* count buffered samples when things are running */
327 x->x_bytesbuffered += n;
328
329 /* encode and send to server */
330 if(x->x_bytesbuffered > x->x_lamechunk)
331 {
332 mp3cast_encode(x); /* encode to mp3 */
333 mp3cast_stream(x); /* stream mp3 to server */
334 x->x_bytesbuffered -= x->x_lamechunk;
335 }
336 }
337 else
338 {
339 x->x_start = -1;
340 }
341 return (w+5);
342 }
343
344 static void mp3cast_dsp(t_mp3cast *x, t_signal **sp)
345 {
346 dsp_add(mp3cast_perform, 4, sp[0]->s_vec, sp[1]->s_vec, x, sp[0]->s_n);
347 }
348
349 /* initialize the lame library */
350 static void mp3cast_tilde_lame_init(t_mp3cast *x)
351 {
352 int ret;
353 x->lgfp = lame_init(); /* set default parameters for now */
354
355 #ifdef _WIN32
356 /* load lame_enc.dll library */
357 HINSTANCE dll;
358 dll=LoadLibrary("lame_enc.dll");
359 if(!dll)
360 {
•361 pd_error(x, "mp3cast~: error loading lame_enc.dll");
362 closesocket(x->x_fd);
363 x->x_fd = -1;
364 outlet_float(x->x_obj.ob_outlet, 0);
365 post("mp3cast~: connection closed");
366 return;
367 }
368 #endif /* _WIN32 */
369 {
370 const char *lameVersion = get_lame_version();
•371 logpost(NULL, 4, "%s", "mp3cast~ : using lame version : %s", lameVersion );
372 }
373
374
375 /* setting lame parameters */
376 lame_set_num_channels( x->lgfp, 2);
377 lame_set_in_samplerate( x->lgfp, sys_getsr() );
378 lame_set_out_samplerate( x->lgfp, x->x_samplerate );
379 lame_set_brate( x->lgfp, x->x_bitrate );
380 lame_set_mode( x->lgfp, x->x_mp3mode );
381 lame_set_quality( x->lgfp, x->x_mp3quality );
382 lame_set_emphasis( x->lgfp, 1 );
383 lame_set_original( x->lgfp, 1 );
384 lame_set_copyright( x->lgfp, 1 ); /* viva free music societies !!! */
385 lame_set_disable_reservoir( x->lgfp, 0 );
386 //lame_set_padding_type( x->lgfp, PAD_NO ); /* deprecated function */
387 ret = lame_init_params( x->lgfp );
388 if ( ret<0 )
389 {
390 post( "mp3cast~ : error : lame params initialization returned : %d", ret );
391 }
392 else
393 {
394 x->x_lame=1;
395 /* magic formula copied from windows dll for MPEG-I */
396 x->x_lamechunk = 2*1152;
397
398 post( "mp3cast~ : lame initialization done. (%d)", x->x_lame );
399 }
400 lame_init_bitstream( x->lgfp );
401 }
402
403 char *mp3cast_base64_encode(char *data)
404 {
405 int len = strlen(data);
406 char *out = t_getbytes(len*4/3 + 4);
407 char *result = out;
408 int chunk;
409
410 while(len > 0)
411 {
412 chunk = (len >3)?3:len;
413 *out++ = base64table[(*data & 0xFC)>>2];
414 *out++ = base64table[((*data & 0x03)<<4) | ((*(data+1) & 0xF0) >> 4)];
415
416 switch(chunk)
417 {
418 case 3:
419 *out++ = base64table[((*(data+1) & 0x0F)<<2) | ((*(data+2) & 0xC0)>>6)];
420 *out++ = base64table[(*(data+2)) & 0x3F];
421 break;
422 case 2:
423 *out++ = base64table[((*(data+1) & 0x0F)<<2)];
424 *out++ = '=';
425 break;
426 case 1:
427 *out++ = '=';
428 *out++ = '=';
429 break;
430 }
431 data += chunk;
432 len -= chunk;
433 }
434 *out = 0;
435
436 return result;
437 }
438
439 /* connect to server */
440 static void mp3cast_connect(t_mp3cast *x, t_symbol *hostname, t_floatarg fportno)
441 {
442 struct sockaddr_in server;
443 struct hostent *hp;
444 int portno = fportno; /* get port from message box */
445
446 /* information about this broadcast to be send to the server */
447 const char *name = x->x_name; /* name of broadcast */
448 const char *url = x->x_url; /* url of broadcast */
449 const char *genre = x->x_genre; /* genre of broadcast */
450 const char *description = x->x_description; /* description of broadcast */
451 const char *aim = "N/A"; /* aim of broadcast */
452 const char *irc = "#mp3cast"; /* ??? what's this ??? */
453 const char *icq = ""; /* icq id of broadcaster */
454 const char *mountpoint = x->x_mountpoint; /* mountpoint for IceCast server */
455 int isPublic = x->x_isPublic; /* don't publish broadcast on www.shoutcast.com */
456
457 /* variables used for communication with server */
458 const char * buf = 0;
459 char resp[STRBUF_SIZE];
460 unsigned int len;
461 fd_set fdset;
462 struct timeval tv;
463 int sockfd;
464 int ret;
465
466 if(x->x_icecast == 0)portno++; /* use SHOUTcast, portno is one higher */
467
468 if (x->x_fd >= 0)
469 {
•470 pd_error(x, "mp3cast~: already connected");
471 return;
472 }
473
474 sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
475 if (sockfd < 0)
476 {
•477 pd_error(x, "mp3cast~: internal error while attempting to open socket");
478 return;
479 }
480
481 /* connect socket using hostname provided in command line */
482 server.sin_family = AF_INET;
483 hp = gethostbyname(hostname->s_name);
484 if (hp == 0)
485 {
486 post("mp3cast~: bad host?");
487 close(sockfd);
488 return;
489 }
490 memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length);
491
492 /* assign client port number */
493 server.sin_port = htons((unsigned short)portno);
494
495 /* try to connect. */
496 post("mp3cast~: connecting to port %d", portno);
497 if (connect(sockfd, (struct sockaddr *) &server, sizeof (server)) < 0)
498 {
•499 pd_error(x, "mp3cast~: connection failed!\n");
500 #ifdef _WIN32
501 closesocket(sockfd);
502 #else
503 close(sockfd);
504 #endif /* _WIN32 */
505 return;
506 }
507
508 /* sheck if we can read/write from/to the socket */
509 FD_ZERO( &fdset);
510 FD_SET( sockfd, &fdset);
511 tv.tv_sec = 0; /* seconds */
512 tv.tv_usec = 500; /* microseconds */
513
514 ret = select(sockfd + 1, &fdset, NULL, NULL, &tv);
515 if(ret < 0)
516 {
•517 pd_error(x, "mp3cast~: can not read from socket");
518 #ifdef _WIN32
519 closesocket(sockfd);
520 #else
521 close(sockfd);
522 #endif /* _WIN32 */
523 return;
524 }
525 #ifndef _WIN32
526 ret = select(sockfd + 1, NULL, &fdset, NULL, &tv);
527 if(ret < 0)
528 {
•529 pd_error(x, "mp3cast~: can not write to socket");
530 close(sockfd);
531 return;
532 }
533 #endif /* not _WIN32 */
534
535 if(x->x_icecast == 0) /* SHOUTCAST */
536 {
537 /* now try to log in at SHOUTcast server */
538 post("mp3cast~: logging in to SHOUTcast server...");
539
540 /* first line is the passwd */
541 buf = x->x_passwd;
542 send(sockfd, buf, strlen(buf), 0);
543 buf = "\n";
544 send(sockfd, buf, strlen(buf), 0);
545
546 /* header for SHOUTcast server */
547 buf = "icy-name:"; /* name of broadcast */
548 send(sockfd, buf, strlen(buf), 0);
549 buf = name;
550 send(sockfd, buf, strlen(buf), 0);
551 buf = "\nicy-url:"; /* URL of broadcast */
552 send(sockfd, buf, strlen(buf), 0);
553 buf = url;
554 send(sockfd, buf, strlen(buf), 0);
555
556 buf = "\nicy-genre:"; /* genre of broadcast */
557 send(sockfd, buf, strlen(buf), 0);
558 buf = genre;
559 send(sockfd, buf, strlen(buf), 0);
560
561 buf = "\nicy-description:"; /* description of broadcast */
562 send(sockfd, buf, strlen(buf), 0);
563 buf = description;
564 send(sockfd, buf, strlen(buf), 0);
565
566 buf = "\nicy-irc:";
567 send(sockfd, buf, strlen(buf), 0);
568 buf = irc;
569 send(sockfd, buf, strlen(buf), 0);
570 buf = "\nicy-aim:";
571 send(sockfd, buf, strlen(buf), 0);
572 buf = aim;
573 send(sockfd, buf, strlen(buf), 0);
574 buf = "\nicy-icq:";
575 send(sockfd, buf, strlen(buf), 0);
576 buf = icq;
577 send(sockfd, buf, strlen(buf), 0);
578 buf = "\nicy-br:";
579 send(sockfd, buf, strlen(buf), 0);
580 if(sprintf(resp, "%d", x->x_bitrate) == -1) /* convert int to a string */
581 {
•582 pd_error(x, "mp3cast~: wrong bitrate");
583 }
584 send(sockfd, resp, strlen(resp), 0);
585 buf = "\nicy-pub:";
586 send(sockfd, buf, strlen(buf), 0);
587 if(isPublic==0) /* set the public flag for broadcast */
588 {
589 buf = "no";
590 }
591 else
592 {
593 buf ="yes";
594 }
595 send(sockfd, buf, strlen(buf), 0);
596 buf = "\n\n";
597 send(sockfd, buf, strlen(buf), 0);
598 }
599 else if ( x->x_icecast == 1 ) /* IceCast */
600 {
601 /* now try to log in at IceCast server */
602 post("mp3cast~: logging in to IceCast server...");
603
604 /* send the request, a string like:
605 * "SOURCE <password> /<mountpoint>\n" */
606 buf = "SOURCE ";
607 send(sockfd, buf, strlen(buf), 0);
608 buf = x->x_passwd;
609 send(sockfd, buf, strlen(buf), 0);
610 buf = " /";
611 send(sockfd, buf, strlen(buf), 0);
612 buf = mountpoint;
613 send(sockfd, buf, strlen(buf), 0);
614
615 /* send the x-audiocast headers */
616 buf = "\nx-audiocast-bitrate: ";
617 send(sockfd, buf, strlen(buf), 0);
618 if(sprintf(resp, "%d", x->x_bitrate) == -1) /* convert int to a string */
619 {
•620 pd_error(x, "mp3cast~: wrong bitrate");
621 }
622 send(sockfd, resp, strlen(resp), 0);
623
624 buf = "\nx-audiocast-public: ";
625 send(sockfd, buf, strlen(buf), 0);
626 if(isPublic==0) /* set the public flag for broadcast */
627 {
628 buf = "no";
629 }
630 else
631 {
632 buf ="yes";
633 }
634 send(sockfd, buf, strlen(buf), 0);
635
636 buf = "\nx-audiocast-name: ";
637 send(sockfd, buf, strlen(buf), 0);
638 buf = name;
639 send(sockfd, buf, strlen(buf), 0);
640
641 buf = "\nx-audiocast-url: ";
642 send(sockfd, buf, strlen(buf), 0);
643 buf = url;
644 send(sockfd, buf, strlen(buf), 0);
645
646 buf = "\nx-audiocast-genre: ";
647 send(sockfd, buf, strlen(buf), 0);
648 buf = genre;
649 send(sockfd, buf, strlen(buf), 0);
650
651 buf = "\nx-audiocast-description: ";
652 send(sockfd, buf, strlen(buf), 0);
653 buf = description;
654 send(sockfd, buf, strlen(buf), 0);
655
656 buf = "\n\n";
657 send(sockfd, buf, strlen(buf), 0);
658 /* end login for IceCast */
659 }
660 else if ( x->x_icecast == 2 ) /* Icecast 2 */
661 {
662 char *base64; /* buffer to hold 64bit encoded strings */
663 /* send the request, a string like: "SOURCE /<mountpoint> HTTP/1.0\r\n" */
664 buf = "SOURCE /";
665 send(sockfd, buf, strlen(buf), 0);
666 buf = x->x_mountpoint;
667 send(sockfd, buf, strlen(buf), 0);
668 buf = " HTTP/1.0\r\n";
669 send(sockfd, buf, strlen(buf), 0);
670 /* send basic authorization as base64 encoded string */
671 sprintf(resp, "source:%s", x->x_passwd);
672 len = strlen(resp);
673 base64 = mp3cast_base64_encode(resp);
674 sprintf(resp, "Authorization: Basic %s\r\n", base64);
675 send(sockfd, resp, strlen(resp), 0);
676 t_freebytes(base64, len*4/3 + 4);
677 /* send application name */
678 buf = "User-Agent: mp3cast~";
679 send(sockfd, buf, strlen(buf), 0);
680 /* send content type: mpeg */
681 buf = "\r\nContent-Type: audio/mpeg";
682 send(sockfd, buf, strlen(buf), 0);
683 /* send the ice headers */
684 /* name */
685 buf = "\r\nice-name: ";
686 send(sockfd, buf, strlen(buf), 0);
687 buf = x->x_name;
688 send(sockfd, buf, strlen(buf), 0);
689 /* url */
690 buf = "\r\nice-url: ";
691 send(sockfd, buf, strlen(buf), 0);
692 buf = x->x_url;
693 send(sockfd, buf, strlen(buf), 0);
694 /* genre */
695 buf = "\r\nice-genre: ";
696 send(sockfd, buf, strlen(buf), 0);
697 buf = genre;
698 send(sockfd, buf, strlen(buf), 0);
699 /* public */
700 buf = "\r\nice-public: ";
701 send(sockfd, buf, strlen(buf), 0);
702 if(isPublic==0)
703 {
704 buf = "0";
705 }
706 else
707 {
708 buf = "1";
709 }
710 send(sockfd, buf, strlen(buf), 0);
711 /* bitrate */
712 if(sprintf(resp, "\r\nice-audio-info: bitrate=%d", x->x_bitrate) == -1)
713 {
•714 pd_error(x, "shoutcast~: could not create audio-info");
715 }
716 send(sockfd, resp, strlen(resp), 0);
717 /* description */
718 buf = "\r\nice-description: ";
719 send(sockfd, buf, strlen(buf), 0);
720 buf = description;
721 send(sockfd, buf, strlen(buf), 0);
722 /* end of header: write an empty line */
723 buf = "\r\n\r\n";
724 send(sockfd, buf, strlen(buf), 0);
725 }
726
727 /* read the anticipated response: "OK" */
728 len = recv(sockfd, resp, STRBUF_SIZE, 0);
729 if ( strstr( resp, "OK" ) == NULL )
730 {
731 post("mp3cast~: login failed!");
732 if ( len>0 ) post("mp3cast~: server answered : %s", resp);
733 #ifdef _WIN32
734 closesocket(sockfd);
735 #else
736 close(sockfd);
737 #endif /* _WIN32 */
738 return;
739 }
740
741 /* suck anything that the other side has to say */
742 // while (len = recv(sockfd, resp, STRBUF_SIZE,0))
743 // {
744 // post("mp3cast~: server answered : %s", resp);
745 // }
746
747 x->x_fd = sockfd;
748 outlet_float(x->x_obj.ob_outlet, 1);
749 post("mp3cast~: logged in to %s", hp->h_name);
750
751 mp3cast_tilde_lame_init(x);
752
753 }
754
755 /* close connection to SHOUTcast server */
756 static void mp3cast_disconnect(t_mp3cast *x)
757 {
758 int err = -1;
759 if(x->x_lame >= 0)
760 {
761 /* ignore remaining bytes */
762 if ( x->x_mp3size = lame_encode_flush( x->lgfp, x->x_mp3outbuf, 0) < 0 )
763 {
764 post( "mp3cast~ : warning : remaining encoded bytes" );
765 }
766 lame_close( x->lgfp );
767
768 x->x_lame = -1;
769 post("mp3cast~: encoder stream closed");
770 }
771
772 if(x->x_fd >= 0) /* close socket */
773 {
774 #ifdef _WIN32
775 closesocket(x->x_fd);
776 #else
777 close(x->x_fd);
778 #endif /* _WIN32 */
779 x->x_fd = -1;
780 outlet_float(x->x_obj.ob_outlet, 0);
781 post("mp3cast~: connection closed");
782 }
783 }
784
785 /* set password for SHOUTcast server */
786 static void mp3cast_password(t_mp3cast *x, t_symbol *password)
787 {
788 post("mp3cast~ : setting password to %s", password->s_name );
789 x->x_passwd = password->s_name;
790 }
791
792 /* settings for mp3 encoding */
793 static void mp3cast_mpeg(t_mp3cast *x, t_floatarg fsamplerate, t_floatarg fbitrate,
794 t_floatarg fmode, t_floatarg fquality)
795 {
796 x->x_samplerate = fsamplerate;
797 if(fbitrate > MAXDATARATE)
798 {
799 fbitrate = MAXDATARATE;
800 }
801 x->x_bitrate = fbitrate;
802 x->x_mp3mode = fmode;
803 x->x_mp3quality = fquality;
804 post("mp3cast~: setting mp3 stream to %dHz, %dkbit/s, mode %d, quality %d",
805 x->x_samplerate, x->x_bitrate, x->x_mp3mode, x->x_mp3quality);
806 if(x->x_fd>=0)post("mp3cast~ : reconnect to make changes take effect! ");
807 }
808
809 /* print settings */
810 static void mp3cast_print(t_mp3cast *x)
811 {
812 const char * buf = 0;
•813 logpost(NULL, 4, "%s", mp3cast_version);
814 post(" LAME mp3 settings:\n"
815 " output sample rate: %d Hz\n"
816 " bitrate: %d kbit/s", x->x_samplerate, x->x_bitrate);
817 switch(x->x_mp3mode)
818 {
819 case 0 :
820 buf = "stereo";
821 break;
822 case 1 :
823 buf = "joint stereo";
824 break;
825 case 2 :
826 buf = "dual channel";
827 break;
828 case 3 :
829 buf = "mono";
830 break;
831 }
832 post(" mode: %s\n"
833 " quality: %d", buf, x->x_mp3quality);
834 post(" mp3 chunk size: %d", x->x_lamechunk);
835 if(x->x_samplerate!=sys_getsr())
836 {
837 post(" resampling from %d to %d Hz!", (int)sys_getsr(), x->x_samplerate);
838 }
839
840 if ( x->x_icecast == 0)
841 {
842 post(" server type is SHOUTcast");
843 }
844 else if ( x->x_icecast == 1 )
845 {
846 post(" server type is IceCast");
847 }
848 else if ( x->x_icecast == 2 )
849 {
850 post(" server type is IceCast 2");
851 }
852 }
853
854 static void mp3cast_icecast(t_mp3cast *x)
855 {
856 x->x_icecast = 1;
857 post("mp3cast~: set server type to IceCast");
858 }
859
860 static void mp3cast_icecast2(t_mp3cast *x)
861 {
862 x->x_icecast = 2;
863 post("mp3cast~: set server type to IceCast 2");
864 }
865
866 static void mp3cast_shoutcast(t_mp3cast *x)
867 {
868 x->x_icecast = 0;
869 post("mp3cast~: set server type to SHOUTcast");
870 }
871
872 /* set mountpoint for IceCast server */
873 static void mp3cast_mountpoint(t_mp3cast *x, t_symbol *mount)
874 {
875 x->x_mountpoint = mount->s_name;
876 post("mp3cast~: mountpoint set to %s", x->x_mountpoint);
877 }
878
879 /* set namle for IceCast server */
880 static void mp3cast_name(t_mp3cast *x, t_symbol *name)
881 {
882 x->x_name = name->s_name;
883 post("mp3cast~: name set to %s", x->x_name);
884 }
885
886 /* set url for IceCast server */
887 static void mp3cast_url(t_mp3cast *x, t_symbol *url)
888 {
889 x->x_url = url->s_name;
890 post("mp3cast~: url set to %s", x->x_url);
891 }
892
893 /* set genre for IceCast server */
894 static void mp3cast_genre(t_mp3cast *x, t_symbol *genre)
895 {
896 x->x_genre = genre->s_name;
897 post("mp3cast~: genre set to %s", x->x_genre);
898 }
899
900 /* set isPublic for IceCast server */
901 static void mp3cast_isPublic(t_mp3cast *x, t_floatarg isPublic)
902 {
903 x->x_isPublic = isPublic;
904 char* isPublicStr;
905 if(isPublic==0)
906 {
907 isPublicStr = "no";
908 }
909 else
910 {
911 isPublicStr = "yes";
912 }
913 post("mp3cast~: isPublic set to %s", isPublicStr);
914 }
915
916 /* set description for IceCast server */
917 static void mp3cast_description(t_mp3cast *x, t_symbol *description)
918 {
919 x->x_description = description->s_name;
920 post("mp3cast~: description set to %s", x->x_description);
921 }
922
923 /* clean up */
924 static void mp3cast_free(t_mp3cast *x)
925 {
926 if(x->x_lame >= 0)
927 lame_close( x->lgfp );
928 if(x->x_fd >= 0)
929 #ifdef _WIN32
930 closesocket(x->x_fd);
931 #else
932 close(x->x_fd);
933 #endif /* _WIN32 */
934 freebytes(x->x_mp3inbuf, MY_MP3_MALLOC_IN_SIZE*sizeof(short));
935 freebytes(x->x_mp3outbuf, MY_MP3_MALLOC_OUT_SIZE);
936 freebytes(x->x_buffer, MY_MP3_MALLOC_IN_SIZE*sizeof(short));
937 }
938
939 static void *mp3cast_new(void)
940 {
941 t_mp3cast *x = (t_mp3cast *)pd_new(mp3cast_class);
942 inlet_new (&x->x_obj, &x->x_obj.ob_pd, gensym ("signal"), gensym ("signal"));
943 outlet_new(&x->x_obj, gensym("float"));
944 x->x_fd = -1;
945 x->x_lame = -1;
946 x->x_passwd = "pd";
947 x->x_samplerate = sys_getsr();
948 x->x_bitrate = 224;
949 x->x_mp3mode = 1;
950 x->x_mp3quality = 5;
951 x->x_mp3inbuf = getbytes(MY_MP3_MALLOC_IN_SIZE*sizeof(short)); /* buffer for encoder input */
952 x->x_mp3outbuf = getbytes(MY_MP3_MALLOC_OUT_SIZE*sizeof(char)); /* our mp3 stream */
953 x->x_buffer = getbytes(MY_MP3_MALLOC_IN_SIZE*sizeof(short)); /* what we get from pd, converted to PCM */
954 if ((!x->x_buffer)||(!x->x_mp3inbuf)||(!x->x_mp3outbuf)) /* check buffers... */
955 {
•956 pd_error(x, "out of memory!");
957 }
958 x->x_bytesbuffered = 0;
959 x->x_inp = 0;
960 x->x_outp = 0;
961 x->lgfp = NULL;
962 x->x_start = -1;
963 x->x_icecast = 1;
964 x->x_mountpoint = "puredata";
965 x->x_name = "puredata";
966 x->x_url = "http://puredata.info";
967 x->x_genre = "experimental sound";
968 x->x_isPublic = 1;
969 x->x_description = "playing with my patches";
970 return(x);
971 }
972
973 void mp3cast_tilde_setup(void)
974 {
•975 logpost(NULL, 4, "%s", mp3cast_version);
976 mp3cast_class = class_new(gensym("mp3cast~"), (t_newmethod)mp3cast_new, (t_method)mp3cast_free,
977 sizeof(t_mp3cast), 0, 0);
978 CLASS_MAINSIGNALIN(mp3cast_class, t_mp3cast, x_f );
979 class_addmethod(mp3cast_class, (t_method)mp3cast_dsp, gensym("dsp"), 0);
980 class_addmethod(mp3cast_class, (t_method)mp3cast_connect, gensym("connect"), A_SYMBOL, A_FLOAT, 0);
981 class_addmethod(mp3cast_class, (t_method)mp3cast_disconnect, gensym("disconnect"), 0);
982 class_addmethod(mp3cast_class, (t_method)mp3cast_password, gensym("passwd"), A_SYMBOL, 0);
983 class_addmethod(mp3cast_class, (t_method)mp3cast_mpeg, gensym("mpeg"), A_FLOAT, A_FLOAT, A_FLOAT, A_FLOAT, 0);
984 class_addmethod(mp3cast_class, (t_method)mp3cast_print, gensym("print"), 0);
985 class_addmethod(mp3cast_class, (t_method)mp3cast_icecast, gensym("icecast"), 0);
986 class_addmethod(mp3cast_class, (t_method)mp3cast_icecast2, gensym("icecast2"), 0);
987 class_addmethod(mp3cast_class, (t_method)mp3cast_shoutcast, gensym("shoutcast"), 0);
988 class_addmethod(mp3cast_class, (t_method)mp3cast_mountpoint, gensym("mountpoint"), A_SYMBOL, 0);
989 class_addmethod(mp3cast_class, (t_method)mp3cast_name, gensym("name"), A_SYMBOL, 0);
990 class_addmethod(mp3cast_class, (t_method)mp3cast_url, gensym("url"), A_SYMBOL, 0);
991 class_addmethod(mp3cast_class, (t_method)mp3cast_genre, gensym("genre"), A_SYMBOL, 0);
992 class_addmethod(mp3cast_class, (t_method)mp3cast_isPublic, gensym("isPublic"), A_FLOAT, 0);
993 class_addmethod(mp3cast_class, (t_method)mp3cast_description, gensym("description"), A_SYMBOL, 0);
994 }
995