aboutsummaryrefslogtreecommitdiff
path: root/src/freedv_api.c
blob: 5172959d2c977b475b8b1eee3007e95523c20ea5 (plain)
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
/*---------------------------------------------------------------------------*\

  FILE........: freedv_api.c
  AUTHOR......: David Rowe
  DATE CREATED: August 2014

  Library of API functions that implement the FreeDV API, useful for
  embedding FreeDV in other programs.  Please see:

  1. README_freedv.md
  2. Notes on function use in this file
  3. Simple demo programs in the "demo" directory
  4. The full featured command line freedv_tx.c and freedv_rx.c programs

\*---------------------------------------------------------------------------*/

/*
  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 "freedv_api.h"

#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "codec2.h"
#include "codec2_fdmdv.h"
#include "codec2_ofdm.h"
#include "comp_prim.h"
#include "debug_alloc.h"
#include "fdmdv_internal.h"
#include "fmfsk.h"
#include "freedv_api_internal.h"
#include "freedv_vhf_framing.h"
#include "fsk.h"
#include "gp_interleaver.h"
#include "interldpc.h"
#include "mpdecode_core.h"
#include "ofdm_internal.h"
#include "varicode.h"

/* The API version number.  The first version is 10.  Increment if the API
   changes in a way that would require changes by the API user. */

#define VERSION 15

/*
  Version 10   Initial version August 2, 2015.

  Version 11   September 2015
               Added: freedv_zero_total_bit_errors(), freedv_get_sync()
               Changed all input and output sample rates to 8000 sps.  Rates
               for FREEDV_MODE_700 and 700B were 7500.

  Version 12   August 2018 Added OFDM configuration switch structure

  Version 13   November 2019 Removed 700 and 700B modes

  Version 14   May 2020 Number of returned speech samples can vary,  use
               freedv_get_n_max_speech_samples() to allocate buffers.

  Version 15   December 2022 Removing rarely used DPSK support which is not
               needed given fast fading modes
 */

char *ofdm_statemode[] = {"search", "trial", "synced"};

char *rx_sync_flags_to_text[] = {"----", "---T", "--S-", "--ST", "-B--", "-B-T",
                                 "-BS-", "-BST", "E---", "E--T", "E-S-", "E-ST",
                                 "EB--", "EB-T", "EBS-", "EBST"};

/*---------------------------------------------------------------------------* \

  FUNCTION....: freedv_open
  AUTHOR......: David Rowe
  DATE CREATED: 3 August 2014

  Call this first to initialise.  Returns NULL if initialisation
  fails. If a malloc() or calloc() fails in general asserts() will
  fire.

\*---------------------------------------------------------------------------*/

struct freedv *freedv_open(int mode) {
  // defaults for those modes that support the use of adv
  struct freedv_advanced adv = {0, 2, 100, 8000, 1000, 200, "H_256_512_4"};
  return freedv_open_advanced(mode, &adv);
}

struct freedv *freedv_open_advanced(int mode, struct freedv_advanced *adv) {
  struct freedv *f;

  assert(FREEDV_PEAK == OFDM_PEAK);
  assert(FREEDV_VARICODE_MAX_BITS == VARICODE_MAX_BITS);

  if ((FDV_MODE_ACTIVE(FREEDV_MODE_1600, mode) ||
       FDV_MODE_ACTIVE(FREEDV_MODE_700C, mode) ||
       FDV_MODE_ACTIVE(FREEDV_MODE_700D, mode) ||
       FDV_MODE_ACTIVE(FREEDV_MODE_700E, mode) ||
       FDV_MODE_ACTIVE(FREEDV_MODE_2400A, mode) ||
       FDV_MODE_ACTIVE(FREEDV_MODE_2400B, mode) ||
       FDV_MODE_ACTIVE(FREEDV_MODE_800XA, mode) ||
       FDV_MODE_ACTIVE(FREEDV_MODE_2020, mode) ||
       FDV_MODE_ACTIVE(FREEDV_MODE_2020B, mode) ||
       FDV_MODE_ACTIVE(FREEDV_MODE_FSK_LDPC, mode) ||
       FDV_MODE_ACTIVE(FREEDV_MODE_DATAC0, mode) ||
       FDV_MODE_ACTIVE(FREEDV_MODE_DATAC1, mode) ||
       FDV_MODE_ACTIVE(FREEDV_MODE_DATAC3, mode) ||
       FDV_MODE_ACTIVE(FREEDV_MODE_DATAC4, mode) ||
       FDV_MODE_ACTIVE(FREEDV_MODE_DATAC13, mode)) == false)
    return NULL;

  /* set everything to zero just in case */
  f = (struct freedv *)CALLOC(1, sizeof(struct freedv));
  if (f == NULL) return NULL;

  f->mode = mode;

  if (FDV_MODE_ACTIVE(FREEDV_MODE_1600, mode)) freedv_1600_open(f);
  if (FDV_MODE_ACTIVE(FREEDV_MODE_700C, mode)) freedv_700c_open(f);
  if (FDV_MODE_ACTIVE(FREEDV_MODE_700D, mode))
    freedv_ofdm_voice_open(f, "700D");
  if (FDV_MODE_ACTIVE(FREEDV_MODE_700E, mode))
    freedv_ofdm_voice_open(f, "700E");
#ifdef __LPCNET__
  if (FDV_MODE_ACTIVE(FREEDV_MODE_2020, mode) ||
      FDV_MODE_ACTIVE(FREEDV_MODE_2020B, mode))
    freedv_2020x_open(f);
#endif
  if (FDV_MODE_ACTIVE(FREEDV_MODE_2400A, mode)) freedv_2400a_open(f);
  if (FDV_MODE_ACTIVE(FREEDV_MODE_2400B, mode)) freedv_2400b_open(f);
  if (FDV_MODE_ACTIVE(FREEDV_MODE_800XA, mode)) freedv_800xa_open(f);
  if (FDV_MODE_ACTIVE(FREEDV_MODE_FSK_LDPC, mode)) freedv_fsk_ldpc_open(f, adv);
  if (FDV_MODE_ACTIVE(FREEDV_MODE_DATAC0, mode)) freedv_ofdm_data_open(f);
  if (FDV_MODE_ACTIVE(FREEDV_MODE_DATAC1, mode)) freedv_ofdm_data_open(f);
  if (FDV_MODE_ACTIVE(FREEDV_MODE_DATAC3, mode)) freedv_ofdm_data_open(f);
  if (FDV_MODE_ACTIVE(FREEDV_MODE_DATAC4, mode)) freedv_ofdm_data_open(f);
  if (FDV_MODE_ACTIVE(FREEDV_MODE_DATAC13, mode)) freedv_ofdm_data_open(f);

  varicode_decode_init(&f->varicode_dec_states, 1);

  return f;
}

/*---------------------------------------------------------------------------*\

  FUNCTION....: freedv_close
  AUTHOR......: David Rowe
  DATE CREATED: 3 August 2014

  Call to shut down a freedv instance and free memory.

\*---------------------------------------------------------------------------*/

