using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using WaveLib;
using int32 = System.Int32;														// like a typedef
using real64 = System.Double;						


namespace HearingAidLab
{
	/// <summary>
	/// Summary description for Calibrate.
	/// </summary>
	public class Calibrate : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Button btnPlay;
		private System.Windows.Forms.TextBox txtFreq;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.TextBox txtNoiseLevel;
		private System.Windows.Forms.ComboBox cmbNoiseType;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;


		private System.Windows.Forms.Button btnStop;
		private System.Windows.Forms.Button btnOK;
		public Form1 mainmenu;
		public double CalDb;
		private System.Windows.Forms.Label label2;
		public int DSLevel  = 0;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.TextBox txtRef;
		private System.Windows.Forms.TextBox txtLevel;
		public WaveLib.WavePlayer player;
		private System.Windows.Forms.Button btnAtVolume;
		private System.Windows.Forms.Button btnStartNoise;
		private System.Windows.Forms.Button btnStopNoise;
		private System.Windows.Forms.TextBox txtWave;
		private System.Windows.Forms.Label label8;
		private System.Windows.Forms.Button btnWaveBrowse;
		private System.Windows.Forms.Button btnWave;
		private System.Windows.Forms.RadioButton rdPeak;
		private System.Windows.Forms.RadioButton rdRms;
		private System.Windows.Forms.TextBox txtBits;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.Label label9;
		private System.Windows.Forms.Button btnStop2;
		private System.Windows.Forms.GroupBox groupBox3;
		private System.Windows.Forms.GroupBox groupBox4;
		private System.Windows.Forms.GroupBox groupBox5;
		private System.Windows.Forms.Label label10;
		private System.Windows.Forms.Button btnBrowseWrite;
		private System.Windows.Forms.TextBox txtWrite;
		private System.Windows.Forms.Button ClearBuffers;
		private System.Windows.Forms.Button StopAll;
		private System.Windows.Forms.Label label12;
		private System.Windows.Forms.TextBox tbCurSeg;
		private System.Windows.Forms.Label label11;
		private System.Windows.Forms.TextBox txtWavLevel;
		private System.Windows.Forms.ComboBox cmbWavAmp;
		private System.Windows.Forms.Label label14;
		private System.Windows.Forms.Button btnViewUnMod;
		private System.Windows.Forms.Button btnViewMod;
		private System.Windows.Forms.TextBox txtPole1;
		private System.Windows.Forms.TextBox txtPole2;
		private System.Windows.Forms.Label lblCurSeg;
		private System.Windows.Forms.Label label13;
		private System.Windows.Forms.RadioButton rdoPeakNoise;
		private System.Windows.Forms.RadioButton rdoRmsNoise;
		private System.Windows.Forms.Button btnTestFreqShape;
		private System.Windows.Forms.GroupBox groupBox6;
		private System.Windows.Forms.Label label17;
		private System.Windows.Forms.TextBox txtFreqShapeWave;
		private System.Windows.Forms.Button btnFreqShapeWave;
		private System.Windows.Forms.Button btnFreqShapePlay;
		private System.Windows.Forms.Label label15;
		private System.Windows.Forms.TextBox txtFreqShapeFile;
		private System.Windows.Forms.Button btnFreqShapeFile;

        public const int MaxAmplitude = 4194304; // 8388607;

		public Calibrate()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			this.Closing += new CancelEventHandler(Calibrate_Closing);
			player = new WavePlayer();
			
#if DEBUG
			txtWave.Text = @"c:\HAsim 70 dB CalibrationTone.wav";
#else
			this.txtFreqShapeFile.Text = string.Empty;
			this.txtFreqShapeWave.Text = string.Empty;
			txtWave.Text = string.Empty;
			this.txtWrite.Text = string.Empty;
#endif

