File: tfreedv_800XA_rawdata.c

package info (click to toggle)
codec2 1.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 121,056 kB
  • sloc: ansic: 414,118; sh: 2,612; objc: 2,574; python: 2,105; cpp: 2,091; asm: 683; makefile: 598
file content (147 lines) | stat: -rw-r--r-- 4,504 bytes parent folder | download
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
/*---------------------------------------------------------------------------*\

  FILE........: tfreedv_800XA_rawdata.c
  AUTHOR......: Jeroen Vreeken
  DATE CREATED: 24 May 2020

  FreeDV 800XA rawdata test.

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

/*
  Copyright (C) 2020 Jeroen Vreeken <jeroen@vreeken.net>

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library 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
  Lesser 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 <stdio.h>
#include "freedv_api.h"
#include "assert.h"

int main(int argc, char **argv)
{
    struct freedv *f;
    int i;

    printf("freedv_api tests for mode 800XA\n");

    printf("freedv_open(FREEDV_MODE_800XA) ");
    f = freedv_open(FREEDV_MODE_800XA);
    assert(f != NULL);
    printf("Passed\n");

    printf("freedv_get_mode() ");
    int mode = freedv_get_mode(f);
    assert(mode == FREEDV_MODE_800XA);
    printf("Passed\n");

    printf("freedv_get_n_max_modem_samples() ");
    int max_samples = freedv_get_n_max_modem_samples(f);
    assert(max_samples == 660);
    printf("%d Passed\n", max_samples);

    printf("freedv_get_n_nom_modem_samples() ");
    int nom_samples = freedv_get_n_nom_modem_samples(f);
    assert(nom_samples == 640);
    printf("%d Passed\n", nom_samples);

    printf("freedv_get_n_speech_samples() ");
    int speech_samples = freedv_get_n_speech_samples(f);
    assert(speech_samples == 640);
    printf("%d Passed\n", speech_samples);

    printf("freedv_get_n_bits_per_codec_frame() ");
    int codec_bits = freedv_get_bits_per_codec_frame(f);
    assert(codec_bits == 28);
    printf("%d Passed\n", codec_bits);

    printf("freedv_get_n_bits_per_modem_frame() ");
    int frame_bits = freedv_get_bits_per_modem_frame(f);
    assert(frame_bits == 56);
    printf("%d Passed\n", frame_bits);

    /* Note: A codec frame is only 3.5 bytes!
       so the fourth and eight bytes will be half empty!
    */
    unsigned char payload[8] = { 0x12, 0x34, 0x56, 0x70, 0x89, 0xab, 0xcd, 0xe0 };
    unsigned char payload_tx[7] = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde };

    printf("freedv_codec_frames_from_rawdata() ");
    unsigned char codec_frames[8] = { 0 };
    freedv_codec_frames_from_rawdata(f, codec_frames, payload_tx);
    int fails = 0;
    for (i = 0; i < 8; i++) {
	if (codec_frames[i] != payload[i]) {
	    printf("byte %d: 0x%02x does not match expected 0x%02x\n", i, codec_frames[i], payload[i]);
	    fails++;
	}    
    }
    if (fails)
    	goto fail;
    printf("Passed\n");
    
    printf("freedv_rawdata_from_codec_frames() ");
    unsigned char rawdata[7] = { 0 };
    freedv_rawdata_from_codec_frames(f, rawdata, payload);
    fails = 0;
    for (i = 0; i < 7; i++) {
	if (rawdata[i] != payload_tx[i]) {
	    printf("byte %d: 0x%02x does not match expected 0x%02x\n", i, rawdata[i], payload_tx[i]);
	    fails++;
	}
    }
    if (fails)
    	goto fail;
    printf("Passed\n");

    printf("freedv_rawdatatx()/freedv_rawdatarx() ");
    int frames = 0;
    fails = 0;
    {
        short mod[nom_samples * 10];
        for (i = 0; i < 10; i ++) {
	    freedv_rawdatatx(f, mod + i * nom_samples, payload_tx);
        }
        int nin = 0;
        for (i = 0; i < nom_samples * 9; i += nin) {
            nin = freedv_nin(f);
	    unsigned char payload_rx[8] = {0};
            int r = freedv_rawdatarx(f, payload_rx, mod + i);
            if (r == 7) {
	        int b;
                for (b = 0; b < 7; b++) {
	    	    if (payload_tx[b] != payload_rx[b]) {
		        printf("Received codec bits 0x%02x do not match expected 0x%02x\n", payload_rx[b], payload_tx[b]);
		        fails++;
                    }
                }
	        frames++;
	    }
        }
    }
    if (!frames) {
    	printf("Did not decode any frames successfully\n");
	goto fail;
    }
    if (fails)
    	goto fail;
    printf("Passed\n");

    printf("Tests passed\n");
    return 0;
fail:
    printf("Test failed\n");
    return 1;
}