/*******************************************************************************
* test 01, a java modification of putwave.c, described below:
* 
* "putwave.c: calculates 1 second of samples for a 1000 Hz waveform 
*  and puts them out as 1-megabyte blocks, 
*  containing 256 32-bit floating point numbers each, 
*  in binary form (but without any file header information)"
* 
* "translated" by Sky Frostenson 1.19.2004: 
*  -- frequency changed to 200 Hz for no reason really.
*  -- 3 signals now added together, with one central freq and two 
*     with a +- offset (range is +-40) so the end result will
*     sometimes produce beats.  not verified because i had problems
*     implementing the Java sound API.  weak excuse, i know.
* 
* original c prog putwave.c "WaveAsText" Created by Christopher Dobrian on Wed Jan 14 2004.
*******************************************************************************/
  
import javax.sound.sampled.*;
import java.io.*;
import java.util.*;
  
public class test01 {

	public static final int NUM_SECONDS = 1;
	public static final int SAMPLE_RATE = 44100;
	public static final int BUF_SIZE = 256;
	public static final double MAXAMP = .33;  // .33 is assuming 3 signals
	public static final double FREQUENCY = 200.0;
	

	public static void main(String[] args) {

          Random random = new Random(); 
	    int j = random.nextInt(40);
	
	    double amplitude = MAXAMP,
	    	   frequency = FREQUENCY,
		   posFreqOffset = FREQUENCY + j,
		   negFreqOffset = FREQUENCY - j,
	    	   phase = 0.0,
	    	   sig1twopiFoverR = ((2*Math.PI) * frequency / SAMPLE_RATE),
	    	   sig2twopiFoverR = ((2*Math.PI) * posFreqOffset / SAMPLE_RATE),
	    	   sig3twopiFoverR = ((2*Math.PI) * negFreqOffset / SAMPLE_RATE);

	    double[] y = new double[BUF_SIZE]; // buffer of sample values
	    	
	    long n = 0, // the current sample number
	    	 totalsamples = (NUM_SECONDS * SAMPLE_RATE);
	    	 
	    int o = 0; //counter for array
	    	
	    for( n = 0 ; n < totalsamples ; n++ ) {
	    
	        if (o >= BUF_SIZE) {    

	        	for ( o = 0 ; o < BUF_SIZE ; o++ ) {
	            	System.out.println(y[o]);
	            }
	            	
	            o = 0;
	        }
	        y[o++] = (amplitude * Math.sin(sig1twopiFoverR*n+phase))+(amplitude * Math.sin(sig2twopiFoverR*n+phase))+(amplitude * Math.sin(sig3twopiFoverR*n+phase));
	    }
	    
	    for ( o = 0 ; o < BUF_SIZE ; o++ ) {
	    	System.out.println(y[o]);
	    }
	    
	}
}

