Main Page | Modules | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

Audio Resample Functions
[Core Library]


Data Structures

struct  switch_audio_resampler_t
 An audio resampling handle. More...

Functions

switch_status_t switch_resample_create (switch_audio_resampler_t **new_resampler, int from_rate, switch_size_t from_size, int to_rate, uint32_t to_size, switch_memory_pool_t *pool)
 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, float *src, int srclen, float *dst, uint32_t dstlen, int last)
 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.


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
00245 {
00246         double newrate = 0;
00247         int div = 0;
00248 
00249         switch_normalize_volume(vol);
00250 
00251         if (vol > 0) {
00252                 vol++;
00253         } else if (vol < 0) {
00254                 vol--;
00255         }
00256 
00257         newrate = vol * 1.3;
00258 
00259         if (vol < 0) {
00260                 newrate *= -1;
00261                 div++;
00262         }
00263 
00264         if (newrate) {
00265                 int32_t tmp;
00266                 uint32_t x;
00267                 int16_t *fp = data;
00268 
00269                 for (x = 0; x < samples; x++) {
00270                         tmp = (int32_t) (div ? fp[x] / newrate : fp[x] * newrate);
00271                         switch_normalize_to_16bit(tmp);
00272                         fp[x] = (int16_t) tmp;
00273                 }
00274         }
00275 }

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
00148 {
00149         int i;
00150 
00151         if (len % 2) {
00152                 return (-1);
00153         }
00154 
00155         for (i = 1; i < len; i += 2) {
00156                 f[(int) (i / 2)] = (float) (((c[i]) * 0x100) + c[i - 1]);
00157                 f[(int) (i / 2)] /= NORMFACT;
00158                 if (f[(int) (i / 2)] > MAXSAMPLE)
00159                         f[(int) (i / 2)] = MAXSAMPLE;
00160                 if (f[(int) (i / 2)] < -MAXSAMPLE)
00161                         f[(int) (i / 2)] = -MAXSAMPLE;
00162         }
00163         return len / 2;
00164 }

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
00167 {
00168         int i;
00169         float ft;
00170         long l;
00171         for (i = 0; i < len; i++) {
00172                 ft = f[i] * NORMFACT;
00173                 if (ft >= 0) {
00174                         l = (long) (ft + 0.5);
00175                 } else {
00176                         l = (long) (ft - 0.5);
00177                 }
00178                 c[i * 2] = (unsigned char) ((l) & 0xff);
00179                 c[i * 2 + 1] = (unsigned char) (((l) >> 8) & 0xff);
00180         }
00181         return len * 2;
00182 }

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
00129 {
00130         switch_size_t i;
00131         float ft;
00132         for (i = 0; i < len; i++) {
00133                 ft = f[i] * NORMFACT;
00134                 if (ft >= 0) {
00135                         s[i] = (short) (ft + 0.5);
00136                 } else {
00137                         s[i] = (short) (ft - 0.5);
00138                 }
00139                 if ((float) s[i] > MAXSAMPLE)
00140                         s[i] = (short) MAXSAMPLE;
00141                 if (s[i] < (short) -MAXSAMPLE)
00142                         s[i] = (short) -MAXSAMPLE;
00143         }
00144         return len;
00145 }

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
00205 {
00206         int16_t rnd, x;
00207         uint32_t i;
00208         int sum_rnd = 0;
00209 
00210         assert(divisor);
00211 
00212         for (i = 0; i < samples; i++, sum_rnd = 0) {
00213                 for (x = 0; x < 7; x++) {
00214                         rnd = (int16_t) (rand() * sizeof(int16_t));
00215                         sum_rnd += rnd;
00216                 }
00217                 switch_normalize_to_16bit(sum_rnd);
00218                 *data = (int16_t) ((int16_t) sum_rnd / (int) divisor);
00219 
00220                 data++;
00221         }
00222 }

