153a5a1b3Sopenharmony_ci/* Copyright (C) 2007 Jean-Marc Valin 253a5a1b3Sopenharmony_ci 353a5a1b3Sopenharmony_ci File: testresample2.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 PERIOD 32 4353a5a1b3Sopenharmony_ci#define INBLOCK 1024 4453a5a1b3Sopenharmony_ci#define RATE 48000 4553a5a1b3Sopenharmony_ci 4653a5a1b3Sopenharmony_ciint main() 4753a5a1b3Sopenharmony_ci{ 4853a5a1b3Sopenharmony_ci spx_uint32_t i; 4953a5a1b3Sopenharmony_ci float *fin, *fout; 5053a5a1b3Sopenharmony_ci int rate = 1000, off = 0, avail = INBLOCK; 5153a5a1b3Sopenharmony_ci SpeexResamplerState *st = speex_resampler_init(1, RATE, RATE, 4, NULL); 5253a5a1b3Sopenharmony_ci speex_resampler_set_rate(st, RATE, rate); 5353a5a1b3Sopenharmony_ci speex_resampler_skip_zeros(st); 5453a5a1b3Sopenharmony_ci 5553a5a1b3Sopenharmony_ci fin = malloc(INBLOCK*2*sizeof(float)); 5653a5a1b3Sopenharmony_ci for (i=0; i<INBLOCK*2;i++) 5753a5a1b3Sopenharmony_ci fin[i] = sinf ((float)i/PERIOD * 2 * M_PI) * 0.9; 5853a5a1b3Sopenharmony_ci 5953a5a1b3Sopenharmony_ci fout = malloc(INBLOCK*4*sizeof(float)); 6053a5a1b3Sopenharmony_ci 6153a5a1b3Sopenharmony_ci while (1) 6253a5a1b3Sopenharmony_ci { 6353a5a1b3Sopenharmony_ci spx_uint32_t in_len; 6453a5a1b3Sopenharmony_ci spx_uint32_t out_len; 6553a5a1b3Sopenharmony_ci 6653a5a1b3Sopenharmony_ci in_len = avail; 6753a5a1b3Sopenharmony_ci out_len = (in_len * rate + RATE-1) / RATE; 6853a5a1b3Sopenharmony_ci 6953a5a1b3Sopenharmony_ci fprintf (stderr, "%d %d %d %d -> ", rate, off, in_len, out_len); 7053a5a1b3Sopenharmony_ci 7153a5a1b3Sopenharmony_ci speex_resampler_process_float(st, 0, fin + off, &in_len, fout, &out_len); 7253a5a1b3Sopenharmony_ci 7353a5a1b3Sopenharmony_ci fprintf (stderr, "%d %d\n", in_len, out_len); 7453a5a1b3Sopenharmony_ci off += in_len; 7553a5a1b3Sopenharmony_ci avail = avail - in_len + INBLOCK; 7653a5a1b3Sopenharmony_ci 7753a5a1b3Sopenharmony_ci if (off >= INBLOCK) 7853a5a1b3Sopenharmony_ci off -= INBLOCK; 7953a5a1b3Sopenharmony_ci 8053a5a1b3Sopenharmony_ci fwrite(fout, sizeof(float), out_len, stdout); 8153a5a1b3Sopenharmony_ci 8253a5a1b3Sopenharmony_ci rate += 100; 8353a5a1b3Sopenharmony_ci if (rate > 128000) 8453a5a1b3Sopenharmony_ci break; 8553a5a1b3Sopenharmony_ci 8653a5a1b3Sopenharmony_ci speex_resampler_set_rate(st, RATE, rate); 8753a5a1b3Sopenharmony_ci } 8853a5a1b3Sopenharmony_ci speex_resampler_destroy(st); 8953a5a1b3Sopenharmony_ci free(fin); 9053a5a1b3Sopenharmony_ci free(fout); 9153a5a1b3Sopenharmony_ci return 0; 9253a5a1b3Sopenharmony_ci} 9353a5a1b3Sopenharmony_ci 94