-
Notifications
You must be signed in to change notification settings - Fork 6
/
stdc_demod.cpp
352 lines (347 loc) · 15.2 KB
/
stdc_demod.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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include <iostream>
#include <inmarsatc_demodulator.h>
#include <audiofile.h>
#include <alsa/asoundlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <map>
#define BUFSIZE 2048
void printHelp() {
std::cout << "Help: " << std::endl;
std::cout << "stdc_demod - open-source cli program to demodulate inmarsat-C signals using inmarsatc library based on Scytale-C source code" << std::endl;
std::cout << "Keys: " << std::endl;
std::cout << "--help - this help" << std::endl;
std::cout << "--lo-freq <freq> - set demodulator low frequency. default: 500" << std::endl;
std::cout << "--hi-freq <freq> - set demodulator high frequency. default: 4500" << std::endl;
std::cout << "--cent-freq <freq> - set demodulator initial center frequency. default: 2600" << std::endl;
std::cout << "--stats - print demodulator statistics(frequency, etc...)" << std::endl;
std::cout << "--source-file <file-path> - select audiofile source for demodulator" << std::endl;
std::cout << "--source-udp <port> - select udp source for demodulator(compatible with gqrx). default port: 7355" << std::endl;
std::cout << "--source-alsa <device> - select alsa source for demodulator. default device: 'default'" << std::endl;
std::cout << "--out-udp <ip> <port> - send demodulated symbols via udp to specified ip:port. default: 127.0.0.1:15003" << std::endl;
std::cout << "(one source and one out parameters should be selected)" << std::endl;
}
int parseArg(int argc, int* position, char* argv[], std::map<std::string, std::string>* params, bool recursive) {
std::string arg1 = std::string(argv[*position]);
//i would be using switch() here... but it's not available for strings, so...
if(arg1 == "--help") {
return 1;
} else if(arg1 == "--stats") {
params->insert(std::pair<std::string, std::string>("demodStats", "true"));
return 0;
} else if(arg1 == "--lo-freq") {
int nextpos = *position + 1;
if(nextpos > argc or recursive) {
return 1;
}
int parseRes = parseArg(argc, &nextpos, argv, params, true);
if(parseRes != 2) {
return 1;
}
*position = nextpos;
std::string arg2 = std::string(argv[nextpos]);
params->insert(std::pair<std::string, std::string>("demodLoFreq", arg2));
return 0;
} else if(arg1 == "--hi-freq") {
int nextpos = *position + 1;
if(nextpos > argc or recursive) {
return 1;
}
int parseRes = parseArg(argc, &nextpos, argv, params, true);
if(parseRes != 2) {
return 1;
}
*position = nextpos;
std::string arg2 = std::string(argv[nextpos]);
params->insert(std::pair<std::string, std::string>("demodHiFreq", arg2));
return 0;
} else if(arg1 == "--cent-freq") {
int nextpos = *position + 1;
if(nextpos > argc or recursive) {
return 1;
}
int parseRes = parseArg(argc, &nextpos, argv, params, true);
if(parseRes != 2) {
return 1;
}
*position = nextpos;
std::string arg2 = std::string(argv[nextpos]);
params->insert(std::pair<std::string, std::string>("demodCentFreq", arg2));
return 0;
} else if(arg1 == "--source-file") {
int nextpos = *position + 1;
if(nextpos > argc or recursive) {
return 1;
}
int parseRes = parseArg(argc, &nextpos, argv, params, true);
if(parseRes != 2) {
return 1;
}
*position = nextpos;
std::string arg2 = std::string(argv[nextpos]);
params->insert(std::pair<std::string, std::string>("demodSource", "file"));
params->insert(std::pair<std::string, std::string>("demodSourceFilepath", arg2));
return 0;
} else if(arg1 == "--source-udp") {
std::string arg2;
arg2 = "7355";
int nextpos = *position + 1;
if(nextpos < argc and !recursive) {
int parseRes = parseArg(argc, &nextpos, argv, params, true);
if(parseRes == 2) {
arg2 = std::string(argv[nextpos]);
}
*position = nextpos;
}
params->insert(std::pair<std::string, std::string>("demodSource", "udp"));
params->insert(std::pair<std::string, std::string>("demodSourceUdpPort", arg2));
return 0;
} else if(arg1 == "--source-alsa") {
std::string arg2;
arg2 = "default";
int nextpos = *position + 1;
if(nextpos < argc and !recursive) {
int parseRes = parseArg(argc, &nextpos, argv, params, true);
if(parseRes == 2) {
arg2 = std::string(argv[nextpos]);
}
*position = nextpos;
}
params->insert(std::pair<std::string, std::string>("demodSource", "alsa"));
params->insert(std::pair<std::string, std::string>("demodSourceAlsaDev", arg2));
return 0;
} else if(arg1 == "--out-udp") {
std::string arg2;
std::string arg3;
arg2 = "127.0.0.1";
arg3 = "15003";
int nextpos = *position + 1;
if(nextpos < argc and !recursive) {
int parseRes = parseArg(argc, &nextpos, argv, params, true);
if(parseRes == 2) {
arg2 = std::string(argv[nextpos]);
}
*position = nextpos;
nextpos++;
if(nextpos < argc) {
parseRes = parseArg(argc, &nextpos, argv, params, true);
if(parseRes == 2) {
arg3 = std::string(argv[nextpos]);
}
*position = nextpos;
}
}
params->insert(std::pair<std::string, std::string>("demodOutUdp", "true"));
params->insert(std::pair<std::string, std::string>("demodOutUdpIp", arg2));
params->insert(std::pair<std::string, std::string>("demodOutUdpPort", arg3));
return 0;
} else {
return 2;
}
}
void sendDemodSymbolsViaUdp(uint8_t data[DEMODULATOR_SYMBOLSPERCHUNK], int sockfd, sockaddr_in serveraddr) {
sendto(sockfd, (const char *)data, DEMODULATOR_SYMBOLSPERCHUNK, 0, (const struct sockaddr *) &serveraddr, sizeof(serveraddr));
}
int main(int argc, char* argv[]) {
std::map<std::string, std::string> params;
if(argc < 2) {
printHelp();
return 1;
}
for(int i = 1; i < argc; i++) {
int res = parseArg(argc, &i, argv, ¶ms, false);
if(res == 1 or res == 2) {
std::cout << "Wrong args!" << std::endl;
printHelp();
return 1;
}
}
bool isDemodStats = params.find("demodStats") != params.end() && params["demodStats"] == "true";
if(params.find("demodSource") == params.end() || params.find("demodOutUdp") == params.end() || params["demodOutUdp"] != "true" || params.find("demodOutUdpIp") == params.end() || params.find("demodOutUdpPort") == params.end()) {
std::cout << "Wrong/No source/out selected!" << std::endl;
printHelp();
return 1;
}
std::string demodSource = params["demodSource"];
std::string udpIp = params["demodOutUdpIp"];
std::string udpPort = params["demodOutUdpPort"];
sockaddr_in clientaddr;
memset(&clientaddr, 0, sizeof(clientaddr));
// Filling server information
clientaddr.sin_family = AF_INET;
clientaddr.sin_port = htons(std::stoi(udpPort));
clientaddr.sin_addr.s_addr=inet_addr(udpIp.c_str());
inmarsatc::demodulator::Demodulator demod;
if(params.find("demodLoFreq") != params.end()) {
int loFreq = std::atoi(params["demodLoFreq"].c_str());
demod.setLowFreq(loFreq);
}
if(params.find("demodHiFreq") != params.end()) {
int hiFreq = std::atoi(params["demodHiFreq"].c_str());
demod.setHighFreq(hiFreq);
}
if(params.find("demodCentFreq") != params.end()) {
int centFreq = std::atoi(params["demodCentFreq"].c_str());
demod.setCenterFreq(centFreq);
}
int sockfd;
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
std::cout << "Socket creation failed!" << std::endl;
return 1;
}
if(demodSource == "file") {
if(params.find("demodSourceFilepath") == params.end()) {
std::cout << "File path not specified!" << std::endl;
return 1;
}
std::string filePath = params["demodSourceFilepath"];
AFfilehandle file = afOpenFile(filePath.c_str(), "r", AF_NULL_FILESETUP);
int channels = afGetChannels(file, AF_DEFAULT_TRACK);
double rate = afGetRate(file, AF_DEFAULT_TRACK);
afSetVirtualSampleFormat(file, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16);
if(channels != 1 or rate != 48000) {
std::cout << "Wrong file format! It should be 48k, 1 channel." << std::endl;
return 1;
}
int16_t buf[BUFSIZE];
AFframecount framesRead;
while(true) {
framesRead = afReadFrames(file, AF_DEFAULT_TRACK, buf, BUFSIZE);
if(framesRead <= 0) {
return 0;
}
std::complex<double> cbuf[framesRead];
for(int i = 0; i < framesRead; i+= 1) {
double val = buf[i];
cbuf[i] = std::complex<double>(val,val);
}
std::vector<inmarsatc::demodulator::Demodulator::demodulator_result> res = demod.demodulate(cbuf, framesRead);
if(isDemodStats) {
std::cout << "freq = " << demod.getCenterFreq() << " sync = " << (demod.getIsInSync() ? "true" : "false") << " \r" << std::flush;
}
if(res.size() > 0) {
for(int d = 0; d < (int)res.size(); d++) {
sendDemodSymbolsViaUdp(res[d].bitsDemodulated, sockfd, clientaddr);
}
}
}
} else if(demodSource == "udp") {
if(params.find("demodSourceUdpPort") == params.end()) {
std::cout << "Udp port not specified!" << std::endl;
return 1;
}
std::string udpPort = params["demodSourceUdpPort"];
int clisockfd;
if ((clisockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
std::cout << "Socket creation failed!" << std::endl;
return 1;
}
// Filling server information
sockaddr_in serveraddr, cliaddr;
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(std::stoi(udpPort));
serveraddr.sin_addr.s_addr=INADDR_ANY;
if (bind(clisockfd, (const struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0) {
std::cout << "Binding to port failed!" << std::endl;
return 1;
}
int16_t buf[BUFSIZE];
int received;
socklen_t len_useless = sizeof(cliaddr);
while(true) {
received = recvfrom(clisockfd, (char *)buf, (BUFSIZE*2), MSG_WAITALL, ( struct sockaddr *) &cliaddr, &len_useless);
received = received / 2;//char to int16_t
if(received <= 0) {
return 0;
}
std::complex<double> cbuf[received];
for(int i = 0; i < received; i+= 1) {
double val = buf[i];
cbuf[i] = std::complex<double>(val,val);
}
std::vector<inmarsatc::demodulator::Demodulator::demodulator_result> res = demod.demodulate(cbuf, received);
if(isDemodStats) {
std::cout << "freq = " << demod.getCenterFreq() << " sync = " << (demod.getIsInSync() ? "true" : "false") << " \r" << std::flush;
}
if(res.size() > 0) {
for(int d = 0; d < (int)res.size(); d++) {
sendDemodSymbolsViaUdp(res[d].bitsDemodulated, sockfd, clientaddr);
}
}
}
} else if(demodSource == "alsa") {
if(params.find("demodSourceAlsaDev") == params.end()) {
std::cout << "Alsa device not specified!" << std::endl;
return 1;
}
std::string alsaDev = params["demodSourceAlsaDev"];
int err;
unsigned int rate = 48000;
snd_pcm_t *capture_handle;
snd_pcm_hw_params_t *hw_params;
snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE;
if ((err = snd_pcm_open (&capture_handle, alsaDev.c_str(), SND_PCM_STREAM_CAPTURE, 0)) < 0) {
fprintf (stderr, "cannot open audio device %s (%s)\n", "", snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n", snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_any (capture_handle, hw_params)) < 0) {
fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n", snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_set_access (capture_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
fprintf (stderr, "cannot set access type (%s)\n", snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_set_format (capture_handle, hw_params, format)) < 0) {
fprintf (stderr, "cannot set sample format (%s)\n", snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_set_rate_near (capture_handle, hw_params, &rate, 0)) < 0) {
fprintf (stderr, "cannot set sample rate (%s)\n", snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_set_channels (capture_handle, hw_params, 1)) < 0) {
fprintf (stderr, "cannot set channel count (%s)\n", snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params (capture_handle, hw_params)) < 0) {
fprintf (stderr, "cannot set parameters (%s)\n", snd_strerror (err));
exit (1);
}
snd_pcm_hw_params_free (hw_params);
if ((err = snd_pcm_prepare (capture_handle)) < 0) {
fprintf (stderr, "cannot prepare audio interface for use (%s)\n", snd_strerror (err));
exit (1);
}
int framesRead;
int16_t buf[BUFSIZE];
while(true) {
framesRead = (snd_pcm_readi (capture_handle, buf, BUFSIZE));
if(framesRead <= 0) {
break;
}
std::complex<double> cbuf[framesRead];
for(int i = 0; i < framesRead; i+= 1) {
double val = buf[i];
cbuf[i] = std::complex<double>(val,val);
}
std::vector<inmarsatc::demodulator::Demodulator::demodulator_result> res = demod.demodulate(cbuf, framesRead);
if(isDemodStats) {
std::cout << "freq = " << demod.getCenterFreq() << " sync = " << (demod.getIsInSync() ? "true" : "false") << " \r" << std::flush;
}
if(res.size() > 0) {
for(int d = 0; d < (int)res.size(); d++) {
sendDemodSymbolsViaUdp(res[d].bitsDemodulated, sockfd, clientaddr);
}
}
}
snd_pcm_close (capture_handle);
} else {
std::cout << "Wrong or none demodulator source!" << std::endl;
return 1;
}
}