From adc44bfc597e29507d33e4a57fde9a83982a576d Mon Sep 17 00:00:00 2001 From: drowe67 Date: Thu, 26 Jan 2017 03:55:56 +0000 Subject: [PATCH] added linear and complex resampling options git-svn-id: https://svn.code.sf.net/p/freetel/code@2998 01035d8c-6547-0410-b346-abe4f91aad63 --- codec2-dev/unittest/tsrc.c | 52 ++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/codec2-dev/unittest/tsrc.c b/codec2-dev/unittest/tsrc.c index f73243a1..a16043ea 100644 --- a/codec2-dev/unittest/tsrc.c +++ b/codec2-dev/unittest/tsrc.c @@ -18,6 +18,13 @@ #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]; @@ -26,9 +33,9 @@ int main(int argc, char *argv[]) { SRC_DATA data; int error, nin, nremaining, i; - if (argc != 4) { - printf("usage %s inputRawFile OutputRawFile OutSampleRatio\n", argv[0]); - exit(0); + if (argc < 3) { + display_help(); + exit(1); } if (strcmp(argv[1], "-") == 0) @@ -43,37 +50,50 @@ int main(int argc, char *argv[]) { fout = fopen(argv[2], "wb"); assert(fout != NULL); - src = src_new(SRC_SINC_FASTEST, 1, &error); - //src = src_new(SRC_LINEAR, 1, &error); - assert(src != NULL); - data.data_in = in; data.data_out = out; - data.input_frames = N; - data.output_frames = N; 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 = N; + nin = data.input_frames; nremaining = 0; - while(fread(&in_short[nremaining], sizeof(short), nin, fin) == nin) { + 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); + src_float_to_short_array(out, out_short, data.output_frames_gen*channels); - fwrite(out_short, sizeof(short), data.output_frames_gen, fout); + fwrite(out_short, sizeof(short), data.output_frames_gen*channels, fout); if (fout == stdout) fflush(stdout); - nremaining = N - data.input_frames_used; + 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