Audio Resample Functions
[Core Library]

Collaboration diagram for Audio Resample Functions:

Data Structures

struct  switch_audio_resampler_t
 An audio resampling handle. More...

Defines

#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.
void switch_resample_destroy (switch_audio_resampler_t **resampler)
 Destroy an existing resampler handle.
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.
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.
int switch_char_to_float (char *c, float *f, int len)
 Convert an array of chars to an array of floats.
int switch_float_to_char (float *f, char *c, int len)
 Convert an array of floats to an array of chars.
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.
void switch_generate_sln_silence (int16_t *data, uint32_t samples, uint32_t divisor)
 Generate static noise.
void switch_change_sln_volume (int16_t *data, uint32_t samples, int32_t vol)
 Change the volume of a signed linear audio frame.
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 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__)

Definition at line 86 of file switch_resample.h.

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


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:
data the audio data
samples the number of 2 byte samples
vol the volume factor -4 -> 4

Definition at line 333 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().

00334 {
00335         double newrate = 0;
00336         double pos[4] = {1.3, 2.3, 3.3, 4.3};
00337         double neg[4] = {.80, .60, .40, .20};
00338         double *chart;
00339         uint32_t i;
00340 
00341         if (vol == 0) return;
00342 
00343         switch_normalize_volume(vol);
00344 
00345         if (vol > 0) {
00346                 chart = pos;
00347         } else {
00348                 chart = neg;
00349         }
00350         
00351         i = abs(vol) - 1;
00352         
00353         switch_assert(i < 4);
00354 
00355         newrate = chart[i];
00356 
00357         if (newrate) {
00358                 int32_t tmp;
00359                 uint32_t x;
00360                 int16_t *fp = data;
00361 
00362                 for (x = 0; x < samples; x++) {
00363                         tmp = (int32_t) (fp[x] * newrate);
00364                         switch_normalize_to_16bit(tmp);
00365                         fp[x] = (int16_t) tmp;
00366                 }
00367         }
00368 }

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:
data the audio data
samples the number of 2 byte samples
vol the volume factor -12 -> 12

Definition at line 296 of file switch_resample.c.

References switch_assert, switch_normalize_to_16bit, and switch_normalize_volume_granular.

00297 {
00298         double newrate = 0;
00299         double pos[12] = {1.25, 1.50, 1.75, 2.0, 2.25, 2.50, 2.75, 3.0, 3.25, 3.50, 3.75, 4.0};
00300         double neg[12] = {.917, .834, .751, .668, .585, .502, .419, .336, .253, .017, .087, .004};
00301         double *chart;
00302         uint32_t i;
00303 
00304         if (vol == 0) return;
00305 
00306         switch_normalize_volume_granular(vol);
00307 
00308         if (vol > 0) {
00309                 chart = pos;
00310         } else {
00311                 chart = neg;
00312         }
00313         
00314         i = abs(vol) - 1;
00315         
00316         switch_assert(i < 12);
00317 
00318         newrate = chart[i];
00319 
00320         if (newrate) {
00321                 int32_t tmp;
00322                 uint32_t x;
00323                 int16_t *fp = data;
00324 
00325                 for (x = 0; x < samples; x++) {
00326                         tmp = (int32_t) (fp[x] * newrate);
00327                         switch_normalize_to_16bit(tmp);
00328                         fp[x] = (int16_t) tmp;
00329                 }
00330         }
00331 }

int switch_char_to_float ( char *  c,
float *  f,
int  len 
)

Convert an array of chars to an array of floats.

Parameters:
c the char buffer
f the float buffer
len the length of the buffers
Returns:
the size of the converted buffer

Definition at line 126 of file switch_resample.c.

References MAXSAMPLE, and NORMFACT.

00127 {
00128         int i;
00129 
00130         if (len % 2) {
00131                 return (-1);
00132         }
00133 
00134         for (i = 1; i < len; i += 2) {
00135                 f[(int) (i / 2)] = (float) (((c[i]) * 0x100) + c[i - 1]);
00136                 f[(int) (i / 2)] /= NORMFACT;
00137                 if (f[(int) (i / 2)] > MAXSAMPLE)
00138                         f[(int) (i / 2)] = MAXSAMPLE;
00139                 if (f[(int) (i / 2)] < -MAXSAMPLE)
00140                         f[(int) (i / 2)] = -MAXSAMPLE;
00141         }
00142         return len / 2;
00143 }

int switch_float_to_char ( float *  f,
char *  c,
int  len 
)

Convert an array of floats to an array of chars.

Parameters:
f an array of floats
c an array of chars
len the length of the buffers
Returns:
the size of the converted buffer

Definition at line 145 of file switch_resample.c.

References NORMFACT.

00146 {
00147         int i;
00148         float ft;
00149         long l;
00150         for (i = 0; i < len; i++) {
00151                 ft = f[i] * NORMFACT;
00152                 if (ft >= 0) {
00153                         l = (long) (ft + 0.5);
00154                 } else {
00155                         l = (long) (ft - 0.5);
00156                 }
00157                 c[i * 2] = (unsigned char) ((l) & 0xff);
00158                 c[i * 2 + 1] = (unsigned char) (((l) >> 8) & 0xff);
00159         }
00160         return len * 2;
00161 }

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:
f the float buffer
s the short buffer
len the length of the buffers
Returns:
the size of the converted buffer

Definition at line 107 of file switch_resample.c.

References MAXSAMPLE, and NORMFACT.

