Inheritance: GeneticMIDI.Fractal.BrownNoteGenerator, INoteGenerator
示例#1
0
        static void BrownTest()
        {
            var gen = new ReflectingBrownNoteGenerator(new NoteRangeRestrictor(48, 72, 2, 16, Scales.ScaleTypes[1]), new Random(), -1, 1, -1, 1);

            MelodySequence seq = gen.Generate();
            Console.WriteLine(seq.ToString());
            MusicPlayer player = new MusicPlayer();
            player.Play(seq);

            Console.ReadLine();
        }
        private void Generate()
        {
            int index = optionsTab.SelectedIndex;

            progressGenSlider.Value = 0;

            if (index == 0)
            {
                if (geneticInstrumentBox.SelectedItem == null)
                    return;

                fitnessPlot.ResetAllAxes();
                SetupLinePlot(fitnessPlot, "Average Fitness");
                (fitnessPlot.Model.Series[0] as LineSeries).Points.Clear();
                fitnessPlot.Model.Series[0].Unselect();
                fitnessPlot.InvalidatePlot();

                //GA
                IFitnessFunction fitness = null;

                //Options
                List<IMetric> activeMetrics = new List<IMetric>();
              //  if (metricChromaticTone.IsChecked == true)
              //      activeMetrics.Add(new ChromaticTone());
                if (metricChromaticToneDistance.IsChecked == true)
                    activeMetrics.Add(new ChromaticToneDistance());
                if (metricChromaticToneDuration.IsChecked == true)
                    activeMetrics.Add(new ChromaticToneDuration());
                if (metricMelodicBigram.IsChecked == true)
                    activeMetrics.Add(new MelodicBigram());
                if (metricMelodicInterval.IsChecked == true)
                    activeMetrics.Add(new MelodicInterval());
             //   if (metricPitch.IsChecked == true)
             //       activeMetrics.Add(new Pitch());
            //    if (metricPitchDistance.IsChecked == true)
             //       activeMetrics.Add(new PitchDistance());
            //    if (metricRhythm.IsChecked == true)
            //        activeMetrics.Add(new Rhythm());
                if (metricRhythmicBigram.IsChecked == true)
                    activeMetrics.Add(new RhythmicBigram());
                if (metricRhythmicInterval.IsChecked == true)
                    activeMetrics.Add(new RhythmicInterval());

                fitness = GeneticMIDI.FitnessFunctions.MetricSimilarity.GenerateMetricSimilarityMulti(category.Compositions, activeMetrics.ToArray(), GeneticMIDI.FitnessFunctions.SimilarityType.Cosine);
                /*
                if (fitnessFuncCombo.SelectedIndex == 0)
                    fitness = new GeneticMIDI.FitnessFunctions.MetricSimilarity(seq, activeMetrics.ToArray(), GeneticMIDI.FitnessFunctions.SimilarityType.Cosine);
                if (fitnessFuncCombo.SelectedIndex == 1)
                    fitness = new GeneticMIDI.FitnessFunctions.MetricSimilarity(seq, activeMetrics.ToArray(), GeneticMIDI.FitnessFunctions.SimilarityType.Euclidian);
                if (fitnessFuncCombo.SelectedIndex == 2)
                    fitness = new GeneticMIDI.FitnessFunctions.MetricSimilarity(seq, activeMetrics.ToArray(), GeneticMIDI.FitnessFunctions.SimilarityType.Pearson);
                if (fitnessFuncCombo.SelectedIndex == 3)
                    fitness = new GeneticMIDI.FitnessFunctions.CrossCorrelation(seq);
                if (fitnessFuncCombo.SelectedIndex == 4)
                    fitness = GeneticMIDI.FitnessFunctions.NCD.FromMelodies(category);*/

                Instrument = (PatchNames)geneticInstrumentBox.SelectedItem;

                var gen = new GeneticGenerator(fitness, Instrument, category);
                gen.OnPercentage += gen_OnPercentage;

                gen.MaxGenerations = (int)maxGenerationSlider.Value;
                Generator = gen;

                new Thread(() =>
                {
                    var notes = gen.Generate();

                    var mel = notes;
                    GeneratedSequence = mel;
                    progressGenSlider.Dispatcher.Invoke(() =>
                    {
                        progressGenSlider.Value = 100;
                    });
                }).Start();
            }
            if (index == 1)
            {
                if (instrBox.SelectedItem as ListBoxItem == null || (instrBox.SelectedItem as ListBoxItem).Tag == null)
                    return;
                if ((int)((instrBox.SelectedItem as ListBoxItem).Tag) == -1)
                {
                    // Drum Generator
                    DrumGenerator gen = new DrumGenerator();

                    Generator = gen;

                    Instrument = PatchNames.Helicopter;

                    new Thread(() =>
                    {
                            StartSpinner();
                            gen.Initialize(new Databank(GeneticMIDI.Constants.LOCAL_LIBRARY_PATH));
                            GeneratedSequence = gen.Generate();
                            StopSpinner();

                        progressGenSlider.Dispatcher.Invoke(() =>
                        {
                            progressGenSlider.Value = 100;
                        });

                    }).Start();

                }
                else
                {
                    PatchNames instrument = (PatchNames)((instrBox.SelectedItem as ListBoxItem).Tag);
                    Instrument = instrument;

                    InstrumentalGenerator gen = Generator as InstrumentalGenerator;
                    if (gen == null)
                        return;

                    gen.SetInstrument(Instrument);

                    new Thread(() =>
                    {
                        if (gen.IsInitialized)
                        {
                            StartSpinner();
                            GeneratedSequence = gen.GenerateInstrument(instrument);
                            StopSpinner();
                        }

                        progressGenSlider.Dispatcher.Invoke(() =>
                        {
                            progressGenSlider.Value = 100;
                        });

                    }).Start();
                }
            }
            if(index == 2)
            {
                if (accompInstruBox.Items.Count == 0 || accompTrackBox.Items.Count == 0)
                    return;
                Instrument = (PatchNames)(accompInstruBox.SelectedItem);
                Track track = (accompTrackBox.SelectedItem as ListBoxItem).Tag as Track;

                var melSeq = track.GetMainSequence() as MelodySequence;
                Random rnd = new Random();
                if(accompMethoBox.SelectedIndex == 0)
                {
                    AccompanyGeneratorMarkov gen = new AccompanyGeneratorMarkov(category, Instrument);
                    Generator = gen;
                    new Thread(() =>
                        {

                            StartSpinner();
                            GeneratedSequence = gen.Generate(melSeq,rnd.Next());
                            StopSpinner();
                            progressGenSlider.Dispatcher.Invoke(() =>
                            {
                                progressGenSlider.Value = 100;
                            });
                        }).Start();
                }
                else if (accompMethoBox.SelectedIndex == 1)
                {
                    AccompanimentGeneratorANNFF gen = new AccompanimentGeneratorANNFF(category, Instrument);
                    Generator = gen;
                    gen.SetSequence(melSeq);
                    new Thread(() =>
                        {

                            StartSpinner();
                            gen.Load();
                            GeneratedSequence = gen.Generate();

                            StopSpinner();
                            progressGenSlider.Dispatcher.Invoke(() =>
                                {
                                    progressGenSlider.Value = 100;
                                });

                        }).Start();
                }

            }
            if(index == 3)
            {
                //stochasticLogPlot.Model.Series.Clear();
                stochasticLogPlot.ResetAllAxes();
                SetupLinePlot(stochasticLogPlot, "Log Likelihood");
                (stochasticLogPlot.Model.Series[0] as LineSeries).Points.Clear();
                stochasticLogPlot.Model.Series[0].Unselect();
                stochasticLogPlot.InvalidatePlot();

                if (loadInstrument.SelectedItem == null)
                    return;
                Instrument = (PatchNames)(loadInstrument.SelectedItem);

                SamplingWithReplacement swr = new SamplingWithReplacement(category, Instrument);
                swr.OnProgressChange += swr_OnProgressChange;
                swr.MaxIterations = (int)loadGenerationsSlider.Value;

                Generator = swr;

                new Thread(() =>
                {
                    StartSpinner();
                    GeneratedSequence = swr.Generate();
                    StopSpinner();
                    progressGenSlider.Dispatcher.Invoke(() =>
                    {
                        progressGenSlider.Maximum = 100;
                        progressGenSlider.Value = 100;
                    });

                }).Start();

            }
            if(index == 4)
            {
                if (randomInstrument.SelectedItem == null)
                    return;
                Instrument = (PatchNames)randomInstrument.SelectedItem;

                int centralNotePitch = (int)randomOctave.Value * 12;
                int noteShift = (int)randomPitchVar.Value;
                int minNote = centralNotePitch - noteShift;
                int maxNote = centralNotePitch + noteShift;
                if (minNote <= 0)
                    minNote = 1;
                if (maxNote >= 127)
                    maxNote = 126;
                int durMin = (int)Math.Pow(randomDurationRange.LowerValue, 2);
                int durMax = (int)Math.Pow(randomDurationRange.UpperValue, 2);
                int length = (int)randomLength.Value;

                ScaleType scale = randomScale.SelectedItem as ScaleType;

                var gen = new ReflectingBrownNoteGenerator(new NoteRangeRestrictor(minNote, maxNote, durMin, durMax, scale), new Random(), -2, 2, -1, 1, Instrument);
                Generator = gen;
                gen.MaxNotes = length;

                new Thread(() =>
                {
                    StartSpinner();
                    GeneratedSequence = gen.Generate();
                    StopSpinner();
                    progressGenSlider.Dispatcher.Invoke(() =>
                    {
                        progressGenSlider.Value = 100;
                    });

                }).Start();

                Console.ReadLine();
            }
        }