| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#include "config.h" |
| 32 |
#include <stdio.h> |
| 33 |
#include <alsa/asoundlib.h> |
| 34 |
#include <alsa/pcm_rate.h> |
| 35 |
|
| 36 |
#ifdef USE_LIBSPEEX |
| 37 |
#include <speex/speex_resampler.h> |
| 38 |
#else |
| 39 |
#include "speex_resampler.h" |
| 40 |
#endif |
| 41 |
|
| 42 |
struct rate_src { |
| 43 |
int quality; |
| 44 |
unsigned int channels; |
| 45 |
SpeexResamplerState *st; |
| 46 |
}; |
| 47 |
|
| 48 |
static snd_pcm_uframes_t input_frames(void *obj, snd_pcm_uframes_t frames) |
| 49 |
{ |
| 50 |
spx_uint32_t num, den; |
| 51 |
struct rate_src *rate = obj; |
| 52 |
if (frames == 0) |
| 53 |
return 0; |
| 54 |
speex_resampler_get_ratio(rate->st, &num, &den); |
| 55 |
return (snd_pcm_uframes_t)((frames*num+(den>>1))/den); |
| 56 |
} |
| 57 |
|
| 58 |
static snd_pcm_uframes_t output_frames(void *obj, snd_pcm_uframes_t frames) |
| 59 |
{ |
| 60 |
spx_uint32_t num, den; |
| 61 |
struct rate_src *rate = obj; |
| 62 |
if (frames == 0) |
| 63 |
return 0; |
| 64 |
speex_resampler_get_ratio(rate->st, &num, &den); |
| 65 |
return (snd_pcm_uframes_t)((frames*den+(num>>1))/num); |
| 66 |
} |
| 67 |
|
| 68 |
static void pcm_src_free(void *obj) |
| 69 |
{ |
| 70 |
struct rate_src *rate = obj; |
| 71 |
if (rate->st) |
| 72 |
{ |
| 73 |
speex_resampler_destroy(rate->st); |
| 74 |
rate->st = NULL; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
static int pcm_src_init(void *obj, snd_pcm_rate_info_t *info) |
| 79 |
{ |
| 80 |
struct rate_src *rate = obj; |
| 81 |
int err; |
| 82 |
|
| 83 |
if (! rate->st || rate->channels != info->channels) { |
| 84 |
if (rate->st) |
| 85 |
speex_resampler_destroy(rate->st); |
| 86 |
rate->channels = info->channels; |
| 87 |
rate->st = speex_resampler_init_frac(rate->channels, info->in.period_size, info->out.period_size, info->in.rate, info->out.rate, rate->quality, &err); |
| 88 |
if (! rate->st) |
| 89 |
return -EINVAL; |
| 90 |
} |
| 91 |
|
| 92 |
return 0; |
| 93 |
} |
| 94 |
|
| 95 |
static int pcm_src_adjust_pitch(void *obj, snd_pcm_rate_info_t *info) |
| 96 |
{ |
| 97 |
struct rate_src *rate = obj; |
| 98 |
speex_resampler_set_rate_frac(rate->st, info->in.period_size, info->out.period_size, info->in.rate, info->out.rate); |
| 99 |
return 0; |
| 100 |
} |
| 101 |
|
| 102 |
static void pcm_src_reset(void *obj) |
| 103 |
{ |
| 104 |
struct rate_src *rate = obj; |
| 105 |
speex_resampler_reset_mem(rate->st); |
| 106 |
} |
| 107 |
|
| 108 |
static void pcm_src_convert_s16(void *obj, int16_t *dst, unsigned int dst_frames, |
| 109 |
const int16_t *src, unsigned int src_frames) |
| 110 |
{ |
| 111 |
struct rate_src *rate = obj; |
| 112 |
speex_resampler_process_interleaved_int(rate->st, src, &src_frames, dst, &dst_frames); |
| 113 |
} |
| 114 |
|
| 115 |
static void pcm_src_close(void *obj) |
| 116 |
{ |
| 117 |
free(obj); |
| 118 |
} |
| 119 |
|
| 120 |
#if SND_PCM_RATE_PLUGIN_VERSION >= 0x010002 |
| 121 |
static int get_supported_rates(void *obj, unsigned int *rate_min, |
| 122 |
unsigned int *rate_max) |
| 123 |
{ |
| 124 |
*rate_min = *rate_max = 0; |
| 125 |
return 0; |
| 126 |
} |
| 127 |
|
| 128 |
static void dump(void *obj, snd_output_t *out) |
| 129 |
{ |
| 130 |
snd_output_printf(out, "Converter: libspeex " |
| 131 |
#ifdef USE_LIBSPEEX |
| 132 |
"(external)" |
| 133 |
#else |
| 134 |
"(builtin)" |
| 135 |
#endif |
| 136 |
"\n"); |
| 137 |
} |
| 138 |
#endif |
| 139 |
|
| 140 |
static snd_pcm_rate_ops_t pcm_src_ops = { |
| 141 |
.close = pcm_src_close, |
| 142 |
.init = pcm_src_init, |
| 143 |
.free = pcm_src_free, |
| 144 |
.reset = pcm_src_reset, |
| 145 |
.adjust_pitch = pcm_src_adjust_pitch, |
| 146 |
.convert_s16 = pcm_src_convert_s16, |
| 147 |
.input_frames = input_frames, |
| 148 |
.output_frames = output_frames, |
| 149 |
#if SND_PCM_RATE_PLUGIN_VERSION >= 0x010002 |
| 150 |
.version = SND_PCM_RATE_PLUGIN_VERSION, |
| 151 |
.get_supported_rates = get_supported_rates, |
| 152 |
.dump = dump, |
| 153 |
#endif |
| 154 |
}; |
| 155 |
|
| 156 |
static int pcm_src_open(unsigned int version, void **objp, |
| 157 |
snd_pcm_rate_ops_t *ops, int quality) |
| 158 |
{ |
| 159 |
struct rate_src *rate; |
| 160 |
|
| 161 |
#if SND_PCM_RATE_PLUGIN_VERSION < 0x010002 |
| 162 |
if (version != SND_PCM_RATE_PLUGIN_VERSION) { |
| 163 |
fprintf(stderr, "Invalid rate plugin version %x\n", version); |
| 164 |
return -EINVAL; |
| 165 |
} |
| 166 |
#endif |
| 167 |
rate = calloc(1, sizeof(*rate)); |
| 168 |
if (! rate) |
| 169 |
return -ENOMEM; |
| 170 |
rate->quality = quality; |
| 171 |
|
| 172 |
*objp = rate; |
| 173 |
#if SND_PCM_RATE_PLUGIN_VERSION >= 0x010002 |
| 174 |
if (version == 0x010001) |
| 175 |
memcpy(ops, &pcm_src_ops, sizeof(snd_pcm_rate_old_ops_t)); |
| 176 |
else |
| 177 |
#endif |
| 178 |
*ops = pcm_src_ops; |
| 179 |
return 0; |
| 180 |
} |
| 181 |
|
| 182 |
int SND_PCM_RATE_PLUGIN_ENTRY(speexrate) (unsigned int version, void **objp, |
| 183 |
snd_pcm_rate_ops_t *ops) |
| 184 |
{ |
| 185 |
return pcm_src_open(version, objp, ops, 3); |
| 186 |
} |
| 187 |
|
| 188 |
int SND_PCM_RATE_PLUGIN_ENTRY(speexrate_best) (unsigned int version, void **objp, |
| 189 |
snd_pcm_rate_ops_t *ops) |
| 190 |
{ |
| 191 |
return pcm_src_open(version, objp, ops, 10); |
| 192 |
} |
| 193 |
|
| 194 |
int SND_PCM_RATE_PLUGIN_ENTRY(speexrate_medium) (unsigned int version, void **objp, |
| 195 |
snd_pcm_rate_ops_t *ops) |
| 196 |
{ |
| 197 |
return pcm_src_open(version, objp, ops, 5); |
| 198 |
} |