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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
/* Written by Guenter Geiger <geiger@debian.org> (C) 1999, adapted and changed to streamin13~ by d13@klingt.org */
#include "m_pd.h"
#include "stream13.h"
#include <sys/types.h>
#include <string.h>
#if defined(UNIX) || defined(unix)
#include <sys/socket.h>
#include <sys/errno.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/time.h>
#include <unistd.h>
#define SOCKET_ERROR -1
#else
#include <winsock2.h>
#endif
/* these pragmas are only used for MSVC, not MinGW or Cygwin <hans@at.or.at> */
#ifdef _MSC_VER
#pragma warning( disable : 4244 )
#pragma warning( disable : 4305 )
#endif
#define INBUFSIZE 8192
#define MAXDROPS 99
/* Utility functions */
static void sys_sockerror(char *s)
{
#if defined(UNIX) || defined(unix)
int err = errno;
#else
int err = WSAGetLastError();
if (err == 10054) return;
#endif
post("%s: %s (%d)\n", s, strerror(err), err);
}
static void sys_closesocket(int fd)
{
#if defined(UNIX) || defined(unix)
close(fd);
#endif
#ifdef NT
closesocket(fd);
#endif
}
/* ------------------------ streamin13~ ----------------------------- */
static t_class *streamin13_class;
typedef struct _streamin13
{
t_object x_obj;
int x_connectsocket;
int x_nconnections;
int x_ndrops;
int x_ndone;
int x_fd;
int x_format;
int x_n;
short* cbuf;
int nsamples;
// t_outlet *x_out2;
int blocksperreceive;
int blockssincereceive;
} t_streamin13;
static void streamin13_tempbuf(t_streamin13 *x,int size)
{
int fullsize = size * x->x_n;
if (!x->cbuf)
x->cbuf = getbytes(fullsize*sizeof(t_float)*x->blocksperreceive+4);
else
x->cbuf = resizebytes(x->cbuf,x->nsamples*sizeof(t_float),fullsize*sizeof(t_float)*x->blocksperreceive+4);
x->nsamples = size;
x->blockssincereceive=0;
}
static int streamin13_listen(t_streamin13 *x,int portno)
{
struct sockaddr_in server;
int sockfd;
static int on = 1;
#ifndef NT
shutdown(x->x_connectsocket,SHUT_RDWR);
#else
shutdown(x->x_connectsocket,SD_BOTH);
#endif
sys_closesocket(x->x_connectsocket);
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0)
{
sys_sockerror("streamin-socket");
x->x_connectsocket = 0;
return (0);
}
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
#if defined(UNIX) || defined(unix)
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
post("setsockopt failed\n");
#endif
/* assign server port number */
server.sin_port = htons((u_short)portno);
post("listening to port number %d", portno);
/* name the socket */
if (bind(sockfd, (struct sockaddr *)&server, sizeof(server)) < 0)
{
sys_sockerror("streamin-bind");
sys_closesocket(sockfd);
x->x_connectsocket = 0;
}else {
x->x_connectsocket = sockfd;
x->x_nconnections = 0;
x->x_ndrops = 0;
x->x_format = SF_FLOAT;
x->cbuf = NULL;
x->blocksperreceive=16;
x->blockssincereceive=0;
streamin13_tempbuf(x,64);
}
return(0);
}
static void *streamin13_new(t_floatarg fportno ,t_floatarg xn)
{
int i;
t_streamin13 *x = (t_streamin13 *)pd_new(streamin13_class);
struct sockaddr_in server;
streamin13_listen(x,(int)fportno);
outlet_new(&x->x_obj, &s_signal);
x->x_n = xn;
for (i = 0 ;i < x->x_n - 1;i++){
outlet_new(&x->x_obj, gensym("signal"));
}
return (x);
}
static void streamin13_free(t_streamin13 *x)
{
/* LATER make me clean up open connections */
}
static t_int *streamin13_perform(t_int *w)
{
t_streamin13* x = (t_streamin13*) (w[1]);
int offset = 3;
int i,j;
int n = (int)(w[2]);
struct timeval timeout;
int packsize;
int ret;
int length;
short* cbuf;
#ifndef NT
t_float *out[x->x_n];
#else
t_float **out = (t_float**) malloc(x->x_n * sizeof(t_float*));
#endif
#ifndef NT
fd_set fdset;
#endif
for (i=0;i < x->x_n;i++) {
out[i] = (t_float *)(w[offset+i]);
}
if (x->x_connectsocket){
timeout.tv_sec = 0;
timeout.tv_usec = 0;
if (n != x->nsamples)
streamin13_tempbuf(x,n);
// cbuf = x->cbuf + x->blockssincereceive * n * x->x_n;
#ifndef NT
FD_SET(x->x_connectsocket,&fdset);
if (!select(x->x_connectsocket+1,&fdset,NULL,NULL,&timeout)
|| !FD_ISSET(x->x_connectsocket,&fdset)) {
x->x_ndrops++;
if (x->x_ndrops <= MAXDROPS) {
/* post("streamin13: drop"); */
while (n--){
for (j=0;j<x->x_n;j++){
*(out[j]++) = 0.0;
}
}
}
free(out);
return (w+offset+1+i);
} else {
x->x_ndone++;
}
#endif
if (x->x_ndrops > MAXDROPS) {
post("streamin13: dropped %d signal vectors, received %d sigvecs.",x->x_ndrops,x->x_ndone);
x->x_ndrops=0;
x->x_ndone=0;
}
if (!(x->blockssincereceive < x->blocksperreceive -1)) {
length=x->x_n * x->nsamples * SF_SIZEOF(x->x_format) * x->blocksperreceive +4;
ret = recv(x->x_connectsocket, (char*) x->cbuf,length, 0);
x->blockssincereceive=0;
if (x->x_format != *x->cbuf){
x->x_format= (short) *x->cbuf;
}
cbuf = (short*) x->cbuf + 1;
if (x->blocksperreceive != (short) *cbuf){
x->blocksperreceive=(short) *cbuf;
streamin13_tempbuf(x,x->nsamples);
}
}else{
x->blockssincereceive++;
}
packsize=n * x->x_n * sizeof(short) * x->blocksperreceive + 2;
switch (x->x_format) {
case SF_FLOAT:{
t_float* fbuf = (t_float*) x->cbuf + x->blockssincereceive * n * x->x_n + 1;
while (n--){
for (j=0;j<x->x_n;j++){
*out[j]++ = *fbuf++;
}
}
break;
}
case SF_16BIT:
{
cbuf = x->cbuf + x->blockssincereceive * n * x->x_n + 2;
while (n--){
for (j=0;j<x->x_n;j++){
*out[j]++ = (float) *cbuf++/32767.;
}
}
break;
}
case SF_8BIT: {
signed char* cbuf = (signed char*) x->cbuf + x->blockssincereceive * n * x->x_n + 4;
while (n--){
for (j=0;j<x->x_n;j++){
*out[j]++ = (float) *cbuf++/127.;
}
}
break;
}
default:
break;
}
}
#ifdef NT
free(out);
#endif
return (w+offset+1+i);
}
static void streamin13_dsp(t_streamin13 *x, t_signal **sp)
{
int i;
t_int** myvec = getbytes(sizeof(t_int)*(x->x_n + 3));
myvec[0] = (t_int*)x;
myvec[1] = (t_int*)sp[0]->s_n;
for (i=0;i < x->x_n;i++)
myvec[2 + i] = (t_int*)sp[i]->s_vec;
dsp_addv(streamin13_perform, x->x_n + 3, (t_int*)myvec);
freebytes(myvec,sizeof(t_int)*(x->x_n + 3));
}
static void streamin13_port(t_streamin13 *x,t_floatarg port)
{
streamin13_listen(x, (int)port);
}
void streamin13_setup(void)
{
streamin13_class = class_new(gensym("streamin13~"),
(t_newmethod) streamin13_new, (t_method) streamin13_free,
sizeof(t_streamin13), A_DEFSYM, A_DEFFLOAT, A_DEFFLOAT, 0);
class_addmethod(streamin13_class, nullfn, gensym("signal"), 0);
class_addmethod(streamin13_class, (t_method) streamin13_dsp, gensym("dsp"), 0);
class_addmethod(streamin13_class, (t_method)streamin13_port,gensym("port"),A_DEFFLOAT,0);
}
|