FreeSWITCH API Documentation  1.7.0
Data Structures | Macros | Functions
Audio Resample Functions
+ Collaboration diagram for Audio Resample Functions:

Data Structures

struct  switch_audio_resampler_t
 An audio resampling handle. More...
 

Macros

#define switch_resample_create(_n, _fr, _tr, _ts, _q, _c)   switch_resample_perform_create(_n, _fr, _tr, _ts, _q, _c, __FILE__, __SWITCH_FUNC__, __LINE__)
 

Functions

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. More...
 
void switch_resample_destroy (switch_audio_resampler_t **resampler)
 Destroy an existing resampler handle. More...
 
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. More...
 
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. More...
 
int switch_char_to_float (char *c, float *f, int len)
 Convert an array of chars to an array of floats. More...
 
int switch_float_to_char (float *f, char *c, int len)
 Convert an array of floats to an array of chars. More...
 
int switch_short_to_float (short *s, float *f, int len)
 Convert an array of shorts to an array of floats. More...
 
void switch_swap_linear (int16_t *buf, int len)
 Perform a byteswap on a buffer of 16 bit samples. More...
 
void switch_generate_sln_silence (int16_t *data, uint32_t samples, uint32_t channels, uint32_t divisor)
 Generate static noise. More...
 
void switch_change_sln_volume (int16_t *data, uint32_t samples, int32_t vol)
 Change the volume of a signed linear audio frame. More...
 
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. More...
 

Detailed Description

Macro Definition Documentation

#define switch_resample_create (   _n,
  _fr,
  _tr,
  _ts,
  _q,
  _c 
)    switch_resample_perform_create(_n, _fr, _tr, _ts, _q, _c, __FILE__, __SWITCH_FUNC__, __LINE__)

Function Documentation

void switch_change_sln_volume ( int16_t *  data,
uint32_t  samples,
int32_t  vol 
)

Change the volume of a signed linear audio frame.

Parameters
datathe audio data
samplesthe number of 2 byte samples
volthe volume factor -4 -> 4

Definition at line 365 of file switch_resample.c.

References switch_assert, switch_normalize_to_16bit, and switch_normalize_volume.

Referenced by session_audio_callback(), and switch_ivr_play_file().

366 {
367  double newrate = 0;
368  double pos[4] = {1.3, 2.3, 3.3, 4.3};
369  double neg[4] = {.80, .60, .40, .20};
370  double *chart;
371  uint32_t i;
372 
373  if (vol == 0) return;
374 
376 
377  if (vol > 0) {
378  chart = pos;
379  } else {
380  chart = neg;
381  }
382 
383  i = abs(vol) - 1;
384 
385  switch_assert(i < 4);
386 
387  newrate = chart[i];
388 
389  if (newrate) {
390  int32_t tmp;
391  uint32_t x;
392  int16_t *fp = data;
393 
394  for (x = 0; x < samples; x++) {
395  tmp = (int32_t) (fp[x] * newrate);
397  fp[x] = (int16_t) tmp;
398  }
399  }
400 }
#define switch_normalize_volume(x)
#define switch_normalize_to_16bit(n)
Definition: switch_utils.h:261
#define switch_assert(expr)
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.

Parameters
datathe audio data
samplesthe number of 2 byte samples
volthe volume factor -12 -> 12

Definition at line 326 of file switch_resample.c.

References memset(), switch_assert, switch_normalize_to_16bit, and switch_normalize_volume_granular.

327 {
328  double newrate = 0;
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};
331  double *chart;
332  uint32_t i;
333 
334  if (vol == 0) return;
335 
337 
338  if (vol > 0) {
339  chart = pos;
340  } else {
341  chart = neg;
342  }
343 
344  i = abs(vol) - 1;
345 
346  switch_assert(i < 13);
347 
348  newrate = chart[i];
349 
350  if (newrate) {
351  int32_t tmp;
352  uint32_t x;
353  int16_t *fp = data;
354 
355  for (x = 0; x < samples; x++) {
356  tmp = (int32_t) (fp[x] * newrate);
358  fp[x] = (int16_t) tmp;
359  }
360  } else {
361  memset(data, 0, samples * 2);
362  }
363 }
#define switch_normalize_volume_granular(x)
#define switch_normalize_to_16bit(n)
Definition: switch_utils.h:261
#define switch_assert(expr)
memset(buf, 0, buflen)
int switch_char_to_float ( char *  c,
float *  f,
int  len 
)

Convert an array of chars to an array of floats.

