Changeset 5181:ca57e34b2a35 in roaraudio for libroardsp/filter_lowp.c


Ignore:
Timestamp:
10/22/11 16:17:35 (13 years ago)
Author:
phi
Branch:
default
Phase:
public
Message:

updated filters to support 8 and 32 bit as well as 16 bit

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libroardsp/filter_lowp.c

    r5170 r5181  
    3030int roardsp_lowp_init  (struct roardsp_filter * filter, struct roar_stream * stream, int id) { 
    3131 struct roardsp_lowp * self = roar_mm_malloc(sizeof(struct roardsp_lowp)); 
    32  float freq = filter->rate/2.45; 
    3332 
    3433 ROAR_DBG("roardsp_lowp_init(*): self=%p", self); 
     
    4140 filter->inst = (void*) self; 
    4241 
    43  roardsp_lowp_ctl(filter, ROARDSP_FCTL_FREQ, &freq); 
    44  
    45  return 0; 
     42 return roardsp_filter_reset(filter, ROARDSP_RESET_FULL); 
    4643} 
    4744 
    4845int roardsp_lowp_uninit(struct roardsp_filter * filter) { 
    49  
    5046 roar_mm_free(filter->inst); 
    5147 return 0; 
    5248} 
    5349 
    54 int roardsp_lowp_calc16  (struct roardsp_filter * filter, void * data, size_t samples) { 
    55  struct roardsp_lowp * self = (struct roardsp_lowp *) filter->inst; 
    56  int16_t * samp = (int16_t *) data; 
    57  register int32_t s; 
    58  int i, c; 
    59  int channels = filter->channels; 
     50#define _calcX(bits,twobits) \ 
     51int roardsp_lowp_calc##bits  (struct roardsp_filter * filter, void * data, size_t samples) { \ 
     52 struct roardsp_lowp * self = (struct roardsp_lowp *) filter->inst; \ 
     53 int##bits##_t * samp = (int##bits##_t *) data; \ 
     54 register int##twobits##_t s; \ 
     55 int i, c; \ 
     56 int channels = filter->channels; \ 
     57\ 
     58 if ( channels > ROAR_MAX_CHANNELS ) \ 
     59  return -1; \ 
     60\ 
     61 samples /= channels; \ 
     62\ 
     63 ROAR_DBG("roardsp_lowp_calc16(*): filtering %i frames of %i channels...", samples, channels); \ 
     64\ 
     65/*  *      output[N] = input[N] * A + output[N-1] * B */ \ 
     66\ 
     67 for (i = 0; i < samples; i++) { \ 
     68  for (c = 0; c < channels; c++) { \ 
     69   s = (int##twobits##_t)samp[i*channels + c] * self->a + (int##twobits##_t)self->old[c] * self->b; \ 
     70\ 
     71   s /= 65536; \ 
     72\ 
     73   samp[i*channels + c] = s; \ 
     74   self->old[        c] = s; \ 
     75  } \ 
     76 } \ 
     77\ 
     78 return 0; \ 
     79} 
    6080 
    61  if ( channels > ROAR_MAX_CHANNELS ) 
    62   return -1; 
    63  
    64  samples /= channels; 
    65  
    66  ROAR_DBG("roardsp_lowp_calc16(*): filtering %i frames of %i channels...", samples, channels); 
    67  
    68 //  *      output[N] = input[N] * A + output[N-1] * B 
    69  
    70  for (i = 0; i < samples; i++) { 
    71   for (c = 0; c < channels; c++) { 
    72    s = samp[i*channels + c] * self->a + self->old[c] * self->b; 
    73  
    74    s /= 65536; 
    75  
    76    samp[i*channels + c] = s; 
    77    self->old[        c] = s; 
    78   } 
    79  } 
    80  
    81  return 0; 
    82 } 
     81_calcX(8,32) /* we need 32 bit not 16 bit here as we multiplay with 65536 internally */ 
     82_calcX(16,32) 
     83_calcX(32,64) 
    8384 
    8485int roardsp_lowp_ctl   (struct roardsp_filter * filter, int cmd, void * data) { 
     
    8889 float newfreq; 
    8990 
    90  if ( cmd != ROARDSP_FCTL_FREQ ) 
     91 if ( cmd != ROARDSP_FCTL_FREQ ) { 
     92  roar_err_set(ROAR_ERROR_BADRQC); 
    9193  return -1; 
     94 } 
    9295 
    9396 newfreq = *(float*)data; 
    9497 
    95  lp = exp(-2 * M_PI * newfreq / filter->rate) * 65536; 
     98 lp = exp(-2. * M_PI * newfreq / filter->rate) * 65536.; 
    9699 
    97100 self->b = lp; 
    98  self->a = 65536 - lp; 
     101 self->a = 65536. - lp; 
    99102 
    100  oldfreq = self->freq / 1000; 
    101  self->freq = newfreq * 1000; 
     103 oldfreq = self->freq / 1000.; 
     104 self->freq = newfreq * 1000.; 
    102105 
    103106 *(float*)data = oldfreq; 
Note: See TracChangeset for help on using the changeset viewer.