-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
325 lines (296 loc) · 7.94 KB
/
main.c
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
313
314
315
316
317
318
319
320
321
322
323
324
325
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <sys/select.h>
#include <getopt.h>
#include <pthread.h>
#include <unistd.h>
#include <time.h>
#include "am.h"
#include "fm.h"
#include "enc1.h"
#include "paudio.h"
#include "encoding.h"
#include "rt.h"
#include "wav.h"
#define PING_STR "Κύριε, ἐλέησον"
uint8_t * ping_frm;
int sent = 0;
int received = 0;
time_t start;
typedef struct {
Io * io;
Modulation * mod;
Encapsulation * enc;
enum {
ZERO,
TEST,
CHAT,
GENWAV,
} action;
} Stack;
void usage(char * prog_name) {
printf(
"Usage: %s\n"
"-m|--modulation AM|AMO|AMM|FM default AMM, as it's the only one that cant't be heared if used with high frequencies\n"
"-g|--genwav generates a wav file containing a frame, for testing purposes\n"
"-t|--test outputs a frame into a buffer and attempts to read it\n"
"-c|--chat sends a frame in a while loop over the speaker and listens for it on the microphone\n"
"--frequency <float> the base frequency for the chosen modulation shceme default 22000\n"
"--amplitude <float> the amplitude of the signal between 0 and 1 default 1\n"
"-h|--help you're reading it\n"
,prog_name);
}
static Stack * getStack(int argc, char ** argv) {
Stack * stack = malloc(sizeof(Stack));
stack->enc = initEnc1();
int genwav = 0;
int test = 0;
int chat = 0;
static struct option long_options[] = {
{ "help", no_argument, 0, 'h' },
{ "modulation", required_argument, 0, 'm' },
{ "genwav", no_argument, 0, 'g' },
{ "test", no_argument, 0, 't' },
{ "chat", no_argument, 0, 'c' },
{ "frequency", required_argument, 0, 0 },
{ "amplitude", required_argument, 0, 0 },
{ 0, 0, 0, 0 }
};
int c;
int option_index = 0;
double frequency = 22000;
double amplitude = 1;
enum {
AM,
AMO,
AMM,
FM
} selected_modulation = AMM;
do {
c = getopt_long(argc, argv, "m:gtch", long_options, &option_index);
switch (c) {
case 0:
if (option_index == 5) {
if (sscanf(optarg, "%lf", &frequency) != 1) {
fprintf(stderr, "Frequency requires as the only argument a real number\n");
return NULL;
}
}
if (option_index == 6) {
if (sscanf(optarg, "%lf", &litude) != 1) {
fprintf(stderr, "Frequency requires as the only argument a real number\n");
return NULL;
}
if (amplitude <= 0.0 || amplitude >= 1.0) {
fprintf(stderr, "The amplitude must be between 0 and 1\n");
return NULL;
}
}
break;
case 'h':
usage(argv[0]);
return NULL;
case 'm':
if (strcmp(optarg, "AM") == 0) {
selected_modulation = AM;
break;
}
if (strcmp(optarg, "FM") == 0) {
selected_modulation = FM;
break;
}
if (strcmp(optarg, "AMO") == 0) {
selected_modulation = AMO;
break;
}
if (strcmp(optarg, "AMM") == 0) {
selected_modulation = AMM;
break;
}
fprintf(stderr, "Error: Invalid modulation\n");
return NULL;
case 't':
stack->action = TEST;
break;
case 'g':
stack->action = GENWAV;
break;
case 'c':
stack->action = CHAT;
break;
case -1:
break;
default:
usage(argv[0]);
return NULL;
}
} while (c != -1);
if (stack->action == ZERO) {
fprintf(stderr, "Error: You must suply an action\n");
usage(argv[0]);
return NULL;
}
switch (selected_modulation) {
case AMM:
stack->mod = initAmm(48000, amplitude * INT_MAX, frequency, 250);
break;
case AMO:
stack->mod = initAmo(48000, amplitude * INT_MAX, frequency, 250);
break;
case AM:
stack->mod = initAm(48000, amplitude * INT_MAX, frequency, 2000, 250);
break;
case FM:
stack->mod = initFm(48000, amplitude * INT_MAX, frequency, 2000, 250);
break;
}
if (stack->action == CHAT)
stack->io = initPortAudio(48000);
if (stack->action == GENWAV)
stack->io = initWav("am.wav", 48000);
return stack;
}
int match(uint8_t * base, uint8_t * test, uintmax_t len) {
for (uintmax_t i = 0; i < len; i++) {
if (base[i] && (!test[i]))
return 0;
if ((!base[i]) && test[i])
return 0;
}
return 1;
}
static void * listen(void * vstack) {
Stack * stack = vstack;
uintmax_t signalLen = stack->mod->oSize(stack->mod, stack->enc->frameSize);
uintmax_t fbufSize = (signalLen + stack->mod->sampleSize);
int32_t * fbuf = calloc(fbufSize, sizeof(int32_t));
while (1) {
int32_t * sample = stack->io->read(stack->io,
stack->mod->sampleSize);
mmpush(fbuf, fbufSize * sizeof(int32_t), sample,
stack->mod->sampleSize * sizeof(int32_t));
free(sample);
for (uintmax_t ii = 0; ii < 4; ii++) {
uintmax_t fbufShift = (ii * stack->mod->sampleSize) / 4;
Frame frm = stack->mod->demodulate(stack->mod, fbuf + fbufShift, signalLen);
if (match(ping_frm, frm, stack->enc->frameSize)) {
for (uintmax_t i = 0; i < stack->enc->frameSize; i++) {
printf("%.2x", ((uint8_t *) frm)[i]);
}
printf("\n");
for (uintmax_t i = 0; i < stack->enc->frameSize; i++) {
printf("%.2x", ping_frm[i]);
}
printf("\n");
printf("\n");
}
if (stack->enc->check(stack->enc, frm)) {
uint8_t * data = stack->enc->decapsulate(stack->enc, frm);
received += 1;
printf("M: %s; s: %d; r: %d; r/t: %f \n", data, sent, received,
(float) received / (time(NULL) - start));
free(data);
}
free(frm);
}
}
}
static void * send(void * vstack) {
Stack * stack = vstack;
char * data = malloc(sizeof(char) * 65536);
uintmax_t signalLen = stack->mod->oSize(stack->mod, stack->enc->frameSize);
fd_set set;
start = time(NULL);
while (1) {
/*struct timeval tv;
FD_ZERO(&set);
FD_SET(STDIN_FILENO, &set);
tv.tv_sec = 0;
tv.tv_usec = 0;
int rv = select(STDIN_FILENO + 1, &set, NULL, NULL, &tv);
if ((rv != 0) && (rv != -1)) {
rv = read(STDIN_FILENO, data, 65536);
for (uintmax_t i = 0; i < rv; i += stack->enc->dataSize) {
Frame frm = stack->enc->encapsulate(stack->enc, PING_STR,
strlen(PING_STR));*/
int32_t * signal = stack->mod->modulate(stack->mod, ping_frm,
stack->enc->frameSize);
stack->io->write(stack->io, signal, signalLen);
free(signal);
sent += 1;
//free(frm);
usleep(
3 * 1000000ull * stack->mod->sampleSize
/ stack->mod->sampleRate);
/*}
}
fflush(stdin);
fflush(stdout);
fflush(stderr);
usleep(100);*/
}
}
static int test(void * vstack) {
printf("Testing configuration\n");
Stack * stack = vstack;
Frame frm = stack->enc->encapsulate(stack->enc, PING_STR,
strlen(PING_STR) + 1);
uintmax_t signalLen = stack->mod->oSize(stack->mod, stack->enc->frameSize);
int32_t * signal = stack->mod->modulate(stack->mod, frm,
stack->enc->frameSize);
free(frm);
frm = stack->mod->demodulate(stack->mod, signal, signalLen);
free(signal);
int ret = stack->enc->check(stack->enc, frm);
if (!ret) {
printf("Configuration invalid\n");
for (uintmax_t i = 0; i < stack->enc->frameSize; i++) {
printf("%.2x", ((uint8_t *) frm)[i]);
}
printf("\n");
for (uintmax_t i = 0; i < stack->enc->frameSize; i++) {
printf("%.2x", ping_frm[i]);
}
printf("\n");
} else {
printf("Configuration OK !\n");
}
free(frm);
return ret;
}
static void genwav(void *v) {
Stack * stack = v;
Frame frm = stack->enc->encapsulate(stack->enc, PING_STR,
strlen(PING_STR) + 1);
uintmax_t signalLen = stack->mod->oSize(stack->mod, stack->enc->frameSize);
int32_t * signal = stack->mod->modulate(stack->mod, frm,
stack->enc->frameSize);
stack->io->write(stack->io, signal, signalLen * sizeof(int32_t));
}
int main(int argc, char ** argv) {
Stack * stack = getStack(argc, argv);
int ret = 0;
if (stack == NULL)
return 255;
ping_frm = stack->enc->encapsulate(stack->enc, PING_STR,
strlen(PING_STR) + 1);
if (stack->action == TEST)
ret = test(stack);
if (stack->action == GENWAV) {
genwav(stack);
}
if (stack->action == CHAT) {
printf("Info: Starting soundwave chat.\n");
pthread_t tid1, tid2;
pthread_create(&tid1, NULL, &listen, stack);
pthread_create(&tid2, NULL, &send, stack);
void *status;
pthread_join(tid1, &status);
pthread_join(tid2, &status);
}
if (stack->action != TEST)
stack->io->close(stack->io);
return ret;
}