Parameters
cthe char buffer
fthe float buffer
lenthe length of the buffers
Returns
the size of the converted buffer

Definition at line 140 of file switch_resample.c.

References MAXSAMPLE, and NORMFACT.

141 {
142  int i;
143 
144  if (len % 2) {
145  return (-1);
146  }
147 
148  for (i = 1; i < len; i += 2) {
149  f[(int) (i / 2)] = (float) (((c[i]) * 0x100) + c[i - 1]);
150  f[(int) (i / 2)] /= NORMFACT;
151  if (f[(int) (i / 2)] > MAXSAMPLE)
152  f[(int) (i / 2)] = MAXSAMPLE;
153  if (f[(int) (i / 2)] < -MAXSAMPLE)
154  f[(int) (i / 2)] = -MAXSAMPLE;
155  }
156  return len / 2;
157 }
#define MAXSAMPLE
#define NORMFACT
int switch_float_to_char ( float *  f,
char *  c,
int  len 
)

Convert an array of floats to an array of chars.

Parameters
fan array of floats
can array of chars
lenthe length of the buffers
Returns
the size of the converted buffer

Definition at line 159 of file switch_resample.c.

References NORMFACT.

160 {
161  int i;
162  float ft;
163  long l;
164  for (i = 0; i < len; i++) {
165  ft = f[i] * NORMFACT;
166  if (ft >= 0) {
167  l = (long) (ft + 0.5);
168  } else {
169  l = (long) (ft - 0.5);
170  }
171  c[i * 2] = (unsigned char) ((l) & 0xff);
172  c[i * 2 + 1] = (unsigned char) (((l) >> 8) & 0xff);
173  }
174  return len * 2;
175 }
#define NORMFACT
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.

Parameters
fthe float buffer
sthe short buffer
lenthe length of the buffers
Returns
the size of the converted buffer

Definition at line 121 of file switch_resample.c.

References MAXSAMPLE, and NORMFACT.

122 {
123  switch_size_t i;
124  float ft;
125  for (i = 0; i < len; i++) {
126  ft = f[i] * NORMFACT;
127  if (ft >= 0) {
128  s[i] = (short) (ft + 0.5);
129  } else {
130  s[i] = (short) (ft - 0.5);
131  }
132  if ((float) s[i] > MAXSAMPLE)
133  s[i] = (short) MAXSAMPLE / 2;
134  if (s[i] < (short) -MAXSAMPLE)
135  s[i] = (short) -MAXSAMPLE / 2;
136  }
137  return len;
138 }
#define MAXSAMPLE
#define NORMFACT
uintptr_t switch_size_t
void switch_generate_sln_silence ( int16_t *  data,
uint32_t  samples,
uint32_t  channels,
uint32_t  divisor 
)

Generate static noise.

Parameters
datathe audio data buffer
samplesthe number of 2 byte samples
divisorthe volume factor

Definition at line 198 of file switch_resample.c.

References memset(), and switch_micro_time_now().

Referenced by audio_bridge_thread(), session_audio_callback(), switch_ivr_collect_digits_count(), switch_ivr_originate(), switch_ivr_park(), switch_ivr_record_file(), switch_ivr_sleep(), and switch_ivr_wait_for_answer().

199 {
200  int16_t s;
201  uint32_t x, i, j;
202  int sum_rnd = 0;
203  int16_t rnd2 = (int16_t) switch_micro_time_now() + (int16_t) (intptr_t) data;
204 
205  if (channels == 0) channels = 1;
206 
207  assert(divisor);
208 
209  if (divisor == (uint32_t)-1) {
210  memset(data, 0, samples * 2);
211  return;
212  }
213 
214  for (i = 0; i < samples; i++, sum_rnd = 0) {
215  for (x = 0; x < 6; x++) {
216  rnd2 = rnd2 * 31821U + 13849U;
217  sum_rnd += rnd2;
218  }
219 
220  s = (int16_t) ((int16_t) sum_rnd / (int) divisor);
221 
222  for (j = 0; j < channels; j++) {
223  *data = s;
224  data++;
225  }
226 
227 
228  }
229 }
switch_time_t switch_micro_time_now(void)
Get the current epoch time in microseconds.
Definition: switch_time.c:310
memset(buf, 0, buflen)
void switch_resample_destroy ( switch_audio_resampler_t **  resampler)

Destroy an existing resampler handle.

Parameters
resamplerthe resampler handle to destroy

Definition at line 108 of file switch_resample.c.

Referenced by switch_core_asr_close(), switch_core_file_close(), switch_core_media_set_codec(), switch_core_session_read_frame(), switch_core_session_reset(), switch_core_session_write_frame(), and switch_core_speech_close().

