-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhearnet.cpp
262 lines (231 loc) · 5.99 KB
/
hearnet.cpp
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
/**
* @file hearnet.c
*
* "Play" your network.
*
* @author Hans Fugal
*
* heavily modified by Leonard Ritter
*
*/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <jack/jack.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pcap.h>
#include <unistd.h>
#include <math.h>
#include <memory.h>
#include <time.h>
#include <string>
/* macros */
/// length of grain in samples
#define GRAIN_PERIOD 44
/// Our favorite irrational number
#define PI 3.14159265358979
#define MAX_VOICES 16
/* data */
jack_port_t *output_port;
jack_nframes_t srate;
struct voice
{
bool active;
float attack;
float attacklength;
float sinpos;
float sinfreq;
float amp;
float decaylength;
int age;
};
voice voices[MAX_VOICES];
void init_voices()
{
memset(voices,0,sizeof(voices));
}
voice* get_free_voice()
{
for (int index = 0; index < MAX_VOICES; index++)
{
if (!voices[index].active)
return &voices[index];
}
return 0;
}
float dbtoamp(float db)
{
return pow(2,db/6);
}
/*}}}*/
/** process nframes frames (jack callback) */
int process (jack_nframes_t nframes, void *arg)/*{{{*/
{
jack_default_audio_sample_t *out = (jack_default_audio_sample_t *) jack_port_get_buffer (output_port, nframes);
memset(out, 0, sizeof(jack_default_audio_sample_t)*nframes);
for (int index = 0; index != MAX_VOICES; index++)
{
if (voices[index].active)
{
voice* current_voice = &voices[index];
float* pout = out;
for (int s = 0; s != nframes; s++)
{
*pout += sin(current_voice->sinpos) * current_voice->amp * current_voice->attack;
if (*pout > 1.0f)
{
*pout = 1.0f;
}
else if (*pout < -1.0f)
{
*pout = -1.0f;
}
if (current_voice->attack < 1.0f)
{
current_voice->attack += 1.0f / ((float)srate * current_voice->attacklength);
}
else
{
current_voice->amp *= 1.0f - (1.0f / ((float)srate * current_voice->decaylength));
}
current_voice->sinpos += current_voice->sinfreq / (float)srate;
if (current_voice->amp < 0.001)
{
current_voice->active = false;
}
pout++;
}
}
}
return 0;
}/*}}}*/
/** jack callback to set sample rate */
int set_srate (jack_nframes_t nframes, void *arg)/*{{{*/
{
printf("Setting srate to %luHz\n", nframes);
srate = nframes;
return 0;
}/*}}}*/
/** jack error callback */
void error (const char *desc)/*{{{*/
{
fprintf (stderr, "JACK error: %s\n", desc);
}/*}}}*/
/** jack shutdown callback */
void shutdown(void *arg)/*{{{*/
{
exit(1);
}/*}}}*/
/** packet handler called by pcap_dispatch in main() */
void packet_handler(u_char * args, const struct pcap_pkthdr *pcap_hdr, const u_char * p)/*{{{*/
{
voice* new_voice = get_free_voice();
if (new_voice)
{
memset(new_voice,0,sizeof(voice));
new_voice->sinpos = 0.0f;
float factor2 = (float)(rand()%5 + 1);
//float factor = pow(2,(float)(rand()%12)*4.0f / 12.0f);
float factor = pow(2,(float)(pcap_hdr->len / 256.0f)*3.0f / 12.0f) * factor2;
new_voice->sinfreq = 55.0f * 2.0f * 3.14159f * factor + ((float)(rand()%5) * factor);
new_voice->amp = 0.5 / (float)(MAX_VOICES); // if they all add up, we dont get bursts
new_voice->decaylength = (rand()%100 + 100) / 1000.0f;
new_voice->attack = 0.0f;
new_voice->attacklength = (rand()%20 + 1) / 1000.0f; // 10ms
// Since we took out the mutex, we depend on this last
// assignment not happening out-of-order. I don't know of a
// portable memory barrier, so we'll just let it float. I
// doubt it will be a noticeable problem, but if it is then
// figure out some way to put a barrier here.
new_voice->active = true;
}
}/*}}}*/
void usage(void)/*{{{*/
{
fprintf(stderr,
"\n"
"usage: hearnet [interface [filter-expression]]\n"
"Default interface is eth0.\n"
);
exit(1);
}/*}}}*/
/* main */
int main(int argc, char **argv)/*{{{*/
{
int retval;
char *dev = "eth0";
char client_name[80];
if (argc > 1)
dev = argv[1];
// libpcap stuff /*{{{*/
pcap_t *hdl_pcap;
char perrbuf[PCAP_ERRBUF_SIZE];
hdl_pcap = pcap_open_live(dev, BUFSIZ, 0, 0, perrbuf);
if (hdl_pcap == NULL)
{
fprintf(stderr,"pcap_open_live; %s\n", perrbuf);
usage();
}
if (argc > 2) {
bpf_program bpfprog;
std::string s;
for (int i=2; i<argc; i++)
s += std::string(argv[i]) + std::string(" ");
char *cstr = (char*)malloc(s.size()+1);
strncpy(cstr,s.c_str(),s.size()+1);
if (pcap_compile(hdl_pcap, &bpfprog, cstr, 1, 0XFFFFFFFF) != -1) {
pcap_setfilter(hdl_pcap, &bpfprog);
} else {
printf("Error compiling filter: %s\n", pcap_geterr(hdl_pcap));
exit(1);
}
}
seteuid(getuid());
/*}}}*/
snprintf(client_name, sizeof(client_name), "hearnet %s", dev);
// jack stuff {{{
jack_client_t *client;
const char **ports;
jack_set_error_function(error);
if ((client = jack_client_new(client_name)) == 0)
{
fprintf(stderr, "jack server not running?\n");
return 1;
}
jack_set_process_callback(client,process,0);
jack_set_sample_rate_callback(client,set_srate, 0);
jack_on_shutdown(client, shutdown, 0);
printf ("engine sample rate: %lu\n", jack_get_sample_rate(client));
output_port = jack_port_register(client, "output", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
if (jack_activate(client))
{
fprintf(stderr, "cannot activate client\n");
return 1;
}
if ((ports = jack_get_ports (client, NULL, NULL, JackPortIsPhysical|JackPortIsInput)) == NULL) {
fprintf(stderr, "Cannot find any physical playback ports\n");
exit(1);
}
int i=0;
while (ports[i]!=NULL)
{
if (jack_connect(client, jack_port_name(output_port), ports[i]))
fprintf(stderr,"cannot connect output ports\n");
i++;
}
free (ports);
/*}}}*/
// init (empty) voice table
init_voices();
timeval tv_start;
gettimeofday(&tv_start,0);
srand(tv_start.tv_sec);
while (1)
{
pcap_dispatch(hdl_pcap, 1, packet_handler, 0);
}
return 0;
}/*}}}*/
// vim:sw=4:ts=8:tw=78:fdm=marker