
import java.util.*;
import java.io.FileOutputStream;

public class hnwfile{
	// 16000 16 bit mono signed big endian
	// size of number of samples to smooth
	// lager = more coarse sounds
	static int harshness = 60;
	// 200 * 100 = 10 minutes
	static int noiselen = 200 * 100;  // length of piece
	//-------------------------------------------//
	public static void main(String args[]){
		new hnwfile();
	}//end main
	//-------------------------------------------//
	public hnwfile(){//constructor
		playData();
	}//end constructor

	private void playData() {
		try{
			FileOutputStream out = null;
			out = new FileOutputStream("HNW.AU");
			out.write(46); // .
			out.write(115); // S
			out.write(110); // N
			out.write(100); // D
			// Header size
			out.write(0); // 0
			out.write(0); // 0
			out.write(0); // 0
			out.write(24); // 24
			// Data size - default ffff
			out.write(255); // ff
			out.write(255); // ff
			out.write(255); // ff
			out.write(255); // ff
			// sample type 3 = pcm
			out.write(0); // 0
			out.write(0); // 0
			out.write(0); // 
			out.write(3); // 
			// sample rate 16000
			out.write(0); // 0
			out.write(0); // 0
			out.write(62); // 16000
			out.write(128); // 
			// 2 channels
			out.write(0); // 0
			out.write(0); // 0
			out.write(0); // 
			out.write(2); //
			//
			// need to generate random numbers
			Random generator = new Random();
			// harshness the higher this value the more rough / harsh
			double ss[] = new double[harshness];
			double sa;
			short s; // signed two byte integer  for PCM data


			for(int z = -1; z < noiselen; z++){ // make this big for long pieces or
				// if noiselen was 0 from commandline run forever!
				// note only play half buffer to stop gaps???
				if (noiselen < 1) z = -2;
				//----------------------- loop ------------------------

				for(int cnt = 0; cnt < 1000; cnt = cnt + 2 ){
					// for testing so i can see the data	 
					//  for(int cnt = 0; cnt < 100; cnt = cnt + 2 ){
					int r = generator.nextInt();


					s = (short) r;
					// store new sample - roll out others	   
					for(int x = 0; x < harshness-1; x++){
						ss[x] = ss[x+1];
					} // end of x for
					ss[harshness-1] = (short)s;
					// average  the last harshness number of samples
					// remove the higher pitches
					sa = 0;
					for(int x = 0; x < harshness; x++){
						sa = sa + ss[x];
					}
					sa = sa / harshness;
					s = (short) sa;
					//============ Process  for new sounds ========		   

					//	   loudness====  !!!!
					// basically no wimpy numbers - only fat ones
					if (s > 0 & s < 30000) {s = (short) (s + 30000);}
					if (s < 0 & s > -30000) {s = (short) (s - 30000);}

					//   System.out.println(s);
					//=============== End of Process ============
					// as the output stream is a byte array load the low/high integer	   
					byte hexBase ; // A byte of all ones
					hexBase = (byte) 255;
					byte b1 = (byte) (hexBase & s);
					byte b2 = (byte) ((hexBase <<(8)& s) >> 8);
					out.write(b2);
					out.write(b1);
				} // end of process this 200 bytes
			} // end of for loop = length of piece
			out.close();
		}catch (Exception e) {
			e.printStackTrace();
			System.exit(0);
		}//end catch

	}//end playData

}//end outer class 