void freedv_close(struct freedv *freedv) {
  assert(freedv != NULL);

  FREE(freedv->tx_payload_bits);
  FREE(freedv->rx_payload_bits);
  if (freedv->codec2) codec2_destroy(freedv->codec2);

  if (FDV_MODE_ACTIVE(FREEDV_MODE_1600, freedv->mode)) {
    FREE(freedv->fdmdv_bits);
    FREE(freedv->fdmdv_tx_bits);
    FREE(freedv->fdmdv_rx_bits);
    fdmdv_destroy(freedv->fdmdv);
  }

  if (FDV_MODE_ACTIVE(FREEDV_MODE_700C, freedv->mode)) {
    cohpsk_destroy(freedv->cohpsk);
    quisk_filt_destroy(freedv->ptFilter8000to7500);
    FREE(freedv->ptFilter8000to7500);
    quisk_filt_destroy(freedv->ptFilter7500to8000);
    FREE(freedv->ptFilter7500to8000);
  }

  if (FDV_MODE_ACTIVE(FREEDV_MODE_700D, freedv->mode) ||
      FDV_MODE_ACTIVE(FREEDV_MODE_700E, freedv->mode)) {
    FREE(freedv->rx_syms);
    FREE(freedv->rx_amps);
    FREE(freedv->ldpc);
    ofdm_destroy(freedv->ofdm);
  }

  if (FDV_MODE_ACTIVE(FREEDV_MODE_2020, freedv->mode) ||
      FDV_MODE_ACTIVE(FREEDV_MODE_2020B, freedv->mode)) {
    FREE(freedv->codeword_symbols);
    FREE(freedv->codeword_amps);
    FREE(freedv->ldpc);
    FREE(freedv->passthrough_2020);
    ofdm_destroy(freedv->ofdm);
#ifdef __LPCNET__
    lpcnet_freedv_destroy(freedv->lpcnet);
#endif
  }

  if (FDV_MODE_ACTIVE(FREEDV_MODE_2400A, freedv->mode) ||
      FDV_MODE_ACTIVE(FREEDV_MODE_800XA, freedv->mode)) {
    fsk_destroy(freedv->fsk);
    fvhff_destroy_deframer(freedv->deframer);
  }

  if (FDV_MODE_ACTIVE(FREEDV_MODE_2400B, freedv->mode)) {
    fmfsk_destroy(freedv->fmfsk);
    fvhff_destroy_deframer(freedv->deframer);
  }

  if (FDV_MODE_ACTIVE(FREEDV_MODE_FSK_LDPC, freedv->mode)) {
    fsk_destroy(freedv->fsk);
    FREE(freedv->ldpc);
    FREE(freedv->frame_llr);
    FREE(freedv->twoframes_llr);
    FREE(freedv->twoframes_hard);
  }

  if (FDV_MODE_ACTIVE(FREEDV_MODE_DATAC0, freedv->mode) ||
      FDV_MODE_ACTIVE(FREEDV_MODE_DATAC1, freedv->mode) ||
      FDV_MODE_ACTIVE(FREEDV_MODE_DATAC3, freedv->mode) ||
      FDV_MODE_ACTIVE(FREEDV_MODE_DATAC4, freedv->mode) ||
      FDV_MODE_ACTIVE(FREEDV_MODE_DATAC13, freedv->mode)) {
    FREE(freedv->rx_syms);
    FREE(freedv->rx_amps);
    FREE(freedv->ldpc);
    ofdm_destroy(freedv->ofdm);
  }

  FREE(freedv);
}

/* helper function, unpacked bits are much easier to work with inside the modem
 */

static void codec2_encode_upacked(struct freedv *f, uint8_t unpacked_bits[],
                                  short speech_in[]) {
  int n_packed = (f->bits_per_codec_frame + 7) / 8;
  uint8_t packed_codec_bits[n_packed];

  codec2_encode(f->codec2, packed_codec_bits, speech_in);
  freedv_unpack(unpacked_bits, packed_codec_bits, f->bits_per_codec_frame);
}

static int is_ofdm_mode(struct freedv *f) {
  return FDV_MODE_ACTIVE(FREEDV_MODE_2020, f->mode) ||
         FDV_MODE_ACTIVE(FREEDV_MODE_2020B, f->mode) ||
         FDV_MODE_ACTIVE(FREEDV_MODE_700D, f->mode) ||
         FDV_MODE_ACTIVE(FREEDV_MODE_700E, f->mode) ||
         FDV_MODE_ACTIVE(FREEDV_MODE_DATAC0, f->mode) ||
         FDV_MODE_ACTIVE(FREEDV_MODE_DATAC1, f->mode) ||
         FDV_MODE_ACTIVE(FREEDV_MODE_DATAC3, f->mode) ||
         FDV_MODE_ACTIVE(FREEDV_MODE_DATAC4, f->mode) ||
         FDV_MODE_ACTIVE(FREEDV_MODE_DATAC13, f->mode);
}

static int is_ofdm_data_mode(struct freedv *f) {
  return FDV_MODE_ACTIVE(FREEDV_MODE_DATAC0, f->mode) ||
         FDV_MODE_ACTIVE(FREEDV_MODE_DATAC1, f->mode) ||
         FDV_MODE_ACTIVE(FREEDV_MODE_DATAC3, f->mode) ||
         FDV_MODE_ACTIVE(FREEDV_MODE_DATAC4, f->mode) ||
         FDV_MODE_ACTIVE(FREEDV_MODE_DATAC13, f->mode);
}

/*---------------------------------------------------------------------------*\

  FUNCTION....: freedv_tx
  AUTHOR......: David Rowe
  DATE CREATED: 3 August 2014

  Takes a frame of input speech samples, encodes and modulates them to
  produce a frame of modem samples that can be sent to the
  transmitter.  See demo/freedv_700d_tx.c for an example.

  speech_in[] is sampled at freedv_get_speech_sample_rate() Hz, and
  the user must supply exactly freedv_get_n_speech_samples(). The peak
  level should be between +/- 16384 and +/- 32767.

  The modem signal mod_out[] is sampled at
  freedv_get_modem_sample_rate() and is always exactly
  freedv_get_n_nom_modem_samples() long.  mod_out[] will be scaled
  such that the peak level is around +/-16384.

  mod_out[] has a higher RMS power than SSB with the same peak level.
  In other words, the crest factor or peak to average power ratio is
  lower than typical SSB voice.  Ensure your transmitter is capable of
  continuous high RMS power operation, or consider reducing Tx power.

\*---------------------------------------------------------------------------*/

/* real-valued short output */

void freedv_tx(struct freedv *f, short mod_out[], short speech_in[]) {
  assert(f != NULL);
  COMP tx_fdm[f->n_nom_modem_samples];
  int i;

  /* FSK and MEFSK/FMFSK modems work only on real samples. It's simpler to just
   * stick them in the real sample tx/rx functions than to add a comp->real
   * converter to comptx */

  if ((FDV_MODE_ACTIVE(FREEDV_MODE_2400A, f->mode)) ||
      (FDV_MODE_ACTIVE(FREEDV_MODE_2400B, f->mode)) ||
      (FDV_MODE_ACTIVE(FREEDV_MODE_800XA, f->mode))) {
    /* 800XA has two codec frames per modem frame */
    if (FDV_MODE_ACTIVE(FREEDV_MODE_800XA, f->mode)) {
      codec2_encode(f->codec2, &f->tx_payload_bits[0], &speech_in[0]);
      codec2_encode(f->codec2, &f->tx_payload_bits[4], &speech_in[320]);
    } else {
      codec2_encode(f->codec2, f->tx_payload_bits, speech_in);
    }
    freedv_tx_fsk_voice(f, mod_out);
  } else {
    freedv_comptx(f, tx_fdm, speech_in);
    for (i = 0; i < f->n_nom_modem_samples; i++) mod_out[i] = tx_fdm[i].real;
  }
}

/* complex float output version of freedv_tx() */

void freedv_comptx(struct freedv *f, COMP mod_out[], short speech_in[]) {
  assert(f != NULL);

  assert(FDV_MODE_ACTIVE(FREEDV_MODE_1600, f->mode) ||
         FDV_MODE_ACTIVE(FREEDV_MODE_700C, f->mode) ||
         FDV_MODE_ACTIVE(FREEDV_MODE_2400A, f->mode) ||
         FDV_MODE_ACTIVE(FREEDV_MODE_2400B, f->mode) ||
         FDV_MODE_ACTIVE(FREEDV_MODE_700D, f->mode) ||
         FDV_MODE_ACTIVE(FREEDV_MODE_700E, f->mode) ||
         FDV_MODE_ACTIVE(FREEDV_MODE_2020, f->mode) ||
         FDV_MODE_ACTIVE(FREEDV_MODE_2020B, f->mode));

  if (FDV_MODE_ACTIVE(FREEDV_MODE_1600, f->mode)) {
    codec2_encode_upacked(f, f->tx_payload_bits, speech_in);
    freedv_comptx_fdmdv_1600(f, mod_out);
  }

  /* all these modes need to pack a bunch of codec frames into one modem frame
   * ... */

  if (FDV_MODE_ACTIVE(FREEDV_MODE_700C, f->mode)) {
    for (int j = 0; j < f->n_codec_frames; j++) {
      codec2_encode_upacked(f, f->tx_payload_bits + j * f->bits_per_codec_frame,
                            speech_in);
      speech_in += codec2_samples_per_frame(f->codec2);
    }
    freedv_comptx_700c(f, mod_out);
  }

  if (FDV_MODE_ACTIVE(FREEDV_MODE_700D, f->mode) ||
      FDV_MODE_ACTIVE(FREEDV_MODE_700E, f->mode)) {
    /* buffer up bits until we get enough encoded bits for interleaver */

    for (int j = 0; j < f->n_codec_frames; j++) {
      int offset = j * f->bits_per_codec_frame;
      codec2_encode_upacked(f, f->tx_payload_bits + offset, speech_in);
      speech_in += codec2_samples_per_frame(f->codec2);
    }

    freedv_comptx_ofdm(f, mod_out);
  }

#ifdef __LPCNET__
  if (FDV_MODE_ACTIVE(FREEDV_MODE_2020, f->mode) ||
      FDV_MODE_ACTIVE(FREEDV_MODE_2020B, f->mode)) {
    /* buffer up bits until we get enough encoded bits for interleaver */

    for (int j = 0; j < f->n_codec_frames; j++) {
      int offset = j * f->bits_per_codec_frame;
      lpcnet_enc(f->lpcnet, speech_in, (char *)f->tx_payload_bits + offset);
      speech_in += lpcnet_samples_per_frame(f->lpcnet);
    }

    freedv_comptx_2020(f, mod_out);
  }
#endif

  /* 2400 A and B are handled by the real-mode TX */
  if (FDV_MODE_ACTIVE(FREEDV_MODE_2400A, f->mode) ||
      FDV_MODE_ACTIVE(FREEDV_MODE_2400B, f->mode)) {
    codec2_encode(f->codec2, f->tx_payload_bits, speech_in);
    freedv_comptx_fsk_voice(f, mod_out);
  }
}

