1
/* $Id: resample_speex.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
#include <pjmedia/resample.h>
21
#include <pjmedia/errno.h>
22
#include <pj/assert.h>
26
#if PJMEDIA_RESAMPLE_IMP==PJMEDIA_RESAMPLE_SPEEX
28
#include <speex/speex_resampler.h>
30
#define THIS_FILE "resample_speex.c"
33
struct pjmedia_resample
35
SpeexResamplerState *state;
36
unsigned in_samples_per_frame;
37
unsigned out_samples_per_frame;
41
PJ_DEF(pj_status_t) pjmedia_resample_create( pj_pool_t *pool,
42
pj_bool_t high_quality,
43
pj_bool_t large_filter,
44
unsigned channel_count,
47
unsigned samples_per_frame,
48
pjmedia_resample **p_resample)
50
pjmedia_resample *resample;
54
PJ_ASSERT_RETURN(pool && p_resample && rate_in &&
55
rate_out && samples_per_frame, PJ_EINVAL);
57
resample = PJ_POOL_ZALLOC_T(pool, pjmedia_resample);
58
PJ_ASSERT_RETURN(resample, PJ_ENOMEM);
69
resample->in_samples_per_frame = samples_per_frame;
70
resample->out_samples_per_frame = rate_out / (rate_in / samples_per_frame);
71
resample->state = speex_resampler_init(channel_count, rate_in, rate_out,
73
if (resample->state == NULL || err != RESAMPLER_ERR_SUCCESS)
77
*p_resample = resample;
80
"resample created: quality=%d, ch=%d, in/out rate=%d/%d",
81
quality, channel_count, rate_in, rate_out));
86
PJ_DEF(void) pjmedia_resample_run( pjmedia_resample *resample,
87
const pj_int16_t *input,
90
spx_uint32_t in_length, out_length;
92
PJ_ASSERT_ON_FAIL(resample, return);
94
in_length = resample->in_samples_per_frame;
95
out_length = resample->out_samples_per_frame;
97
speex_resampler_process_interleaved_int(resample->state,
98
(const __int16 *)input, &in_length,
99
(__int16 *)output, &out_length);
101
pj_assert(in_length == resample->in_samples_per_frame);
102
pj_assert(out_length == resample->out_samples_per_frame);
106
PJ_DEF(unsigned) pjmedia_resample_get_input_size(pjmedia_resample *resample)
108
PJ_ASSERT_RETURN(resample != NULL, 0);
109
return resample->in_samples_per_frame;
113
PJ_DEF(void) pjmedia_resample_destroy(pjmedia_resample *resample)
115
PJ_ASSERT_ON_FAIL(resample, return);
116
if (resample->state) {
117
speex_resampler_destroy(resample->state);
118
resample->state = NULL;
122
#else /* PJMEDIA_RESAMPLE_IMP==PJMEDIA_RESAMPLE_SPEEX */
124
int pjmedia_resample_speex_excluded;
126
#endif /* PJMEDIA_RESAMPLE_IMP==PJMEDIA_RESAMPLE_SPEEX */