Exemplo n.º 1
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (TestCase1.Checked)
            {
                List <User> t    = TestcaseLoader.LoadTestcase1Testing(@"C:\test\Complete SpeakerID Dataset\TestingList.txt");
                Stopwatch   time = new Stopwatch();

                FileStream   f = new FileStream("Projectsequences1.txt", FileMode.Open);
                StreamReader s = new StreamReader(f);
                //Random rd = new Random();

                seq = AudioOperations.ExtractFeatures(t[0].UserTemplates[0]);
                double min2 = double.MaxValue;
                string ans2 = "";
                //Console.Write("Enter The Window Index :");
                // int w = Console.Read();
                while (s.Peek() != -1)
                {
                    double res2     = 0;
                    string sequence = "";
                    string _name    = s.ReadLine();
                    sequence = s.ReadLine();
                    string[] arr  = sequence.Split('#');
                    string[] arr3 = new string[arr.Length];
                    Sequence seq2 = new Sequence();
                    seq2.Frames = new MFCCFrame[arr.Length];
                    for (int i = 0; i < arr.Length; i++)
                    {
                        seq2.Frames[i] = new MFCCFrame();
                        for (int o = 0; o < arr.Length; o++)
                        {
                            arr3 = arr[o].Split('@');
                        }
                        for (int j = 0; j < 13; j++)
                        {
                            seq2.Frames[i].Features[j] = double.Parse(arr3[j]);
                        }
                    }
                    time.Start();
                    res2 = PRUNINGDTW.DTW2fun(seq, seq2);
                    time.Stop();
                    if (res2 < min2)
                    {
                        min2 = res2;
                        ans2 = _name;
                    }
                }
                Console.WriteLine("NEW DTW");
                Console.WriteLine(ans2 + " " + min2);

                Console.WriteLine("Time elapsed: {0:hh\\:mm\\:ss}", time.Elapsed);
                s.Close();
            }

            if (TestCase2.Checked)
            {
                List <User> t    = TestcaseLoader.LoadTestcase2Testing(@"C:\test\Complete SpeakerID Dataset\TestingList.txt");
                Stopwatch   time = new Stopwatch();

                FileStream   f = new FileStream("Projectsequences2.txt", FileMode.Open);
                StreamReader s = new StreamReader(f);

                seq = AudioOperations.ExtractFeatures(t[0].UserTemplates[0]);
                double min2 = double.MaxValue;
                string ans2 = "";
                //Console.Write("Enter The Window Index :");
                // int w = Console.Read();
                while (s.Peek() != -1)
                {
                    double res2     = 0;
                    string sequence = "";
                    string _name    = s.ReadLine();
                    sequence = s.ReadLine();
                    string[] arr  = sequence.Split('#');
                    string[] arr3 = new string[arr.Length];
                    Sequence seq2 = new Sequence();
                    seq2.Frames = new MFCCFrame[arr.Length];
                    for (int i = 0; i < arr.Length; i++)
                    {
                        seq2.Frames[i] = new MFCCFrame();
                        for (int o = 0; o < arr.Length; o++)
                        {
                            arr3 = arr[o].Split('@');
                        }
                        for (int j = 0; j < 13; j++)
                        {
                            seq2.Frames[i].Features[j] = double.Parse(arr3[j]);
                        }
                    }
                    time.Start();
                    res2 = PRUNINGDTW.DTW2fun(seq, seq2);
                    time.Stop();
                    if (res2 < min2)
                    {
                        min2 = res2;
                        ans2 = _name;
                    }
                }
                Console.WriteLine("NEW DTW");
                Console.WriteLine(ans2 + " " + min2);

                Console.WriteLine("Time elapsed: {0:hh\\:mm\\:ss}", time.Elapsed);
                s.Close();
            }

            if (TestCase3.Checked)
            {
                List <User> t    = TestcaseLoader.LoadTestcase3Testing(@"C:\test\Complete SpeakerID Dataset\TestingList.txt");
                Stopwatch   time = new Stopwatch();
                for (int k = 0; k < t.Count; k++)
                {
                    FileStream   f       = new FileStream("Projectsequences3.txt", FileMode.Open);
                    StreamReader s       = new StreamReader(f);
                    Random       rd      = new Random();
                    int          counter = rd.Next(0, t[k].UserTemplates.Count);
                    seq = AudioOperations.ExtractFeatures(t[k].UserTemplates[counter]);
                    double min2 = double.MaxValue;
                    string ans2 = "";
                    //Console.Write("Enter The Window Index :");
                    // int w = Console.Read();
                    while (s.Peek() != -1)
                    {
                        double res2     = 0;
                        string sequence = "";
                        string _name    = s.ReadLine();
                        sequence = s.ReadLine();
                        string[] arr  = sequence.Split('#');
                        string[] arr3 = new string[arr.Length];
                        Sequence seq2 = new Sequence();
                        seq2.Frames = new MFCCFrame[arr.Length];
                        for (int i = 0; i < arr.Length; i++)
                        {
                            seq2.Frames[i] = new MFCCFrame();
                            for (int o = 0; o < arr.Length; o++)
                            {
                                arr3 = arr[o].Split('@');
                            }
                            for (int j = 0; j < 13; j++)
                            {
                                seq2.Frames[i].Features[j] = double.Parse(arr3[j]);
                            }
                        }
                        time.Start();
                        res2 = PRUNINGDTW.DTW2fun(seq, seq2);
                        if (res2 < min2)
                        {
                            min2 = res2;
                            ans2 = _name;
                        }
                        Console.WriteLine("NEW DTW");
                        Console.WriteLine(ans2 + " " + min2);
                        time.Stop();
                        Console.WriteLine("Time elapsed: {0:hh\\:mm\\:ss}", time.Elapsed);
                    }
                    Console.WriteLine(ans2);
                    Console.WriteLine(min2);
                    MessageBox.Show(ans2 + " " + min2);
                    s.Close();
                }
            }

            if (Normal.Checked)
            {
                FileStream   f    = new FileStream("Projectsequences.txt", FileMode.Open);
                StreamReader s    = new StreamReader(f);
                Stopwatch    time = new Stopwatch();
                double       min2 = double.MaxValue;
                string       ans2 = "";
                //Console.Write("Enter The Window Index :");
                // int w = Console.Read();
                while (s.Peek() != -1)
                {
                    double res2     = 0;
                    string sequence = "";
                    string _name    = s.ReadLine();
                    sequence = s.ReadLine();
                    string[] arr  = sequence.Split('#');
                    string[] arr3 = new string[arr.Length];
                    Sequence seq2 = new Sequence();
                    seq2.Frames = new MFCCFrame[arr.Length];
                    for (int i = 0; i < arr.Length; i++)
                    {
                        seq2.Frames[i] = new MFCCFrame();
                        for (int o = 0; o < arr.Length; o++)
                        {
                            arr3 = arr[o].Split('@');
                        }
                        for (int j = 0; j < 13; j++)
                        {
                            seq2.Frames[i].Features[j] = double.Parse(arr3[j]);
                        }
                    }
                    time.Start();
                    res2 = PRUNINGDTW.DTW2fun(seq, seq2);
                    if (res2 < min2)
                    {
                        min2 = res2;
                        ans2 = _name;
                    }
                    Console.WriteLine("NEW DTW");
                    Console.WriteLine(ans2 + " " + min2);
                    time.Stop();
                    Console.WriteLine("Time elapsed: {0:hh\\:mm\\:ss}", time.Elapsed);
                }
                Console.WriteLine(ans2);
                Console.WriteLine(min2);
                s.Close();
            }
        }
