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
|
/*---------------------------------------------------------------------------*\
FILE........: freedv_mixed_rx.c
AUTHOR......: Jeroen Vreeken & David Rowe
DATE CREATED: May 2020
Demo receive program for FreeDV API that demonstrates shows mixed
VHF packet data and speech frames.
\*---------------------------------------------------------------------------*/
/*
Copyright (C) 2014 David Rowe
All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 2.1, as
published by the Free Software Foundation. This program is
distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "codec2.h"
#include "freedv_api.h"
#include "modem_stats.h"
struct my_callback_state {
int calls;
FILE *ftxt;
};
/* Called when a packet has been received */
void my_datarx(void *callback_state, unsigned char *packet, size_t size) {
struct my_callback_state *pstate = (struct my_callback_state *)callback_state;
pstate->calls++;
if (pstate->ftxt != NULL) {
size_t i;
fprintf(pstate->ftxt, "data (%zd bytes): ", size);
for (i = 0; i < size; i++) {
fprintf(pstate->ftxt, "0x%02x ", packet[i]);
}
fprintf(pstate->ftxt, "\n");
}
}
/* Called when a new packet can be send */
void my_datatx(void *callback_state, unsigned char *packet, size_t *size) {
/* This should not happen while receiving.. */
fprintf(stderr, "datarx callback called, this should not happen!\n");
*size = 0;
}
int main(int argc, char *argv[]) {
FILE *fin, *fout, *ftxt;
struct freedv *freedv;
int nin, nout, nout_total = 0, frame = 0;
struct my_callback_state my_cb_state = {0};
int mode;
int use_codecrx, verbose;
struct CODEC2 *c2 = NULL;
int i;
if (argc < 4) {
printf(
"usage: %s 2400A|2400B|800XA InputModemSpeechFile OutputSpeechRawFile\n"
" [--codecrx] [-v]\n",
argv[0]);
printf("e.g %s 2400A hts1a_fdmdv.raw hts1a_out.raw\n", argv[0]);
exit(1);
}
mode = -1;
if (!strcmp(argv[1], "2400A")) mode = FREEDV_MODE_2400A;
if (!strcmp(argv[1], "2400B")) mode = FREEDV_MODE_2400B;
if (!strcmp(argv[1], "800XA")) mode = FREEDV_MODE_800XA;
assert(mode != -1);
if (strcmp(argv[2], "-") == 0)
fin = stdin;
else if ((fin = fopen(argv[2], "rb")) == NULL) {
fprintf(stderr, "Error opening input raw modem sample file: %s: %s.\n",
argv[2], strerror(errno));
exit(1);
}
if (strcmp(argv[3], "-") == 0)
fout = stdout;
else if ((fout = fopen(argv[3], "wb")) == NULL) {
fprintf(stderr, "Error opening output speech sample file: %s: %s.\n",
argv[3], strerror(errno));
exit(1);
}
use_codecrx = 0;
verbose = 0;
if (argc > 4) {
for (i = 4; i < argc; i++) {
if (strcmp(argv[i], "--codecrx") == 0) {
int c2_mode;
if ((mode == FREEDV_MODE_700C) || (mode == FREEDV_MODE_700D) ||
(mode == FREEDV_MODE_800XA)) {
c2_mode = CODEC2_MODE_700C;
} else {
c2_mode = CODEC2_MODE_1300;
}
use_codecrx = 1;
c2 = codec2_create(c2_mode);
assert(c2 != NULL);
}
if (strcmp(argv[i], "-v") == 0) {
verbose = 1;
}
if (strcmp(argv[i], "-vv") == 0) {
verbose = 2;
}
}
}
freedv = freedv_open(mode);
assert(freedv != NULL);
freedv_set_verbose(freedv, verbose);
short speech_out[freedv_get_n_max_speech_samples(freedv)];
short demod_in[freedv_get_n_max_modem_samples(freedv)];
ftxt = fopen("freedv_rx_log.txt", "wt");
assert(ftxt != NULL);
my_cb_state.ftxt = ftxt;
freedv_set_callback_data(freedv, my_datarx, my_datatx, &my_cb_state);
/* Note we need to work out how many samples demod needs on each
call (nin). This is used to adjust for differences in the tx and rx
sample clock frequencies. Note also the number of output
speech samples is time varying (nout). */
nin = freedv_nin(freedv);
while (fread(demod_in, sizeof(short), nin, fin) == nin) {
frame++;
if (use_codecrx == 0) {
/* usual case: use the freedv_api to do everything: speech decoding,
* demodulating */
nout = freedv_rx(freedv, speech_out, demod_in);
} else {
/* demo of codecrx mode - separate demodulation and speech decoding */
int bits_per_codec_frame = freedv_get_bits_per_codec_frame(freedv);
int bits_per_modem_frame = freedv_get_bits_per_modem_frame(freedv);
int bytes_per_codec_frame = (bits_per_codec_frame + 7) / 8;
int bytes_per_modem_frame = (bits_per_modem_frame + 7) / 8;
int codec_frames = bits_per_modem_frame / bits_per_codec_frame;
int samples_per_frame = codec2_samples_per_frame(c2);
unsigned char encoded[bytes_per_codec_frame * codec_frames];
unsigned char rawdata[bytes_per_modem_frame];
nout = 0;
/* Use the freedv_api to demodulate only */
int ncodec = freedv_rawdatarx(freedv, rawdata, demod_in);
freedv_codec_frames_from_rawdata(freedv, encoded, rawdata);
/* decode the speech ourself (or send it to elsewhere, e.g. network) */
if (ncodec) {
unsigned char *enc_frame = encoded;
short *speech_frame = speech_out;
for (i = 0; i < codec_frames; i++) {
codec2_decode(c2, speech_frame, enc_frame);
enc_frame += bytes_per_codec_frame;
speech_frame += samples_per_frame;
nout += samples_per_frame;
}
}
}
fprintf(ftxt, "Demod of %d samples resulted %d speech samples\n", nin,
nout);
if (nout == 0) {
/* We did not get any audio.
This means the modem is (probably) synced, but a data frame was
received Fill in the 'blanks' use by data frames with silence
*/
nout = freedv_get_n_speech_samples(freedv);
memset(speech_out, 0, nout * sizeof(short));
}
nin = freedv_nin(freedv);
fwrite(speech_out, sizeof(short), nout, fout);
nout_total += nout;
/* if this is in a pipeline, we probably don't want the usual
buffering to occur */
if (fout == stdout) fflush(stdout);
}
fclose(ftxt);
fclose(fin);
fclose(fout);
fprintf(stderr,
"frames decoded: %d output speech samples: %d, data packets: %d\n",
frame, nout_total, my_cb_state.calls);
freedv_close(freedv);
return 0;
}
|