private Melody[] GetMelody() { var durations = new[] { NoteDuration.Eighth, NoteDuration.Quarter, }; var generator = new MelodyGenerator(new RandomDurationGenerator(durations: durations)); var melody = generator.Generate(Gammas.CMaj5, 600); var bassDurations = new[] { NoteDuration.Half, NoteDuration.Half, NoteDuration.Quarter }; var bassGenerator = new MelodyGenerator(new RandomDurationGenerator(durations: bassDurations)); var bass = bassGenerator.GenerateMajChords(Gammas.CMaj3, 200); var drums = new MelodyGenerator(new RandomDurationGenerator(durations: new [] { NoteDuration.Whole })) .Generate(new [] { NoteKey.C2 }, 100); var synth = new MelodyGenerator(new RandomDurationGenerator(durations: new [] { NoteDuration.Eighth })) .Generate(Gammas.CMaj4, 800); return(new[] { melody, bass, synth }); }
private static void Main(string[] args) { var converter = new MidiConverter(); var midi = MidiFile.Read(@"Files\hey-jude.mid"); var input = converter.MidiToMelody(midi); var inputToSave = converter.MelodyToMidi(input); converter.SaveMidi(inputToSave, @"Files\input.mid"); var measureMutationOperator = new List <IMeasureMutationOperator> { new MeasureInversionOperator(), new MeasureReorganizationOperator(), new MeasureReplacementOperator(), new MeasureBrokerOperator(), new MeasureExchangeOperator(), }; var melodyMutationOperators = new List <IMelodyMutationOperator> { new MelodyExchangeOperator(), new MelodyInversionOperator(), }; var crossoverOperator = new RandomCutOffCrossoverOperator(measureMutationOperator, melodyMutationOperators); var initializer = new RandomInitializer(input); var selector = new TournamentSelector(); var replacementOperator = new HalfReplacementOperator(crossoverOperator, selector); var fitnessCalculator = new MelodySimilarityFitnessCalculator(); var stoppingCriterionChecker = new MultipleStoppingCriterionChecker(); var geneticAlgorithm = new MelodyGenerator(crossoverOperator, fitnessCalculator, initializer, measureMutationOperator, melodyMutationOperators, replacementOperator, selector, stoppingCriterionChecker); var geneticEventsManager = Singleton <GeneticEventsManager> .Instance(); geneticEventsManager.Set(input); // Test new List <Melody>() { input }.ForEach(currentMelody => fitnessCalculator.Calculate(input, currentMelody)); var output = geneticAlgorithm.Generate(); var outputToSave = converter.MelodyToMidi(output); converter.SaveMidi(outputToSave, @"Files\output.mid"); Console.ReadLine(); }
private void PlayNew() { PlayChord(); return; var durations = new[] { NoteDuration.Eighth, NoteDuration.Quarter, }; var generator = new MelodyGenerator(new RandomDurationGenerator(durations: durations)); var melody = generator.Generate(Gammas.CMaj5, 80); var bassDurations = new[] { NoteDuration.Half, NoteDuration.Half, NoteDuration.Quarter }; var bassGenerator = new MelodyGenerator(new RandomDurationGenerator(durations: bassDurations)); var bass = bassGenerator.GenerateMajChords(Gammas.CMaj3, 40); //var drums = new MelodyGenerator(new RandomDurationGenerator(durations: new [] {NoteDuration.Whole})) // .Generate(new [] {NoteKey.C2}, 40); using var memoryStream = File.OpenWrite("temp.wav"); using var binaryWriter = new BinaryWriter(memoryStream); var recorder = new WaveRecorder(); //var wave = recorder.Record(binaryWriter, new [] {melody, bass}); //new WaveVisualiser().Visualize(wave, 800, 600).Save("temp.png", ImageFormat.Png); //memoryStream.Position = 0; //new SoundPlayer(memoryStream).Play(); }