Exemplo n.º 2
0
        private void btnIdentify_Click(object sender, EventArgs e)
        {
            /// <summary>
            /// updating status label
            /// </summary>
            ///
            metroLabel1.Visible    = true;
            pruningToggle1.Visible = true;
            labelStatus.Text       = "Status: Identifying speaker and running DTW algorithm...";


            List <User> test = new List <User>();
            string      cnt  = "";// To choose a testcase

            if (Testcase1_radioButton.Checked == true)
            {
                test = TestcaseLoader.LoadTestcase1Testing(@"C:\SpeakerID\Test Cases\Complete SpeakerID Dataset\TestingList.txt"); cnt = "1";
            }
            else if (Testcase2_radioButton.Checked == true)
            {
                test = TestcaseLoader.LoadTestcase2Testing(@"C:\SpeakerID\Test Cases\Complete SpeakerID Dataset\TestingList.txt"); cnt = "2";
            }
            else if (Testcase3_radioButton.Checked == true)
            {
                test = TestcaseLoader.LoadTestcase3Testing(@"C:\SpeakerID\Test Cases\Complete SpeakerID Dataset\TestingList.txt"); cnt = "3";
            }
            else if (caseMilestone1.Checked) //First testcase
            {
                AudioSignal signalTemp = AudioOperations.OpenAudioFile("C:\\SpeakerID\\Test Cases\\Sample Test\\Input sample\\ItIsPlausible_Rich_US_English.wav");
                signalTemp = AudioOperations.RemoveSilence(signalTemp); //don't analyze (template function)
                User userTemp = new User();
                userTemp.UserTemplates = new List <AudioSignal>();
                userTemp.UserName      = ("Rich");
                userTemp.UserTemplates.Add(signalTemp);
                test.Add(userTemp);
                cnt = "4";
            }
            else if (caseMilestone2.Checked)
            {
                AudioSignal signalTemp = AudioOperations.OpenAudioFile("C:\\SpeakerID\\Test Cases\\Pruning Test\\1 min\\[Input] Why Study Algorithms - (1 min).wav");
                signalTemp = AudioOperations.RemoveSilence(signalTemp);
                User userTemp = new User();
                userTemp.UserTemplates = new List <AudioSignal>();
                userTemp.UserName      = ("BigOh (1 min)");
                userTemp.UserTemplates.Add(signalTemp);
                test.Add(userTemp);
                cnt = "5";
            }
            else if (caseMilestone3.Checked)
            {
                AudioSignal signalTemp = AudioOperations.OpenAudioFile("C:\\SpeakerID\\Test Cases\\Pruning Test\\4 min\\[Input] Why Study Algorithms - (4 min).wav");
                signalTemp = AudioOperations.RemoveSilence(signalTemp);
                User userTemp = new User();
                userTemp.UserTemplates = new List <AudioSignal>();
                userTemp.UserName      = ("BigOh (4 min)");
                userTemp.UserTemplates.Add(signalTemp);
                test.Add(userTemp);
                cnt = "6";
            }
            for (int y = 0; y < test.Count; ++y)
            {
                Random      rnd = new Random(); //Generate random number to choose an audio sample to test it against the database
                AudioSignal sig = new AudioSignal();
                sig = test[y].UserTemplates[0]; //rnd.Next(0, test[y].UserTemplates.Count)];
                Sequence compareSequence_input = new Sequence();
                compareSequence_input = AudioOperations.ExtractFeatures(sig);
                FileStream   FS        = new FileStream("C:/SpeakerID/case" + cnt + "/Relations.txt", FileMode.Open);
                StreamReader SR        = new StreamReader(FS);
                string       relations = SR.ReadLine();
                SR.Close();
                /// <summary>
                ///  Split the string relations into an array of strings
                ///   Delimiter: #
                ///  Then split each string into name and path
                ///   Delimiter: %
                ///   Filling an array of "relationItem"s with the names and sequence paths of all sequences in the database, and leaving the
                ///   distance attribute in all the array items as they are "NULL", until calculated using the DTW algorithm
                /// </summary>
                ///
                string[]       relationsStrArr    = relations.Split('#');
                relationItem[] relationsStructArr = new relationItem[relationsStrArr.Length - 1];
                matchedPerson  person             = new matchedPerson();
                person.distance = -1;

                for (int i = 0; i < relationsStructArr.Length; i++)
                {
                    string[] stringArrTemp = relationsStrArr[i].Split('%');
                    relationsStructArr[i].nameStr      = stringArrTemp[0];
                    relationsStructArr[i].sequencePath = stringArrTemp[1];
                }
                Stopwatch sw;
                TimeSpan  TS = TimeSpan.Zero;
                for (int i = 0; i < relationsStructArr.Length; i++) //Loops on all sequences in DB
                {
                    /// Reset the "compareSequence_output" variable and prepare SR for reading the sequence
                    Sequence compareSequence_output = new Sequence();
                    FS = new FileStream(relationsStructArr[i].sequencePath, FileMode.Open);
                    SR = new StreamReader(FS);
                    /// Resetting the "file" variable
                    string file = "";
                    file += SR.ReadLine();
                    string[] strTempFrames = file.Split('#');                            //strTemp now contains all Frames in a sequence
                    compareSequence_output.Frames = new MFCCFrame[strTempFrames.Length]; //Setting number of frames in output sequence
                    for (int j = 0; j < strTempFrames.Length; j++)                       //Loops on all frames in a sequence
                    {
                        string[]  strTempFeatures = strTempFrames[j].Split('%');
                        MFCCFrame frame           = new MFCCFrame();
                        for (int k = 0; k < 13; k++)
                        {
                            frame.Features[k] = double.Parse(strTempFeatures[k]);                          //Loops on all 13 features in a frame and converts string to double
                        }
                        compareSequence_output.Frames[j] = new MFCCFrame();
                        for (int k = 0; k < 13; k++)
                        {
                            compareSequence_output.Frames[j].Features[k] = frame.Features[k];
                        }
                    }

                    /// Save the difference between compareSequence_output and compareSequence_input in relationsStructArr[i].distance
                    if (pruningToggle1.Checked == true)
                    {
                        sw = Stopwatch.StartNew();
                        relationsStructArr[i].distance = Pruning_DTW(compareSequence_input, compareSequence_output);
                        sw.Stop();
                        TS += sw.Elapsed;
                    }
                    else
                    {
                        sw = Stopwatch.StartNew();
                        relationsStructArr[i].distance = DTW_improved(compareSequence_input, compareSequence_output);
                        sw.Stop();
                        TS += sw.Elapsed;
                    }

                    // update person object
                    if (relationsStructArr[i].distance < person.distance || person.distance == -1)
                    {
                        person.distance = relationsStructArr[i].distance;
                        person.name     = relationsStructArr[i].nameStr;
                    }
                    /// Close the SR
                    SR.Close();
                    Console.WriteLine(relationsStructArr[i].nameStr);
                }

                /// <summary>
                ///  Updating status label and showing search results
                /// </summary>
                ///
                labelStatus.Text = "Status: Searching done.";
                Console.WriteLine(test[y].UserName);
                MessageBox.Show("Search completed\nThe closest match to this speaker is: " + person.name + "\nTime Elapsed for DTW only: " + TS.Hours + ':' + TS.Minutes + ':' + TS.Seconds + ':' + TS.Milliseconds + "\nTotal cost: " + person.distance, "Search Results");
                labelStatus.Text = "Status: Ready";
            }
        }