1 /* Copyright (C) 2007 Jean-Marc Valin
2 
3    File: testresample.c
4    Testing the resampling code
5 
6    Redistribution and use in source and binary forms, with or without
7    modification, are permitted provided that the following conditions are
8    met:
9 
10    1. Redistributions of source code must retain the above copyright notice,
11    this list of conditions and the following disclaimer.
12 
13    2. Redistributions in binary form must reproduce the above copyright
14    notice, this list of conditions and the following disclaimer in the
15    documentation and/or other materials provided with the distribution.
16 
17    3. The name of the author may not be used to endorse or promote products
18    derived from this software without specific prior written permission.
19 
20    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23    DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24    INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28    STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30    POSSIBILITY OF SUCH DAMAGE.
31 */
32 
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36 
37 #include "speex/speex_resampler.h"
38 #include <stdio.h>
39 #include <math.h>
40 #include <stdlib.h>
41 
42 #define NN 256
43 
main()44 int main()
45 {
46    spx_uint32_t i;
47    short *in;
48    short *out;
49    float *fin, *fout;
50    int count = 0;
51    SpeexResamplerState *st = speex_resampler_init(1, 8000, 12000, 10, NULL);
52    speex_resampler_set_rate(st, 96000, 44100);
53    speex_resampler_skip_zeros(st);
54 
55    in = malloc(NN*sizeof(short));
56    out = malloc(2*NN*sizeof(short));
57    fin = malloc(NN*sizeof(float));
58    fout = malloc(2*NN*sizeof(float));
59    while (1)
60    {
61       spx_uint32_t in_len;
62       spx_uint32_t out_len;
63       fread(in, sizeof(short), NN, stdin);
64       if (feof(stdin))
65          break;
66       for (i=0;i<NN;i++)
67          fin[i]=in[i];
68       in_len = NN;
69       out_len = 2*NN;
70       /*if (count==2)
71          speex_resampler_set_quality(st, 10);*/
72       speex_resampler_process_float(st, 0, fin, &in_len, fout, &out_len);
73       for (i=0;i<out_len;i++)
74          out[i]=floor(.5+fout[i]);
75       /*speex_warning_int("writing", out_len);*/
76       fwrite(out, sizeof(short), out_len, stdout);
77       count++;
78    }
79    speex_resampler_destroy(st);
80    free(in);
81    free(out);
82    free(fin);
83    free(fout);
84    return 0;
85 }
86 
87