private static void output_expInterval(int id, Constants.ExpIntervalofPair expIntervals, bool createOutputFile, string output_path)
        {
            char[] delimeters = new char[] { ' ', '\t' };
            if (createOutputFile)
            {
                string[] lines = { "#id #StartingTime(Pair_FirstParticipant) #EndingTime(Pair_FirstParticipant) #StartingTime(Pair_SecondParticipant) #EndingTime(Pair_SecondParticipant)", id + " " + expIntervals.starting_time_pair1 + " " + expIntervals.ending_time_pair_1 + " " + expIntervals.starting_time_pair2 + " " + expIntervals.ending_time_pair_2 };

                System.IO.File.WriteAllLines(output_path, lines);
            }
            else
            {
                string[] starting_ending_time_file           = File.ReadAllLines(output_path);
                Dictionary <string, string> dict_output_file = new Dictionary <string, string>();
                string writeToFile = "";
                foreach (String line in starting_ending_time_file)
                {
                    string line_to_write = line;
                    //if (line.Contains('#')) //header
                    //{
                    //    continue;
                    //}
                    if (String.IsNullOrEmpty(line) || String.IsNullOrWhiteSpace(line) || line.Trim(new[] { ' ', '\t', '\n' }).Length <= 0)
                    {
                        continue;
                    }
                    else if (line.Split(delimeters)[0].Equals(id.ToString()))
                    {
                        line_to_write = id + " " + expIntervals.starting_time_pair1 + " " + expIntervals.ending_time_pair_1 + " " + expIntervals.starting_time_pair2 + " " + expIntervals.ending_time_pair_2;
                    }

                    writeToFile += line_to_write + "\n";
                }
                writeToFile += id + " " + expIntervals.starting_time_pair1 + " " + expIntervals.ending_time_pair_1 + " " + expIntervals.starting_time_pair2 + " " + expIntervals.ending_time_pair_2;
                System.IO.File.WriteAllText(output_path, writeToFile);
            }
        }
        public Constants.ExpIntervalofPair findSegmentsWithUtilizingSynchInfo()
        {
            Dictionary <int, string> dictionary = new Dictionary <int, string>();


            int starting_time_difference_between_participants_in_pair = 0;

            char[]   delimiterChars = { ' ', '\t' };
            string[] words_base, words_ref;
            string   segment_num = "";
            string   line_base = "", line_ref = "";
            int      seg_interval_start_base = 0, seg_interval_end_base = 0;
            int      seg_interval_start_ref = 0, seg_interval_end_ref = 0;


            System.IO.StreamReader file_base = new System.IO.StreamReader(Constants.base_file_path);
            System.IO.StreamReader file_ref  = new System.IO.StreamReader(Constants.ref_file_path);
            while ((line_base = file_base.ReadLine()) != null)
            {
                words_base = line_base.Split(delimiterChars, StringSplitOptions.RemoveEmptyEntries);
                if (words_base.Count() <= 3)
                {
                    continue;
                }
                segment_num = words_base[0];
                if (Convert.ToInt32(segment_num) == (Convert.ToInt32(Constants.starting_segment_num_base) + 1)) //entered starting segment num represents just before the experiment, i.e., bip like sound, so we start from the next segment to exclude bip like sound
                {
                    seg_interval_start_base = Convert.ToInt32(words_base[1]);
                    break;
                }
            }

            while ((line_ref = file_ref.ReadLine()) != null)
            {
                words_ref = line_ref.Split(delimiterChars, StringSplitOptions.RemoveEmptyEntries);
                if (words_ref.Count() <= 3)
                {
                    continue;
                }
                segment_num = words_ref[0];
                if (Convert.ToInt32(segment_num) == (Convert.ToInt32(Constants.starting_segment_num_ref) + 1))
                {
                    seg_interval_start_ref = Convert.ToInt32(words_ref[1]);
                    break;
                }
            }


            starting_time_difference_between_participants_in_pair = seg_interval_start_base - seg_interval_start_ref;
            List <int> intervals = new List <int>();
            string     audio_of_the_one_selected_for_resegment     = controls.rbOptionFirstParticipant_determine_interval_resegment.Checked ? "FirstParticipant" : "SecondParticipant";
            string     audio_of_the_one_not_selected_for_resegment = controls.rbOptionFirstParticipant_determine_interval_resegment.Checked ? "SecondParticipant" : "FirstParticipant";

            try
            {
                do
                {
                    words_base = line_base.Split(delimiterChars, StringSplitOptions.RemoveEmptyEntries);
                    if (words_base.Count() <= 3)
                    {
                        continue;
                    }
                    segment_num = words_base[0];
                    if (Convert.ToInt32(segment_num) == (Convert.ToInt32(Constants.ending_segment_num_base) + 1))
                    {
                        break;
                    }
                    seg_interval_start_base = Convert.ToInt32(words_base[1]);
                    seg_interval_end_base   = Convert.ToInt32(words_base[2]);
                    if (!intervals.Contains(seg_interval_start_base))
                    {
                        intervals.Add(seg_interval_start_base);
                        dictionary.Add(seg_interval_start_base, " Start:" + audio_of_the_one_selected_for_resegment + ":" + words_base[0]);
                    }
                    if (!intervals.Contains(seg_interval_end_base))
                    {
                        intervals.Add(seg_interval_end_base);
                        if (!dictionary.ContainsKey(seg_interval_end_base))
                        {
                            dictionary.Add(seg_interval_end_base, " End:" + audio_of_the_one_selected_for_resegment + ":" + words_base[0]);
                        }
                    }
                } while ((line_base = file_base.ReadLine()) != null);
            }
            catch (Exception ex)
            {
                throw ex;
            }


            do
            {
                words_ref = line_ref.Split(delimiterChars, StringSplitOptions.RemoveEmptyEntries);
                if (words_ref.Count() <= 3)
                {
                    continue;
                }
                segment_num = words_ref[0];
                if (Convert.ToInt32(segment_num) == (Convert.ToInt32(Constants.ending_segment_num_ref) + 1))
                {
                    break;
                }
                seg_interval_start_ref = Convert.ToInt32(words_ref[1]) + starting_time_difference_between_participants_in_pair;
                seg_interval_end_ref   = Convert.ToInt32(words_ref[2]) + starting_time_difference_between_participants_in_pair;
                if (!intervals.Contains(seg_interval_start_ref))
                {
                    intervals.Add(seg_interval_start_ref);
                    if (!dictionary.ContainsKey(seg_interval_start_ref))
                    {
                        dictionary.Add(seg_interval_start_ref, " Start:" + audio_of_the_one_not_selected_for_resegment + ":" + words_ref[0]);
                    }
                }

                if (!intervals.Contains(seg_interval_end_ref))
                {
                    intervals.Add(seg_interval_end_ref);
                    if (!dictionary.ContainsKey(seg_interval_end_ref))
                    {
                        dictionary.Add(seg_interval_end_ref, " End:" + audio_of_the_one_not_selected_for_resegment + ":" + words_ref[0]);
                    }
                }
            } while ((line_ref = file_ref.ReadLine()) != null);

            intervals.Sort();

            string starting_time_pair1 = "", ending_time_pair_1 = "", starting_time_pair2 = "", ending_time_pair_2 = "";

            for (int i = 0; i < intervals.Count; i = i + 1)
            {
                if (i == 0)
                {
                    File.WriteAllText(Constants.output_path_resegment + "\\segments_interval.txt", i + " " + intervals[i] + "    " + intervals[(i + 1)] + "    " + (intervals[(i + 1)] - intervals[i]) + "  " + dictionary[intervals[i]] + "    " + dictionary[intervals[(i + 1)]] + "    " + starting_time_difference_between_participants_in_pair + "\n");
                    if (controls.rbOptionFirstParticipant_determine_interval_resegment.Checked)
                    {
                        starting_time_pair1 = intervals[i].ToString();
                        starting_time_pair2 = (intervals[i] - starting_time_difference_between_participants_in_pair).ToString();
                    }
                    else
                    {
                        starting_time_pair2 = intervals[i].ToString();
                        starting_time_pair1 = (intervals[i] - starting_time_difference_between_participants_in_pair).ToString();
                    }
                }

                else
                {
                    File.AppendAllText(Constants.output_path_resegment + "\\segments_interval.txt", i + " " + intervals[i] + "    " + intervals[(i + 1)] + "    " + (intervals[(i + 1)] - intervals[i]) + "  " + dictionary[intervals[i]] + dictionary[intervals[(i + 1)]] + "    " + "\n");
                }

                if (i + 2 == intervals.Count())
                {
                    if (controls.rbOptionFirstParticipant_determine_interval_resegment.Checked)
                    {
                        ending_time_pair_1 = intervals[(i + 1)].ToString();
                        ending_time_pair_2 = (intervals[(i + 1)] - starting_time_difference_between_participants_in_pair).ToString();
                    }
                    else
                    {
                        ending_time_pair_2 = intervals[(i + 1)].ToString();
                        ending_time_pair_1 = (intervals[(i + 1)] - starting_time_difference_between_participants_in_pair).ToString();
                    }
                    break;
                }
            }

            Constants.ExpIntervalofPair expInterval = new Constants.ExpIntervalofPair(starting_time_pair1, ending_time_pair_1, starting_time_pair2, ending_time_pair_2);

            return(expInterval);
        }
        public void btn_save_resegment_determine_interval_multiple(object sender, EventArgs e)
        {
            try
            {
                bool   validationError = false;
                int    id;
                string initialSegment_pair1, initialSegment_pair2, lastSegment_pair1, lastSegment_pair2;
                validationError = validation_MultipleFiles(out id, out initialSegment_pair1, out initialSegment_pair2, out lastSegment_pair1, out lastSegment_pair2);

                validationError = validationResegmentFields(validationError);

                if (validationError)
                {
                    return;
                }

                else
                {
                    if (controls.rbOptionFirstParticipant_determine_interval_resegment.Checked)
                    {
                        Constants.base_file_path            = controls.txt_interval_determine_interval_multiple_1.Text;
                        Constants.ref_file_path             = controls.txt_interval_determine_interval_multiple_2.Text;
                        Constants.starting_segment_num_base = controls.txt_starting_number_determine_interval_multiple_1.Text;
                        Constants.starting_segment_num_ref  = controls.txt_starting_number_determine_interval_multiple_2.Text;
                        Constants.ending_segment_num_base   = controls.txt_final_number_determine_interval_multiple_1.Text;
                        Constants.ending_segment_num_ref    = controls.txt_final_number_determine_interval_multiple_2.Text;
                    }
                    else if (controls.rbOptionSecondParticipant_determine_interval_resegment.Checked)
                    {
                        Constants.base_file_path            = controls.txt_interval_determine_interval_multiple_2.Text;
                        Constants.ref_file_path             = controls.txt_interval_determine_interval_multiple_1.Text;
                        Constants.starting_segment_num_base = controls.txt_starting_number_determine_interval_multiple_2.Text;
                        Constants.starting_segment_num_ref  = controls.txt_starting_number_determine_interval_multiple_1.Text;
                        Constants.ending_segment_num_base   = controls.txt_final_number_determine_interval_multiple_2.Text;
                        Constants.ending_segment_num_ref    = controls.txt_final_number_determine_interval_multiple_1.Text;
                    }


                    Constants.output_path_resegment = controls.txt_outputFolder_resegment.Text;
                    Constants.ExpIntervalofPair expIntervals = findSegmentsWithUtilizingSynchInfo();
                    string path_resegmented = Constants.output_path_resegment + "\\segments_interval.txt"; //resegmented file path created in findSegmentsWithUtilizingSynchInfo call

                    bool   createOutputFile = false;
                    string output_path      = controls.txt_outputFile_determine_interval.Text;
                    if (!File.Exists(output_path))
                    {
                        createOutputFile = true;
                    }

                    output_expInterval(id, expIntervals, createOutputFile, output_path);


                    string fileName = controls.txt_resegment_file.Text;
                    string _javadir = UtilityFunctions.LocateJava();

                    ProcessStartInfo start = new ProcessStartInfo();
                    if (!_javadir.Equals(String.Empty))
                    {
                        start.FileName = _javadir + "java.exe";
                    }
                    else
                    {
                        start.FileName = "java.exe";
                    }

                    start.Arguments = "-cp " + Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\speech_analysis\\Sphinx4Files\\sphinx4-core-all-1.0.jar edu.cmu.sphinx.tools.endpoint.Segmenter -i " + fileName + " -o " + controls.txt_outputFolder_resegment.Text + "\\ -comb " + path_resegmented;

                    start.UseShellExecute        = false;
                    start.RedirectStandardInput  = true;
                    start.RedirectStandardOutput = true;

                    //Start the Process
                    Process java = new Process();
                    java.StartInfo = start;
                    bool isStarted = java.Start();
                    java.WaitForExit();
                    int exitCode = java.ExitCode;
                    java.Close();
                    if (exitCode == 1)
                    {
                        MessageBox.Show("Java error thrown, please be sure to pre-request wriiten in walkthrough to run java.");
                    }
                    else
                    {
                        MessageBox.Show("Succesfully done!!");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Please be sure the files exist!!");
            }
        }
        private bool specifyIntervals(int id, string initialSegment_pair1, string initialSegment_pair2, string lastSegment_pair1, string lastSegment_pair2)
        {
            string segmented_file_path_pair1 = controls.txt_interval_determine_interval_multiple_1.Text;
            string segmented_file_path_pair2 = controls.txt_interval_determine_interval_multiple_2.Text;

            string[] segmentedAudios_pair1, segmentedAudios_pair2;

            char[] delimeters = new char[] { ' ', '\t' };
            string starting_time_pair1 = "", ending_time_pair_1 = "", starting_time_pair2 = "", ending_time_pair_2 = "";

            if (File.Exists(segmented_file_path_pair1) && File.Exists(segmented_file_path_pair2))
            {
                segmentedAudios_pair1 = File.ReadAllLines(segmented_file_path_pair1);
                foreach (String line in segmentedAudios_pair1)
                {
                    if (String.IsNullOrEmpty(line) || String.IsNullOrWhiteSpace(line) || line.Trim(new[] { ' ', '\t', '\n' }).Length <= 0)
                    {
                        continue;
                    }
                    if (line.Split(delimeters, StringSplitOptions.RemoveEmptyEntries)[0].Equals(initialSegment_pair1))
                    {
                        starting_time_pair1 = line.Split(delimeters, StringSplitOptions.RemoveEmptyEntries)[2];
                    }
                    if (line.Split(delimeters, StringSplitOptions.RemoveEmptyEntries)[0].Equals(lastSegment_pair1))
                    {
                        ending_time_pair_1 = line.Split(delimeters, StringSplitOptions.RemoveEmptyEntries)[2];
                        break;
                    }
                }

                segmentedAudios_pair2 = File.ReadAllLines(segmented_file_path_pair2);
                foreach (String line in segmentedAudios_pair2)
                {
                    if (String.IsNullOrEmpty(line) || String.IsNullOrWhiteSpace(line) || line.Trim(new[] { ' ', '\t', '\n' }).Length <= 0)
                    {
                        continue;
                    }
                    if (line.Split(delimeters, StringSplitOptions.RemoveEmptyEntries)[0].Equals(initialSegment_pair2))
                    {
                        starting_time_pair2 = line.Split(delimeters, StringSplitOptions.RemoveEmptyEntries)[2];
                    }
                    if (line.Split(delimeters, StringSplitOptions.RemoveEmptyEntries)[0].Equals(lastSegment_pair2))
                    {
                        ending_time_pair_2 = line.Split(delimeters, StringSplitOptions.RemoveEmptyEntries)[2];
                        break;
                    }
                }

                bool   createOutputFile = false;
                string output_path      = controls.txt_outputFile_determine_interval.Text;
                if (!File.Exists(output_path))
                {
                    createOutputFile = true;
                }
                Constants.ExpIntervalofPair expIntervals = new Constants.ExpIntervalofPair(starting_time_pair1, ending_time_pair_1, starting_time_pair2, ending_time_pair_2);
                output_expInterval(id, expIntervals, createOutputFile, output_path);

                return(true);
            }
            else
            {
                return(false);
            }
        }