// Copyright 2016-2021 XMOS LIMITED.
// This Software is subject to the terms of the XMOS Public Licence: Version 1.

#if UART_DEBUG || DEBUG_MEMORY_LOG_ENABLED
#define DEBUG_PRINT_ENABLE 1
#endif

#include <xs1.h>
#include <xua_conf.h>
#include "ringbuf.h"
#include <xcore/chanend.h>
#include <xcore/channel.h>
#include "dnr_dsp_buf.h"
#include "xc_ptr.h"
#include "debug_print.h"

#if DNR_ENABLE == 1
unsigned  g_dsp_eof = 0;
extern float g_dnr_level; 
void dnr_exchange_buffer(int32_t *data) {
    static unsigned buff_index = 0;
    int dnr_level = (int)g_dnr_level;
    if (dnr_level != 0) {
        write_to_sample_in_buf(data[1], buff_index, 0);
        read_from_sample_out_buf(&data[1],buff_index, 0);
    }
    data[0] = data[1];


    buff_index++;
#if DNR_11MS
    if (buff_index == 128)
    {
#else
    if (buff_index == 512)
    {
#endif
        buf_slot_rotate();
        buff_index = 0;
        SET_SHARED_GLOBAL(g_dsp_eof, 1);
    }
}


void Aizip_DNR_init(void);

void dnr_dsp_proc_task(void)
{
    Aizip_DNR_init();
    while(1) {
        unsigned dsp_eof = 0;
        GET_SHARED_GLOBAL(dsp_eof, g_dsp_eof);
        if (dsp_eof) {
            SET_SHARED_GLOBAL(g_dsp_eof, 0);
            start_dsp_processing();
        }
    }
}

#endif