1
/* Rate converter plugin using Public Parrot Hack
2
Copyright (C) 2007 Jean-Marc Valin
4
Redistribution and use in source and binary forms, with or without
5
modification, are permitted provided that the following conditions are
8
1. Redistributions of source code must retain the above copyright notice,
9
this list of conditions and the following disclaimer.
11
2. Redistributions in binary form must reproduce the above copyright
12
notice, this list of conditions and the following disclaimer in the
13
documentation and/or other materials provided with the distribution.
15
3. The name of the author may not be used to endorse or promote products
16
derived from this software without specific prior written permission.
18
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
22
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
POSSIBILITY OF SUCH DAMAGE.
32
#include <alsa/asoundlib.h>
33
#include <alsa/pcm_rate.h>
36
#include <speex/speex_resampler.h>
38
#include "speex_resampler.h"
43
unsigned int channels;
44
SpeexResamplerState *st;
47
static snd_pcm_uframes_t input_frames(void *obj, snd_pcm_uframes_t frames)
49
spx_uint32_t num, den;
50
struct rate_src *rate = obj;
53
speex_resampler_get_ratio(rate->st, &num, &den);
54
return (snd_pcm_uframes_t)((frames*num+(den>>1))/den);
57
static snd_pcm_uframes_t output_frames(void *obj, snd_pcm_uframes_t frames)
59
spx_uint32_t num, den;
60
struct rate_src *rate = obj;
63
speex_resampler_get_ratio(rate->st, &num, &den);
64
return (snd_pcm_uframes_t)((frames*den+(num>>1))/num);
67
static void pcm_src_free(void *obj)
69
struct rate_src *rate = obj;
72
speex_resampler_destroy(rate->st);
77
static int pcm_src_init(void *obj, snd_pcm_rate_info_t *info)
79
struct rate_src *rate = obj;
81
if (! rate->st || rate->channels != info->channels) {
83
speex_resampler_destroy(rate->st);
84
rate->channels = info->channels;
85
rate->st = speex_resampler_init_frac(rate->channels, info->in.period_size, info->out.period_size, info->in.rate, info->out.rate, rate->quality);
93
static int pcm_src_adjust_pitch(void *obj, snd_pcm_rate_info_t *info)
95
struct rate_src *rate = obj;
96
speex_resampler_set_rate_frac(rate->st, info->in.period_size, info->out.period_size, info->in.rate, info->out.rate);
100
static void pcm_src_reset(void *obj)
102
struct rate_src *rate = obj;
103
speex_resampler_reset_mem(rate->st);
106
static void pcm_src_convert_s16(void *obj, int16_t *dst, unsigned int dst_frames,
107
const int16_t *src, unsigned int src_frames)
109
struct rate_src *rate = obj;
110
speex_resampler_process_interleaved_int(rate->st, src, &src_frames, dst, &dst_frames);
113
static void pcm_src_close(void *obj)
118
static snd_pcm_rate_ops_t pcm_src_ops = {
119
.close = pcm_src_close,
120
.init = pcm_src_init,
121
.free = pcm_src_free,
122
.reset = pcm_src_reset,
123
.adjust_pitch = pcm_src_adjust_pitch,
124
.convert_s16 = pcm_src_convert_s16,
125
.input_frames = input_frames,
126
.output_frames = output_frames,
129
static int pcm_src_open(unsigned int version, void **objp,
130
snd_pcm_rate_ops_t *ops, int quality)
132
struct rate_src *rate;
134
if (version != SND_PCM_RATE_PLUGIN_VERSION) {
135
fprintf(stderr, "Invalid rate plugin version %x\n", version);
139
rate = calloc(1, sizeof(*rate));
142
rate->quality = quality;
149
int SND_PCM_RATE_PLUGIN_ENTRY(speexrate) (unsigned int version, void **objp,
150
snd_pcm_rate_ops_t *ops)
152
return pcm_src_open(version, objp, ops, 3);
155
int SND_PCM_RATE_PLUGIN_ENTRY(speexrate_best) (unsigned int version, void **objp,
156
snd_pcm_rate_ops_t *ops)
158
return pcm_src_open(version, objp, ops, 10);
161
int SND_PCM_RATE_PLUGIN_ENTRY(speexrate_medium) (unsigned int version, void **objp,
162
snd_pcm_rate_ops_t *ops)
164
return pcm_src_open(version, objp, ops, 5);