forked from 0815employee/codec2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
freedv_api.c
1639 lines (1356 loc) · 55.3 KB
/
freedv_api.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
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
/*---------------------------------------------------------------------------*\
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;
VLA_CALLOC(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);
VLA_FREE(packed_codec_bits);
}
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);
VLA_CALLOC(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;
}
VLA_FREE(tx_fdm);
}
/* 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;
VLA_CALLOC(uint8_t, packed_bytes, nbytes);
freedv_pack(packed_bytes, unpacked_bits, nbits);
unsigned short ret = freedv_gen_crc16(packed_bytes, nbytes);
VLA_FREE(packed_bytes);
return ret;
}
/* 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;
VLA_CALLOC(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);
VLA_FREE(packed_bytes);
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);
VLA_CALLOC(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;
VLA_FREE(mod_out_comp);
}
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;
COMP *tx_preamble = (COMP *)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);
VLA_CALLOC(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;
VLA_FREE(mod_out_comp);
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;
COMP *tx_postamble = (COMP *)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);
VLA_CALLOC(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;
VLA_FREE(mod_out_comp);
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)) {
VLA_CALLOC(float, rx_float, f->n_max_modem_samples);
for (i = 0; i < nin; i++) {
rx_float[i] = ((float)demod_in[i]);
}
int ret = freedv_floatrx(f, speech_out, rx_float);
VLA_FREE(rx_float);
return ret;
}
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);
VLA_CALLOC(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;
}
int ret = freedv_comprx(f, speech_out, rx_fdm);
VLA_FREE(rx_fdm);
return ret;
}
/* 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
}
VLA_CALLOC(short, demod_in_short, f->nin_prev);
for (int i = 0; i < f->nin_prev; i++) demod_in_short[i] = demod_in[i].real;
int ret = freedv_bits_to_speech(f, speech_out, demod_in_short, rx_status);
VLA_FREE(demod_in_short);
return ret;
}
/* 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;
VLA_CALLOC(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);
VLA_FREE(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));
VLA_CALLOC(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];
VLA_FREE(tmp);
} 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 {