switch_status_t switch_resample_create switch_audio_resampler_t **  new_resampler,
int  from_rate,
switch_size_t  from_size,
int  to_rate,
uint32_t  to_size,
switch_memory_pool_t pool
 

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
from_size the size of the buffer to allocate for the from data
to_rate the rate to transfer to in hz
to_size the size of the buffer to allocate for the to data
pool the memory pool to use for buffer allocation
Returns:
SWITCH_STATUS_SUCCESS if the handle was created
00058 {
00059 #ifdef DISABLE_RESAMPLE
00060         *new_resampler = NULL;
00061         return SWITCH_STATUS_NOTIMPL;
00062 #else
00063         switch_audio_resampler_t *resampler;
00064         double lto_rate, lfrom_rate;
00065 
00066         if ((resampler = switch_core_alloc(pool, sizeof(*resampler))) == 0) {
00067                 return SWITCH_STATUS_MEMERR;
00068         }
00069 
00070         resampler->from_rate = from_rate;
00071         resampler->to_rate = to_rate;
00072         lto_rate = (double) resampler->to_rate;
00073         lfrom_rate = (double) resampler->from_rate;
00074         resampler->factor = (lto_rate / lfrom_rate);
00075         resampler->rfactor = (lfrom_rate / lto_rate);
00076 
00077         resampler->resampler = resample_open(QUALITY, resampler->factor, resampler->factor);
00078         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Activate Resampler %d->%d %f\n", resampler->from_rate, resampler->to_rate,
00079                                           resampler->factor);
00080         resampler->from_size = resample_buffer(to_rate, from_rate, (uint32_t) from_size);
00081         resampler->from = (float *) switch_core_alloc(pool, resampler->from_size * sizeof(float));
00082         resampler->to_size = resample_buffer(to_rate, from_rate, (uint32_t) to_size);;
00083         resampler->to = (float *) switch_core_alloc(pool, resampler->to_size * sizeof(float));
00084 
00085         *new_resampler = resampler;
00086         return SWITCH_STATUS_SUCCESS;
00087 #endif
00088 }

Here is the call graph for this function:

void switch_resample_destroy switch_audio_resampler_t **  resampler  ) 
 

Destroy an existing resampler handle.

Parameters:
resampler the resampler handle to destroy
00116 {
00117 
00118         if (resampler && *resampler) {
00119 #ifndef DISABLE_RESAMPLE
00120                 if ((*resampler)->resampler) {
00121                         resample_close((*resampler)->resampler);
00122                 }
00123 #endif
00124                 *resampler = NULL;
00125         }
00126 }

uint32_t switch_resample_process switch_audio_resampler_t resampler,
float *  src,
int  srclen,
float *  dst,
uint32_t  dstlen,
int  last
 

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
dst the destination data
dstlen the length of the destination data
last parameter denoting the last sample is being resampled
Returns:
the used size of dst
00091 {
00092 #ifdef DISABLE_RESAMPLE
00093         return 0;
00094 #else
00095         int o = 0, srcused = 0, srcpos = 0, out = 0;
00096 
00097         for (;;) {
00098                 int srcBlock = MIN(srclen - srcpos, srclen);
00099                 int lastFlag = (last && (srcBlock == srclen - srcpos));
00100                 o = resample_process(resampler->resampler, resampler->factor, &src[srcpos], srcBlock, lastFlag, &srcused, &dst[out], dstlen - out);
00101                 /* printf("resampling %d/%d (%d) %d %f\n",  srcpos, srclen,  MIN(dstlen-out, dstlen), srcused, factor); */
00102 
00103                 srcpos += srcused;
00104                 if (o >= 0) {
00105                         out += o;
00106                 }
00107                 if (o < 0 || (o == 0 && srcpos == srclen)) {
00108                         break;
00109                 }
00110         }
00111         return out;
00112 #endif
00113 }

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
00185 {
00186         int i;
00187 
00188         for (i = 0; i < len; i++) {
00189                 f[i] = (float) (s[i]) / NORMFACT;
00190                 /* f[i] = (float) s[i]; */
00191         }
00192         return len;
00193 }

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
00197 {
00198         int i;
00199         for (i = 0; i < len; i++) {
00200                 buf[i] = ((buf[i] >> 8) & 0x00ff) | ((buf[i] << 8) & 0xff00);
00201         }
00202 }


Generated on Fri Oct 10 11:25:13 2008 for FreeSWITCH by  doxygen 1.3.9.1