			// Send reference to calling window
			player.ifrmParentForm = this;
			player.Init();
			if ( player.Open()==false )
			{
				MessageBox.Show("Unable to open sound card!","Audio Error",MessageBoxButtons.OK,MessageBoxIcon.Stop);
				return;
			}
			cmbNoiseType.SelectedValueChanged += new EventHandler(cmbNoiseType_SelectedValueChanged);
			cmbNoiseType.SelectedIndex = 0;
			cmbWavAmp.SelectedIndex = 0;
		}

		/// <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.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Calibrate));
            this.label1 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.btnPlay = new System.Windows.Forms.Button();
            this.txtBits = new System.Windows.Forms.TextBox();
            this.txtRef = new System.Windows.Forms.TextBox();
            this.txtFreq = new System.Windows.Forms.TextBox();
            this.label4 = new System.Windows.Forms.Label();
            this.label5 = new System.Windows.Forms.Label();
            this.txtPole1 = new System.Windows.Forms.TextBox();
            this.txtNoiseLevel = new System.Windows.Forms.TextBox();
            this.cmbNoiseType = new System.Windows.Forms.ComboBox();
            this.btnStop = new System.Windows.Forms.Button();
            this.btnOK = new System.Windows.Forms.Button();
            this.label2 = new System.Windows.Forms.Label();
            this.txtLevel = new System.Windows.Forms.TextBox();
            this.label6 = new System.Windows.Forms.Label();
            this.label7 = new System.Windows.Forms.Label();
            this.btnAtVolume = new System.Windows.Forms.Button();
            this.btnStartNoise = new System.Windows.Forms.Button();
            this.btnStopNoise = new System.Windows.Forms.Button();
            this.txtWave = new System.Windows.Forms.TextBox();
            this.label8 = new System.Windows.Forms.Label();
            this.btnWaveBrowse = new System.Windows.Forms.Button();
            this.btnWave = new System.Windows.Forms.Button();
            this.rdPeak = new System.Windows.Forms.RadioButton();
            this.rdRms = new System.Windows.Forms.RadioButton();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.label9 = new System.Windows.Forms.Label();
            this.btnStop2 = new System.Windows.Forms.Button();
            this.groupBox3 = new System.Windows.Forms.GroupBox();
            this.label13 = new System.Windows.Forms.Label();
            this.rdoPeakNoise = new System.Windows.Forms.RadioButton();
            this.rdoRmsNoise = new System.Windows.Forms.RadioButton();
            this.txtPole2 = new System.Windows.Forms.TextBox();
            this.label12 = new System.Windows.Forms.Label();
            this.groupBox4 = new System.Windows.Forms.GroupBox();
            this.btnViewMod = new System.Windows.Forms.Button();
            this.btnViewUnMod = new System.Windows.Forms.Button();
            this.label14 = new System.Windows.Forms.Label();
            this.cmbWavAmp = new System.Windows.Forms.ComboBox();
            this.label11 = new System.Windows.Forms.Label();
            this.txtWavLevel = new System.Windows.Forms.TextBox();
            this.groupBox5 = new System.Windows.Forms.GroupBox();
            this.label10 = new System.Windows.Forms.Label();
            this.txtWrite = new System.Windows.Forms.TextBox();
            this.btnBrowseWrite = new System.Windows.Forms.Button();
            this.ClearBuffers = new System.Windows.Forms.Button();
            this.StopAll = new System.Windows.Forms.Button();
            this.tbCurSeg = new System.Windows.Forms.TextBox();
            this.lblCurSeg = new System.Windows.Forms.Label();
            this.btnTestFreqShape = new System.Windows.Forms.Button();
            this.groupBox6 = new System.Windows.Forms.GroupBox();
            this.label15 = new System.Windows.Forms.Label();
            this.txtFreqShapeFile = new System.Windows.Forms.TextBox();
            this.btnFreqShapeFile = new System.Windows.Forms.Button();
            this.label17 = new System.Windows.Forms.Label();
            this.txtFreqShapeWave = new System.Windows.Forms.TextBox();
            this.btnFreqShapeWave = new System.Windows.Forms.Button();
            this.btnFreqShapePlay = new System.Windows.Forms.Button();
            this.groupBox1.SuspendLayout();
            this.groupBox2.SuspendLayout();
            this.groupBox3.SuspendLayout();
            this.groupBox4.SuspendLayout();
            this.groupBox5.SuspendLayout();
            this.groupBox6.SuspendLayout();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(8, 47);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(80, 23);
            this.label1.TabIndex = 0;
            this.label1.Text = "Bit Resolution:";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label3
            // 
            this.label3.Location = new System.Drawing.Point(16, 71);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(72, 23);
            this.label3.TabIndex = 2;
            this.label3.Text = "Ref dB Level:";
            this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // btnPlay
            // 
            this.btnPlay.Image = ((System.Drawing.Image)(resources.GetObject("btnPlay.Image")));
            this.btnPlay.Location = new System.Drawing.Point(224, 23);
            this.btnPlay.Name = "btnPlay";
            this.btnPlay.Size = new System.Drawing.Size(80, 23);
            this.btnPlay.TabIndex = 4;
            this.btnPlay.Click += new System.EventHandler(this.btnPlay_Click);
            // 
            // txtBits
            // 
            this.txtBits.Location = new System.Drawing.Point(96, 48);
            this.txtBits.Name = "txtBits";
            this.txtBits.Size = new System.Drawing.Size(100, 20);
            this.txtBits.TabIndex = 2;
            this.txtBits.Text = "24";
            // 
            // txtRef
            // 
            this.txtRef.Location = new System.Drawing.Point(96, 72);
            this.txtRef.Name = "txtRef";
            this.txtRef.Size = new System.Drawing.Size(100, 20);
            this.txtRef.TabIndex = 3;
            // 
            // txtFreq
            // 
            this.txtFreq.Location = new System.Drawing.Point(96, 24);
            this.txtFreq.Name = "txtFreq";
            this.txtFreq.Size = new System.Drawing.Size(100, 20);
            this.txtFreq.TabIndex = 1;
            this.txtFreq.Text = "1000";
            // 
            // label4
            // 
            this.label4.Location = new System.Drawing.Point(24, 23);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(64, 23);
            this.label4.TabIndex = 8;
            this.label4.Text = "Test Freq:";
            this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label5
            // 
            this.label5.Location = new System.Drawing.Point(8, 64);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(100, 23);
            this.label5.TabIndex = 9;
            this.label5.Text = "Pole 1:";
            this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // txtPole1
            // 
            this.txtPole1.Location = new System.Drawing.Point(112, 64);
            this.txtPole1.Name = "txtPole1";
            this.txtPole1.Size = new System.Drawing.Size(100, 20);
            this.txtPole1.TabIndex = 10;
            this.txtPole1.Text = "2000";
            // 
            // txtNoiseLevel
            // 
            this.txtNoiseLevel.Location = new System.Drawing.Point(112, 112);
            this.txtNoiseLevel.Name = "txtNoiseLevel";
            this.txtNoiseLevel.Size = new System.Drawing.Size(100, 20);
            this.txtNoiseLevel.TabIndex = 12;
            // 
            // cmbNoiseType
            // 
            this.cmbNoiseType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cmbNoiseType.Items.AddRange(new object[] {
            "Broad Band",
            "Band Pass",
            "Band Stop",
            "Hi Pass",
            "Low Pass"});
            this.cmbNoiseType.Location = new System.Drawing.Point(112, 40);
            this.cmbNoiseType.Name = "cmbNoiseType";
            this.cmbNoiseType.Size = new System.Drawing.Size(100, 21);
            this.cmbNoiseType.TabIndex = 9;
            // 
            // btnStop
            // 
            this.btnStop.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btnStop.Image = ((System.Drawing.Image)(resources.GetObject("btnStop.Image")));
            this.btnStop.Location = new System.Drawing.Point(224, 71);
            this.btnStop.Name = "btnStop";
            this.btnStop.Size = new System.Drawing.Size(80, 23);
            this.btnStop.TabIndex = 5;
            this.btnStop.Click += new System.EventHandler(this.btnStop_Click);
            // 
            // btnOK
            // 
            this.btnOK.Location = new System.Drawing.Point(608, 8);
            this.btnOK.Name = "btnOK";
            this.btnOK.Size = new System.Drawing.Size(80, 23);
            this.btnOK.TabIndex = 17;
            this.btnOK.Text = "OK";
            this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(32, 47);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(72, 23);
            this.label2.TabIndex = 26;
            this.label2.Text = "Target dB:";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // txtLevel
            // 
            this.txtLevel.Location = new System.Drawing.Point(112, 48);
            this.txtLevel.Name = "txtLevel";
            this.txtLevel.Size = new System.Drawing.Size(100, 20);
            this.txtLevel.TabIndex = 6;
            this.txtLevel.Text = "80";
            // 
            // label6
            // 
            this.label6.Location = new System.Drawing.Point(8, 112);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(100, 23);
            this.label6.TabIndex = 10;
            this.label6.Text = "NoiseLevel (dB):";
            this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label7
            // 
            this.label7.Location = new System.Drawing.Point(8, 40);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(100, 23);
            this.label7.TabIndex = 13;
            this.label7.Text = "Noise Type:";
            this.label7.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // btnAtVolume
            // 
            this.btnAtVolume.Image = ((System.Drawing.Image)(resources.GetObject("btnAtVolume.Image")));
            this.btnAtVolume.Location = new System.Drawing.Point(224, 25);
            this.btnAtVolume.Name = "btnAtVolume";
            this.btnAtVolume.Size = new System.Drawing.Size(80, 23);
            this.btnAtVolume.TabIndex = 7;
            this.btnAtVolume.Click += new System.EventHandler(this.btnAtVolume_Click);
            // 
            // btnStartNoise
            // 
            this.btnStartNoise.Image = ((System.Drawing.Image)(resources.GetObject("btnStartNoise.Image")));
            this.btnStartNoise.Location = new System.Drawing.Point(224, 17);
            this.btnStartNoise.Name = "btnStartNoise";
            this.btnStartNoise.Size = new System.Drawing.Size(80, 23);
            this.btnStartNoise.TabIndex = 13;
            this.btnStartNoise.Click += new System.EventHandler(this.btnStartNoise_Click);
            // 
            // btnStopNoise
            // 
            this.btnStopNoise.Image = ((System.Drawing.Image)(resources.GetObject("btnStopNoise.Image")));
            this.btnStopNoise.Location = new System.Drawing.Point(224, 63);
            this.btnStopNoise.Name = "btnStopNoise";
            this.btnStopNoise.Size = new System.Drawing.Size(80, 23);
            this.btnStopNoise.TabIndex = 14;
            this.btnStopNoise.Click += new System.EventHandler(this.btnStopNoise_Click);
            // 
            // txtWave
            // 
            this.txtWave.Location = new System.Drawing.Point(120, 24);
            this.txtWave.Name = "txtWave";
            this.txtWave.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.txtWave.Size = new System.Drawing.Size(472, 20);
            this.txtWave.TabIndex = 16;
            // 
            // label8
            // 
            this.label8.Location = new System.Drawing.Point(56, 24);
            this.label8.Name = "label8";
            this.label8.Size = new System.Drawing.Size(64, 23);
            this.label8.TabIndex = 32;
            this.label8.Text = "Wave File:";
            this.label8.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // btnWaveBrowse
            // 
            this.btnWaveBrowse.Location = new System.Drawing.Point(600, 23);
            this.btnWaveBrowse.Name = "btnWaveBrowse";
            this.btnWaveBrowse.Size = new System.Drawing.Size(80, 23);
            this.btnWaveBrowse.TabIndex = 15;
            this.btnWaveBrowse.Text = "Browse";
            this.btnWaveBrowse.Click += new System.EventHandler(this.btnWaveBrowse_Click);
            // 
            // btnWave
            // 
            this.btnWave.Image = ((System.Drawing.Image)(resources.GetObject("btnWave.Image")));
            this.btnWave.Location = new System.Drawing.Point(248, 48);
            this.btnWave.Name = "btnWave";
            this.btnWave.Size = new System.Drawing.Size(80, 48);
            this.btnWave.TabIndex = 19;
            this.btnWave.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
            this.btnWave.Click += new System.EventHandler(this.btnWave_Click);
            // 
            // rdPeak
            // 
            this.rdPeak.Checked = true;
            this.rdPeak.Location = new System.Drawing.Point(112, 24);
            this.rdPeak.Name = "rdPeak";
            this.rdPeak.Size = new System.Drawing.Size(56, 24);
            this.rdPeak.TabIndex = 33;
            this.rdPeak.TabStop = true;
            this.rdPeak.Text = "Peak";
            // 
            // rdRms
            // 
            this.rdRms.Location = new System.Drawing.Point(168, 24);
            this.rdRms.Name = "rdRms";
            this.rdRms.Size = new System.Drawing.Size(56, 24);
            this.rdRms.TabIndex = 34;
            this.rdRms.Text = "RMS";
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.label3);
            this.groupBox1.Controls.Add(this.btnPlay);
            this.groupBox1.Controls.Add(this.txtBits);
            this.groupBox1.Controls.Add(this.txtRef);
            this.groupBox1.Controls.Add(this.txtFreq);
            this.groupBox1.Controls.Add(this.label4);
            this.groupBox1.Controls.Add(this.label1);
            this.groupBox1.Controls.Add(this.btnStop);
            this.groupBox1.Location = new System.Drawing.Point(8, 8);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(376, 100);
            this.groupBox1.TabIndex = 35;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Set Calibration Level";
            // 
            // groupBox2
            // 
            this.groupBox2.Controls.Add(this.label9);
            this.groupBox2.Controls.Add(this.btnAtVolume);
            this.groupBox2.Controls.Add(this.rdPeak);
            this.groupBox2.Controls.Add(this.rdRms);
            this.groupBox2.Controls.Add(this.label2);
            this.groupBox2.Controls.Add(this.txtLevel);
            this.groupBox2.Controls.Add(this.btnStop2);
            this.groupBox2.Location = new System.Drawing.Point(8, 112);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(376, 104);
            this.groupBox2.TabIndex = 36;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "Test Calibration";
            // 
            // label9
            // 
            this.label9.Location = new System.Drawing.Point(8, 24);
            this.label9.Name = "label9";
            this.label9.Size = new System.Drawing.Size(96, 23);
            this.label9.TabIndex = 35;
            this.label9.Text = "Attenuation Mode:";
            this.label9.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // btnStop2
            // 
            this.btnStop2.Image = ((System.Drawing.Image)(resources.GetObject("btnStop2.Image")));
            this.btnStop2.Location = new System.Drawing.Point(224, 72);
            this.btnStop2.Name = "btnStop2";
            this.btnStop2.Size = new System.Drawing.Size(80, 23);
            this.btnStop2.TabIndex = 8;
            this.btnStop2.Click += new System.EventHandler(this.btnStop2_Click);
            // 
            // groupBox3
            // 
            this.groupBox3.Controls.Add(this.label13);
            this.groupBox3.Controls.Add(this.rdoPeakNoise);
            this.groupBox3.Controls.Add(this.rdoRmsNoise);
            this.groupBox3.Controls.Add(this.txtPole2);
            this.groupBox3.Controls.Add(this.label12);
            this.groupBox3.Controls.Add(this.label5);
            this.groupBox3.Controls.Add(this.txtPole1);
            this.groupBox3.Controls.Add(this.txtNoiseLevel);
            this.groupBox3.Controls.Add(this.cmbNoiseType);
            this.groupBox3.Controls.Add(this.label6);
            this.groupBox3.Controls.Add(this.label7);
            this.groupBox3.Controls.Add(this.btnStartNoise);
            this.groupBox3.Controls.Add(this.btnStopNoise);
            this.groupBox3.Location = new System.Drawing.Point(8, 216);
            this.groupBox3.Name = "groupBox3";
            this.groupBox3.Size = new System.Drawing.Size(376, 144);
            this.groupBox3.TabIndex = 37;
            this.groupBox3.TabStop = false;
            this.groupBox3.Text = "Play/Create Noise";
            // 
            // label13
            // 
            this.label13.Location = new System.Drawing.Point(12, 16);
            this.label13.Name = "label13";
            this.label13.Size = new System.Drawing.Size(96, 23);
            this.label13.TabIndex = 38;
            this.label13.Text = "Attenuation Mode:";
            this.label13.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // rdoPeakNoise
            // 
            this.rdoPeakNoise.Location = new System.Drawing.Point(112, 16);
            this.rdoPeakNoise.Name = "rdoPeakNoise";
            this.rdoPeakNoise.Size = new System.Drawing.Size(56, 24);
            this.rdoPeakNoise.TabIndex = 36;
            this.rdoPeakNoise.Text = "Peak";
            // 
            // rdoRmsNoise
            // 
            this.rdoRmsNoise.Checked = true;
            this.rdoRmsNoise.Location = new System.Drawing.Point(168, 16);
            this.rdoRmsNoise.Name = "rdoRmsNoise";
            this.rdoRmsNoise.Size = new System.Drawing.Size(56, 24);
            this.rdoRmsNoise.TabIndex = 37;
            this.rdoRmsNoise.TabStop = true;
            this.rdoRmsNoise.Text = "RMS";
            // 
            // txtPole2
            // 
            this.txtPole2.Enabled = false;
            this.txtPole2.Location = new System.Drawing.Point(112, 88);
            this.txtPole2.Name = "txtPole2";
            this.txtPole2.Size = new System.Drawing.Size(100, 20);
            this.txtPole2.TabIndex = 11;
            this.txtPole2.Text = "4000";
            // 
            // label12
            // 
            this.label12.Location = new System.Drawing.Point(8, 88);
            this.label12.Name = "label12";
            this.label12.Size = new System.Drawing.Size(100, 23);
            this.label12.TabIndex = 14;
            this.label12.Text = "Pole 2:";
            this.label12.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // groupBox4
            // 
            this.groupBox4.Controls.Add(this.btnViewMod);
            this.groupBox4.Controls.Add(this.btnViewUnMod);
            this.groupBox4.Controls.Add(this.label14);
            this.groupBox4.Controls.Add(this.cmbWavAmp);
            this.groupBox4.Controls.Add(this.label11);
            this.groupBox4.Controls.Add(this.txtWavLevel);
            this.groupBox4.Controls.Add(this.label8);
            this.groupBox4.Controls.Add(this.txtWave);
            this.groupBox4.Controls.Add(this.btnWaveBrowse);
            this.groupBox4.Controls.Add(this.btnWave);
            this.groupBox4.Location = new System.Drawing.Point(8, 368);
            this.groupBox4.Name = "groupBox4";
            this.groupBox4.Size = new System.Drawing.Size(688, 104);
            this.groupBox4.TabIndex = 38;
            this.groupBox4.TabStop = false;
            this.groupBox4.Text = "Play Wave File";
            // 
            // btnViewMod
            // 
            this.btnViewMod.Location = new System.Drawing.Point(488, 48);
            this.btnViewMod.Name = "btnViewMod";
            this.btnViewMod.Size = new System.Drawing.Size(128, 48);
            this.btnViewMod.TabIndex = 21;
            this.btnViewMod.Text = "View HAL Modified Wave";
            this.btnViewMod.Click += new System.EventHandler(this.btnViewMod_Click);
            // 
            // btnViewUnMod
            // 
            this.btnViewUnMod.Location = new System.Drawing.Point(344, 48);
            this.btnViewUnMod.Name = "btnViewUnMod";
            this.btnViewUnMod.Size = new System.Drawing.Size(128, 48);
            this.btnViewUnMod.TabIndex = 20;
            this.btnViewUnMod.Text = "View Wave Form";
            this.btnViewUnMod.Click += new System.EventHandler(this.btnViewUnMod_Click);
            // 
            // label14
            // 
            this.label14.Location = new System.Drawing.Point(16, 72);
            this.label14.Name = "label14";
            this.label14.Size = new System.Drawing.Size(104, 23);
            this.label14.TabIndex = 36;
            this.label14.Text = "Level:";
            this.label14.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // cmbWavAmp
            // 
            this.cmbWavAmp.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cmbWavAmp.Items.AddRange(new object[] {
            "RMS",
            "Peak",
            "None"});
            this.cmbWavAmp.Location = new System.Drawing.Point(120, 48);
            this.cmbWavAmp.Name = "cmbWavAmp";
            this.cmbWavAmp.Size = new System.Drawing.Size(104, 21);
            this.cmbWavAmp.TabIndex = 17;
            // 
            // label11
            // 
            this.label11.Location = new System.Drawing.Point(16, 48);
            this.label11.Name = "label11";
            this.label11.Size = new System.Drawing.Size(104, 23);
            this.label11.TabIndex = 34;
            this.label11.Text = "Amplification Type:";
            this.label11.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // txtWavLevel
            // 
            this.txtWavLevel.Location = new System.Drawing.Point(120, 72);
            this.txtWavLevel.Name = "txtWavLevel";
            this.txtWavLevel.Size = new System.Drawing.Size(104, 20);
            this.txtWavLevel.TabIndex = 18;
            this.txtWavLevel.Text = "80";
            // 
            // groupBox5
            // 
            this.groupBox5.Controls.Add(this.label10);
            this.groupBox5.Controls.Add(this.txtWrite);
            this.groupBox5.Controls.Add(this.btnBrowseWrite);
            this.groupBox5.Location = new System.Drawing.Point(392, 216);
            this.groupBox5.Name = "groupBox5";
            this.groupBox5.Size = new System.Drawing.Size(304, 120);
            this.groupBox5.TabIndex = 39;
            this.groupBox5.TabStop = false;
            this.groupBox5.Text = "Write to File";
            // 
            // label10
            // 
            this.label10.Location = new System.Drawing.Point(8, 32);
            this.label10.Name = "label10";
            this.label10.Size = new System.Drawing.Size(288, 56);
            this.label10.TabIndex = 0;
            this.label10.Text = "If a filename is entered below, the wav data that is played using any of the opti" +
                "ons to the left will be written to the selected file.";
            // 
            // txtWrite
            // 
            this.txtWrite.Location = new System.Drawing.Point(8, 88);
            this.txtWrite.Name = "txtWrite";
            this.txtWrite.Size = new System.Drawing.Size(200, 20);
            this.txtWrite.TabIndex = 33;
            // 
            // btnBrowseWrite
            // 
            this.btnBrowseWrite.Location = new System.Drawing.Point(216, 88);
            this.btnBrowseWrite.Name = "btnBrowseWrite";
            this.btnBrowseWrite.Size = new System.Drawing.Size(80, 23);
            this.btnBrowseWrite.TabIndex = 34;
            this.btnBrowseWrite.Text = "Browse";
            this.btnBrowseWrite.Click += new System.EventHandler(this.btnBrowseWrite_Click);
            // 
            // ClearBuffers
            // 
            this.ClearBuffers.Location = new System.Drawing.Point(608, 40);
            this.ClearBuffers.Name = "ClearBuffers";
            this.ClearBuffers.Size = new System.Drawing.Size(80, 23);
            this.ClearBuffers.TabIndex = 35;
            this.ClearBuffers.Text = "Clear Buffers";
            this.ClearBuffers.Click += new System.EventHandler(this.ClearBuffers_Click);
            // 
            // StopAll
            // 
            this.StopAll.Location = new System.Drawing.Point(608, 72);
            this.StopAll.Name = "StopAll";
            this.StopAll.Size = new System.Drawing.Size(80, 23);
            this.StopAll.TabIndex = 40;
            this.StopAll.Text = "Stop All";
            this.StopAll.Click += new System.EventHandler(this.StopAll_Click);
            // 
            // tbCurSeg
            // 
            this.tbCurSeg.Location = new System.Drawing.Point(640, 184);
            this.tbCurSeg.Name = "tbCurSeg";
            this.tbCurSeg.Size = new System.Drawing.Size(48, 20);
            this.tbCurSeg.TabIndex = 41;
            this.tbCurSeg.Text = "-1";
            // 
            // lblCurSeg
            // 
            this.lblCurSeg.Location = new System.Drawing.Point(584, 184);
            this.lblCurSeg.Name = "lblCurSeg";
            this.lblCurSeg.Size = new System.Drawing.Size(48, 24);
            this.lblCurSeg.TabIndex = 42;
            this.lblCurSeg.Text = "Cur Seg";
            this.lblCurSeg.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // btnTestFreqShape
            // 
            this.btnTestFreqShape.Location = new System.Drawing.Point(416, 40);
            this.btnTestFreqShape.Name = "btnTestFreqShape";
            this.btnTestFreqShape.Size = new System.Drawing.Size(128, 23);
            this.btnTestFreqShape.TabIndex = 43;
            this.btnTestFreqShape.Text = "TestComplexStims";
            this.btnTestFreqShape.Click += new System.EventHandler(this.btnTestFreqShape_Click);
            // 
            // groupBox6
            // 
            this.groupBox6.Controls.Add(this.label15);
            this.groupBox6.Controls.Add(this.txtFreqShapeFile);
            this.groupBox6.Controls.Add(this.btnFreqShapeFile);
            this.groupBox6.Controls.Add(this.label17);
            this.groupBox6.Controls.Add(this.txtFreqShapeWave);
            this.groupBox6.Controls.Add(this.btnFreqShapeWave);
            this.groupBox6.Controls.Add(this.btnFreqShapePlay);
            this.groupBox6.Location = new System.Drawing.Point(8, 480);
            this.groupBox6.Name = "groupBox6";
            this.groupBox6.Size = new System.Drawing.Size(688, 104);
            this.groupBox6.TabIndex = 39;
            this.groupBox6.TabStop = false;
            this.groupBox6.Text = "Test Frequency Shaping";
            // 
            // label15
            // 
            this.label15.Location = new System.Drawing.Point(32, 47);
            this.label15.Name = "label15";
            this.label15.Size = new System.Drawing.Size(88, 23);
            this.label15.TabIndex = 35;
            this.label15.Text = "FreqShape File:";
            this.label15.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // txtFreqShapeFile
            // 
            this.txtFreqShapeFile.Location = new System.Drawing.Point(120, 48);
            this.txtFreqShapeFile.Name = "txtFreqShapeFile";
            this.txtFreqShapeFile.Size = new System.Drawing.Size(472, 20);
            this.txtFreqShapeFile.TabIndex = 34;
            this.txtFreqShapeFile.Text = "C:\\Visual Studio Projects\\HearingAidLab_ARSC\\FreqShape.txt";
            // 
            // btnFreqShapeFile
            // 
            this.btnFreqShapeFile.Location = new System.Drawing.Point(600, 47);
            this.btnFreqShapeFile.Name = "btnFreqShapeFile";
            this.btnFreqShapeFile.Size = new System.Drawing.Size(80, 23);
            this.btnFreqShapeFile.TabIndex = 33;
            this.btnFreqShapeFile.Text = "Browse";
            this.btnFreqShapeFile.Click += new System.EventHandler(this.btnFreqShapeFile_Click);
            // 
            // label17
            // 
            this.label17.Location = new System.Drawing.Point(56, 24);
            this.label17.Name = "label17";
            this.label17.Size = new System.Drawing.Size(64, 23);
            this.label17.TabIndex = 32;
            this.label17.Text = "Wave File:";
            this.label17.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // txtFreqShapeWave
            // 
            this.txtFreqShapeWave.Location = new System.Drawing.Point(120, 24);
            this.txtFreqShapeWave.Name = "txtFreqShapeWave";
            this.txtFreqShapeWave.Size = new System.Drawing.Size(472, 20);
            this.txtFreqShapeWave.TabIndex = 16;
            this.txtFreqShapeWave.Text = "c:\\MasterChirp.wav";
            // 
            // btnFreqShapeWave
            // 
            this.btnFreqShapeWave.Location = new System.Drawing.Point(600, 23);
            this.btnFreqShapeWave.Name = "btnFreqShapeWave";
            this.btnFreqShapeWave.Size = new System.Drawing.Size(80, 23);
            this.btnFreqShapeWave.TabIndex = 15;
            this.btnFreqShapeWave.Text = "Browse";
            this.btnFreqShapeWave.Click += new System.EventHandler(this.btnFreqShapeWave_Click);
            // 
            // btnFreqShapePlay
            // 
            this.btnFreqShapePlay.Image = ((System.Drawing.Image)(resources.GetObject("btnFreqShapePlay.Image")));
            this.btnFreqShapePlay.Location = new System.Drawing.Point(600, 72);
            this.btnFreqShapePlay.Name = "btnFreqShapePlay";
            this.btnFreqShapePlay.Size = new System.Drawing.Size(80, 23);
            this.btnFreqShapePlay.TabIndex = 19;
            this.btnFreqShapePlay.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
            this.btnFreqShapePlay.Click += new System.EventHandler(this.btnFreqShapePlay_Click);
            // 
            // Calibrate
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(704, 590);
            this.Controls.Add(this.btnTestFreqShape);
            this.Controls.Add(this.lblCurSeg);
            this.Controls.Add(this.tbCurSeg);
            this.Controls.Add(this.StopAll);
            this.Controls.Add(this.groupBox5);
            this.Controls.Add(this.groupBox4);
            this.Controls.Add(this.groupBox3);
            this.Controls.Add(this.groupBox2);
            this.Controls.Add(this.groupBox1);
            this.Controls.Add(this.btnOK);
            this.Controls.Add(this.ClearBuffers);
            this.Controls.Add(this.groupBox6);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Name = "Calibrate";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
            this.Text = "Simple Calibration";
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.groupBox2.ResumeLayout(false);
            this.groupBox2.PerformLayout();
            this.groupBox3.ResumeLayout(false);
            this.groupBox3.PerformLayout();
            this.groupBox4.ResumeLayout(false);
            this.groupBox4.PerformLayout();
            this.groupBox5.ResumeLayout(false);
            this.groupBox5.PerformLayout();
            this.groupBox6.ResumeLayout(false);
            this.groupBox6.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		private void btnOK_Click(object sender, System.EventArgs e)
		{
			// Just in case they didn't hit the stop button
			if ( player.ibolArscRunning )
				player.Stop();

			if(txtRef.Text.Length>0)
			{
				double d = float.Parse(txtRef.Text);
				CalDb = d;
			}
			this.Close();
		}
		
		public void SetDb()
		{
			this.txtRef.Text = CalDb.ToString();
		}
		private void btnPlay_Click(object sender, System.EventArgs e)
		{
			// Focus should be on the stop button
			this.btnStop.Focus();

			int nFreq = int.Parse(this.txtFreq.Text);
			int nMax = int.Parse(this.txtBits.Text);
			nMax = (int) Math.Pow(2,nMax);
			if(player.Signals[0]!=null)
			{
				player.Signals[0].Stop();
				player.Signals[0].Dispose();
			}
			player.Signals[0] = new WaveBuffer();
			player.Signals[0].Tone(MaxAmplitude,nFreq,0,4000,50,50,WaveBuffer.Ears.Both,true);
			//player.Signals[0].Pulse(MaxAmplitude,nFreq,300,20,20,50,"Left",true);
			if(txtWrite.Text.Length>0)
			{
				FileInfo file = new FileInfo(txtWrite.Text);
				player.Signals[0].WriteWave(file);
			}
			player.Signals[0].Play();				// Doesn't actually play . . . just sets the Signal status.
			player.Play();							// This does the actual play
			this.btnStop2.Focus();
			return;
		}
		private void btnAtVolume_Click(object sender, System.EventArgs e)
		{
			WaveLib.AmplificationTypes amptype = WaveLib.AmplificationTypes.Peak;
			if(rdRms.Checked==true)
				amptype = AmplificationTypes.RMS;
			int nFreq = int.Parse(this.txtFreq.Text);
			int nMax = int.Parse(this.txtBits.Text);
			nMax = (int) Math.Pow(2,nMax);
			double d = float.Parse(txtRef.Text);
			if(txtLevel.Text.Length==0)
				txtLevel.Text = "70";
			double fLevel = double.Parse(txtLevel.Text);
			fLevel -= d;
			//double V1 = MaxAmplitude / (Math.Pow(10,d/20));
			double V2 = MaxAmplitude*(Math.Pow(10,fLevel/20));
			if(player.Signals[0]!=null)
			{
				player.Signals[0].Stop();
				player.Signals[0].Dispose();
			}
			player.Signals[0] = new WaveBuffer();
			player.Signals[0].Tone(MaxAmplitude,nFreq,0,4000,20,20,WaveBuffer.Ears.Both,true);
			//player.Signals[0].Pulse(MaxAmplitude,nFreq,300,20,20,50,"Both",false);
			player.Signals[0].AmplificationType = amptype;
			player.Signals[0].SetLevel((int) V2);
			player.Signals[0].Play();
			player.Play();
			if(txtWrite.Text.Length>0)
			{
				FileInfo file = new FileInfo(txtWrite.Text);
				player.Signals[0].WriteWave(file);
			}
			this.btnStop2.Focus();
		}

		private void btnStop_Click(object sender, System.EventArgs e)
		{
			try
			{
                if ( player.Signals[0]!=null )
                {
                    player.Signals[0].Stop();
                    player.Stop();									// tone added this
                    player.Signals[0].Dispose();
                }
               
				//player.ibolArscRunning=false;
			}
			catch(Exception err)
			{
				MessageBox.Show(err.Message);
			}
		}

		private void btnStop2_Click(object sender, System.EventArgs e)
		{
			this.btnStop_Click(sender, e);
		}

		//Noise stuff
		private void btnStartNoise_Click(object sender, System.EventArgs e)
		{
			player.Stop();
			string str,strPath;
			int nPole1, nPole2;
			double d,fLevel,V2;

			try{nPole1 = int.Parse(this.txtPole1.Text);}
			catch(Exception)
			{
				MessageBox.Show("Please use integer values only in the Pole 1 field!");
				return;
			}
			try{nPole2 = int.Parse(this.txtPole2.Text);}
			catch(Exception)
			{
				MessageBox.Show("Please use integer values only in the Pole 2 field!");
				return;
			}
			WaveLib.AmplificationTypes amptype = WaveLib.AmplificationTypes.Peak;
			if(rdoRmsNoise.Checked==true)
				amptype = AmplificationTypes.RMS;

			if(this.txtRef.Text.Length>0)
			{
				d = float.Parse(txtRef.Text);
				fLevel = double.Parse(txtNoiseLevel.Text);
				fLevel -= d;
				V2 = MaxAmplitude*(Math.Pow(10,fLevel/20));
			}
			else
				V2 = MaxAmplitude;
			
			str = string.Empty;
			strPath = string.Empty;
			//Generate Normally Distributed Noise
			MemoryStream ms = null;
			if(cmbNoiseType.SelectedIndex==0)//broadband
			{
				SigPro.GenerateNoise(out ms,4000);//4 seconds of noise
			}
			else if(cmbNoiseType.SelectedIndex==4)//low pass
			{
				if(SigPro.GenerateFilteredNoise(out ms, 4000,"lowpass", nPole1, 0,20,20)==false)
					return;
			}
			else if(cmbNoiseType.SelectedIndex==3)//hi pass
			{
				if(SigPro.GenerateFilteredNoise(out ms, 4000,"hipass", nPole1, 0,20,20)==false)
					return;
			}
			else if(cmbNoiseType.SelectedIndex==2)//band stop
			{
				if(SigPro.GenerateFilteredNoise(out ms, 4000,"bandstop", nPole1, nPole2,20,20)==false)
					return;
			}
			else if(cmbNoiseType.SelectedIndex==1)//band pass
			{
				try
				{
					if(SigPro.GenerateFilteredNoise(out ms, 4000,"bandpass", nPole1, nPole2,20,20)==false)
						return;
					//WaveBuffer buf = new WaveBuffer();
					//testing narrow band noise
					//if(buf.NarrowBandNoise(ARSC.SCALE_31BIT,2000,4,4000,20,20,"both")==false)
					//	return;
					//testing narrowbandpulse
					//if(buf.NarrowBandPulse(ARSC.SCALE_31BIT,2000,2,300,50,20,20,"both")==false)
					//	return;
					//ms = buf.ms;
				}
				catch(Exception err)
				{
					return;
				}
			}
			if(player.Signals[1]!=null)
			{
				player.Signals[1].Stop();
				player.Signals[1].Dispose();
			}
			if(player.Signals[0]!=null)
				player.Signals[0].Play();
			if(player.Signals[2]!=null)
				player.Signals[2].Play();
			
			player.Signals[1] = new WaveBuffer();
			player.Signals[1].AmplificationType = amptype;
			//player.Signals[1].AmplificationType = WaveLib.AmplificationTypes.None;
			player.Signals[1].SetMS(ms, "both");
			player.Signals[1].SetLevel((int)V2);
			player.Signals[1].Play();
			player.Play();
			if(txtWrite.Text.Length>0)
			{
				FileInfo file = new FileInfo(txtWrite.Text);
				player.Signals[1].WriteWave(file);
			}
			
			this.Focus();
			this.btnStopNoise.Focus();
		}

		private void btnStopNoise_Click(object sender, System.EventArgs e)
		{
			if(player.Signals[1]!=null)
			{
				player.Signals[1].Stop();
				player.Signals[1].Dispose();
			}
			if(player.Signals[0]!=null)
			{
				player.Signals[0].Stop();
			}
			if(player.Signals[2]!=null)
			{
				player.Signals[2].Stop();
			}
		
		}

		private void cmbNoiseType_SelectedValueChanged(object sender, EventArgs e)
		{
			if(cmbNoiseType.SelectedIndex==0)
			{
				txtPole1.Enabled = false;
				txtPole2.Enabled = false;
			}
			else if(cmbNoiseType.SelectedIndex == 1 || cmbNoiseType.SelectedIndex == 2)
			{
				txtPole1.Enabled = true;
				txtPole2.Enabled = true;
			}
			else if(cmbNoiseType.SelectedIndex == 3 || cmbNoiseType.SelectedIndex == 4)
			{
				txtPole1.Enabled = true;
				txtPole2.Enabled = false;
			}
		}

		private void btnNoiseHelp_Click(object sender, System.EventArgs e)
		{
			MessageBox.Show("Noise is generated using the MatLab HALGenNoise.m script, and is "+
				"then filtered using the HALFilter.m script.  HALFilter uses an FFT filter to "+
				"zero undesired frequencies.  You can use the MatLab help command to view "+
				"the parameters for these scripts, and they can be run from MatLab.");

		}
		
		private void ClearBuffers_Click(object sender, System.EventArgs e)
		{
			player.Stop();
			player.Signals[0] = new WaveBuffer();
			player.Signals[1] = new WaveBuffer();
			player.Signals[2] = new WaveBuffer();
		}

		private void StopAll_Click(object sender, System.EventArgs e)
		{
			player.Stop();
		}

		
		private void Calibrate_Closing(object sender, CancelEventArgs e)
		{
			player.Stop();
			player.Close();			// Closes ARSC device
			player.Dispose();
		}

		private void btnBrowseWrite_Click(object sender, System.EventArgs e)
		{
			SaveFileDialog dlg = new SaveFileDialog();
			dlg.Filter = "Wave file (*.wav)|*.wav";
			dlg.Filter += "|All files|*.*";

			if(dlg.ShowDialog()==DialogResult.OK)
			{
				txtWrite.Text = dlg.FileName;
			}
		
		}

		

		//Wave File Stuff
		private void btnWaveBrowse_Click(object sender, System.EventArgs e)
		{
			OpenFileDialog dlg = new OpenFileDialog();
			dlg.Filter = "Wave file (*.wav)|*.wav";
			dlg.Filter += "|All files|*.*";
			dlg.Multiselect = false;
			if(dlg.ShowDialog()==DialogResult.OK)
			{
				txtWave.Text = dlg.FileName;
			}
		}

		private void btnWave_Click(object sender, System.EventArgs e)
		{
			WaveLib.AmplificationTypes amptype = WaveLib.AmplificationTypes.RMS;
			if(cmbWavAmp.SelectedIndex==0)
				amptype = AmplificationTypes.RMS;
			else if(cmbWavAmp.SelectedIndex==1)
				amptype = AmplificationTypes.Peak;
			else if(cmbWavAmp.SelectedIndex==2)
				amptype = AmplificationTypes.None;

			if(player.Signals[2]!=null)
			{
				player.Signals[2].Stop();
				player.Signals[2].Dispose();
			}
			if(txtWavLevel.Text.Length==0)
				txtWavLevel.Text = "80";
			if(txtWave.Text.Length==0)
				return;
			player.Signals[2] = new WaveBuffer();
			FileInfo f = new FileInfo(txtWave.Text);
			if(f.Exists==false)
			{
				MessageBox.Show ( "File Does Not Exist: " + txtWave.Text );
				return;
			}
			if ( ! player.Signals[2].WaveFile(f,MaxAmplitude,WaveBuffer.Ears.Both,false,amptype) )
				return;

			double d = double.Parse(txtRef.Text);					// Level reference; typically 140dB
			int nLevel = int.Parse(this.txtWavLevel.Text);
			if ( nLevel==0 )
				nLevel = 80;
			double fLevel = nLevel - d;
			double V2 = MaxAmplitude*(Math.Pow(10,fLevel/20));
			if(V2>MaxAmplitude)
				V2 = MaxAmplitude;
			player.Signals[2].SetLevel((int)V2);
			if(txtWrite.Text.Length>0)
			{
				FileInfo file = new FileInfo(txtWrite.Text);
				player.Signals[2].WriteStereoWave(file);
			}
		    //player.Signals[2].Play();
			//player.Play();
			
            player.PlayMemoryStream(player.Signals[2].ms);
		
		}

		private void btnViewUnMod_Click(object sender, System.EventArgs e)
		{
			if(txtWave.Text.Length==0)
				return;
			player.Signals[2] = new WaveBuffer();
			FileInfo f = new FileInfo(txtWave.Text);
			if(f.Exists==false)
			{
				MessageBox.Show ( "File Does Not Exist: " + txtWave.Text );
				return;
			}
			if ( ! player.Signals[2].WaveFile(f,MaxAmplitude,WaveBuffer.Ears.Both,false,AmplificationTypes.None) )
				return;
			GraphWav dlg = new GraphWav();
			dlg.Show();
			dlg.Init();
			dlg.Graph(player.Signals[2]);
		}

		private void btnViewMod_Click(object sender, System.EventArgs e)
		{
			WaveLib.AmplificationTypes amptype = WaveLib.AmplificationTypes.RMS;
			if(cmbWavAmp.SelectedIndex==0)
				amptype = AmplificationTypes.RMS;
			else if(cmbWavAmp.SelectedIndex==1)
				amptype = AmplificationTypes.Peak;
			else if(cmbWavAmp.SelectedIndex==2)
				amptype = AmplificationTypes.None;

			player.Signals[2] = new WaveBuffer();
			FileInfo f = new FileInfo(txtWave.Text);
			if(f.Exists==false)
			{
				MessageBox.Show ( "File Does Not Exist: " + txtWave.Text );
				return;
			}
			if ( ! player.Signals[2].WaveFile(f,MaxAmplitude,WaveBuffer.Ears.Both,false,amptype) )
				return;

			double d = double.Parse(txtRef.Text);					// Level reference; typically 140dB
			int nLevel = int.Parse(this.txtWavLevel.Text);
			if ( nLevel==0 )
				nLevel = 80;
			double fLevel = nLevel - d;
			double V2 = MaxAmplitude*(Math.Pow(10,fLevel/20));
			if(V2>MaxAmplitude)
				V2 = MaxAmplitude;
			player.Signals[2].SetLevel((int)V2);
			GraphWav dlg = new GraphWav();
			dlg.Show();
			dlg.Init();
			dlg.Graph(player.Signals[2]);
		}



		private void btnTestFreqShape_Click(object sender, System.EventArgs e)
		{
			this.btnTestFreqShape.Text = "Working";
			this.btnTestFreqShape.Update();
			ComplexStim cs = new ComplexStim();
			cs.nTotalLength = 2000;
			cs.nNumberOfStims = 2;
			Stim s = new Stim();
			s.strType = "tone";
			s.nFreq = 500;
			s.nLength = 0;
			s.nRise = 20;
			s.nFall = 20;
			s.strEar = "right";
			s.nOrigin = 60;
			s.nPhase = 90;
			s.nStart = 1000;
			s.nStop = 2000;
			s.bAdapt = true;
			s.nLength = s.nStop-s.nStart;
			s.nPhase = (int) (360*s.nFreq*s.nStart/1000.0+s.nPhase);
            cs.listStims.Add(s);
			s = new Stim();
			s.strType = "tone";
			s.nFreq = 2000;
			s.nLength = 0;
			s.nRise = 20;
			s.nFall = 20;
			s.strEar = "left";
			s.nOrigin = 70;
			s.nPhase = 270;
			s.nStart = 0;
			s.nStop = 1000;
			s.bAdapt = true;
			s.nLength = s.nStop-s.nStart;
			s.nPhase = (int) (360*s.nFreq*s.nStart/1000.0+s.nPhase);
			cs.listStims.Add(s);
			Stim st = (Stim) cs;
			mainmenu.CreateStim(ref st);
			cs.SetLevel(20,this.mainmenu);
			cs.Mix();

			if(txtWrite.Text.Length>0)
			{
				FileInfo file = new FileInfo(@"C:\complex.wav");
				cs.buffer.WriteStereoWave(file);
			}
			player.Signals[1] = cs.buffer;
			player.Signals[1].Play();
			player.Play();
			this.btnTestFreqShape.Text = "Test Complex";
		}

		private void btnFreqShapeWave_Click(object sender, System.EventArgs e)
		{
			OpenFileDialog dlg = new OpenFileDialog();
			dlg.Filter = "Wave file (*.wav)|*.wav";
			dlg.Filter += "|All files|*.*";
			dlg.Multiselect = false;
			if(dlg.ShowDialog()==DialogResult.OK)
			{
				txtFreqShapeWave.Text = dlg.FileName;
			}
		}

		private void btnFreqShapeFile_Click(object sender, System.EventArgs e)
		{
			OpenFileDialog dlg = new OpenFileDialog();
			dlg.Filter = "Text file (*.txt)|*.txt";
			dlg.Filter += "|All files|*.*";
			dlg.Multiselect = false;
			if(dlg.ShowDialog()==DialogResult.OK)
			{
				txtFreqShapeFile.Text = dlg.FileName;
			}
		}

		private void btnFreqShapePlay_Click(object sender, System.EventArgs e)
		{
			if(txtFreqShapeWave.Text.Length<=0)
				return;
			if(txtFreqShapeFile.Text.Length<=0)
				return;
			FileInfo f = new FileInfo(txtFreqShapeWave.Text);
			mainmenu.def = new Definitions();
			mainmenu.ReadFreqShapeFile(txtFreqShapeFile.Text);
			int V2 = mainmenu.GetV2((int) mainmenu.CalDb,1000,"both");
			WaveBuffer bt = SigPro.FreqShape(f,mainmenu.def.LAtten,mainmenu.def.LFreqs,mainmenu.def.RAtten,mainmenu.def.RFreqs,V2);
			if(bt==null)
			{
				MessageBox.Show("Error FreqShaping stim");
				return;
			}
			if(player.Signals[1]!=null)
			{
				player.Signals[1].Stop();
				player.Signals[1].Dispose();
			}
			if(player.Signals[0]!=null)
				player.Signals[0].Play();
			if(player.Signals[2]!=null)
				player.Signals[2].Play();
			
			player.Signals[1] = new WaveBuffer();
			player.Signals[1].SetMS(bt.ms, "both");
			//V2 = mainmenu.GetV2(30,1000,"both");
			//player.Signals[1].SetLevel((int)V2);
			player.Signals[1].Play();
			player.Play();
			if(txtWrite.Text.Length>0)
			{
				FileInfo file = new FileInfo(txtWrite.Text);
				player.Signals[1].WriteStereoWave(file);
				//player.Signals[1].WriteWave(file);
			}
		}

		

	
		
		
	}
}