109 {
110 
111  if (resampler && *resampler) {
112  if ((*resampler)->resampler) {
113  speex_resampler_destroy((*resampler)->resampler);
114  }
115  free((*resampler)->to);
116  free(*resampler);
117  *resampler = NULL;
118  }
119 }
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.

Parameters
new_resamplerNULL pointer to aim at the new handle
from_ratethe rate to transfer from in hz
to_ratethe rate to transfer to in hz
qualitythe quality desired
Returns
SWITCH_STATUS_SUCCESS if the handle was created

Definition at line 55 of file switch_resample.c.

References switch_audio_resampler_t::channels, switch_audio_resampler_t::factor, switch_audio_resampler_t::from_rate, switch_audio_resampler_t::resampler, switch_audio_resampler_t::rfactor, switch_assert, switch_resample_calc_buffer_size, SWITCH_STATUS_GENERR, SWITCH_STATUS_SUCCESS, switch_zmalloc, switch_audio_resampler_t::to, switch_audio_resampler_t::to_rate, and switch_audio_resampler_t::to_size.

59 {
60  int err = 0;
61  switch_audio_resampler_t *resampler;
62  double lto_rate, lfrom_rate;
63 
64  switch_zmalloc(resampler, sizeof(*resampler));
65 
66  if (!channels) channels = 1;
67 
68  resampler->resampler = speex_resampler_init(channels, from_rate, to_rate, quality, &err);
69 
70  if (!resampler->resampler) {
71  free(resampler);
72  return SWITCH_STATUS_GENERR;
73  }
74 
75  *new_resampler = resampler;
76  lto_rate = (double) resampler->to_rate;
77  lfrom_rate = (double) resampler->from_rate;
78  resampler->from_rate = from_rate;
79  resampler->to_rate = to_rate;
80  resampler->factor = (lto_rate / lfrom_rate);
81  resampler->rfactor = (lfrom_rate / lto_rate);
82  resampler->channels = channels;
83 
84  //resampler->to_size = resample_buffer(to_rate, from_rate, (uint32_t) to_size);
85 
86  resampler->to_size = switch_resample_calc_buffer_size(resampler->to_rate, resampler->from_rate, to_size) / 2;
87  resampler->to = malloc(resampler->to_size * sizeof(int16_t) * resampler->channels);
88  switch_assert(resampler->to);
89 
90  return SWITCH_STATUS_SUCCESS;
91 }
#define switch_resample_calc_buffer_size(_to, _from, _srclen)
#define switch_zmalloc(ptr, len)
An audio resampling handle.
#define switch_assert(expr)
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.

Parameters
resamplerthe resample handle
srcthe source data
srclenthe length of the source data
Returns
the used size of dst

Definition at line 93 of file switch_resample.c.

References switch_assert, and switch_resample_calc_buffer_size.

Referenced by switch_core_asr_feed(), switch_core_file_read(), switch_core_file_write(), switch_core_session_read_frame(), switch_core_session_write_frame(), and switch_core_speech_read_tts().

94 {
95  int to_size = switch_resample_calc_buffer_size(resampler->to_rate, resampler->from_rate, srclen) / 2;
96 
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);
100  switch_assert(resampler->to);
101  }
102 
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;
106 }
#define switch_resample_calc_buffer_size(_to, _from, _srclen)
#define switch_assert(expr)
int switch_short_to_float ( short *  s,
float *  f,
int  len 
)

Convert an array of shorts to an array of floats.

Parameters
san array of shorts
fan array of floats
lenthe size of the buffers
Returns
the size of the converted buffer

Definition at line 177 of file switch_resample.c.

References NORMFACT.

178 {
179  int i;
180 
181  for (i = 0; i < len; i++) {
182  f[i] = (float) (s[i]) / NORMFACT;
183  /* f[i] = (float) s[i]; */
184  }
185  return len;
186 }
#define NORMFACT
void switch_swap_linear ( int16_t *  buf,
int  len 
)

Perform a byteswap on a buffer of 16 bit samples.

Parameters
bufan array of samples
lenthe size of the array

Definition at line 189 of file switch_resample.c.

Referenced by read_rtp_packet(), rtp_common_write(), and switch_ivr_play_file().

190 {
191  int i;
192  for (i = 0; i < len; i++) {
193  buf[i] = ((buf[i] >> 8) & 0x00ff) | ((buf[i] << 8) & 0xff00);
194  }
195 }
switch_byte_t switch_byte_t * buf