Changeset 5173:96a378947ca8 in roaraudio


Ignore:
Timestamp:
10/22/11 00:44:25 (12 years ago)
Author:
phi
Branch:
default
Phase:
public
Message:

imporved the clip filter

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • include/libroardsp/filters.h

    r4953 r5173  
    6464}; 
    6565 
     66enum roardsp_clip_mode { 
     67 ROARDSP_CLIP_MODE_LIMIT = 0, 
     68 ROARDSP_CLIP_MODE_ZERO  = 1, 
     69 ROARDSP_CLIP_MODE_WARP  = 2, 
     70 ROARDSP_CLIP_MODE_NOISE  = 3, 
     71}; 
     72 
     73struct roardsp_clip { 
     74 enum roardsp_clip_mode mode; 
     75 int32_t limit; 
     76}; 
     77 
    6678struct roardsp_swap { 
    6779 int map[ROAR_MAX_CHANNELS]; 
     
    116128int roardsp_quantify_reset (struct roardsp_filter * filter, int what); 
    117129 
     130int roardsp_clip_init  (struct roardsp_filter * filter, struct roar_stream * stream, int id); 
     131int roardsp_clip_uninit(struct roardsp_filter * filter); 
    118132int roardsp_clip_calc16(struct roardsp_filter * filter, void * data, size_t samples); 
    119133int roardsp_clip_ctl   (struct roardsp_filter * filter, int cmd, void * data); 
  • libroardsp/filter.c

    r5171 r5173  
    4747 {ROARDSP_FILTER_QUANTIFY, "Quantifier", roardsp_quantify_init, NULL, roardsp_quantify_ctl, roardsp_quantify_reset, { 
    4848           {NULL, NULL, NULL},{NULL, NULL, NULL},{roardsp_quantify_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}}, 
    49  {ROARDSP_FILTER_CLIP, "Clip", roardsp_quantify_init, NULL, roardsp_clip_ctl, roardsp_clip_reset, { 
     49 {ROARDSP_FILTER_CLIP, "Clip", roardsp_clip_init, roardsp_clip_uninit, roardsp_clip_ctl, roardsp_clip_reset, { 
    5050           {NULL, NULL, NULL},{NULL, NULL, NULL},{roardsp_clip_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}}, 
    5151 {ROARDSP_FILTER_DOWNMIX, "downmix", roardsp_quantify_init, NULL, roardsp_downmix_ctl, roardsp_downmix_reset, { 
  • libroardsp/filter_clip.c

    r4708 r5173  
    2626#include "libroardsp.h" 
    2727 
     28int roardsp_clip_init  (struct roardsp_filter * filter, struct roar_stream * stream, int id) { 
     29 if ( (filter->inst = roar_mm_malloc(sizeof(struct roardsp_clip))) == NULL ) 
     30  return -1; 
     31 
     32 return roardsp_filter_reset(filter, ROARDSP_RESET_FULL); 
     33} 
     34 
     35int roardsp_clip_uninit(struct roardsp_filter * filter) { 
     36 roar_mm_free(filter->inst); 
     37 return 0; 
     38} 
     39 
     40static inline int16_t _clip16(int16_t s, struct roardsp_clip * self) { 
     41 switch (self->mode) { 
     42  case ROARDSP_CLIP_MODE_LIMIT: 
     43    return s > 0 ? self->limit : -self->limit; 
     44   break; 
     45  case ROARDSP_CLIP_MODE_ZERO: 
     46    return 0; 
     47   break; 
     48  case ROARDSP_CLIP_MODE_WARP: 
     49    if ( s > 0 ) { 
     50     return s - self->limit - 32768; 
     51    } else { 
     52     return 32767 - s + self->limit; 
     53    } 
     54   break; 
     55  case ROARDSP_CLIP_MODE_NOISE: 
     56    return (s > 0 ? 1 : -1) * (self->limit - (roar_random_uint16() & 0xFF)); 
     57   break; 
     58 } 
     59 
     60 ROAR_WARN("_clip16(s=%i, self=%p{.mode=%i, ...}) = 0 // ERROR: Bad mode", (int)s, self, (int)self->mode); 
     61 return 0; 
     62} 
     63 
    2864int roardsp_clip_calc16  (struct roardsp_filter * filter, void * data, size_t samples) { 
     65 struct roardsp_clip * self = filter->inst; 
    2966 int16_t * samp = (int16_t *) data; 
    30  register int32_t s = (ROAR_INSTINT)filter->inst; 
     67 register int32_t s = self->limit; 
    3168 size_t i; 
    3269 
    3370 for (i = 0; i < samples; i++) { 
    3471  if ( samp[i] > s ) { 
    35    samp[i]  = s; 
     72   samp[i]  = _clip16(samp[i], self); 
    3673  } else if ( -samp[i] > s ) { 
    37    samp[i]  = -s; 
     74   samp[i]  = _clip16(samp[i], self); 
    3875  } 
    3976 } 
     
    4481 
    4582int roardsp_clip_ctl   (struct roardsp_filter * filter, int cmd, void * data) { 
     83 struct roardsp_clip * self = filter->inst; 
    4684 int32_t old; 
    4785 
    48  if ( cmd == ROARDSP_FCTL_LIMIT ) { 
    49   old = (ROAR_INSTINT)filter->inst; 
    50   filter->inst = (void*)(ROAR_INSTINT)labs(*(int32_t*)data); 
    51   *(int32_t*)data = old; 
    52  } else { 
    53   ROAR_DBG("roardsp_clip_ctl(*) = -1"); 
    54   return -1; 
     86 switch (cmd) { 
     87  case ROARDSP_FCTL_LIMIT: 
     88    old = self->limit; 
     89    self->limit = labs(*(int32_t*)data); 
     90    *(int32_t*)data = old; 
     91   break; 
     92  case ROARDSP_FCTL_MODE: 
     93    old = self->mode; 
     94    self->mode = *(int32_t*)data; 
     95    *(int32_t*)data = old; 
     96   break; 
     97  default: 
     98    ROAR_DBG("roardsp_clip_ctl(*) = -1"); 
     99    return -1; 
     100   break; 
    55101 } 
    56102 
     
    62108int roardsp_clip_reset (struct roardsp_filter * filter, int what) { 
    63109 int32_t n = 16384; 
     110 int32_t mode = ROARDSP_CLIP_MODE_LIMIT; 
    64111 
    65112 if ( filter == NULL ) 
     
    73120  case ROARDSP_RESET_FULL: 
    74121    roardsp_clip_ctl(filter, ROARDSP_FCTL_LIMIT, &n); 
     122    roardsp_clip_ctl(filter, ROARDSP_FCTL_MODE, &mode); 
    75123    return  0; 
    76124   break; 
Note: See TracChangeset for help on using the changeset viewer.