/*  $Id: echofilter_generic.cpp,v 1.6 2003/11/11 10:13:25 grumbel Exp $
**
**  ClanLib Game SDK
**  Copyright (C) 2003  The ClanLib Team
**  For a total list of contributers see the file CREDITS.
**
**  This library is free software; you can redistribute it and/or
**  modify it under the terms of the GNU Lesser General Public
**  License as published by the Free Software Foundation; either
**  version 2.1 of the License, or (at your option) any later version.
**
**  This library is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
**  Lesser General Public License for more details.
**
**  You should have received a copy of the GNU Lesser General Public
**  License along with this library; if not, write to the Free Software
**  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
**
*/

#include "Sound/precomp.h"

#ifdef _MSC_VER
#pragma warning (disable:4786)
#endif

#include "echofilter_generic.h"
#include <memory.h>

CL_EchoFilter::CL_EchoFilter(int buffer_size, int shift_factor)
{
	impl = new CL_EchoFilter_Generic;
	impl->buffer_size = buffer_size;
	impl->shift_factor = shift_factor;
	impl->pos = 0;

	impl->buffer[0] = new int[buffer_size];
	memset(impl->buffer[0], 0, sizeof(int)*buffer_size);

	impl->buffer[1] = new int[buffer_size];
	memset(impl->buffer[1], 0, sizeof(int)*buffer_size);
}

CL_EchoFilter::~CL_EchoFilter()
{
	delete[] impl->buffer[0];
	delete[] impl->buffer[1];
	delete impl;
}

void CL_EchoFilter::filter(int **sample_data, int num_samples, int channels)
{
	int start_pos = impl->pos;

	for (int c=0; c<2; c++)
	{
		if (c == channels) break;

		int *data = sample_data[c];
		int *buffer = impl->buffer[c];
		int &pos = impl->pos;
		int &buffer_size = impl->buffer_size;
		int &shift_factor = impl->shift_factor;

		pos = start_pos;

		for (int i=0; i<num_samples; i++)
		{
			buffer[pos] /= shift_factor;
			buffer[pos] += data[i];
			data[i] = buffer[pos];
			pos++;
			if (pos == buffer_size) pos = 0;
		}
	}
}
