-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathfreedv_rx.c
343 lines (292 loc) · 10.7 KB
/
freedv_rx.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*---------------------------------------------------------------------------*\
FILE........: freedv_rx.c
AUTHOR......: David Rowe
DATE CREATED: August 2014
Demo/development receive program for FreeDV API functions:
Example usage (all one line):
$ cd codec2/build_linux/src
$ ./freedv_tx 1600 ../../raw/ve9qrp_10s.raw - | ./freedv_rx 1600 - - | aplay
-f S16
\*---------------------------------------------------------------------------*/
/*
Copyright (C) 2014 David Rowe
All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 2.1, as
published by the Free Software Foundation. This program is
distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <errno.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "freedv_api.h"
#include "modem_stats.h"
#include "reliable_text.h"
#define NDISCARD \
5 /* BER measure optionally discards first few frames after sync */
/* optioal call-back function for received txt characters */
void my_put_next_rx_char(void *states, char c) {
fprintf((FILE *)states, "%c", c);
}
static FILE *reliable_tx_fp;
reliable_text_t reliable_text_obj;
void on_reliable_text_rx(reliable_text_t rt, const char *txt_ptr, int length,
void *state) {
fprintf(reliable_tx_fp, "%s\n", txt_ptr);
reliable_text_reset(reliable_text_obj);
}
int main(int argc, char *argv[]) {
FILE *fin, *fout, *ftxt_rx = NULL;
int nin, nout, nout_total = 0, frame = 0;
struct MODEM_STATS stats = {0};
int mode;
int sync;
float snr_est;
float clock_offset, foff;
int use_testframes, verbose, discard, use_complex, use_reliabletext;
int use_squelch;
float squelch = 0;
struct freedv *freedv;
int use_passthroughgain;
float passthroughgain = 0.0;
char f2020[80] = {0};
#ifdef __LPCNET__
sprintf(f2020, "|2020|2020B");
#endif
if (argc < 4) {
helpmsg:
fprintf(
stderr,
"usage: %s [options] 1600|700C|700D|700E|2400A|2400B|800XA%s "
"InputModemSpeechFile OutputSpeechRawFile\n"
"\n"
" --discard Reset BER stats on loss of sync, helps us "
"get sensible BER results\n"
" --reliabletext txt Send 'txt' using reliable text protocol\n"
" --txtrx filename Store reliable text output to filename\n"
" --squelch leveldB Set squelch level\n"
" --testframes testframes assumed to be received instead "
"of coded speech, measure BER/PER\n"
" --usecomplex Complex int16 input samples (default real "
"int16)\n"
" -v Verbose level 1\n"
" --vv Verbose level 2\n"
"\n",
argv[0], f2020);
fprintf(stderr, "example: $ %s 1600 hts1a_fdmdv.raw hts1a_out.raw \n",
argv[0]);
exit(1);
}
use_testframes = verbose = discard = use_complex = use_squelch = 0;
use_reliabletext = 0;
use_passthroughgain = 0;
int o = 0;
int opt_idx = 0;
while (o != -1) {
static struct option long_opts[] = {
{"discard", no_argument, 0, 'i'},
{"help", no_argument, 0, 'h'},
{"reliabletext", no_argument, 0, 'r'},
{"squelch", required_argument, 0, 's'},
{"txtrx", required_argument, 0, 'x'},
{"testframes", no_argument, 0, 't'},
{"usecomplex", no_argument, 0, 'c'},
{"verbose1", no_argument, 0, 'v'},
{"vv", no_argument, 0, 'w'},
{"passthroughgain", required_argument, 0, 'p'},
{0, 0, 0, 0}};
o = getopt_long(argc, argv, "idhr:s:x:tcvwp:", long_opts, &opt_idx);
switch (o) {
case 'i':
discard = 1;
break;
case 'c':
use_complex = 1;
break;
case 'p':
use_passthroughgain = 1;
passthroughgain = atof(optarg);
break;
case 'r':
use_reliabletext = 1;
break;
case 's':
use_squelch = 1;
squelch = atof(optarg);
break;
case 't':
use_testframes = 1;
break;
case 'x':
ftxt_rx = fopen(optarg, "wt");
assert(ftxt_rx != NULL);
break;
case 'v':
verbose = 1;
break;
case 'w':
verbose = 2;
break;
case 'h':
case '?':
goto helpmsg;
break;
}
}
int dx = optind;
if ((argc - dx) < 3) {
fprintf(stderr, "too few arguments.\n");
goto helpmsg;
}
mode = -1;
if (!strcmp(argv[dx], "1600")) mode = FREEDV_MODE_1600;
if (!strcmp(argv[dx], "700C")) mode = FREEDV_MODE_700C;
if (!strcmp(argv[dx], "700D")) mode = FREEDV_MODE_700D;
if (!strcmp(argv[dx], "700E")) mode = FREEDV_MODE_700E;
if (!strcmp(argv[dx], "2400A")) mode = FREEDV_MODE_2400A;
if (!strcmp(argv[dx], "2400B")) mode = FREEDV_MODE_2400B;
if (!strcmp(argv[dx], "800XA")) mode = FREEDV_MODE_800XA;
#ifdef __LPCNET__
if (!strcmp(argv[dx], "2020")) mode = FREEDV_MODE_2020;
if (!strcmp(argv[dx], "2020B")) mode = FREEDV_MODE_2020B;
#endif
if (mode == -1) {
fprintf(stderr, "Error in mode: %s\n", argv[dx]);
exit(1);
}
if (strcmp(argv[dx + 1], "-") == 0)
fin = stdin;
else if ((fin = fopen(argv[dx + 1], "rb")) == NULL) {
fprintf(stderr, "Error opening input raw modem sample file: %s: %s.\n",
argv[dx + 1], strerror(errno));
exit(1);
}
if (strcmp(argv[dx + 2], "-") == 0)
fout = stdout;
else if ((fout = fopen(argv[dx + 2], "wb")) == NULL) {
fprintf(stderr, "Error opening output speech sample file: %s: %s.\n",
argv[dx + 2], strerror(errno));
exit(1);
}
freedv = freedv_open(mode);
assert(freedv != NULL);
/* set up a few options, calling these is optional -------------------------*/
freedv_set_test_frames(freedv, use_testframes);
if (verbose == 2) freedv_set_verbose(freedv, verbose);
if (use_squelch) {
freedv_set_snr_squelch_thresh(freedv, squelch);
freedv_set_squelch_en(freedv, true);
}
if (use_passthroughgain) freedv_passthrough_gain(freedv, passthroughgain);
/* install optional handler for received txt characters */
if (ftxt_rx != NULL) {
if (use_reliabletext) {
reliable_tx_fp = ftxt_rx;
reliable_text_obj = reliable_text_create();
assert(reliable_text_obj != NULL);
reliable_text_set_string(reliable_text_obj, "AB1CDEF", 7); // not used
reliable_text_use_with_freedv(reliable_text_obj, freedv,
on_reliable_text_rx, NULL);
} else {
freedv_set_callback_txt(freedv, my_put_next_rx_char, NULL, ftxt_rx);
}
}
/* note use of API functions to tell us how big our buffers need to be -----*/
short speech_out[freedv_get_n_max_speech_samples(freedv)];
short demod_in[freedv_get_n_max_modem_samples(freedv)];
/* We need to work out how many samples the demod needs on each
call (nin). This is used to adjust for differences in the tx
and rx sample clock frequencies. Note also the number of
output speech samples "nout" is time varying. */
nin = freedv_nin(freedv);
while (fread(demod_in, sizeof(short), nin, fin) == nin) {
frame++;
if (use_complex) {
/* exercise the complex version of the API (useful
for testing 700D which has a different code path for
short samples) */
COMP demod_in_complex[nin];
for (int i = 0; i < nin; i++) {
demod_in_complex[i].real = (float)demod_in[i];
demod_in_complex[i].imag = 0.0f;
}
nout = freedv_comprx(freedv, speech_out, demod_in_complex);
} else {
// most common interface - real shorts in, real shorts out
nout = freedv_rx(freedv, speech_out, demod_in);
}
/* IMPORTANT: don't forget to do this in the while loop to
ensure we fread the correct number of samples: ie update
"nin" before every call to freedv_rx()/freedv_comprx() */
nin = freedv_nin(freedv);
/* optionally read some stats */
freedv_get_modem_stats(freedv, &sync, &snr_est);
freedv_get_modem_extended_stats(freedv, &stats);
int total_bit_errors = freedv_get_total_bit_errors(freedv);
clock_offset = stats.clock_offset;
foff = stats.foff;
if (discard && (sync == 0)) {
// discard BER results if we get out of sync, helps us get sensible BER
// results
freedv_set_total_bits(freedv, 0);
freedv_set_total_bit_errors(freedv, 0);
freedv_set_total_bits_coded(freedv, 0);
freedv_set_total_bit_errors_coded(freedv, 0);
}
fwrite(speech_out, sizeof(short), nout, fout);
nout_total += nout;
if (verbose == 1) {
fprintf(stderr,
"frame: %d sync: %d nin: %d snr: %3.2f dB bit errors: %d "
"clock_off: %6.2f foff: %5.2f\n",
frame, sync, nin, snr_est, total_bit_errors, clock_offset, foff);
}
/* if using pipes we probably don't want the usual buffering
to occur */
if (fout == stdout) fflush(stdout);
}
if (ftxt_rx != NULL) fclose(ftxt_rx);
fclose(fin);
fclose(fout);
fprintf(stderr, "frames decoded: %d output speech samples: %d\n", frame,
nout_total);
/* finish up with some stats */
if (freedv_get_test_frames(freedv)) {
int Tbits = freedv_get_total_bits(freedv);
int Terrs = freedv_get_total_bit_errors(freedv);
float uncoded_ber = (float)Terrs / Tbits;
fprintf(stderr, "BER......: %5.4f Tbits: %8d Terrs: %8d\n",
(double)uncoded_ber, Tbits, Terrs);
if ((mode == FREEDV_MODE_700D) || (mode == FREEDV_MODE_700E) ||
(mode == FREEDV_MODE_2020) || (mode == FREEDV_MODE_2020B)) {
int Tbits_coded = freedv_get_total_bits_coded(freedv);
int Terrs_coded = freedv_get_total_bit_errors_coded(freedv);
float coded_ber = (float)Terrs_coded / Tbits_coded;
fprintf(stderr, "Coded BER: %5.4f Tbits: %8d Terrs: %8d\n",
(double)coded_ber, Tbits_coded, Terrs_coded);
int Tpackets = freedv_get_total_packets(freedv);
int Tpacket_errors = freedv_get_total_packet_errors(freedv);
float per = (float)Tpacket_errors / Tpackets;
fprintf(stderr, "Coded PER: %5.4f Tpkts: %8d Tpers: %8d\n", per,
Tpackets, Tpacket_errors);
/* set return code for Ctest */
if ((uncoded_ber < 0.1f) && (coded_ber < 0.01f))
return 0;
else
return 1;
}
}
if (use_reliabletext) {
reliable_text_destroy(reliable_text_obj);
}
freedv_close(freedv);
return 0;
}