/*---------------------------------------------------------------------------*/
/*
  File: slec.h

  Line echo canceller.
*/
/*---------------------------------------------------------------------------*/

#include "platform.h"
#include "sdsputil.h"
#include "slec.h"

#define FILE_ "slec.cpp"

NAMESPACE_BEGIN

/*---------------------------------------------------------------------------*/

void softdsp_lec_init(t_softdsp_lec *p_lec, word options, word span_samples, word predelay_samples, word sparse_span_samples)
{
    word i;

    p_lec->state = 0;
    p_lec->predelay_size = predelay_samples;
    p_lec->predelay_read_pos = 0;
    p_lec->predelay_write_pos = 0;
    for(i = 0; i < predelay_samples; i++) {
        p_lec->predelay_sample_buffer[i] = 0;
    }
    sparse_lec_init (&(p_lec->ec), sparse_span_samples, span_samples);
    p_lec->ec.options = options & ~DSP_LEC_RESET_COEFFICIENTS;
}

void softdsp_lec_flush(t_softdsp_lec *p_lec)
{
    word i;

    for(i = 0; i < p_lec->predelay_size; i++) {
        p_lec->predelay_sample_buffer[i] = 0;
    }
    sparse_lec_flush(&(p_lec->ec));
}

void softdsp_lec_s_exec(t_softdsp_lec *p_lec, const short *sin_samples, short *sout_samples, word sample_count)
{
    word sample_pos;

    for(sample_pos = 0; sample_pos < sample_count; sample_pos++) {
        sout_samples[sample_pos] = sparse_lec_exec (&(p_lec->ec), p_lec->predelay_sample_buffer[p_lec->predelay_read_pos], sin_samples[sample_pos]);
        p_lec->predelay_read_pos = (p_lec->predelay_read_pos < p_lec->predelay_size - 1) ? p_lec->predelay_read_pos + 1 : 0;
    }
    p_lec->state = p_lec->ec.state;
}

void softdsp_lec_r_exec(t_softdsp_lec *p_lec, const short *rin_samples, word sample_count)
{
    word sample_pos;

    for(sample_pos = 0; sample_pos < sample_count; sample_pos++) {
        p_lec->predelay_sample_buffer[p_lec->predelay_write_pos] = rin_samples[sample_pos];
        p_lec->predelay_write_pos = (p_lec->predelay_write_pos < p_lec->predelay_size - 1) ? p_lec->predelay_write_pos + 1 : 0;
    }
}


NAMESPACE_END

/*---------------------------------------------------------------------------*/
