153a5a1b3Sopenharmony_ci/* Copyright (C) 2007 Jean-Marc Valin 253a5a1b3Sopenharmony_ci 353a5a1b3Sopenharmony_ci File: testresample.c 453a5a1b3Sopenharmony_ci Testing the resampling code 553a5a1b3Sopenharmony_ci 653a5a1b3Sopenharmony_ci Redistribution and use in source and binary forms, with or without 753a5a1b3Sopenharmony_ci modification, are permitted provided that the following conditions are 853a5a1b3Sopenharmony_ci met: 953a5a1b3Sopenharmony_ci 1053a5a1b3Sopenharmony_ci 1. Redistributions of source code must retain the above copyright notice, 1153a5a1b3Sopenharmony_ci this list of conditions and the following disclaimer. 1253a5a1b3Sopenharmony_ci 1353a5a1b3Sopenharmony_ci 2. Redistributions in binary form must reproduce the above copyright 1453a5a1b3Sopenharmony_ci notice, this list of conditions and the following disclaimer in the 1553a5a1b3Sopenharmony_ci documentation and/or other materials provided with the distribution. 1653a5a1b3Sopenharmony_ci 1753a5a1b3Sopenharmony_ci 3. The name of the author may not be used to endorse or promote products 1853a5a1b3Sopenharmony_ci derived from this software without specific prior written permission. 1953a5a1b3Sopenharmony_ci 2053a5a1b3Sopenharmony_ci THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 2153a5a1b3Sopenharmony_ci IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2253a5a1b3Sopenharmony_ci OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 2353a5a1b3Sopenharmony_ci DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 2453a5a1b3Sopenharmony_ci INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 2553a5a1b3Sopenharmony_ci (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 2653a5a1b3Sopenharmony_ci SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2753a5a1b3Sopenharmony_ci HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 2853a5a1b3Sopenharmony_ci STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 2953a5a1b3Sopenharmony_ci ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 3053a5a1b3Sopenharmony_ci POSSIBILITY OF SUCH DAMAGE. 3153a5a1b3Sopenharmony_ci*/ 3253a5a1b3Sopenharmony_ci 3353a5a1b3Sopenharmony_ci#ifdef HAVE_CONFIG_H 3453a5a1b3Sopenharmony_ci#include "config.h" 3553a5a1b3Sopenharmony_ci#endif 3653a5a1b3Sopenharmony_ci 3753a5a1b3Sopenharmony_ci#include "speex/speex_resampler.h" 3853a5a1b3Sopenharmony_ci#include <stdio.h> 3953a5a1b3Sopenharmony_ci#include <math.h> 4053a5a1b3Sopenharmony_ci#include <stdlib.h> 4153a5a1b3Sopenharmony_ci 4253a5a1b3Sopenharmony_ci#define NN 256 4353a5a1b3Sopenharmony_ci 4453a5a1b3Sopenharmony_ciint main() 4553a5a1b3Sopenharmony_ci{ 4653a5a1b3Sopenharmony_ci spx_uint32_t i; 4753a5a1b3Sopenharmony_ci short *in; 4853a5a1b3Sopenharmony_ci short *out; 4953a5a1b3Sopenharmony_ci float *fin, *fout; 5053a5a1b3Sopenharmony_ci int count = 0; 5153a5a1b3Sopenharmony_ci SpeexResamplerState *st = speex_resampler_init(1, 8000, 12000, 10, NULL); 5253a5a1b3Sopenharmony_ci speex_resampler_set_rate(st, 96000, 44100); 5353a5a1b3Sopenharmony_ci speex_resampler_skip_zeros(st); 5453a5a1b3Sopenharmony_ci 5553a5a1b3Sopenharmony_ci in = malloc(NN*sizeof(short)); 5653a5a1b3Sopenharmony_ci out = malloc(2*NN*sizeof(short)); 5753a5a1b3Sopenharmony_ci fin = malloc(NN*sizeof(float)); 5853a5a1b3Sopenharmony_ci fout = malloc(2*NN*sizeof(float)); 5953a5a1b3Sopenharmony_ci while (1) 6053a5a1b3Sopenharmony_ci { 6153a5a1b3Sopenharmony_ci spx_uint32_t in_len; 6253a5a1b3Sopenharmony_ci spx_uint32_t out_len; 6353a5a1b3Sopenharmony_ci fread(in, sizeof(short), NN, stdin); 6453a5a1b3Sopenharmony_ci if (feof(stdin)) 6553a5a1b3Sopenharmony_ci break; 6653a5a1b3Sopenharmony_ci for (i=0;i<NN;i++) 6753a5a1b3Sopenharmony_ci fin[i]=in[i]; 6853a5a1b3Sopenharmony_ci in_len = NN; 6953a5a1b3Sopenharmony_ci out_len = 2*NN; 7053a5a1b3Sopenharmony_ci /*if (count==2) 7153a5a1b3Sopenharmony_ci speex_resampler_set_quality(st, 10);*/ 7253a5a1b3Sopenharmony_ci speex_resampler_process_float(st, 0, fin, &in_len, fout, &out_len); 7353a5a1b3Sopenharmony_ci for (i=0;i<out_len;i++) 7453a5a1b3Sopenharmony_ci out[i]=floor(.5+fout[i]); 7553a5a1b3Sopenharmony_ci /*speex_warning_int("writing", out_len);*/ 7653a5a1b3Sopenharmony_ci fwrite(out, sizeof(short), out_len, stdout); 7753a5a1b3Sopenharmony_ci count++; 7853a5a1b3Sopenharmony_ci } 7953a5a1b3Sopenharmony_ci speex_resampler_destroy(st); 8053a5a1b3Sopenharmony_ci free(in); 8153a5a1b3Sopenharmony_ci free(out); 8253a5a1b3Sopenharmony_ci free(fin); 8353a5a1b3Sopenharmony_ci free(fout); 8453a5a1b3Sopenharmony_ci return 0; 8553a5a1b3Sopenharmony_ci} 8653a5a1b3Sopenharmony_ci 87