annotate DeMaskingLT/ListenToAudio.cs @ 3:e0bd22354f3f tip

Fixed name bug
author Geogaddi\David <d.m.ronan@qmul.ac.uk>
date Fri, 07 Jul 2017 12:34:24 +0100
parents e8147674e932
children
rev   line source
d@0 1 using System;
d@0 2 using System.Collections.Generic;
d@0 3 using System.ComponentModel;
d@0 4 using System.Diagnostics;
d@0 5 using System.IO;
d@0 6 using System.Linq;
d@0 7 using System.Windows.Forms;
d@0 8 using CsvHelper;
d@0 9
d@0 10 namespace DeMaskingLT
d@0 11 {
d@0 12 public partial class ListenToAudio : Form
d@0 13 {
d@0 14 System.Media.SoundPlayer _player = new System.Media.SoundPlayer();
d@0 15 private PerceivedEmotion _perceivedEmotion;
d@0 16
d@0 17 private String _track1FileName = "C:\\Users\\David\\Desktop\\PO-12 Samples\\PO-12 Mix - Not Cut.wav";
d@0 18 private String _track2FileName = "C:\\Users\\David\\Desktop\\PO-12 Samples\\PO-12 Techno Loop 1.wav";
d@0 19 private String _track3FileName = "C:\\Users\\David\\Desktop\\PO-12 Samples\\PO-12 Techno Loop 2.wav";
d@0 20 private String _track4FileName = "C:\\Users\\David\\Desktop\\PO-12 Samples\\PO-12 Techno Loop 3.wav";
d@0 21 private String _track5FileName = "C:\\Users\\David\\Desktop\\PO-12 Samples\\PO-12 Techno Loop 4.wav";
d@0 22
d@0 23 ExperimentData _experimentData = new ExperimentData();
d@0 24 List<Result> _results = new List<Result>();
d@0 25 private Logging _logger;
d@0 26
d@0 27 private bool _track1Played = false;
d@0 28 private bool _track2Played = false;
d@0 29 private bool _track3Played = false;
d@0 30 private bool _track4Played = false;
d@0 31 private bool _track5Played = false;
d@0 32
d@0 33 private Stopwatch _stopWatchListenToSong = new Stopwatch();
d@0 34
d@0 35 private int _track1TimesMoved = 0;
d@0 36 private int _track2TimesMoved = 0;
d@0 37 private int _track3TimesMoved = 0;
d@0 38 private int _track4TimesMoved = 0;
d@0 39 private int _track5TimesMoved = 0;
d@0 40
d@0 41 private int _track1CurrentValue = 0;
d@0 42 private int _track2CurrentValue = 0;
d@0 43 private int _track3CurrentValue = 0;
d@0 44 private int _track4CurrentValue = 0;
d@0 45 private int _track5CurrentValue = 0;
d@0 46
d@3 47 private int _testNumber = 4;
d@0 48
d@0 49 public ListenToAudio()
d@0 50 {
d@0 51 InitializeComponent();
d@0 52 }
d@0 53
d@0 54 private void trackBarTrack1_ValueChanged(object sender, EventArgs e)
d@0 55 {
d@0 56 textBoxTrack1.Text = trackBarTrack1.Value.ToString();
d@0 57 }
d@0 58
d@0 59 private void trackBarTrack2_ValueChanged(object sender, EventArgs e)
d@0 60 {
d@0 61 textBoxTrack2.Text = trackBarTrack2.Value.ToString();
d@0 62 }
d@0 63
d@0 64 private void trackBarTrack3_ValueChanged(object sender, EventArgs e)
d@0 65 {
d@0 66 textBoxTrack3.Text = trackBarTrack3.Value.ToString();
d@0 67 }
d@0 68
d@0 69 private void trackBarTrack4_ValueChanged(object sender, EventArgs e)
d@0 70 {
d@0 71 textBoxTrack4.Text = trackBarTrack4.Value.ToString();
d@0 72 }
d@0 73
d@0 74 private void trackBarTrack5_ValueChanged(object sender, EventArgs e)
d@0 75 {
d@0 76 textBoxTrack5.Text = trackBarTrack5.Value.ToString();
d@0 77 }
d@0 78
d@0 79 private void backgroundWorkerAudio_DoWork(object sender, DoWorkEventArgs e)
d@0 80 {
d@0 81 if (sender.Equals(buttonPlayTrack1))
d@0 82 {
d@0 83 foreach (ToolStripStatusLabel label in statusStrip1.Items)
d@0 84 {
d@0 85 label.Text = "Mix 1 is now playing";
d@0 86 }
d@0 87
d@0 88 PlayMusic(_track1FileName);
d@0 89
d@0 90 }
d@0 91 else if (sender.Equals(buttonPlayTrack2))
d@0 92 {
d@0 93 foreach (ToolStripStatusLabel label in statusStrip1.Items)
d@0 94 {
d@0 95 label.Text = "Mix 2 is now playing";
d@0 96 }
d@0 97
d@0 98 PlayMusic(_track2FileName);
d@0 99
d@0 100 }
d@0 101 else if (sender.Equals(buttonPlayTrack3))
d@0 102 {
d@0 103 foreach (ToolStripStatusLabel label in statusStrip1.Items)
d@0 104 {
d@0 105 label.Text = "Mix 3 is now playing";
d@0 106 }
d@0 107
d@0 108 PlayMusic(_track3FileName);
d@0 109 }
d@0 110 else if (sender.Equals(buttonPlayTrack4))
d@0 111 {
d@0 112 foreach (ToolStripStatusLabel label in statusStrip1.Items)
d@0 113 {
d@0 114 label.Text = "Mix 4 is now playing";
d@0 115 }
d@0 116
d@0 117 PlayMusic(_track4FileName);
d@0 118
d@0 119 }
d@0 120 else if (sender.Equals(buttonPlayTrack5))
d@0 121 {
d@0 122 foreach (ToolStripStatusLabel label in statusStrip1.Items)
d@0 123 {
d@0 124 label.Text = "Mix 5 is now playing";
d@0 125 }
d@0 126
d@0 127 PlayMusic(_track5FileName);
d@0 128 }
d@0 129 }
d@0 130
d@0 131 private void PlayMusic(String audioFile)
d@0 132 {
d@0 133 _player.Stop();
d@0 134 _player.SoundLocation = audioFile;
d@0 135 _player.Play();
d@0 136 }
d@0 137
d@0 138 private void trackBarTrack1_MouseDown(object sender, MouseEventArgs e)
d@0 139 {
d@0 140 CheckSumbitButtonEnabled();
d@0 141 backgroundWorkerAudio_DoWork(trackBarTrack1, null);
d@0 142 }
d@0 143
d@0 144 private void trackBarTrack2_MouseDown(object sender, MouseEventArgs e)
d@0 145 {
d@0 146 CheckSumbitButtonEnabled();
d@0 147 backgroundWorkerAudio_DoWork(trackBarTrack2, null);
d@0 148 }
d@0 149
d@0 150 private void trackBarTrack3_MouseDown(object sender, MouseEventArgs e)
d@0 151 {
d@0 152 CheckSumbitButtonEnabled();
d@0 153 backgroundWorkerAudio_DoWork(trackBarTrack3, null);
d@0 154 }
d@0 155
d@0 156 private void trackBarTrack4_MouseDown(object sender, MouseEventArgs e)
d@0 157 {
d@0 158 CheckSumbitButtonEnabled();
d@0 159 backgroundWorkerAudio_DoWork(trackBarTrack4, null);
d@0 160 }
d@0 161
d@0 162 private void trackBarTrack5_MouseDown(object sender, MouseEventArgs e)
d@0 163 {
d@0 164 CheckSumbitButtonEnabled();
d@0 165 backgroundWorkerAudio_DoWork(trackBarTrack5, null);
d@0 166 }
d@0 167
d@0 168 private void CheckSumbitButtonEnabled()
d@0 169 {
d@0 170 if (_track1Played && _track2Played && _track3Played && _track4Played && _track5Played)
d@0 171 {
d@0 172 buttonSubmit.Enabled = true;
d@0 173 }
d@0 174 }
d@0 175
d@0 176 private void ListenToAudio_Load(object sender, EventArgs e)
d@0 177 {
d@0 178
d@0 179 this.CenterToScreen();
d@0 180
d@0 181 Name nameDialog = new Name();
d@0 182 String currentUser = String.Empty;
d@0 183 String testType = String.Empty;
d@0 184
d@0 185 this.Hide();
d@0 186 nameDialog.ShowDialog(this);
d@0 187
d@0 188 if (nameDialog.radioButtonTestA.Checked)
d@0 189 {
d@0 190 testType = "A";
d@0 191 textBoxTestInstructions.Text = "Please rate the mixes in terms of your own overall preference.";
d@0 192 }
d@0 193 else if (nameDialog.radioButtonTestB.Checked)
d@0 194 {
d@0 195 testType = "B";
d@0 196 textBoxTestInstructions.Text = "Please rate the mixes in terms of the ability to distinguish the sources (i.e. the lack of masking).";
d@0 197 }
d@0 198
d@0 199 nameDialog.ShowDialog(this);
d@0 200
d@0 201 _experimentData.NameOfParticipant = nameDialog.tbName.Text;
d@0 202
d@0 203 nameDialog.ShowDialog(this);
d@0 204
d@0 205 //Create log file for Data
d@0 206 _logger = new Logging(testType + "_" + _experimentData.NameOfParticipant + "_LTResults.txt", ",");
d@0 207
d@0 208
d@0 209 string path = Directory.GetCurrentDirectory();
d@0 210 path = path + "\\Files.txt";
d@0 211
d@0 212 List <String> audioFiles = new List<string>();
d@0 213
d@0 214 try
d@0 215 {
d@0 216 using (var sr = new StreamReader(path))
d@0 217 {
d@0 218 var reader = new CsvReader(sr);
d@0 219
d@0 220 while (true)
d@0 221 {
d@0 222 reader.Configuration.HasHeaderRecord = false;
d@0 223 var row = reader.Read();
d@0 224 if (reader.CurrentRecord == null)
d@0 225 {
d@0 226 break;
d@0 227 }
d@0 228 audioFiles.Add(reader.CurrentRecord[0]);
d@0 229 }
d@0 230 }
d@0 231
d@0 232 }
d@0 233 catch (Exception ex)
d@0 234 {
d@0 235 MessageBox.Show(ex.Message, this.Name, MessageBoxButtons.OK, MessageBoxIcon.Warning);
d@0 236 throw;
d@0 237 }
d@0 238
d@0 239 int songIndex = 0;
d@0 240 foreach (var pathAudio in audioFiles)
d@0 241 {
d@0 242
d@0 243 ExperimentData expdata = new ExperimentData();
d@0 244 string [] fileEntries = Directory.GetFiles(pathAudio, "*.wav");
d@0 245
d@0 246 foreach (string fileName in fileEntries)
d@0 247 {
d@0 248
d@0 249 _experimentData.Songs[songIndex].Add(fileName);
d@0 250
d@0 251 }
d@0 252
d@0 253 songIndex++;
d@0 254 }
d@0 255
d@0 256
d@0 257 //Randomise Everyting for the experiment
d@0 258 var rnd = new Random();
d@0 259 _experimentData.Song1Mixes = _experimentData.Song1Mixes.OrderBy(x => rnd.Next()).ToList();
d@0 260 rnd = new Random();
d@0 261 _experimentData.Song2Mixes = _experimentData.Song2Mixes.OrderBy(x => rnd.Next()).ToList();
d@0 262 rnd = new Random();
d@0 263 _experimentData.Song3Mixes = _experimentData.Song3Mixes.OrderBy(x => rnd.Next()).ToList();
d@0 264 rnd = new Random();
d@0 265 _experimentData.Song4Mixes = _experimentData.Song4Mixes.OrderBy(x => rnd.Next()).ToList();
d@0 266 rnd = new Random();
d@0 267 _experimentData.Song5Mixes = _experimentData.Song5Mixes.OrderBy(x => rnd.Next()).ToList();
d@0 268
d@0 269 rnd = new Random();
d@0 270 _experimentData.Songs = _experimentData.Songs.OrderBy(x => rnd.Next()).ToList();
d@0 271
d@0 272 int songNum = _testNumber + 1;
d@0 273 this.Text = "Experiment - Song " + songNum.ToString();
d@0 274
d@0 275 AssignSliders();
d@0 276
d@0 277 _stopWatchListenToSong.Reset();
d@0 278 _stopWatchListenToSong.Start();
d@0 279 }
d@0 280
d@0 281 private void AssignSliders()
d@0 282 {
d@0 283 //TODO: Put in the real mixes and run the fuckin experiment.
d@0 284 _track1FileName = _experimentData.Songs[_testNumber][0];
d@0 285 _track2FileName = _experimentData.Songs[_testNumber][1];
d@0 286 _track3FileName = _experimentData.Songs[_testNumber][2];
d@0 287 _track4FileName = _experimentData.Songs[_testNumber][3];
d@0 288 _track5FileName = _experimentData.Songs[_testNumber][4];
d@0 289
d@0 290 Random rnd = new Random();
d@0 291 trackBarTrack1.Value = rnd.Next(0, 100);
d@0 292 trackBarTrack2.Value = rnd.Next(0, 100);
d@0 293 trackBarTrack3.Value = rnd.Next(0, 100);
d@0 294 trackBarTrack4.Value = rnd.Next(0, 100);
d@0 295 trackBarTrack5.Value = rnd.Next(0, 100);
d@0 296
d@0 297 _track1CurrentValue = trackBarTrack1.Value;
d@0 298 _track2CurrentValue = trackBarTrack2.Value;
d@0 299 _track3CurrentValue = trackBarTrack3.Value;
d@0 300 _track4CurrentValue = trackBarTrack4.Value;
d@0 301 _track5CurrentValue = trackBarTrack5.Value;
d@0 302
d@0 303 _track1Played = false;
d@0 304 _track2Played = false;
d@0 305 _track3Played = false;
d@0 306 _track4Played = false;
d@0 307 _track5Played = false;
d@0 308
d@0 309 _track1TimesMoved = 0;
d@0 310 _track2TimesMoved = 0;
d@0 311 _track3TimesMoved = 0;
d@0 312 _track4TimesMoved = 0;
d@0 313 _track5TimesMoved = 0;
d@0 314
d@0 315 }
d@0 316
d@0 317 private void buttonSubmit_Click(object sender, EventArgs e)
d@0 318 {
d@0 319 _player.Stop();
d@0 320
d@0 321 foreach (ToolStripStatusLabel label in statusStrip1.Items)
d@0 322 {
d@0 323 label.Text = "";
d@0 324 }
d@0 325
d@0 326 _stopWatchListenToSong.Stop();
d@0 327 TimeSpan ts = _stopWatchListenToSong.Elapsed;
d@0 328
d@0 329 // Format and display the TimeSpan value.
d@0 330 string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
d@0 331 ts.Hours, ts.Minutes, ts.Seconds,
d@0 332 ts.Milliseconds / 10);
d@0 333
d@0 334 Result currentResult = new Result(_experimentData.NameOfParticipant, elapsedTime,
d@0 335 new MixScore(Path.GetFileName(_track1FileName), trackBarTrack1.Value), _track1TimesMoved,
d@0 336 new MixScore(Path.GetFileName(_track2FileName), trackBarTrack2.Value), _track2TimesMoved,
d@0 337 new MixScore(Path.GetFileName(_track3FileName), trackBarTrack3.Value), _track3TimesMoved,
d@0 338 new MixScore(Path.GetFileName(_track4FileName), trackBarTrack4.Value), _track4TimesMoved,
d@0 339 new MixScore(Path.GetFileName(_track5FileName), trackBarTrack5.Value), _track5TimesMoved);
d@0 340 _results.Add(currentResult);
d@0 341
d@0 342 _logger.WriteLTData(currentResult);
d@0 343
d@0 344 if (_testNumber == 4)
d@0 345 {
d@0 346 ThankYou thankYouDialog = new ThankYou("Thank you for taking part in this part of the experiment. Please continue to the next section."); // Show testDialog as a modal dialog and determine if DialogResult = OK.
d@0 347 if (thankYouDialog.ShowDialog(this) == DialogResult.OK)
d@0 348 {
d@0 349 // Read the contents of testDialog's TextBox.
d@0 350
d@0 351 }
d@0 352 _logger.CloseFile();
d@0 353 _perceivedEmotion = new PerceivedEmotion(_experimentData.NameOfParticipant);
d@0 354 _perceivedEmotion.Show(this);
d@0 355 this.Hide();
d@0 356 }
d@0 357 else
d@0 358 {
d@0 359 ThankYou thankYouDialog = new ThankYou(); // Show testDialog as a modal dialog and determine if DialogResult = OK.
d@0 360 if (thankYouDialog.ShowDialog(this) == DialogResult.OK)
d@0 361 {
d@0 362 // Read the contents of testDialog's TextBox.
d@0 363
d@0 364 }
d@0 365
d@0 366 _testNumber++;
d@0 367 int songNum = _testNumber + 1;
d@0 368 this.Text = "Perceived Emotion Experiment - Song " + songNum.ToString();
d@0 369 AssignSliders();
d@0 370 _stopWatchListenToSong.Reset();
d@0 371 _stopWatchListenToSong.Start();
d@0 372 buttonSubmit.Enabled = false;
d@0 373 }
d@0 374
d@0 375
d@0 376 }
d@0 377
d@0 378 private void buttonPlayTrack1_Click(object sender, EventArgs e)
d@0 379 {
d@0 380 _track1Played = true;
d@0 381 CheckSumbitButtonEnabled();
d@0 382 timerStatusBar.Interval = 33000;
d@0 383 timerStatusBar.Start();
d@0 384 backgroundWorkerAudio_DoWork(buttonPlayTrack1, null);
d@0 385 }
d@0 386
d@0 387 private void buttonPlayTrack2_Click(object sender, EventArgs e)
d@0 388 {
d@0 389 _track2Played = true;
d@0 390 CheckSumbitButtonEnabled();
d@0 391 timerStatusBar.Interval = 33000;
d@0 392 timerStatusBar.Start();
d@0 393 backgroundWorkerAudio_DoWork(buttonPlayTrack2, null);
d@0 394 }
d@0 395
d@0 396 private void buttonPlayTrack3_Click(object sender, EventArgs e)
d@0 397 {
d@0 398 _track3Played = true;
d@0 399 CheckSumbitButtonEnabled();
d@0 400 timerStatusBar.Interval = 33000;
d@0 401 timerStatusBar.Start();
d@0 402 backgroundWorkerAudio_DoWork(buttonPlayTrack3, null);
d@0 403 }
d@0 404
d@0 405 private void buttonPlayTrack4_Click(object sender, EventArgs e)
d@0 406 {
d@0 407 _track4Played = true;
d@0 408 CheckSumbitButtonEnabled();
d@0 409 timerStatusBar.Interval = 33000;
d@0 410 timerStatusBar.Start();
d@0 411 backgroundWorkerAudio_DoWork(buttonPlayTrack4, null);
d@0 412 }
d@0 413
d@0 414 private void buttonPlayTrack5_Click(object sender, EventArgs e)
d@0 415 {
d@0 416 _track5Played = true;
d@0 417 CheckSumbitButtonEnabled();
d@0 418 timerStatusBar.Interval = 33000;
d@0 419 timerStatusBar.Start();
d@0 420 backgroundWorkerAudio_DoWork(buttonPlayTrack5, null);
d@0 421 }
d@0 422
d@0 423 private void buttonStopTrack1_Click(object sender, EventArgs e)
d@0 424 {
d@0 425 _player.Stop();
d@0 426
d@0 427 foreach (ToolStripStatusLabel label in statusStrip1.Items)
d@0 428 {
d@0 429 label.Text = "";
d@0 430 }
d@0 431 }
d@0 432
d@0 433 private void buttonStopTrack2_Click(object sender, EventArgs e)
d@0 434 {
d@0 435 _player.Stop();
d@0 436
d@0 437 foreach (ToolStripStatusLabel label in statusStrip1.Items)
d@0 438 {
d@0 439 label.Text = "";
d@0 440 }
d@0 441 }
d@0 442
d@0 443 private void buttonStopTrack3_Click(object sender, EventArgs e)
d@0 444 {
d@0 445 _player.Stop();
d@0 446
d@0 447 foreach (ToolStripStatusLabel label in statusStrip1.Items)
d@0 448 {
d@0 449 label.Text = "";
d@0 450 }
d@0 451 }
d@0 452
d@0 453 private void buttonStopTrack4_Click(object sender, EventArgs e)
d@0 454 {
d@0 455 _player.Stop();
d@0 456
d@0 457 foreach (ToolStripStatusLabel label in statusStrip1.Items)
d@0 458 {
d@0 459 label.Text = "";
d@0 460 }
d@0 461 }
d@0 462
d@0 463 private void buttonStopTrack5_Click(object sender, EventArgs e)
d@0 464 {
d@0 465 _player.Stop();
d@0 466
d@0 467 foreach (ToolStripStatusLabel label in statusStrip1.Items)
d@0 468 {
d@0 469 label.Text = "";
d@0 470 }
d@0 471 }
d@0 472
d@0 473 private void timerStatusBar_Tick(object sender, EventArgs e)
d@0 474 {
d@0 475 timerStatusBar.Stop();
d@0 476 foreach (ToolStripStatusLabel label in statusStrip1.Items)
d@0 477 {
d@0 478 label.Text = "";
d@0 479 }
d@0 480 }
d@0 481
d@0 482 private void trackBarTrack1_MouseUp(object sender, MouseEventArgs e)
d@0 483 {
d@0 484
d@0 485 if (trackBarTrack1.Value != _track1CurrentValue)
d@0 486 {
d@0 487 _track1TimesMoved++;
d@0 488 }
d@0 489
d@0 490 _track1CurrentValue = trackBarTrack1.Value;
d@0 491 }
d@0 492
d@0 493 private void trackBarTrack2_MouseUp(object sender, MouseEventArgs e)
d@0 494 {
d@0 495
d@0 496 if (trackBarTrack2.Value != _track2CurrentValue)
d@0 497 {
d@0 498 _track2TimesMoved++;
d@0 499 }
d@0 500
d@0 501 _track2CurrentValue = trackBarTrack2.Value;
d@0 502 }
d@0 503
d@0 504 private void trackBarTrack3_MouseUp(object sender, MouseEventArgs e)
d@0 505 {
d@0 506
d@0 507 if (trackBarTrack3.Value != _track3CurrentValue)
d@0 508 {
d@0 509 _track3TimesMoved++;
d@0 510 }
d@0 511
d@0 512 _track3CurrentValue = trackBarTrack3.Value;
d@0 513 }
d@0 514
d@0 515 private void trackBarTrack4_MouseUp(object sender, MouseEventArgs e)
d@0 516 {
d@0 517
d@0 518 if (trackBarTrack4.Value != _track4CurrentValue)
d@0 519 {
d@0 520 _track4TimesMoved++;
d@0 521 }
d@0 522
d@0 523 _track4CurrentValue = trackBarTrack4.Value;
d@0 524 }
d@0 525
d@0 526 private void trackBarTrack5_MouseUp(object sender, MouseEventArgs e)
d@0 527 {
d@0 528
d@0 529 if (trackBarTrack5.Value != _track5CurrentValue)
d@0 530 {
d@0 531 _track5TimesMoved++;
d@0 532 }
d@0 533
d@0 534 _track5CurrentValue = trackBarTrack5.Value;
d@0 535 }
d@0 536
d@0 537 private void ListenToAudio_FormClosing(object sender, FormClosingEventArgs e)
d@0 538 {
d@0 539 _player.Stop();
d@0 540 _logger.CloseFile();
d@0 541 }
d@0 542 }
d@0 543 }