File: tsrc.c

package info (click to toggle)
codec2 0.9.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 113,072 kB
  • sloc: ansic: 412,877; python: 4,004; sh: 1,540; objc: 817; asm: 683; makefile: 588
file content (109 lines) | stat: -rw-r--r-- 2,830 bytes parent folder | download | duplicates (3)
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
/*
   tsrc.c
   David Rowe
   Sat Nov 3 2012

   Unit test for libresample code.

   build: gcc tsrc.c -o tsrc -lm -lsamplerate

  */

#include <assert.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <samplerate.h>
#include <unistd.h>

#define N    10000                   /* processing buffer size */

void display_help(void) {
    fprintf(stderr, "\nusage: tsrc inputRawFile OutputRawFile OutSampleRatio [-l] [-c]\n");
    fprintf(stderr, "\nUse - for stdin/stdout\n\n");
    fprintf(stderr, "-l fast linear resampler\n");
    fprintf(stderr, "-c complex (two channel) resampling\n\n");
}

int main(int argc, char *argv[]) {
    FILE       *fin, *fout;
    short       in_short[N], out_short[N];
    float       in[N], out[N];
    SRC_STATE  *src;
    SRC_DATA    data;
    int         error, nin, nremaining, i;

    if (argc < 3) {
	display_help();
	exit(1);
    }

    if (strcmp(argv[1], "-") == 0) 
        fin = stdin;
    else
        fin = fopen(argv[1], "rb");
    assert(fin != NULL);

    if (strcmp(argv[2], "-") == 0) 
        fout = stdout;
    else
        fout = fopen(argv[2], "wb");
    assert(fout != NULL);

    data.data_in = in;
    data.data_out = out;
    data.end_of_input = 0;
    data.src_ratio = atof(argv[3]);

    int channels = 1;
    int resampler = SRC_SINC_FASTEST;
    int opt;
    while ((opt = getopt(argc, argv, "lc")) != -1) {
        switch (opt) {
        case 'l': resampler = SRC_LINEAR; break;
        case 'c': channels = 2; break;
        default:
            display_help();
            exit(1);
        }
    }

    data.input_frames = N/channels;
    data.output_frames = N/channels;

    src = src_new(resampler, channels, &error);
    assert(src != NULL);

    int total_in = 0;
    int total_out = 0;

    nin = data.input_frames;
    nremaining = 0;
    while(fread(&in_short[nremaining*channels], sizeof(short)*channels, nin, fin) == nin) {
	src_short_to_float_array(in_short, in, N);
	error = src_process(src, &data);
        assert(error == 0);
	src_float_to_short_array(out, out_short, data.output_frames_gen*channels);

	fwrite(out_short, sizeof(short), data.output_frames_gen*channels, fout);
        if (fout == stdout) fflush(stdout);

        nremaining = data.input_frames - data.input_frames_used;
        nin = data.input_frames_used;
	//fprintf(stderr, "input frames: %d output_frames %d nremaining: %d\n", 
        //        (int)data.input_frames_used, (int)data.output_frames_gen, nremaining);
        for(i=0; i<nremaining*channels; i++)
            in_short[i] = in_short[i+nin*channels];

        total_in  += data.input_frames_used;
        total_out += data.output_frames_gen;
    }

    //fprintf(stderr, "total_in: %d total_out: %d\n", total_in, total_out);

    fclose(fout);
    fclose(fin);

    return 0;
}