38 #include <speex/speex_resampler.h>
40 #define NORMFACT (float)0x8000
41 #define MAXSAMPLE (float)0x7FFF
42 #define MAXSAMPLEC (char)0x7F
46 #define MIN(a,b) ((a) < (b) ? (a) : (b))
50 #define MAX(a,b) ((a) > (b) ? (a) : (b))
53 #define resample_buffer(a, b, c) a > b ? ((a / 1000) / 2) * c : ((b / 1000) / 2) * c
56 uint32_t from_rate, uint32_t to_rate,
58 int quality, uint32_t channels, const
char *file, const
char *func,
int line)
62 double lto_rate, lfrom_rate;
66 if (!channels) channels = 1;
68 resampler->
resampler = speex_resampler_init(channels, from_rate, to_rate, quality, &err);
75 *new_resampler = resampler;
76 lto_rate = (double) resampler->
to_rate;
77 lfrom_rate = (
double) resampler->
from_rate;
80 resampler->
factor = (lto_rate / lfrom_rate);
81 resampler->
rfactor = (lfrom_rate / lto_rate);
87 resampler->
to = malloc(resampler->
to_size *
sizeof(int16_t) * resampler->
channels);
97 if (to_size > resampler->to_size) {
98 resampler->to_size = to_size;
99 resampler->to = realloc(resampler->to, resampler->to_size *
sizeof(int16_t) * resampler->channels);
103 resampler->to_len = resampler->to_size;
104 speex_resampler_process_interleaved_int(resampler->resampler, src, &srclen, resampler->to, &resampler->to_len);
105 return resampler->to_len;
111 if (resampler && *resampler) {
112 if ((*resampler)->resampler) {
113 speex_resampler_destroy((*resampler)->resampler);
115 free((*resampler)->to);
125 for (i = 0; i < len; i++) {
128 s[i] = (short) (ft + 0.5);
130 s[i] = (short) (ft - 0.5);
148 for (i = 1; i < len; i += 2) {
149 f[(int) (i / 2)] = (float) (((c[i]) * 0x100) + c[i - 1]);
164 for (i = 0; i < len; i++) {
167 l = (long) (ft + 0.5);
169 l = (long) (ft - 0.5);
171 c[i * 2] = (
unsigned char) ((l) & 0xff);
172 c[i * 2 + 1] = (
unsigned char) (((l) >> 8) & 0xff);
181 for (i = 0; i < len; i++) {
192 for (i = 0; i < len; i++) {
193 buf[i] = ((buf[i] >> 8) & 0x00ff) | ((buf[i] << 8) & 0xff00);
205 if (channels == 0) channels = 1;
209 if (divisor == (uint32_t)-1) {
210 memset(data, 0, samples * 2);
214 for (i = 0; i < samples; i++, sum_rnd = 0) {
215 for (x = 0; x < 6; x++) {
216 rnd2 = rnd2 * 31821U + 13849U;
220 s = (int16_t) ((int16_t) sum_rnd / (int) divisor);
222 for (j = 0; j < channels; j++) {
236 if (channels == 0) channels = 1;
238 if (samples > other_samples) {
244 for (i = 0; i < x * channels; i++) {
245 z = data[i] + other_data[i];
247 data[i] = (int16_t) z;
259 if (channels == 0) channels = 1;
261 if (samples > other_samples) {
267 for (i = 0; i < x * channels; i++) {
268 data[i] -= other_data[i];
281 if (orig_channels > channels) {
282 for (i = 0; i < samples; i++) {
284 for (j = 0; j < orig_channels; j++) {
285 z += data[i * orig_channels + j];
287 data[i] = (int16_t) z;
290 }
else if (orig_channels < channels) {
295 uint32_t k = 0, len = samples * orig_channels;
297 for (i = 0; i < len; i++) {
298 data[i+len] = data[i];
301 for (i = 0; i < samples; i++) {
302 for (j = 0; j < channels; j++) {
303 data[k++] = data[i + samples];
308 uint32_t k = 0, len = samples * 2 * orig_channels;
309 int16_t *orig = NULL;
312 memcpy(orig, data, len);
314 for (i = 0; i < samples; i++) {
315 for (j = 0; j < channels; j++) {
329 double pos[13] = {1.25, 1.50, 1.75, 2.0, 2.25, 2.50, 2.75, 3.0, 3.25, 3.50, 3.75, 4.0, 4.5};
330 double neg[13] = {.917, .834, .751, .668, .585, .502, .419, .336, .253, .087, .017, .004, 0.0};
334 if (vol == 0)
return;
355 for (x = 0; x < samples; x++) {
356 tmp = (int32_t) (fp[x] * newrate);
358 fp[x] = (int16_t) tmp;
361 memset(data, 0, samples * 2);
368 double pos[4] = {1.3, 2.3, 3.3, 4.3};
369 double neg[4] = {.80, .60, .40, .20};
373 if (vol == 0)
return;
394 for (x = 0; x < samples; x++) {
395 tmp = (int32_t) (fp[x] * newrate);
397 fp[x] = (int16_t) tmp;
switch_time_t switch_micro_time_now(void)
Get the current epoch time in microseconds.
int switch_char_to_float(char *c, float *f, int len)
Convert an array of chars to an array of floats.
void switch_generate_sln_silence(int16_t *data, uint32_t samples, uint32_t channels, uint32_t divisor)
Generate static noise.
#define switch_resample_calc_buffer_size(_to, _from, _srclen)
void switch_resample_destroy(switch_audio_resampler_t **resampler)
Destroy an existing resampler handle.
void switch_change_sln_volume(int16_t *data, uint32_t samples, int32_t vol)
Change the volume of a signed linear audio frame.
#define switch_normalize_volume_granular(x)
switch_byte_t switch_byte_t * buf
#define switch_normalize_volume(x)
switch_size_t switch_float_to_short(float *f, short *s, switch_size_t len)
Convert an array of floats to an array of shorts.
#define switch_zmalloc(ptr, len)
switch_status_t switch_resample_perform_create(switch_audio_resampler_t **new_resampler, uint32_t from_rate, uint32_t to_rate, uint32_t to_size, int quality, uint32_t channels, const char *file, const char *func, int line)
Prepare a new resampler handle.
uint32_t switch_merge_sln(int16_t *data, uint32_t samples, int16_t *other_data, uint32_t other_samples, int channels)
int switch_short_to_float(short *s, float *f, int len)
Convert an array of shorts to an array of floats.
void switch_swap_linear(int16_t *buf, int len)
Perform a byteswap on a buffer of 16 bit samples.
uint32_t switch_unmerge_sln(int16_t *data, uint32_t samples, int16_t *other_data, uint32_t other_samples, int channels)
int switch_float_to_char(float *f, char *c, int len)
Convert an array of floats to an array of chars.
An audio resampling handle.
switch_status_t
Common return values.
void switch_change_sln_volume_granular(int16_t *data, uint32_t samples, int32_t vol)
Change the volume of a signed linear audio frame with more granularity.
#define switch_normalize_to_16bit(n)
void switch_mux_channels(int16_t *data, switch_size_t samples, uint32_t orig_channels, uint32_t channels)
uint32_t switch_resample_process(switch_audio_resampler_t *resampler, int16_t *src, uint32_t srclen)
Resample one float buffer into another using specifications of a given handle.