/* $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" 
 */

// StaticWave.java

//
// Description:
//	Static sound wave file
//	  
// Author:
//	Peter Pilgrim 
//	Wed May 26 12:53:22 BST 1999
//	  
// RCS HEADER
// 
// $Author$
// $Date$
// $Source$
// $Revision$    $State$    $Locker$
//
// ================================================================================
// History
//	  $Log$
// 
// 
//


package xenon.soundkit;

import java.util.*;

/**
 * StaticWave defines the principle  waveforms that are commonly used
 * in Electronics. These are the Sine, Square, Triangle, and Ramp 
 * periodic forms.
 *
 * 
 * @author Peter Pilgrim Sun Jun 14 23:27:16 BST 1998
 * @version $Id$ 
 *
 * NB: Note a ramp is also known as a sawtooth.
 *
 */
public class StaticWave extends Wave
{
    /** sine wave type */
    public final static int 	Sine = 1;
    /** square wave type */
    public final static int 	Square = 2;
    /** triangle wave type */
    public final static int 	Triangle = 3;
    /** ramp forwards wave type */
    public final static int 	RampForward = 4;
    /** ramp backwards wave type */
    public final static int 	RampBackward = 5;
    /** periodic white noise wave type */
    public final static int 	PeriodicWhiteNoise = 6;

    public static StaticWave	SINE_WAVE = new StaticWave( Sine );
    public static StaticWave	SQUARE_WAVE = new StaticWave( Square );
    public static StaticWave	TRIANGLE_WAVE = new StaticWave( Triangle );
    public static StaticWave	RAMP_FORWARD_WAVE = new StaticWave( RampForward );
    public static StaticWave	RAMP_BACKWARD_WAVE = new StaticWave( RampBackward );
    public static StaticWave	PERIODIC_WHITE_NOISE_WAVE = new StaticWave( PeriodicWhiteNoise );
    
    /** The waveform type */
    protected int waveform_type;
    

    /** 
     * Construct the waveform from an enumerated type
     *
     * @param waveform the waveform type
     *
     */
    public StaticWave( int waveform_type ) 
    {
	super();
	
	this.waveform_type = waveform_type;
	generateWaveform();
    }
    
    /** generates the waveform */
    protected void generateWaveform()
    {
	//
	//  2 pi == 360 degrees
	//   
	wave = new double[WAVELENGTH];
	int j;
	double twopi = Math.PI * 2.0;

	switch ( waveform_type ) {
	  case Sine:
	      /* Create the sine wave lookup table */
	    for (j=0; j<WAVELENGTH; ++j ) {
		wave[j] = Math.sin( twopi * j / (double)WAVELENGTH );
	    }
	    break;

	  case Square:
	      /* Create the square wave lookup table */
	      for (j=0; j<WAVELENGTH/2; ++j ) {
		  wave[j] = 1.0;
		  // DEBUG: System.out.println( " wave[" + j + "]  " + wave[j] );
	      }
	      for (j=WAVELENGTH/2; j<WAVELENGTH/2; ++j ) {
		  wave[j] = -1.0;
		  // DEBUG: System.out.println( " wave[" + j + "]  " + wave[j] );
	      }
	      break;
	      
	  case Triangle:
	      /* Create the triange wave lookup table */
	      for (j=0; j<WAVELENGTH/2; ++j) 
		  wave[j] = 1.0 - 4 * j/(double)WAVELENGTH;
	      for (j=WAVELENGTH/2; j<WAVELENGTH; ++j) 
		  wave[j] = -3.0 + 4 * j/(double)WAVELENGTH;
	      break;
	      
	  case RampForward:
	      /* Create the forward ramp lookup table */
	      for (j=0; j<WAVELENGTH; ++j) 
		  wave[j] = -1.0 +  2 * j/(double)WAVELENGTH;
	      break;


	  case RampBackward:
	      /* Create the backward ramp lookup table */
	      for (j=0; j<WAVELENGTH; ++j) 
		  wave[j] = 1.0 -  2 * j/(double)WAVELENGTH;
	      break;

	  case PeriodicWhiteNoise:
	      /* White Noise ramp lookup table */
	      for (j=0; j<WAVELENGTH; ++j) 
		  wave[j] = -1.0 + 2.0 * Math.random();
	      break;
	      
	  default:
	      throw new IllegalArgumentException(
		  "Unknown waveform type:"+waveform_type+" supplied" );
	}
    }

    /** Returns the waveform type */
    public int getWaveformType()
    {
	return (waveform_type);
    }

    /** Returns displayable name for the waveform */
    public String getDisplayName()
    {
	switch ( waveform_type ) {
	  case Sine:
	      return "Sine";
	      
	  case Square:
	      return "Square";
	      
	  case Triangle:
	      return "Triangle";
	      
	  case RampForward:
	      return "Ramp Forward";

	  case RampBackward:
	      return "Ramp Backward";

	  case PeriodicWhiteNoise:
	      return "Periodic White Noise";
	}
	    
	return "Static Unknown";
    }
}

// fini
