using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using GenericGraph;
using WaveLib;

namespace HearingAidLab
{
	/// <summary>
	/// Summary description for GraphWav.
	/// </summary>
	public class GraphWav : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.Panel panel2;
		private GGraph graph1;
		private GGraph graph2;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public GraphWav()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			this.WindowState = FormWindowState.Maximized;

			graph1 = new GGraph();
			graph2 = new GGraph();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(GraphWav));
			this.panel1 = new System.Windows.Forms.Panel();
			this.panel2 = new System.Windows.Forms.Panel();
			this.SuspendLayout();
			// 
			// panel1
			// 
			this.panel1.Location = new System.Drawing.Point(8, 16);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(232, 176);
			this.panel1.TabIndex = 0;
			// 
			// panel2
			// 
			this.panel2.Location = new System.Drawing.Point(8, 208);
			this.panel2.Name = "panel2";
			this.panel2.Size = new System.Drawing.Size(232, 176);
			this.panel2.TabIndex = 1;
			// 
			// GraphWav
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(792, 566);
			this.Controls.Add(this.panel1);
			this.Controls.Add(this.panel2);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Name = "GraphWav";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "GraphWav";
			this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
			this.Load += new System.EventHandler(this.GraphWav_Load);
			this.ResumeLayout(false);

		}
		#endregion
		public void Init()
		{
			int x = this.Width;
			int y = this.Height;
			int BottomSpace = 50;
			int ySize = (y-BottomSpace-50)/2;

			panel1.Size = new Size(x-50,ySize);
			panel2.Size = new Size(x-50,ySize);
			
			panel1.Location = new Point(8,30);
			panel2.Location = new Point(8,40+ySize);
			graph1.SetPanel(panel1);
			graph2.SetPanel(panel2);
			graph1.SetSize(panel1.Size);
			graph2.SetSize(panel2.Size);
			graph1.title = "Time Domain";
			graph2.title = "Frequency Domain";
			graph1.xlabel = "Samples (Time)";
			graph2.xlabel = "Frequency (Hz)";
			graph1.ylabel = "Amplitude";
			graph2.ylabel = "Amplitude";
			graph1.BackgroundColor = Color.White;
			graph2.BackgroundColor = Color.White;
			graph2.ForeColor = Color.Red;
			graph1.ForeColor = Color.Blue;
			graph1.listLines.Add(new GLine());
			graph2.listLines.Add(new GLine());
			graph2.bFFT = true;
		}
		public void Draw()
		{
			graph1.Draw(PointToScreen(panel1.Location));
			graph2.Draw(PointToScreen(panel2.Location));
			
		}
		protected override void OnPaint(PaintEventArgs e)
		{
			base.OnPaint (e);
			Draw();
			
		}
		public unsafe void Graph(WaveBuffer buf)
		{
			MemoryStream ms = buf.ms;
			BinaryReader rd = buf.reader;
			ms.Seek(0,SeekOrigin.Begin);
			int nIdealSamples = (int) (ms.Length/4/2);
			int nRealSamples = 32768;
			while(nRealSamples<nIdealSamples)
			{
				nRealSamples <<= 1;
			}
			float[] x = new float[nRealSamples];
			int nCount = 0;
			int p=0;
			for(int z =0;z<ms.Length;z+=8)
			{
				p++;
				int i = rd.ReadInt32();
				if(p==8)
				{
					graph1.AddPoint(0,(double)i);
					p=0;
				}
				x[nCount++] = i;
				rd.ReadInt32();
			}
			int result = 0;
			fixed(float* ptr = &x[0])
			{
				result = SigPro.sp_rcfft(ptr,nRealSamples);
			}
			double dist;
			p=0;
			for(int z=4;z<nIdealSamples;z++)
			{
				dist = x[z]*x[z]+x[z+1]*x[z+1];
				p++;
				if(p==8)
				{
					graph2.AddPoint(0,(double)Math.Sqrt(dist));
					p=0;
				}
				z++;
			}
			Draw();
			
		}

		private void GraphWav_Load(object sender, System.EventArgs e)
		{
		
		}
	}
}
