コード例 #1
0
ファイル: Program.cs プロジェクト: stefan-j/GeneticMIDI
        static void ANNFFNAccomp()
        {
            Databank db = new Databank("lib");
            var cat = db.Load("Classical");

            Composition inputComp = cat.Compositions[6];
            MelodySequence inputSeq = inputComp.Tracks[0].GetMainSequence() as MelodySequence;

            AccompanimentGeneratorANNFF gen = new AccompanimentGeneratorANNFF(cat, PatchNames.Acoustic_Grand);
            //gen.Initialize();
            gen.SetSequence(inputSeq);
            gen.Train();

            var outMel = gen.Generate();

            MusicPlayer player = new MusicPlayer();

            Composition comp = new Composition();
            Track t = new Track(PatchNames.Orchestral_Strings, 6);
            t.AddSequence(outMel);
            comp.Add(t);
            comp.Add(inputComp.Tracks[0]);
            player.Play(comp);
            comp.WriteToMidi("ann_ff_accomp.mid");
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: stefan-j/GeneticMIDI
        static void TrainANNFFAccomp()
        {
            Console.WriteLine("This application trains accompaniment neural networks");
            var cats = Databank.GetCategories();

            Databank db = new Databank("lib");

            var popularInstruments = new PatchNames[]{PatchNames.Acoustic_Grand,PatchNames.String_Ensemble_1,
                PatchNames.Acoustic_Bass,PatchNames.Trumpet,PatchNames.Vibraphone,PatchNames.Electric_Grand,
                PatchNames.French_Horn,PatchNames.Flute,PatchNames.Trombone,PatchNames.Music_Box};

            foreach (var catName in cats)
            {

                Console.WriteLine("Category {0}", catName);
                var cat = db.Load(catName);

                if (cat.Compositions.Length > 3000)
                    Console.WriteLine("Skipping category {0} due to length", cat.CategoryName);

                foreach (var instr in popularInstruments)
                {
                    Console.WriteLine("Training ANN for {0} - {1}, {2} epochs", cat.ToString(), instr.ToString(), 6000);

                    try
                    {
                        AccompanimentGeneratorANNFF ann = new AccompanimentGeneratorANNFF(cat, instr);
                        ann.Epochs = 5000;
                        ann.Train();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }

                }

            }

            Console.ReadLine();
        }
コード例 #3
0
        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();
            }
        }