/* pack bits */
void freedv_pack(uint8_t *bytes, uint8_t *bits, int nbits) {
  memset(bytes, 0, (nbits + 7) / 8);
  int bit = 7, byte = 0;
  for (int i = 0; i < nbits; i++) {
    bytes[byte] |= bits[i] << bit;
    bit--;
    if (bit < 0) {
      bit = 7;
      byte++;
    }
  }
}

/* unpack bits, MSB first */
void freedv_unpack(uint8_t *bits, uint8_t *bytes, int nbits) {
  int bit = 7, byte = 0;
  for (int i = 0; i < nbits; i++) {
    bits[i] = (bytes[byte] >> bit) & 0x1;
    bit--;
    if (bit < 0) {
      bit = 7;
      byte++;
    }
  }
}

/* compute the CRC16 of a frame of unpacked bits */
unsigned short freedv_crc16_unpacked(unsigned char unpacked_bits[], int nbits) {
  assert((nbits % 8) == 0);
  int nbytes = nbits / 8;
  uint8_t packed_bytes[nbytes];
  freedv_pack(packed_bytes, unpacked_bits, nbits);
  return freedv_gen_crc16(packed_bytes, nbytes);
}

/* Return non-zero if CRC16 of a frame of unpacked bits is correct */
int freedv_check_crc16_unpacked(unsigned char unpacked_bits[], int nbits) {
  assert((nbits % 8) == 0);
  int nbytes = nbits / 8;
  uint8_t packed_bytes[nbytes];
  freedv_pack(packed_bytes, unpacked_bits, nbits);
  uint16_t tx_crc16 =
      (packed_bytes[nbytes - 2] << 8) | packed_bytes[nbytes - 1];
  uint16_t rx_crc16 = freedv_crc16_unpacked(unpacked_bits, nbits - 16);
  return tx_crc16 == rx_crc16;
}

/* send raw frames of bytes, or speech data that was compressed externally,
 * complex float output */
void freedv_rawdatacomptx(struct freedv *f, COMP mod_out[],
                          unsigned char *packed_payload_bits) {
  assert(f != NULL);

  freedv_unpack(f->tx_payload_bits, packed_payload_bits,
                f->bits_per_modem_frame);

  if (FDV_MODE_ACTIVE(FREEDV_MODE_1600, f->mode))
    freedv_comptx_fdmdv_1600(f, mod_out);
  if (FDV_MODE_ACTIVE(FREEDV_MODE_700C, f->mode))
    freedv_comptx_700c(f, mod_out);
  if (FDV_MODE_ACTIVE(FREEDV_MODE_700D, f->mode) ||
      FDV_MODE_ACTIVE(FREEDV_MODE_DATAC0, f->mode) ||
      FDV_MODE_ACTIVE(FREEDV_MODE_DATAC1, f->mode) ||
      FDV_MODE_ACTIVE(FREEDV_MODE_DATAC3, f->mode) ||
      FDV_MODE_ACTIVE(FREEDV_MODE_DATAC4, f->mode) ||
      FDV_MODE_ACTIVE(FREEDV_MODE_DATAC13, f->mode))
    freedv_comptx_ofdm(f, mod_out);

  if (FDV_MODE_ACTIVE(FREEDV_MODE_FSK_LDPC, f->mode)) {
    freedv_tx_fsk_ldpc_data(f, mod_out);
  }
}

/* send raw frames of bytes, or speech data that was compressed externally, real
 * short output */
void freedv_rawdatatx(struct freedv *f, short mod_out[],
                      unsigned char *packed_payload_bits) {
  assert(f != NULL);
  COMP mod_out_comp[f->n_nat_modem_samples];

  /* Some FSK modes used packed bits, and coincidentally support real samples
   * natively */
  if (FDV_MODE_ACTIVE(FREEDV_MODE_2400A, f->mode) ||
      FDV_MODE_ACTIVE(FREEDV_MODE_2400B, f->mode) ||
      FDV_MODE_ACTIVE(FREEDV_MODE_800XA, f->mode)) {
    freedv_codec_frames_from_rawdata(f, f->tx_payload_bits,
                                     packed_payload_bits);
    freedv_tx_fsk_voice(f, mod_out);
    return; /* output is already real */
  }

  freedv_rawdatacomptx(f, mod_out_comp, packed_payload_bits);

  /* convert complex to real */
  for (int i = 0; i < f->n_nat_modem_samples; i++)
    mod_out[i] = mod_out_comp[i].real;
}

int freedv_rawdatapreamblecomptx(struct freedv *f, COMP mod_out[]) {
  assert(f != NULL);
  int npreamble_samples = 0;

  if (f->mode == FREEDV_MODE_FSK_LDPC) {
    struct FSK *fsk = f->fsk;

    int npreamble_symbols = 50 * (fsk->mode >> 1);
    int npreamble_bits = npreamble_symbols * (fsk->mode >> 1);
    npreamble_samples = fsk->Ts * npreamble_symbols;
    // fprintf(stderr, "npreamble_symbols: %d npreamble_bits: %d
    // npreamble_samples: %d Nbits: %d N: %d\n", npreamble_symbols,
    // npreamble_bits, npreamble_samples, fsk->Nbits, fsk->N);

    assert(npreamble_samples <
           f->n_nom_modem_samples); /* caller probably using an array of this
                                       size */
    freedv_tx_fsk_ldpc_data_preamble(f, mod_out, npreamble_bits,
                                     npreamble_samples);
  } else if (is_ofdm_data_mode(f)) {
    struct OFDM *ofdm = f->ofdm;
    complex float *tx_preamble = (complex float *)mod_out;
    memcpy(tx_preamble, ofdm->tx_preamble,
           sizeof(COMP) * ofdm->samplesperframe);
    ofdm_hilbert_clipper(ofdm, tx_preamble, ofdm->samplesperframe);
    npreamble_samples = ofdm->samplesperframe;
  }

  return npreamble_samples;
}

int freedv_rawdatapreambletx(struct freedv *f, short mod_out[]) {
  assert(f != NULL);
  COMP mod_out_comp[f->n_nat_modem_samples];

  int npreamble_samples = freedv_rawdatapreamblecomptx(f, mod_out_comp);
  assert(npreamble_samples <= f->n_nat_modem_samples);

  /* convert complex to real */
  for (int i = 0; i < npreamble_samples; i++) mod_out[i] = mod_out_comp[i].real;

  return npreamble_samples;
}

int freedv_rawdatapostamblecomptx(struct freedv *f, COMP mod_out[]) {
  assert(f != NULL);
  int npostamble_samples = 0;

  if (is_ofdm_data_mode(f)) {
    struct OFDM *ofdm = f->ofdm;
    complex float *tx_postamble = (complex float *)mod_out;
    memcpy(tx_postamble, ofdm->tx_postamble,
           sizeof(COMP) * ofdm->samplesperframe);
    ofdm_hilbert_clipper(ofdm, tx_postamble, ofdm->samplesperframe);
    npostamble_samples = ofdm->samplesperframe;
  }

  return npostamble_samples;
}

