using System; using System.Collections.Generic; using System.IO; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Storage; using Microsoft.Xna.Framework.Content; using Util; using tAC_Engine.Graphics.Entities; namespace AuditoryStimuli { public class AuditoryStimuli : GameScreen { private float SOUND_DELAY = 1f; private float lastTime = 0f; private float nextPlay; private float MAX_JITTER = .5f; private int MAX_PLAYS = 100; private EntitySprite cross; private SpriteBatch msb; private Logger mLogger; private string mSound; private List volumes = new List(); private List toneCount = new List(); public override void Initialize() { base.Initialize(); nextPlay = SOUND_DELAY + (float)Util.Random.NextDouble(0f, MAX_JITTER); try { mLogger = GetLogger(); mLogger.Initialize(); StreamReader input = File.OpenText(RootDirectory + @"\Config\AuditoryConfig.script"); string inputLine; while (!input.EndOfStream) { inputLine = input.ReadLine(); string[] args = inputLine.Split('='); if (args.Length == 2) { args[1] = args[1].Trim(); switch (args[0].Trim()) { case "AS_SOUND_DELAY": SOUND_DELAY = (float)Convert.ToDouble(args[1]); break; case "AS_MAX_JITTER": MAX_JITTER = (float)Convert.ToDouble(args[1]); break; case "AS_MAX_PLAYS": MAX_PLAYS = Convert.ToInt32(args[1]); break; case "AS_VOLUME1": volumes[0] = (float)Convert.ToDouble(args[1]); break; case "AS_VOLUME2": volumes[1] = (float)Convert.ToDouble(args[1]); break; case "AS_VOLUME3": volumes[2] = (float)Convert.ToDouble(args[1]); break; case "AS_VOLUME4": volumes[3] = (float)Convert.ToDouble(args[1]); break; case "AS_SOUND": mSound = args[1]; break; } } } mLogger.Write((int)EventCode.Start, "AS_START"); } catch (IOException e) { Console.WriteLine("Config File Error: Config File is Corrupt or not found"); } } protected override void LoadContent() { base.LoadContent(); for (int i = 0; i < 4; i++) volumes.Add(0); for (int i = 0; i < volumes.Count; i++) toneCount.Add(0); SoundManager.SoundManager.UnloadAudioProject(); SoundManager.SoundManager.LoadAudioProject(Content.RootDirectory, @"AudioProject.xgs"); GraphicsDevice.Clear(Color.Black); msb = new SpriteBatch(GraphicsDevice); cross = new EntitySprite(Content, "Cross"); cross.Height = GraphicsDevice.DisplayMode.Height; cross.Width = GraphicsDevice.DisplayMode.Width; cross.Position = Vector2.Zero; cross.SpriteBatch = msb; AddComponent(cross); } protected override void UnloadContent() { base.UnloadContent(); SoundManager.SoundManager.UnloadAudioProject(); } public override void Update(GameTime gameTime) { base.Update(gameTime); SoundManager.SoundManager.Update(); if (Keyboard.GetState().IsKeyDown(Keys.Escape)) { mLogger.Write((int)EventCode.End, "AS_END"); Done(); } if (gameTime.TotalGameTime.TotalSeconds - lastTime >= nextPlay) { int volumeNumber = Util.Random.Next(volumes.Count); while (toneCount[volumeNumber] >= MAX_PLAYS) { volumeNumber = Util.Random.Next(volumes.Count); } SoundManager.SoundManager.EffectVolume = volumes[volumeNumber]; SoundManager.SoundManager.PlayEffect(mSound); mLogger.Write((int)Enum.Parse(typeof(EventCode), "TonePlayed_" + (volumeNumber+1), true), "AS_TONE_PLAYED Volume=" + volumes[volumeNumber] + " Tone="+ (volumeNumber + 1)); toneCount[volumeNumber]++; lastTime = (float)gameTime.TotalGameTime.TotalSeconds; nextPlay = SOUND_DELAY + (float)Util.Random.NextDouble(0f, MAX_JITTER); bool quit = true; foreach (int i in toneCount) { if (i < MAX_PLAYS) quit = false; } if (quit) { mLogger.Write((int)EventCode.End, "AS_END"); Done(); } } } } }