Changeset 1333:986b64dde6be in mediastreamer2


Ignore:
Timestamp:
Mar 31, 2011 10:33:27 AM (2 years ago)
Author:
Simon Morlat <simon.morlat@…>
Branch:
default
Children:
1334:27399177e928, 1336:afbd05489f6e
Message:

implement smart sample thrower to synchronize both path of echo canceller. works well.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/speexec.c

    r1332 r1333  
    3131#include <malloc.h> /* for alloca */ 
    3232#endif 
     33 
     34typedef struct _AudioFlowController{ 
     35        int target_samples; 
     36        int total_samples; 
     37        int current_pos; 
     38        int current_dropped; 
     39}AudioFlowController; 
     40 
     41void audio_flow_controller_init(AudioFlowController *ctl){ 
     42        ctl->target_samples=0; 
     43        ctl->total_samples=0; 
     44        ctl->current_pos=0; 
     45        ctl->current_dropped=0; 
     46} 
     47 
     48void audio_flow_controller_set_target(AudioFlowController *ctl, int samples_to_drop, int total_samples){ 
     49        ctl->target_samples=samples_to_drop; 
     50        ctl->total_samples=total_samples; 
     51        ctl->current_pos=0; 
     52        ctl->current_dropped=0; 
     53} 
     54 
     55mblk_t * audio_flow_controller_process(AudioFlowController *ctl, mblk_t *m){ 
     56        if (ctl->total_samples>0 && ctl->target_samples>0){ 
     57                int nsamples=(m->b_wptr-m->b_rptr)/2; 
     58                int th_dropped; 
     59                int todrop; 
     60         
     61                ctl->current_pos+=nsamples; 
     62                th_dropped=(ctl->target_samples*ctl->current_pos)/ctl->total_samples; 
     63                todrop=th_dropped-ctl->current_dropped; 
     64                if (todrop>0){ 
     65                        if (todrop>nsamples) todrop=nsamples; 
     66                        m->b_wptr-=todrop*2; 
     67                        //ms_message("th_dropped=%i, current_dropped=%i, %i samples dropped.",th_dropped,ctl->current_dropped,todrop); 
     68                        ctl->current_dropped+=todrop; 
     69                } 
     70        } 
     71        return m; 
     72} 
     73 
    3374 
    3475//#define EC_DUMP 1 
     
    4182static const float smooth_factor=0.05; 
    4283static const int framesize=128; 
     84static const int flow_control_interval_ms=5000; 
    4385 
    4486 
     
    5597        int nominal_ref_samples; 
    5698        int min_ref_samples; 
     99        AudioFlowController afc; 
    57100#ifdef EC_DUMP 
    58101        FILE *echofile; 
     
    131174        s->min_ref_samples=-1; 
    132175        s->nominal_ref_samples=delay_samples; 
     176        audio_flow_controller_init(&s->afc); 
    133177} 
    134178 
     
    156200        if (f->inputs[0]!=NULL){ 
    157201                while((refm=ms_queue_get(f->inputs[0]))!=NULL){ 
    158                         mblk_t *cp=dupmsg(refm); 
     202                        mblk_t *cp=dupmsg(audio_flow_controller_process(&s->afc,refm)); 
    159203                        ms_bufferizer_put(&s->delayed_ref,cp); 
    160204                        ms_queue_put(f->outputs[0],refm); 
     
    176220                        refm->b_wptr+=nbytes; 
    177221                        ms_bufferizer_put(&s->delayed_ref,refm); 
     222                        ms_queue_put(f->outputs[0],dupmsg(refm)); 
    178223                        if (!s->using_zeroes){ 
    179224                                ms_warning("Not enough ref samples, using zeroes"); 
     
    211256 
    212257        /*verify our ref buffer does not become too big, meaning that we are receiving more samples than we are sending*/ 
    213         if (f->ticker->time % 5000 == 0 && s->min_ref_samples!=-1){ 
     258        if (f->ticker->time % flow_control_interval_ms == 0 && s->min_ref_samples!=-1){ 
    214259                int diff=s->min_ref_samples-s->nominal_ref_samples; 
    215260                if (diff>nbytes){ 
    216                         int purge=diff-(nbytes/2); 
    217                         ms_warning("echo canceller: we are accumulating too much reference signal, purging now %i bytes",purge); 
    218                         ms_bufferizer_skip_bytes(&s->delayed_ref,purge); 
     261                        int purge=(diff-(nbytes/2))/2; 
     262                        ms_warning("echo canceller: we are accumulating too much reference signal, need to throw out %i samples",purge); 
     263                        audio_flow_controller_set_target(&s->afc,purge,(flow_control_interval_ms*s->samplerate)/1000); 
    219264                } 
    220265                s->min_ref_samples=-1; 
Note: See TracChangeset for help on using the changeset viewer.