int freedv_rawdatapostambletx(struct freedv *f, short mod_out[]) {
  assert(f != NULL);
  COMP mod_out_comp[f->n_nat_modem_samples];

  int npostamble_samples = freedv_rawdatapostamblecomptx(f, mod_out_comp);
  assert(npostamble_samples <= f->n_nat_modem_samples);

  /* convert complex to real */
  for (int i = 0; i < npostamble_samples; i++)
    mod_out[i] = mod_out_comp[i].real;

  return npostamble_samples;
}

/* VHF packet data tx function */
void freedv_datatx(struct freedv *f, short mod_out[]) {
  assert(f != NULL);
  if (FDV_MODE_ACTIVE(FREEDV_MODE_2400A, f->mode) ||
      FDV_MODE_ACTIVE(FREEDV_MODE_2400B, f->mode) ||
      FDV_MODE_ACTIVE(FREEDV_MODE_800XA, f->mode)) {
    freedv_tx_fsk_data(f, mod_out);
  }
}

/* VHF packet data: returns how many tx frames are queued up but not sent yet */
int freedv_data_ntxframes(struct freedv *f) {
  assert(f != NULL);
  if (FDV_MODE_ACTIVE(FREEDV_MODE_2400A, f->mode) ||
      FDV_MODE_ACTIVE(FREEDV_MODE_2400B, f->mode)) {
    if (f->deframer->fdc)
      return freedv_data_get_n_tx_frames(f->deframer->fdc, 8);
  } else if (FDV_MODE_ACTIVE(FREEDV_MODE_800XA, f->mode)) {
    if (f->deframer->fdc)
      return freedv_data_get_n_tx_frames(f->deframer->fdc, 6);
  }
  return 0;
}

int freedv_nin(struct freedv *f) {
  if (FDV_MODE_ACTIVE(FREEDV_MODE_700C, f->mode))
    // For mode 700C, the input rate is 8000 sps, but the modem rate is 7500 sps
    // For mode 700C, we request a larger number of Rx samples that will be
    // decimated to f->nin samples
    return (16 * f->nin + f->ptFilter8000to7500->decim_index) / 15;
  else
    return f->nin;
}

int freedv_codec_frames_from_rawdata(struct freedv *f,
                                     unsigned char *codec_frames,
                                     unsigned char *rawdata) {
  int cbit = 7;
  int cbyte = 0;
  int rbit = 7;
  int rbyte = 0;
  int modem_bits = freedv_get_bits_per_modem_frame(f);
  int codec_bits = freedv_get_bits_per_codec_frame(f);
  int nr_cbits = 0;
  int i;

  codec_frames[0] = 0;
  for (i = 0; i < modem_bits; i++) {
    codec_frames[cbyte] |= ((rawdata[rbyte] >> rbit) & 1) << cbit;

    rbit--;
    if (rbit < 0) {
      rbit = 7;
      rbyte++;
    }

    cbit--;
    if (cbit < 0) {
      cbit = 7;
      cbyte++;
      codec_frames[cbyte] = 0;
    }
    nr_cbits++;
    if (nr_cbits == codec_bits) {
      if (cbit) {
        cbyte++;
        codec_frames[cbyte] = 0;
      }
      cbit = 7;
      nr_cbits = 0;
    }
  }
  return f->n_codec_frames;
}

int freedv_rawdata_from_codec_frames(struct freedv *f, unsigned char *rawdata,
                                     unsigned char *codec_frames) {
  int cbit = 7;
  int cbyte = 0;
  int rbit = 7;
  int rbyte = 0;
  int modem_bits = freedv_get_bits_per_modem_frame(f);
  int codec_bits = freedv_get_bits_per_codec_frame(f);
  int nr_cbits = 0;
  int i;

  rawdata[rbyte] = 0;
  for (i = 0; i < modem_bits; i++) {
    rawdata[rbyte] |= ((codec_frames[cbyte] >> cbit) & 1) << rbit;

    rbit--;
    if (rbit < 0) {
      rbit = 7;
      rbyte++;
      rawdata[rbyte] = 0;
    }

    cbit--;
    if (cbit < 0) {
      cbit = 7;
      cbyte++;
    }

    nr_cbits++;
    if (nr_cbits == codec_bits) {
      if (cbit) cbyte++;
      cbit = 7;
      nr_cbits = 0;
    }
  }
  return f->n_codec_frames;
}

/*---------------------------------------------------------------------------*\

  FUNCTION....: freedv_rx
  AUTHOR......: David Rowe
  DATE CREATED: 3 August 2014

  Takes samples from the radio receiver and decodes
  them, producing a frame of decoded speech samples.  See
  demo/freedv_700d_rx.c for an example.

  demod_in[] is an array of received samples sampled at
  freedv_get_modem_sample_rate().  To account for difference in the
  transmit and receive sample clock frequencies, the number of
  demod_in[] samples is time varying. You MUST call freedv_nin()
  BEFORE EACH call to freedv_rx() and pass exactly that many samples
  to this function:

  short demod_in[freedv_get_n_max_modem_samples(f)];
  short speech_out[freedv_get_n_max_speech_samples(f)];

  nin = freedv_nin(f);  // num input samples for first read
  while(fread(demod_in, sizeof(short), nin, fin) == nin) {
      nout = freedv_rx(f, speech_out, demod_in);
      fwrite(speech_out, sizeof(short), nout, fout);
      nin = freedv_nin(f); // num input samples for next read
  }

  To help set your buffer sizes, The maximum value of freedv_nin() is
  freedv_get_n_max_modem_samples().

  freedv_rx() returns the number of output speech samples available in
  speech_out[], which is sampled at freedv_get_speech_sample_rate().
  You should ALWAYS check the return value of freedv_rx(), and read
  EXACTLY that number of speech samples from speech_out[].

  Not every call to freedv_rx will return speech samples; in some
  modes several modem frames are processed before speech samples are
  returned.  When squelch is active, zero samples may be returned.

  The peak level of demod_in[] is not critical, as the demod works
  well over a wide range of amplitude scaling.  However avoid clipping
  (overload, or samples pinned to +/- 32767).  speech_out[] will peak
  at just less than +/-32767.

  When squelch is disabled, this function echoes the demod_in[]
  samples to speech_out[].  This allows the user to listen to the
  channel, which is useful for tuning FreeDV signals or reception of
  non-FreeDV signals.

\*---------------------------------------------------------------------------*/

int freedv_rx(struct freedv *f, short speech_out[], short demod_in[]) {
  assert(f != NULL);
  int i;
  int nin = freedv_nin(f);
  f->nin_prev = nin;

  assert(nin <= f->n_max_modem_samples);

  /* FSK Rx happens in real floats, so convert to those and call their demod
   * here */
  if (FDV_MODE_ACTIVE(FREEDV_MODE_2400A, f->mode) ||
      FDV_MODE_ACTIVE(FREEDV_MODE_2400B, f->mode) ||
      FDV_MODE_ACTIVE(FREEDV_MODE_800XA, f->mode)) {
    float rx_float[f->n_max_modem_samples];
    for (i = 0; i < nin; i++) {
      rx_float[i] = ((float)demod_in[i]);
    }
    return freedv_floatrx(f, speech_out, rx_float);
  }

  if (FDV_MODE_ACTIVE(FREEDV_MODE_1600, f->mode) ||
      FDV_MODE_ACTIVE(FREEDV_MODE_700C, f->mode) ||
      FDV_MODE_ACTIVE(FREEDV_MODE_2020, f->mode) ||
      FDV_MODE_ACTIVE(FREEDV_MODE_2020B, f->mode)) {
    float gain = 1.0f;

    assert(nin <= f->n_max_modem_samples);
    COMP rx_fdm[f->n_max_modem_samples];

    for (i = 0; i < nin; i++) {
      rx_fdm[i].real = gain * (float)demod_in[i];
      rx_fdm[i].imag = 0.0f;
    }
    return freedv_comprx(f, speech_out, rx_fdm);
  }

  /* special low memory version for 700D, to help with stm32 port */
  if (FDV_MODE_ACTIVE(FREEDV_MODE_700D, f->mode) ||
      FDV_MODE_ACTIVE(FREEDV_MODE_700E, f->mode)) {
    float gain = 2.0f; /* keep levels the same as Octave simulations and C unit
                          tests for real signals */
    return freedv_shortrx(f, speech_out, demod_in, gain);
  }

  assert(1); /* should never get here */
  return 0;
}

