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; } }
public void PlayBackTest(string filename) { Composition composition = MidiFileReader.Read(filename); int[] tones = new int[128]; // Play the composition keeping track of what nodes are on/off composition.PlayBack( note => { tones[note]++; }, note => { tones[note]--; Assert.IsTrue(tones[note] >= 0); }, IncludeDurations: false ); // At the end all tones are released foreach (int i in tones) { Assert.AreEqual(0, tones[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}"); }
static void Main(string[] args) { if (args.Length < 1) { Console.WriteLine("Playing all songs:"); foreach (MethodInfo mi in typeof(Compositions).GetMethods()) { if (mi.GetParameters().Length == 0 && mi.ReturnType == typeof(Melody12Tone)) { Console.Write(mi.Name + " "); Main(new[] { mi.Name }); Console.WriteLine(); Console.WriteLine("Wait for 1 second..."); Thread.Sleep(1000); } } Console.WriteLine(); return; } if (args.Length == 2 && args[0] == "midi") { var composition = MidiFileReader.Read(args[1]); composition.PlayBack( note => { midiOut.Send(MidiMessage.StartNote(note, 100, 1).RawData); midiOut.Send(MidiMessage.StartNote(note + 4, 100, 1).RawData); }, note => midiOut.Send(MidiMessage.StopNote(note, 100, 1).RawData)); return; } if (args[0] == "RandomSequencer") { RandomSequencer(); return; } MethodInfo miStatic = typeof(Compositions).GetMethod(args[0]); Func <Melody12Tone> dgComposition = Delegate.CreateDelegate(typeof(Func <Melody12Tone>), miStatic) as Func <Melody12Tone>; Melody12Tone m12tone = dgComposition(); List <int> lastnotes = new List <int>(); foreach (var nwd in m12tone.Notes()) { foreach (int note in lastnotes) { midiOut.Send(MidiMessage.StopNote(note, 100, 1).RawData); } // Play the note, and if it's pause mute the last note if (!nwd.IsPause) { Note note = nwd; lastnotes = new List <int>(); do { midiOut.Send(MidiMessage.StartNote(note.note, 100, 1).RawData); lastnotes.Add(note.note); //note = note.otherNote; } while (false /*note != null*/); } else { foreach (var note in lastnotes) { midiOut.Send(MidiMessage.StartNote(note, 0, 1).RawData); } } Fraction fract = nwd.Duration; Console.Write(fract + " "); Thread.Sleep(60 * 1000 * fract.p / fract.q / m12tone.tempo); } // Mute the last note(s) foreach (var note in lastnotes) { midiOut.Send(MidiMessage.StartNote(note, 0, 1).RawData); } }