static void Main(string[] args) { Stopwatch stopwatch = new Stopwatch(); Console.Write("Enter seed: "); string seed = Console.ReadLine(); Console.WriteLine("Generating Midi..."); stopwatch.Restart(); // Generate a MIDI using Abundant Music using (Composition composition = MidiComposer.Compose(seed)) { stopwatch.Stop(); Console.WriteLine("Seed: " + composition.Seed); Console.WriteLine("Generated in: " + stopwatch.Elapsed); // Play the MIDI using managed-midi // https://github.com/atsushieno/managed-midi var access = MidiAccessManager.Default; var music = MidiMusic.Read(composition.Midi); using (var player = new MidiPlayer(music, access)) { long totalPlayTime = player.GetTotalPlayTimeMilliseconds(); Console.WriteLine("Play time: " + TimeSpan.FromMilliseconds(totalPlayTime).ToString("g")); player.Play(); while (player.State == PlayerState.Playing) { } } } }
public void NextGenClick(object sender, RoutedEventArgs routedEventArgs) { Individu[] newPop = new Individu[Population.MAXINDIVIDUS]; for (int i = 0; i < Population.MAXINDIVIDUS; i++) { int rnd = MidiComposer.GetRandom(0, 100); Individu newInd; if (rnd < Population.CROSSOVER) { Individu parent1 = SelectParent(); Individu parent2 = SelectParent(); newInd = new Individu(parent1, parent2); } else { Individu parent = SelectParent(); newInd = new Individu(parent); } newInd.Mutate(); newPop[i] = newInd; } Generation mg = new Generation(newPop); Survival(); Gens.Add(mg); //populations[nbPopulation] = new Population(nbPopulation + 1, newPop); }
public void Mutate() { for (int i = 0; i < NBNOTES; i++) { int rnd = MidiComposer.GetRandom(0, 101); if (rnd < Population.MUTARATE) { _notes[i] = MidiComposer.GetRandom(24, 96); } } }
public Individu() { // c. Ajouter des notes // Chaque note est comprise entre 0 et 127 (12 correspond au type de note, fixe ici à des 1/4) // L'équivalence avec les notes / octaves est disponible ici : https://andymurkin.files.wordpress.com/2012/01/midi-int-midi-note-no-chart.jpg // Ici 16 notes aléatoire entre 16 et 96 (pour éviter certaines notes trop aigues ou trop graves) for (int i = 0; i < NBNOTES; i++) { _notes[i] = MidiComposer.GetRandom(24, 96); } init(); }
public void Play() { MidiComposer mc = new MidiComposer(); if (!File.Exists(_midiFileName)) { mc.CreateAndPlayMusic(_notes, _midiFileName, true); } else { mc.PlayMIDI(_midiFileName); } }
public Individu(Individu papa, Individu maman) { int rnd = MidiComposer.GetRandom(0, NBNOTES); for (int i = 0; i < NBNOTES; i++) { if (i < rnd) { _notes[i] = papa._notes[i]; } else { _notes[i] = maman._notes[i]; } } init(); }
/// <summary> /// Compare le fitness de 2 individus aleatoires. /// </summary> /// <returns>L'individu ayant le fitness le plus élevé</returns> private Individu SelectParent() { int rnd1 = MidiComposer.GetRandom(0, Population.MAXINDIVIDUS); int rnd2 = MidiComposer.GetRandom(0, Population.MAXINDIVIDUS); Generation g = Gens.Last(); Individu i1 = g.Individus[rnd1]; Individu i2 = g.Individus[rnd2]; if (i1.Fitness > i2.Fitness) { return(i1); } else { return(i2); } }
private static void Save(Individu selected) { FolderBrowserDialog dialog = new FolderBrowserDialog(); DialogResult result = dialog.ShowDialog(); string path = dialog.SelectedPath; string filename = selected.MidiFileName; try { MidiComposer mc = new MidiComposer(); if (!File.Exists(filename)) { mc.CreateAndPlayMusic(selected.Notes, selected.MidiFileName, false); } File.Copy(filename, path + "\\" + filename); //SaveButton.IsEnabled = true; } catch (Exception exception) { Debug.WriteLine("Erreur: " + exception.Message); } }