/* complex sample input version of freedv_rx() */

int freedv_comprx(struct freedv *f, short speech_out[], COMP demod_in[]) {
  assert(f != NULL);
  assert(f->nin <= f->n_max_modem_samples);
  int rx_status = 0;
  f->nin_prev = freedv_nin(f);

  if (FDV_MODE_ACTIVE(FREEDV_MODE_1600, f->mode)) {
    rx_status = freedv_comprx_fdmdv_1600(f, demod_in);
  }
  if (FDV_MODE_ACTIVE(FREEDV_MODE_700C, f->mode)) {
    rx_status = freedv_comprx_700c(f, demod_in);
  }

  if ((FDV_MODE_ACTIVE(FREEDV_MODE_2400A, f->mode)) ||
      (FDV_MODE_ACTIVE(FREEDV_MODE_2400B, f->mode)) ||
      (FDV_MODE_ACTIVE(FREEDV_MODE_800XA, f->mode))) {
    rx_status = freedv_comprx_fsk(f, demod_in);
  }

  if (FDV_MODE_ACTIVE(FREEDV_MODE_700D, f->mode) ||
      FDV_MODE_ACTIVE(FREEDV_MODE_700E, f->mode)) {
    rx_status =
        freedv_comp_short_rx_ofdm(f, (void *)demod_in, 0, 2.0f);  // was 1.0 ??
  }

  if (FDV_MODE_ACTIVE(FREEDV_MODE_2020, f->mode) ||
      FDV_MODE_ACTIVE(FREEDV_MODE_2020B, f->mode)) {
#ifdef __LPCNET__
    rx_status = freedv_comprx_2020(f, demod_in);
#endif
  }

  short demod_in_short[f->nin_prev];

  for (int i = 0; i < f->nin_prev; i++) demod_in_short[i] = demod_in[i].real;

  return freedv_bits_to_speech(f, speech_out, demod_in_short, rx_status);
}

/* memory efficient real short version - just for 700D on the SM1000 */

int freedv_shortrx(struct freedv *f, short speech_out[], short demod_in[],
                   float gain) {
  assert(f != NULL);
  int rx_status = 0;
  f->nin_prev = f->nin;

  // At this stage short interface only supported for 700D, to help
  // memory requirements on stm32
  assert((f->mode == FREEDV_MODE_700D) || (f->mode == FREEDV_MODE_700E));
  assert(f->nin <= f->n_max_modem_samples);

  if (FDV_MODE_ACTIVE(FREEDV_MODE_700D, f->mode) ||
      FDV_MODE_ACTIVE(FREEDV_MODE_700E, f->mode)) {
    rx_status = freedv_comp_short_rx_ofdm(f, (void *)demod_in, 1, gain);
  }

  return freedv_bits_to_speech(f, speech_out, demod_in, rx_status);
}

/* helper function, unpacked bits are much easier to work with inside the modem
 */

static void codec2_decode_upacked(struct freedv *f, short speech_out[],
                                  uint8_t unpacked_bits[]) {
  int n_packed = (f->bits_per_codec_frame + 7) / 8;
  uint8_t packed_codec_bits[n_packed];

  freedv_pack(packed_codec_bits, unpacked_bits, f->bits_per_codec_frame);
  codec2_decode(f->codec2, speech_out, packed_codec_bits);
}

/*---------------------------------------------------------------------------* \

  FUNCTION....: freedv_rx_bits_to_speech
  AUTHOR......: David Rowe
  DATE CREATED: May 2020

  The *_rx functions takes off air samples, demodulates and (for some
  modes) FEC decodes, giving us a frame of bits.

  This function captures a lot of tricky logic that has been distilled
  through experience:

  There may not be a frame of bits returned on every call freedv_*rx* call.
  When there are valid bits we need to run the speech decoder.
  We may not have demod sync, so various pass through options may happen
  with the input samples.
  We may squelch based on SNR.
  Need to handle various codecs, and varying number of codec frames per modem
  frame.
  Squelch audio if test frames are being sent.
  Determine how many speech samples to return, which will vary if in
  sync/out of sync Work with real and complex inputs (complex wrapper)
  Attenuate audio on pass through.
  Deal with 700D first frame burble, and different sync states from OFDM
  modes like 700D.
  Output no samples if squelched, we assume it's OK for the audio sink to run
  dry.
  A FIFO is required on output to smooth sample flow to audio sink.
  Don't decode when we are sending test frames

\*---------------------------------------------------------------------------*/

int freedv_bits_to_speech(struct freedv *f, short speech_out[],
                          short demod_in[], int rx_status) {
  int nout = 0;
  int decode_speech = 0;
  if ((rx_status & FREEDV_RX_SYNC) == 0) {
    if (!f->squelch_en) {
      /* pass through received samples so we can hear what's going on, e.g.
       * during tuning */

      if ((f->mode == FREEDV_MODE_2020) || (f->mode == FREEDV_MODE_2020B)) {
        /* 8kHz modem sample rate but 16 kHz speech sample
           rate, so we need to resample */
        nout = 2 * f->nin_prev;
        assert(nout <= freedv_get_n_max_speech_samples(f));
        float tmp[nout];
        for (int i = 0; i < nout / 2; i++)
          f->passthrough_2020[FDMDV_OS_TAPS_16K + i] = demod_in[i];
        fdmdv_8_to_16(tmp, &f->passthrough_2020[FDMDV_OS_TAPS_16K], nout / 2);
        for (int i = 0; i < nout; i++)
          speech_out[i] = f->passthrough_gain * tmp[i];
      } else {
        /* Speech and modem rates might be different */
        int rate_factor = f->modem_sample_rate / f->speech_sample_rate;
        nout = f->nin_prev / rate_factor;
        for (int i = 0; i < nout; i++)
          speech_out[i] = f->passthrough_gain * demod_in[i * rate_factor];
      }
    }
  }

  if ((rx_status & FREEDV_RX_SYNC) && (rx_status & FREEDV_RX_BITS) &&
      !f->test_frames) {
    /* following logic is tricky so spell it out clearly, see table
       in: https://github.com/drowe67/codec2/pull/111 */

    if (!f->squelch_en) {
      decode_speech = 1;
    } else {
      /* squelch is enabled */

      /* anti-burble case - don't decode on trial sync unless the
         frame has no bit errors.  This prevents short lived trial
         sync cases generating random bursts of audio */
      if (rx_status & FREEDV_RX_TRIAL_SYNC) {
        if ((rx_status & FREEDV_RX_BIT_ERRORS) == 0) decode_speech = 1;
      } else {
        /* sync is solid - decode even through fades as there is still some
         * speech info there */
        if (f->snr_est > f->snr_squelch_thresh) decode_speech = 1;
      }
    }
  }

  if (decode_speech) {
    if (FDV_MODE_ACTIVE(FREEDV_MODE_2020, f->mode) ||
        FDV_MODE_ACTIVE(FREEDV_MODE_2020B, f->mode)) {
#ifdef __LPCNET__
      /* LPCNet decoder */

      int bits_per_codec_frame = lpcnet_bits_per_frame(f->lpcnet);
      int data_bits_per_frame = f->ldpc->data_bits_per_frame;
      int frames = data_bits_per_frame / bits_per_codec_frame;

      nout = f->n_speech_samples;
      for (int i = 0; i < frames; i++) {
        lpcnet_dec(f->lpcnet,
                   (char *)f->rx_payload_bits + i * bits_per_codec_frame,
                   speech_out);
        /* ear protection: on frames with errors and clipping, reduce level by
         * 12dB */
        if (rx_status & FREEDV_RX_BIT_ERRORS) {
          int max = 0.0;
          for (int j = 0; j < lpcnet_samples_per_frame(f->lpcnet); j++)
            if (abs(speech_out[j]) > max) max = abs(speech_out[j]);
          if (max == 32767)
            for (int j = 0; j < lpcnet_samples_per_frame(f->lpcnet); j++)
              speech_out[j] *= 0.25;
        }

        speech_out += lpcnet_samples_per_frame(f->lpcnet);
      }

#endif
    } else {
      /* codec 2 decoder */

      if (FDV_MODE_ACTIVE(FREEDV_MODE_700D, f->mode) ||
          FDV_MODE_ACTIVE(FREEDV_MODE_700E, f->mode)) {
        nout = f->n_speech_samples;
        for (int i = 0; i < f->n_codec_frames; i++) {
          codec2_decode_upacked(
              f, speech_out, f->rx_payload_bits + i * f->bits_per_codec_frame);
          speech_out += codec2_samples_per_frame(f->codec2);
        }
      } else {
        /* non-interleaved Codec 2 modes */

        nout = f->n_speech_samples;
        if ((FDV_MODE_ACTIVE(FREEDV_MODE_2400A, f->mode)) ||
            (FDV_MODE_ACTIVE(FREEDV_MODE_2400B, f->mode)))
          codec2_decode(f->codec2, speech_out, f->rx_payload_bits);
        else if (FDV_MODE_ACTIVE(FREEDV_MODE_800XA, f->mode)) {
          codec2_decode(f->codec2, &speech_out[0], &f->rx_payload_bits[0]);
          codec2_decode(f->codec2, &speech_out[320], &f->rx_payload_bits[4]);
        } else {
          for (int i = 0; i < f->n_codec_frames; i++) {
            codec2_decode_upacked(
                f, speech_out,
                f->rx_payload_bits + i * f->bits_per_codec_frame);
            speech_out += codec2_samples_per_frame(f->codec2);
          }
        }
      }
    }
  }

  if (f->verbose == 3) {
    fprintf(stderr, "    sqen: %d nout: %d decsp: %d\n", f->squelch_en, nout,
            decode_speech);
  }

  f->rx_status = rx_status;
  assert(nout <= freedv_get_n_max_speech_samples(f));
  return nout;
}

