/* $Id$
 *
 * Copyright (c) 1999 Xenonsoft Limited. All Rights Reserved.
 *
 * This software is protected by copyright. You are hereby notified from
 * now by reading this message. This software is also the confidential 
 * and proprietary information of Xenonsoft Limited.  ("Confidential
 * Information"). 
 *
 * This software is distributed under the Xenonsoft Public end user 
 * License ("XPeL"), where the machine-readable source code is provided 
 * under the "Open Source" model.
 * For more information, please read the file "LICENSE-XPL.txt" 
 */

// DelaySoundFilter.java

//
// Description:
//	A simple delay sound filter
//	  
// Author:
//	Peter Pilgrim Wed Jun 23 00:01:03 BST 1999
//	  
// RCS HEADER
// 
// $Author$
// $Date$
// $Source$
// $Revision$    $State$    $Locker$
//
// ================================================================================
// History
//	  $Log$
// 
// 

package xenon.soundkit;

/**
 * A simple delay sound filter 
 *
 * TODO: need to make some improvement here with signal procs
 *
 * @author Peter Pilgrim Wed Jun 23 00:03:41 BST 1999
 * @version $Id$
 */
public class DelaySoundFilter extends SoundFilter {
    
    protected int delay;
    
    /**
     * @param delay the time delay factor
     */
    public DelaySoundFilter( int delay )
    {
	this( null, delay );
    }
    

    /**
     * @param parent the parent sound filter
     * @param delay the time delay factor
     */
    public DelaySoundFilter( SoundFilter parent, int delay )
    {
	super( parent );

	if ( delay < 0 )
	    throw new IllegalArgumentException(
		"delay value cannot be negative." );
	this.delay = delay;
    }
    
    /**
     * filters the sound linear data by added a delayed element
     *
     * @param se the sound engine
     * @param data_buffer the sound filter data buffer
     */
    public void filterLinearData( 
	SoundEngine se, 
	SoundFilterDataBuffer data_buffer )
    {
	// System.out.println("DelaySoundFilter("+delay+")" );
	
	if ( parent != null ) 
	    parent.filterLinearData( se, data_buffer );
	
	int    clip_length   = data_buffer.getClipLength();
	int    filter_length = data_buffer.filter_buffer.length;
	int [] filter_buffer = data_buffer.filter_buffer;

	// TODO: parameterise the coeffs
	// System.out.println( "length:"+filter_length+" clip_length:"+clip_length+" buffer:"+filter_buffer );
	
	for (int j=delay; j<clip_length; ++j) {
	    filter_buffer[j] = (int) 
		( 0.5*filter_buffer[j] + 0.5*filter_buffer[j-delay] );
	}
    }
    
    
}

// fini