00108 {
00109         switch_size_t i;
00110         float ft;
00111         for (i = 0; i < len; i++) {
00112                 ft = f[i] * NORMFACT;
00113                 if (ft >= 0) {
00114                         s[i] = (short) (ft + 0.5);
00115                 } else {
00116                         s[i] = (short) (ft - 0.5);
00117                 }
00118                 if ((float) s[i] > MAXSAMPLE)
00119                         s[i] = (short) MAXSAMPLE / 2;
00120                 if (s[i] < (short) -MAXSAMPLE)
00121                         s[i] = (short) -MAXSAMPLE / 2;
00122         }
00123         return len;
00124 }

void switch_generate_sln_silence ( int16_t *  data,
uint32_t  samples,
uint32_t  divisor 
)

Generate static noise.

Parameters:
data the audio data buffer
samples the number of 2 byte samples
divisor the volume factor

Definition at line 184 of file switch_resample.c.

References switch_micro_time_now().

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

00185 {
00186         int16_t x;
00187         uint32_t i;
00188         int sum_rnd = 0;
00189         int16_t rnd2 = (int16_t) switch_micro_time_now() + (int16_t) (intptr_t) data;
00190 
00191         assert(divisor);
00192 
00193         if (divisor == (uint32_t)-1) {
00194                 memset(data, 0, sizeof(*data));
00195                 return;
00196         }
00197 
00198         for (i = 0; i < samples; i++, sum_rnd = 0) {
00199                 for (x = 0; x < 6; x++) {
00200                         rnd2 = rnd2 * 31821U + 13849U;
00201                         sum_rnd += rnd2;
00202                 }
00203                 //switch_normalize_to_16bit(sum_rnd);
00204                 *data = (int16_t) ((int16_t) sum_rnd / (int) divisor);
00205 
00206                 data++;
00207         }
00208 }

void switch_resample_destroy ( switch_audio_resampler_t **  resampler  ) 

Destroy an existing resampler handle.

Parameters:
resampler the resampler handle to destroy

Definition at line 94 of file switch_resample.c.

Referenced by switch_core_file_close(), switch_core_session_reset(), switch_core_session_write_frame(), and switch_core_speech_close().

00095 {
00096 
00097         if (resampler && *resampler) {
00098                 if ((*resampler)->resampler) {
00099                         speex_resampler_destroy((*resampler)->resampler);
00100                 }
00101                 free((*resampler)->to);
00102                 free(*resampler);
00103                 *resampler = NULL;
00104         }
00105 }

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_resampler NULL pointer to aim at the new handle
from_rate the rate to transfer from in hz
to_rate the rate to transfer to in hz
quality the quality desired
Returns:
SWITCH_STATUS_SUCCESS if the handle was created

Definition at line 55 of file switch_resample.c.

References resample_buffer, SWITCH_STATUS_GENERR, SWITCH_STATUS_SUCCESS, and switch_zmalloc.

00059 {
00060         int err = 0;
00061         switch_audio_resampler_t *resampler;
00062         double lto_rate, lfrom_rate;
00063 
00064         switch_zmalloc(resampler, sizeof(*resampler));
00065 
00066         resampler->resampler = speex_resampler_init(channels ? channels : 1, from_rate, to_rate, quality, &err);
00067 
00068         if (!resampler->resampler) {
00069                 free(resampler);
00070                 return SWITCH_STATUS_GENERR;
00071         }
00072 
00073         *new_resampler = resampler;
00074         lto_rate = (double) resampler->to_rate;
00075         lfrom_rate = (double) resampler->from_rate;
00076         resampler->from_rate = from_rate;
00077         resampler->to_rate = to_rate;
00078         resampler->factor = (lto_rate / lfrom_rate);
00079         resampler->rfactor = (lfrom_rate / lto_rate);
00080         resampler->to_size = resample_buffer(to_rate, from_rate, (uint32_t) to_size);
00081         resampler->to = malloc(resampler->to_size * sizeof(int16_t));
00082 
00083         return SWITCH_STATUS_SUCCESS;
00084 }

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:
resampler the resample handle
src the source data
srclen the length of the source data
Returns:
the used size of dst

Definition at line 87 of file switch_resample.c.

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

00088 {
00089         resampler->to_len = resampler->to_size;
00090         speex_resampler_process_interleaved_int(resampler->resampler, src, &srclen, resampler->to, &resampler->to_len);
00091         return resampler->to_len;
00092 }

int switch_short_to_float ( short *  s,
float *  f,
int  len 
)

Convert an array of shorts to an array of floats.

Parameters:
s an array of shorts
f an array of floats
len the size of the buffers
Returns:
the size of the converted buffer

Definition at line 163 of file switch_resample.c.

References NORMFACT.

00164 {
00165         int i;
00166 
00167         for (i = 0; i < len; i++) {
00168                 f[i] = (float) (s[i]) / NORMFACT;
00169                 /* f[i] = (float) s[i]; */
00170         }
00171         return len;
00172 }

void switch_swap_linear ( int16_t *  buf,
int  len 
)

Perform a byteswap on a buffer of 16 bit samples.

Parameters:
buf an array of samples
len the size of the array

Definition at line 175 of file switch_resample.c.

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

00176 {
00177         int i;
00178         for (i = 0; i < len; i++) {
00179                 buf[i] = ((buf[i] >> 8) & 0x00ff) | ((buf[i] << 8) & 0xff00);
00180         }
00181 }


Generated on Wed May 16 04:00:19 2012 for FreeSWITCH API Documentation by  doxygen 1.4.7