/* a way to receive raw frames of bytes, or speech data that will be
 * decompressed externally */
int freedv_rawdatarx(struct freedv *f, unsigned char *packed_payload_bits,
                     short demod_in[]) {
  assert(f != NULL);
  int nin = freedv_nin(f);
  assert(nin <= f->n_max_modem_samples);
  COMP demod_in_comp[f->n_max_modem_samples];

  for (int i = 0; i < nin; i++) {
    demod_in_comp[i].real = (float)demod_in[i];
    demod_in_comp[i].imag = 0.0;
  }

  return freedv_rawdatacomprx(f, packed_payload_bits, demod_in_comp);
}

/* a way to receive raw frames of bytes, or speech data that will be
 * decompressed externally */
int freedv_rawdatacomprx(struct freedv *f, unsigned char *packed_payload_bits,
                         COMP demod_in[]) {
  assert(f != NULL);
  int ret = 0;
  int rx_status = 0;

  /* FSK modes used packed bits internally */
  if (FDV_MODE_ACTIVE(FREEDV_MODE_2400A, f->mode) ||
      FDV_MODE_ACTIVE(FREEDV_MODE_2400B, f->mode) ||
      FDV_MODE_ACTIVE(FREEDV_MODE_800XA, f->mode)) {
    rx_status = freedv_comprx_fsk(f, demod_in);
    f->rx_status = rx_status;
    if (rx_status & FREEDV_RX_BITS) {
      ret = (freedv_get_bits_per_modem_frame(f) + 7) / 8;
      freedv_rawdata_from_codec_frames(f, packed_payload_bits,
                                       f->rx_payload_bits);
    }
    return ret;
  }

  if (FDV_MODE_ACTIVE(FREEDV_MODE_1600, f->mode))
    rx_status = freedv_comprx_fdmdv_1600(f, demod_in);
  if (FDV_MODE_ACTIVE(FREEDV_MODE_700C, f->mode))
    rx_status = freedv_comprx_700c(f, demod_in);
  if (FDV_MODE_ACTIVE(FREEDV_MODE_700D, f->mode) ||
      FDV_MODE_ACTIVE(FREEDV_MODE_DATAC0, f->mode) ||
      FDV_MODE_ACTIVE(FREEDV_MODE_DATAC1, f->mode) ||
      FDV_MODE_ACTIVE(FREEDV_MODE_DATAC3, f->mode) ||
      FDV_MODE_ACTIVE(FREEDV_MODE_DATAC4, f->mode) ||
      FDV_MODE_ACTIVE(FREEDV_MODE_DATAC13, f->mode))
    rx_status = freedv_comp_short_rx_ofdm(f, (void *)demod_in, 0, 1.0f);
  if (FDV_MODE_ACTIVE(FREEDV_MODE_FSK_LDPC, f->mode)) {
    rx_status = freedv_rx_fsk_ldpc_data(f, demod_in);
  }

  if (rx_status & FREEDV_RX_BITS) {
    ret = (f->bits_per_modem_frame + 7) / 8;
    freedv_pack(packed_payload_bits, f->rx_payload_bits,
                f->bits_per_modem_frame);
  }

  /* might want to check this for errors, e.g. if reliable data is important */
  f->rx_status = rx_status;

  return ret;
}

/*---------------------------------------------------------------------------* \

  FUNCTION....: freedv_get_version
  AUTHOR......: Jim Ahlstrom
  DATE CREATED: 28 July 2015

  Return the version of the FreeDV API.  This is meant to help API
  users determine when incompatible changes have occurred.

\*---------------------------------------------------------------------------*/

int freedv_get_version(void) { return VERSION; }

/*---------------------------------------------------------------------------* \

  FUNCTION....: freedv_get_hash
  AUTHOR......: David Rowe
  DATE CREATED: July 2020

  Return the a string with the Git hash of the repo used to build this code.

\*---------------------------------------------------------------------------*/

static char git_hash[] = GIT_HASH;
char *freedv_get_hash(void) { return git_hash; }

/*---------------------------------------------------------------------------*\

  FUNCTION....: freedv_set_callback_txt
  AUTHOR......: Jim Ahlstrom
  DATE CREATED: 28 July 2015

  Set the callback functions and the callback state pointer that will
  be used for the aux txt channel.  The freedv_callback_rx is a
  function pointer that will be called to return received characters.
  The freedv_callback_tx is a function pointer that will be called to
  send transmitted characters.  The callback state is a user-defined
  void pointer that will be passed to the callback functions.  Any or
  all can be NULL, and the default is all NULL.

  The function signatures are:
    void receive_char(void *callback_state, char c);
    char transmit_char(void *callback_state);

\*---------------------------------------------------------------------------*/

void freedv_set_callback_txt(struct freedv *f, freedv_callback_rx rx,
                             freedv_callback_tx tx, void *state) {
  if (FDV_MODE_ACTIVE(FREEDV_MODE_800XA, f->mode) == false) {
    f->freedv_put_next_rx_char = rx;
    f->freedv_get_next_tx_char = tx;
    f->callback_state = state;
  }
}

/*---------------------------------------------------------------------------*\

  FUNCTION....: freedv_set_callback_txt_sym
  AUTHOR......: Mooneer Salem
  DATE CREATED: 19 August 2021

  Set the callback functions and the callback state pointer that will
  be used to provide the raw symbols for the aux txt channel. The
  freedv_callback_rx_sym is a function pointer that will be called to
  return received symbols. The callback state is a user-defined
  void pointer that will be passed to the callback function.  Any or
  all can be NULL, and the default is all NULL.

  The function signature is:
    void receive_sym(void *callback_state, COMP sym, COMP amp);

  Note: Active for OFDM modes only (700D/E, 2020).
\*---------------------------------------------------------------------------*/

void freedv_set_callback_txt_sym(struct freedv *f, freedv_callback_rx_sym rx,
                                 void *state) {
  if (FDV_MODE_ACTIVE(FREEDV_MODE_700D, f->mode) ||
      FDV_MODE_ACTIVE(FREEDV_MODE_700E, f->mode) ||
      FDV_MODE_ACTIVE(FREEDV_MODE_2020, f->mode) ||
      FDV_MODE_ACTIVE(FREEDV_MODE_2020B, f->mode)) {
    f->freedv_put_next_rx_symbol = rx;
    f->callback_state_sym = state;
  }
}

