File: test_convert.c

package info (click to toggle)
rat 4.2.22-2.2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,896 kB
  • ctags: 3,717
  • sloc: ansic: 36,542; tcl: 2,740; sh: 2,675; makefile: 295
file content (218 lines) | stat: -rw-r--r-- 5,971 bytes parent folder | download | duplicates (5)
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

/* test_convert [-list] -c <n> -ifmt <fmt1> -ofmt <fmt2> */
/* Where :
   -list  lists converters.
   -c <n> selects converter n 
   and <fmt> has form rate=8000,channels=1
 */

#include "config_unix.h"
#include "config_win32.h"
#include "debug.h"
#include "memory.h"
#include "util.h"
#include "audio_types.h"
#include "codec_types.h"
#include "codec.h"
#include "converter_types.h"
#include "converter.h"

static int
list_converters()
{
        const converter_details_t *cd;
        int i, n;
        n = converter_get_count();
        for(i = 0; i < n; i++) {
                cd = converter_get_details(i);
                printf("%d %s\n", i, cd->name);
        }
        fflush(stdout);
        exit(-1);
}

static void
usage()
{
	printf("test_convert [-list] -c <n> -ifmt <fmt> -ofmt <ofmt>\n"\
	       "where:\n\t-list\t lists converters.\n" \
	       "\t-c <n> \t selects converter n\n" \
	       "\tand formats are of form \"rate=8000,channels=1\n");

        exit(-1);
}

static char* 
get_token(char *name, char *src, int src_len)
{
        char *cur;
        int cur_len, done;

        done = 0;
        cur  = src;
        while (done < src_len) {
                cur_len = strlen(cur);
                if (!strcmp(cur, name) && done + cur_len + 1 < src_len) {
                        return cur + cur_len + 1;
                }
                cur += cur_len + 1;
        }
        return NULL;
}

static int
parse_fmt(char *fmt, uint16_t *channels, uint16_t *rate)
{
        char *f;
        int fmtlen;
        int found = 0;

        if (fmt == NULL) {
                return FALSE;
        }

        fmtlen = strlen(fmt);
        f = fmt;
        while(*f) {
                if (*f ==',' || *f == '=') {
                        *f = 0;
                }
                f++;
        }

        f = get_token("rate", fmt, fmtlen);
        if (f != NULL) {
                *rate = (uint16_t)(atoi(f));
                found++;
        }

        f = get_token("channels", fmt, fmtlen);
        if (f != NULL) {
                *channels = (uint16_t)(atoi(f));
                found++;
        }

        return (found==2);
}

static void
dump_frame(coded_unit *cu, int offset)
{
        uint16_t rate, channels;
        sample *src;
        int i, j, n;
        codec_get_native_info( cu->id, &rate, &channels);

        src = (sample*)cu->data;

        n = cu->data_len / (sizeof(sample) * channels);
        for (i = 0; i < n; i++) {
                printf("%.4f ", (double)(i + offset) * 1000.0/(double)rate);
                for(j = 0; j < channels; j++) {
                        printf("%d ", src[i*channels+j]);
                }
                printf("\n");
        }
        printf("\n");
}

#define TEST_FRAME_LEN 160
#define TEST_FRAMES    8

static int
test_converter(int idx, converter_fmt_t *cf)
{
        struct s_converter *c;
        const converter_details_t *cd;
        sample *src;
        coded_unit cu_in, cu_out;
        int i, j, n;

        n = converter_get_count();
        if (idx < 0 || idx > n) {
                fprintf(stderr, "Converter index out of range\n");
                return FALSE;
        }

        cd = converter_get_details(idx);
        printf("#Using %s\n", cd->name);
        if (converter_create(cd->id, cf, &c) == FALSE) {
                fprintf(stderr, "Could not create converter\n");
                return FALSE;
        }

        /* Allocate and initialize source buffer */
        src = (sample*)xmalloc(TEST_FRAME_LEN * TEST_FRAMES * cf->src_channels * sizeof(sample));
        for(i = 0; i < TEST_FRAME_LEN * TEST_FRAMES; i++) {
                for(j = 0; j < cf->src_channels; j++) {
                        src[i * cf->src_channels + j] = 16384 * 
                                sin(64 *1000 * M_PI * i / (cf->src_freq * TEST_FRAME_LEN)) * 
                                cos(16 *M_PI * i * 1000 / (TEST_FRAME_LEN * cf->src_freq));
                }
        }

        memset(&cu_in, 0, sizeof(cu_in));
        memset(&cu_out, 0, sizeof(cu_out));

        for(i = 0; i < TEST_FRAMES; i++) {
                cu_in.id       = codec_get_native_coding(cf->src_freq, cf->src_channels);
                cu_in.data     = (u_char*)(src + cf->src_channels * TEST_FRAME_LEN * i);
                cu_in.data_len = cf->src_channels * TEST_FRAME_LEN * sizeof(sample);
                dump_frame(&cu_in, i * TEST_FRAME_LEN);
                converter_process(c, &cu_in, &cu_out);
                dump_frame(&cu_out, i * TEST_FRAME_LEN * cf->dst_freq / cf->src_freq);
                block_free(cu_out.data, cu_out.data_len);
                cu_out.data     = 0;
                cu_out.data_len = 0;
        }

        converter_destroy(&c);
        return TRUE;
}

int 
main(int argc, char *argv[])
{
        int i;
        int idx;
        converter_fmt_t cf;
        converters_init();

        if (argc == 1) {
                usage();
        }
        
        i = 1;
        while(i < argc) {
                switch(argv[i][1]) {
                case 'h':
                case '?':
                        usage();
                        break;
                case 'l':
                        list_converters();
                        break;
                case 'c':
                        idx = atoi(argv[i+1]);
                        i+=2;
                        break;
                case 'i':
                        if (parse_fmt(argv[i+1], &cf.src_channels, &cf.src_freq) == FALSE) {
                                usage();
                        }
                        i+=2;
                        break;
                case 'o':      
                        if (parse_fmt(argv[i+1], &cf.dst_channels, &cf.dst_freq) == FALSE) {
                                usage();
                        }
                        i+=2;
                        break;
                }
        }

        test_converter(idx, &cf);

        converters_free();
        return 0;
}