private void BtnOpen_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Filter = "Midi Files|*.mid";
            openFileDialog.Title  = "Select a Midi File";

            if (openFileDialog.ShowDialog().Value)
            {
                string filename = openFileDialog.FileName;

                //todo: using Dispatcher.Invoke doesn't work
                Dispatcher.Invoke(() => { txt.Text += $"File {filename} loading...\r\n"; });
                composition = MidiFileReader.Read(filename);
                Dispatcher.Invoke(() => { txt.Text += "File loaded.\r\n"; this.UpdateLayout(); });

                txt.Text += "Analysing...\r\n";
                var allNodes = ChomskyGrammarAnalysis.Reduce(composition.GetVoices(), new List <TwelveToneSet>()
                {
                    TwelveToneSet.majorScale
                });
                txt.Text += $"Analysis finished!\r\n";

                txt.Text += "Post analysis...\r\n";
                ChomskyGrammarAnalysis.PostAnalysis(composition.GetVoices());
                txt.Text += "Post analysis finished!\r\n";

                this.Title = $"{filename} - Music Analysis";

                listView.ItemsSource = allNodes;
            }
        }
Exemple #2
0
        private void Test(string filename, int take = int.MaxValue, Fraction?prec = null)
        {
            var lists = MidiFileReader.Read(filename, prec).GetVoices().Take(take).ToList();

            // Save copy
            var copy = FlatCopy(lists);
            //AssertEqual(lists, copy);

            // Perform Chomsky reduction
            var allNodes = ChomskyGrammarAnalysis.Reduce(lists, new List <TwelveToneSet>()
            {
                TwelveToneSet.majorScale
            });

            AssertEqual(lists, copy);

            // At least one diatonic?
            //Assert.IsTrue(allNodes.Exists(mpl => mpl.IsDiatonic));

            // Post analysis
            ChomskyGrammarAnalysis.PostAnalysis(lists);
            ChomskyGrammarAnalysis.Print(allNodes);
            CheckPostAnalysis(allNodes, lists.Count);

            int totalNotesBefore = copy.Sum(list => list.Count);
            int totalNotesAfter  = allNodes.Sum(list => list.children.Count(imp => imp is NoteWithDuration));
            int totalLeafs       = allNodes.Sum(list => list.IsLeaf ? 1 : 0);
            int totalLeafNotes   = allNodes.Sum(list => list.IsLeaf ? list.Count : 0);

            Debug.WriteLine(filename);
            Debug.WriteLine($"Total notes before: {totalNotesBefore}");
            Debug.WriteLine($"Total notes after: {totalNotesAfter}");
            Debug.WriteLine($"After/before: {100 * totalNotesAfter / totalNotesBefore}%");
            Debug.WriteLine($"Total leaf notes: {totalLeafNotes}");
            Debug.WriteLine($"Leaf notes/before: {100 * totalLeafNotes / totalNotesBefore}%");
            Debug.WriteLine($"Total leafs: {totalLeafs}");
        }