public MelodySequence Generate(MelodySequence seq, int seed) { var targetSeq = NormalizeSequence(seq); MelodySequence outputSequence = new MelodySequence(); var targetSequences = targetSeq.Split(SYNC_AFTER_NOTES); int mainDuration = 0; int accompDuration = 0; foreach(var mel in targetSequences) { var outputMel = table.Chain(mel.ToArray(), seed); MelodySequence outputSeq = new MelodySequence(outputMel); while(outputSeq.Duration > mel.Duration) { outputSeq.RemoveLastNote(); } // outputSeq.RemoveLastNote(); outputSeq.AddPause(mel.Duration - outputSeq.Duration); // outputSeq.RemoveLastNote(); if (mainDuration > accompDuration) outputSeq.AddPause(mainDuration - accompDuration); outputSequence.AddMelodySequence(outputSeq); mainDuration += mel.Duration; accompDuration += outputSeq.Duration; } return outputSequence; }
static void Test8() { MusicPlayer player = new MusicPlayer(); Console.WriteLine("Hidden Markov Model"); var m1 = GetMelodySequence(@"test\other\frere.mid"); var m2 = GetMelodySequence(@"test\other\babaa.mid"); var m3 = GetMelodySequence(@"test\other\twinkle.mid"); var m4 = GetMelodySequence(@"test\hagrid.mid"); var m5 = GetMelodySequence(@"test\harry.mid"); var m6 = GetMelodySequence(@"test\harry2.mid"); MarkovGenerator markov = new MarkovGenerator(new MelodySequence[] { m6 }); while (true) { Composition comp = new Composition(); GeneticGenerator gen = new GeneticGenerator(new MetricSimilarity(m6, new IMetric[] { new Rhythm(), new RhythmicBigram(), new RhythmicInterval() })); var notes2 = gen.Generate(); Track t2 = new Track((PatchNames)0, 10); MelodySequence s = new MelodySequence(); s.AddPause((int)Durations.wn * 10); foreach (Note n in notes2.Notes) { if (n.Pitch <= 0) n.Pitch = 0; else if (n.Pitch > 48) n.Pitch = 40; else if (n.Pitch > 70) n.Pitch = 35; else { n.Pitch = 49; n.Duration *= 4; n.Velocity = 50; s.AddPause(n.Duration * 2); } s.AddNote(n); } t2.AddSequence(s); var notes3 = markov.Generate().Notes as Note[]; MelodySequence baseseq = new MelodySequence(); int max_dur = 0; int max_index = 0; for (int i = (int)(notes3.Length * 0.7f); i < notes3.Length; i++) { if (notes3[i].Duration > max_dur) { max_dur = notes3[i].Duration; max_index = i; } } Note last_note = null; for (int i = 0; i < max_index; i++) { last_note = notes3[i]; baseseq.AddNote(last_note); } baseseq.AddNote(new Note(last_note.Pitch - 12, last_note.Duration)); baseseq.AddNote(new Note(last_note.Pitch - 24, last_note.Duration * 2)); baseseq.AddPause(last_note.Duration * 32); Track t = new Track(PatchNames.Vibraphone, 1); var b1 = baseseq.Clone() as MelodySequence; var b2 = baseseq.Clone() as MelodySequence; var b3 = baseseq.Clone() as MelodySequence; b1.Transpose(12); b2.Transpose(6); b3.Transpose(-12); t.AddSequence(baseseq); t.AddSequence(b1); t.AddSequence(b2); t.AddSequence(b3); comp.Add(t); comp.Add(t2); Console.WriteLine("Press enter key to listen"); Console.ReadLine(); player.Play(comp); } }
public HarmonySequence GetHarmonySequence(MelodySequence seq) { HarmonySequence sequence = new HarmonySequence(); foreach(Note n in seq.ToArray()) { if (n.Pitch > 0 && n.Velocity > 0 && n.Duration > (int)Durations.en) sequence.AddChord(GetChord(n)); else seq.AddPause(n.Duration); } return sequence; }