/*---------------------------------------------------------------------------*\

  FUNCTION....: freedv_set_callback_protocol
  AUTHOR......: Brady OBrien
  DATE CREATED: 21 February 2016

  VHF packet data function.

  Set the callback functions and callback pointer that will be used
  for the protocol data channel. freedv_callback_protorx will be
  called when a frame containing protocol data
  arrives. freedv_callback_prototx will be called when a frame
  containing protocol information is being generated. Protocol
  information is intended to be used to develop protocols and fancy
  features atop VHF freedv, much like those present in DMR.  Protocol
  bits are to be passed in an msb-first char array The number of
  protocol bits are findable with freedv_get_protocol_bits

\*---------------------------------------------------------------------------*/

void freedv_set_callback_protocol(struct freedv *f, freedv_callback_protorx rx,
                                  freedv_callback_prototx tx,
                                  void *callback_state) {
  if (FDV_MODE_ACTIVE(FREEDV_MODE_800XA, f->mode) == false) {
    f->freedv_put_next_proto = rx;
    f->freedv_get_next_proto = tx;
    f->proto_callback_state = callback_state;
  }
}

/*---------------------------------------------------------------------------*\

  FUNCTION....: freedv_set_callback_datarx / freedv_set_callback_datatx
  AUTHOR......: Jeroen Vreeken
  DATE CREATED: 04 March 2016

  VHF packet data function.

  Set the callback functions and callback pointer that will be used
  for the data channel. freedv_callback_datarx will be called when a
  packet has been successfully received. freedv_callback_data_tx will
  be called when transmission of a new packet can begin.  If the
  returned size of the datatx callback is zero the data frame is still
  generated, but will contain only a header update.

\*---------------------------------------------------------------------------*/

void freedv_set_callback_data(struct freedv *f, freedv_callback_datarx datarx,
                              freedv_callback_datatx datatx,
                              void *callback_state) {
  if ((FDV_MODE_ACTIVE(FREEDV_MODE_2400A, f->mode)) ||
      (FDV_MODE_ACTIVE(FREEDV_MODE_2400B, f->mode)) ||
      (FDV_MODE_ACTIVE(FREEDV_MODE_800XA, f->mode))) {
    if (!f->deframer->fdc) f->deframer->fdc = freedv_data_channel_create();
    if (!f->deframer->fdc) return;

    freedv_data_set_cb_rx(f->deframer->fdc, datarx, callback_state);
    freedv_data_set_cb_tx(f->deframer->fdc, datatx, callback_state);
  }
}

/*---------------------------------------------------------------------------*\

  FUNCTION....: freedv_set_data_header
  AUTHOR......: Jeroen Vreeken
  DATE CREATED: 04 March 2016

  VHF packet data function.

  Set the data header for the data channel.  Header compression will
  be used whenever packets from this header are sent.  The header will
  also be used for fill packets when a data frame is requested without
  a packet available.

\*---------------------------------------------------------------------------*/

void freedv_set_data_header(struct freedv *f, unsigned char *header) {
  if ((FDV_MODE_ACTIVE(FREEDV_MODE_2400A, f->mode)) ||
      (FDV_MODE_ACTIVE(FREEDV_MODE_2400B, f->mode)) ||
      (FDV_MODE_ACTIVE(FREEDV_MODE_800XA, f->mode))) {
    if (!f->deframer->fdc) f->deframer->fdc = freedv_data_channel_create();
    if (!f->deframer->fdc) return;

    freedv_data_set_header(f->deframer->fdc, header);
  }
}

/*---------------------------------------------------------------------------*\

  FUNCTION....: freedv_get_modem_stats
  AUTHOR......: Jim Ahlstrom
  DATE CREATED: 28 July 2015

  Return data from the modem.  The arguments are pointers to the data
  items.  The pointers can be NULL if the data item is not wanted.

\*---------------------------------------------------------------------------*/

void freedv_get_modem_stats(struct freedv *f, int *sync, float *snr_est) {
  if (FDV_MODE_ACTIVE(FREEDV_MODE_1600, f->mode))
    fdmdv_get_demod_stats(f->fdmdv, &f->stats);
  if (FDV_MODE_ACTIVE(FREEDV_MODE_700C, f->mode))
    cohpsk_get_demod_stats(f->cohpsk, &f->stats);
  if (sync) *sync = f->sync;
  if (snr_est) *snr_est = f->snr_est;
}

/*---------------------------------------------------------------------------*\

  FUNCTIONS...: freedv_set_*
  AUTHOR......: Jim Ahlstrom
  DATE CREATED: 28 July 2015

  Set some parameters used by FreeDV.  It is possible to write a macro
  using ## for this, but I wasn't sure it would be 100% portable.

\*---------------------------------------------------------------------------*/

void freedv_set_test_frames(struct freedv *f, int val) { f->test_frames = val; }
void freedv_set_test_frames_diversity(struct freedv *f, int val) {
  f->test_frames_diversity = val;
}
void freedv_set_squelch_en(struct freedv *f, bool val) { f->squelch_en = val; }
void freedv_set_total_bit_errors(struct freedv *f, int val) {
  f->total_bit_errors = val;
}
void freedv_set_total_bits(struct freedv *f, int val) { f->total_bits = val; }
void freedv_set_total_bit_errors_coded(struct freedv *f, int val) {
  f->total_bit_errors_coded = val;
}
void freedv_set_total_bits_coded(struct freedv *f, int val) {
  f->total_bits_coded = val;
}
void freedv_set_total_packet_errors(struct freedv *f, int val) {
  f->total_packet_errors = val;
}
void freedv_set_total_packets(struct freedv *f, int val) {
  f->total_packets = val;
}
void freedv_set_varicode_code_num(struct freedv *f, int val) {
  varicode_set_code_num(&f->varicode_dec_states, val);
}
void freedv_set_ext_vco(struct freedv *f, int val) { f->ext_vco = val; }
void freedv_set_snr_squelch_thresh(struct freedv *f, float val) {
  f->snr_squelch_thresh = val;
}
void freedv_set_tx_amp(struct freedv *f, float amp) { f->tx_amp = amp; }
void freedv_passthrough_gain(struct freedv *f, float g) {
  f->passthrough_gain = g;
}

/* supported by 700C, 700D, 700E */

void freedv_set_clip(struct freedv *f, bool val) {
  f->clip_en = val;
  if (is_ofdm_mode(f)) {
    f->ofdm->clip_en = val;
    /* really should have BPF if we clip */
    if (val) ofdm_set_tx_bpf(f->ofdm, true);
  }
}

/* Band Pass Filter to cleanup OFDM tx waveform, only supported by some modes */

void freedv_set_tx_bpf(struct freedv *f, int val) {
  if (is_ofdm_mode(f)) {
    ofdm_set_tx_bpf(f->ofdm, val);
  }
}

void freedv_set_phase_est_bandwidth_mode(struct freedv *f, int val) {
  if (FDV_MODE_ACTIVE(FREEDV_MODE_700D, f->mode) ||
      FDV_MODE_ACTIVE(FREEDV_MODE_2020, f->mode)) {
    ofdm_set_phase_est_bandwidth_mode(f->ofdm, val);
  }
}

// For those FreeDV modes using the codec 2 700C vocoder 700C/D/E/800XA
void freedv_set_eq(struct freedv *f, bool val) {
  if (f->codec2 != NULL) {
    codec2_700c_eq(f->codec2, val);
  }
}

void freedv_set_verbose(struct freedv *f, int verbosity) {
  f->verbose = verbosity;
  if (FDV_MODE_ACTIVE(FREEDV_MODE_700C, f->mode)) {
    cohpsk_set_verbose(f->cohpsk, f->verbose);
  }
  if (is_ofdm_mode(f)) {
    ofdm_set_verbose(f->ofdm, f->verbose - 1);
  }
}

void freedv_set_callback_error_pattern(struct freedv *f,
                                       freedv_calback_error_pattern cb,
                                       void *state) {
  f->freedv_put_error_pattern = cb;
  f->error_pattern_callback_state = state;
}

void freedv_set_carrier_ampl(struct freedv *f, int c, float ampl) {
  if (FDV_MODE_ACTIVE(FREEDV_MODE_700C, f->mode)) {
    cohpsk_set_carrier_ampl(f->cohpsk, c, ampl);
  }
}

/*---------------------------------------------------------------------------* \

  FUNCTIONS...: freedv_set_sync
  AUTHOR......: David Rowe
  DATE CREATED: May 2018

  Extended control of sync state machines for OFDM modes.

  Ensure this is called in the same thread as freedv_rx().

\*---------------------------------------------------------------------------*/

void freedv_set_sync(struct freedv *freedv, int sync_cmd) {
  assert(freedv != NULL);

  if (freedv->ofdm != NULL) {
    ofdm_set_sync(freedv->ofdm, sync_cmd);
  }
}

// this also selects burst mode
void freedv_set_frames_per_burst(struct freedv *freedv, int framesperburst) {
  assert(freedv != NULL);
  if (freedv->ofdm != NULL) {
    // change of nomenclature as we cross into the OFDM modem layer. In the
    // OFDM modem we have packets that contain multiple "modem frames"
    ofdm_set_packets_per_burst(freedv->ofdm, framesperburst);
  }
}

struct FSK *freedv_get_fsk(struct freedv *f) {
  return f->fsk;
}

/*---------------------------------------------------------------------------*\

  FUNCTIONS...: freedv_get_*
  AUTHOR......: Jim Ahlstrom
  DATE CREATED: 28 July 2015

  Get some parameters from FreeDV.

\*---------------------------------------------------------------------------*/

int freedv_get_protocol_bits(struct freedv *f) { return f->n_protocol_bits; }
int freedv_get_mode(struct freedv *f) { return f->mode; }
int freedv_get_test_frames(struct freedv *f) { return f->test_frames; }
int freedv_get_speech_sample_rate(struct freedv *f) {
  return f->speech_sample_rate;
}
int freedv_get_n_speech_samples(struct freedv *f) {
  return f->n_speech_samples;
}
int freedv_get_modem_sample_rate(struct freedv *f) {
  return f->modem_sample_rate;
}
int freedv_get_modem_symbol_rate(struct freedv *f) {
  return f->modem_symbol_rate;
}
int freedv_get_n_max_modem_samples(struct freedv *f) {
  return f->n_max_modem_samples;
}
int freedv_get_n_nom_modem_samples(struct freedv *f) {
  return f->n_nom_modem_samples;
}
int freedv_get_n_tx_modem_samples(struct freedv *f) {
  return f->n_nat_modem_samples;
}
int freedv_get_total_bits(struct freedv *f) { return f->total_bits; }
int freedv_get_total_bit_errors(struct freedv *f) {
  return f->total_bit_errors;
}
int freedv_get_total_bits_coded(struct freedv *f) {
  return f->total_bits_coded;
}
int freedv_get_total_bit_errors_coded(struct freedv *f) {
  return f->total_bit_errors_coded;
}
int freedv_get_total_packets(struct freedv *f) { return f->total_packets; }
int freedv_get_total_packet_errors(struct freedv *f) {
  return f->total_packet_errors;
}
int freedv_get_sync(struct freedv *f) { return f->sync; }
struct CODEC2 *freedv_get_codec2(struct freedv *f) {
  return f->codec2;
}
int freedv_get_bits_per_codec_frame(struct freedv *f) {
  return f->bits_per_codec_frame;
}
int freedv_get_bits_per_modem_frame(struct freedv *f) {
  return f->bits_per_modem_frame;
}
int freedv_get_rx_status(struct freedv *f) { return f->rx_status; }
void freedv_get_fsk_S_and_N(struct freedv *f, float *S, float *N) {
  *S = f->fsk_S[0];
  *N = f->fsk_N[0];
}

/*---------------------------------------------------------------------------*\

  FUNCTIONS...: freedv_set_tuning_range
  AUTHOR......: Simon Lang - DJ2LS
  DATE CREATED: 18 feb 2022
  DEFAULT.....: fmin: -50.0Hz fmax: 50.0Hz
  DESCRIPTION.:

  |<---fmin - | rx centre frequency | + fmax--->|

  Useful for handling frequency offsets,
  e.g. caused by an imprecise VFO, the trade off is more CPU power is required.

\*---------------------------------------------------------------------------*/
int freedv_set_tuning_range(struct freedv *freedv, float val_fmin,
                            float val_fmax) {
  if (is_ofdm_data_mode(freedv) &&
      (strcmp(freedv->ofdm->data_mode, "burst") == 0)) {
    freedv->ofdm->fmin = val_fmin;
    freedv->ofdm->fmax = val_fmax;
    return 1;
  } else {
    return 0;
  }
}

int freedv_get_n_max_speech_samples(struct freedv *f) {
  /* When "passing through" demod samples to the speech output
     (e.g. no sync and squelch off) f->nin bounces around with
     timing variations.  So we may return
     freedv_get_n_max_modem_samples() via the speech_output[]
     array */
  int max_output_passthrough_samples;
  if (FDV_MODE_ACTIVE(FREEDV_MODE_2020, f->mode) ||
      FDV_MODE_ACTIVE(FREEDV_MODE_2020B, f->mode))
    // In 2020 we oversample the input modem samples from 8->16 kHz
    max_output_passthrough_samples = 2 * freedv_get_n_max_modem_samples(f);
  else
    max_output_passthrough_samples = freedv_get_n_max_modem_samples(f);

  if (max_output_passthrough_samples > f->n_speech_samples)
    return max_output_passthrough_samples;
  else
    return f->n_speech_samples;
}

// Now dummy obsolete call
int freedv_get_sync_interleaver(struct freedv *f) { return 1; }

int freedv_get_sz_error_pattern(struct freedv *f) {
  if (FDV_MODE_ACTIVE(FREEDV_MODE_700C, f->mode)) {
    /* if diversity disabled callback sends error pattern for upper and lower
     * carriers */
    return f->sz_error_pattern * (2 - f->test_frames_diversity);
  } else {
    return f->sz_error_pattern;
  }
}

// Get modem status, scatter/eye diagram for plotting, other goodies
void freedv_get_modem_extended_stats(struct freedv *f,
                                     struct MODEM_STATS *stats) {
  if (FDV_MODE_ACTIVE(FREEDV_MODE_1600, f->mode))
    fdmdv_get_demod_stats(f->fdmdv, stats);

  if (FDV_MODE_ACTIVE(FREEDV_MODE_2400A, f->mode) ||
      FDV_MODE_ACTIVE(FREEDV_MODE_800XA, f->mode)) {
    fsk_get_demod_stats(f->fsk,
                        stats); /* eye diagram samples, clock offset etc */
    stats->snr_est =
        f->snr_est; /* estimated when fsk_demod() called in freedv_fsk.c */
    stats->sync = f->sync; /* sync indicator comes from framing layer */
  }

  if (FDV_MODE_ACTIVE(FREEDV_MODE_2400B, f->mode)) {
    fmfsk_get_demod_stats(f->fmfsk, stats);
    stats->snr_est = f->snr_est;
    stats->sync = f->sync;
  }

  if (FDV_MODE_ACTIVE(FREEDV_MODE_700C, f->mode)) {
    cohpsk_get_demod_stats(f->cohpsk, stats);
  }

  if (is_ofdm_mode(f)) {
    // OFDM modem stats updated when demod runs, so copy last update
    // We need to avoid over writing the FFT states which are updated by a
    // different function
    // TODO we need a better design here: Issue #182
#ifndef __EMBEDDED__
    size_t ncopy = (void *)stats->rx_eye - (void *)stats;
    memcpy(stats, &f->stats, ncopy);
#endif
    stats->snr_est = f->snr_est;
    stats->sync = f->sync;
  }
}

int freedv_get_n_tx_preamble_modem_samples(struct freedv *f) {
  if (f->mode == FREEDV_MODE_FSK_LDPC) {
    struct FSK *fsk = f->fsk;
    int npreamble_symbols = 50 * (fsk->mode >> 1);
    return fsk->Ts * npreamble_symbols;
  } else if (is_ofdm_data_mode(f)) {
    return f->ofdm->samplesperframe;
  }

  return 0;
}

int freedv_get_n_tx_postamble_modem_samples(struct freedv *f) {
  if (is_ofdm_data_mode(f)) {
    return f->ofdm->samplesperframe;
  }

  return 0;
}

// from
// http://stackoverflow.com/questions/10564491/function-to-calculate-a-crc16-checksum

unsigned short freedv_gen_crc16(unsigned char *data_p, int length) {
  unsigned char x;
  unsigned short crc = 0xFFFF;

  while (length--) {
    x = crc >> 8 ^ *data_p++;
    x ^= x >> 4;
    crc = (crc << 8) ^ ((unsigned short)(x << 12)) ^
          ((unsigned short)(x << 5)) ^ ((unsigned short)x);
  }

  return crc;
}