Beispiel #1
0
        /// <summary>
        /// Loads compositions from directories and saves as file
        /// </summary>
        /// <param name="libraryDir"></param>
        /// <param name="saveDir"></param>
        /// <returns></returns>
        public static Databank GenerateCategories(string libraryDir, string saveDir)
        {
            Databank db = new Databank();

            db.categories = new Dictionary<string, CompositionCategory>();

            Console.WriteLine("Generating categories");

            GenerateDefaultPaths("");
            foreach (var key in defaultPaths.Keys)
            {
                CompositionCategory cat = new CompositionCategory(key, libraryDir + defaultPaths[key]);
                Console.WriteLine("Generating compositions for category {0}", key);
                cat.LoadCompositions(true);
                db.categories[key] = cat;

                Console.WriteLine("Saving compositions for category {0}", key);
                db.categories[key].Save(saveDir + defaultPaths[key]);

                db.Unload(key); // Free from memory
            }

            GenerateDefaultPaths(libraryDir);

            return db;
        }
        public SamplingWithReplacement(CompositionCategory cat, PatchNames instrument)
        {
            this.MaxIterations = 200;
            this.MaxLength = 100;
            this.instrument = instrument;
            this.category = cat;
            random = new SimpleRNG();
            random.SetSeedFromSystemTime();
            model = new MarkovChain<Note>(1);
            foreach(var comp in cat.Compositions)
            {
                foreach(var track in comp.Tracks)
                {
                    if (!IsValidTrack(track))
                        continue;

                    var mel = (track.GetMainSequence() as MelodySequence).Clone() as MelodySequence;
                    mel.StandardizeDuration();
                    model.Add(mel.ToArray());

                    break;//only first track
                }
            }

            CalculateStats();
        }
Beispiel #3
0
        public static NCD FromMelodies(CompositionCategory category)
        {
            NCD ncd = new NCD();

            string savepath = GetSavePath(category);
            if (File.Exists(savepath))
            {
                //ncd.Deserialize(savepath);
               // return ncd;
            }

            MelodySequence[] seqs = new MelodySequence[category.Compositions.Length];
            for (int i = 0; i < category.Compositions.Length; i++)
                seqs[i] = category.Compositions[i].GetLongestTrack().GetMainSequence() as MelodySequence;

            ncd.songs = new string[seqs.Count()];

            int j = 0;
            foreach (MelodySequence m in seqs)
            {
                ncd.songs[j++] = m.ToString();
            }

            string root = System.IO.Path.GetDirectoryName(savepath);
            if (!System.IO.Directory.Exists(root))
                System.IO.Directory.CreateDirectory(root);
            ncd.Serialize(savepath);

            return ncd;
        }
        public static CompositionCategory LoadFromFile(string fullPath)
        {
            CompositionCategory cat = new CompositionCategory();

            ProtoBuf.Serializer.PrepareSerializer<CompositionCategory>();

            System.IO.FileStream fs = new System.IO.FileStream(fullPath, System.IO.FileMode.Open);
            System.IO.Compression.GZipStream gz = new System.IO.Compression.GZipStream(fs, System.IO.Compression.CompressionMode.Decompress);

               // System.IO.MemoryStream memStream = new System.IO.MemoryStream();
               // gz.CopyTo(memStream);

            cat = ProtoBuf.Serializer.Deserialize<CompositionCategory>(gz);

            fs.Close();

            return cat;
        }
        public GeneticGenerator(IFitnessFunction fitnessFunction, PatchNames instrument = PatchNames.Acoustic_Grand, CompositionCategory cat=null)
        {
            this.fitnessFunction = fitnessFunction;
            this.cat = cat;
            this.MaxGenerations = 1000;
            this.PrintProgress = true;
            this.instrument = instrument;

            if (cat != null)
            {
                // Markov generator
                var mark = new MarkovChainGenerator(instrument, 2);

                // Allowed notes
                HashSet<Durations> durs = new HashSet<Durations>();
                HashSet<int> fullPitches = new HashSet<int>();

                foreach(var c in cat.Compositions)
                {
                    if (c.Tracks.Count < 1)
                        continue;
                    var cloneMel = (c.Tracks[0].GetMainSequence() as MelodySequence).Clone() as MelodySequence;

                    cloneMel.StandardizeDuration();
                    mark.AddMelody(cloneMel);

                    foreach (var n in cloneMel.Notes)
                    {
                        durs.Add(NoteGene.GetClosestDuration(n.Duration));
                        fullPitches.Add(n.Pitch);
                    }

                    avgLength += cloneMel.Length;
                }
                avgLength /= cat.Compositions.Length;
                GPCustomTree.generator = mark;

                NoteGene.AllowedDurations = durs.ToArray();
                NoteGene.AllowedFullPitches = fullPitches.ToArray();

            }
        }
Beispiel #6
0
        public static NCD FromCompositions(CompositionCategory category)
        {
            NCD ncd = new NCD();

            string savepath = GetSavePath(category);
            if (File.Exists(savepath))
            {
                ncd.Deserialize(savepath);
                return ncd;
            }

            Composition[] seqs = category.Compositions;

            ncd.songs = new string[seqs.Count()];

            int i = 0;
            int j = 0;
            foreach (var comp in seqs)
            {
                Console.WriteLine("NCD: {0}", j++ / seqs.Length);
                var c = comp;
                if (MaxTracks > 0)
                {
                    var newComp = new Composition();
                    for(int k = 0; k < MaxTracks && k < comp.Tracks.Count; k++)
                    {
                        newComp.Add(comp.Tracks[k]);
                    }
                    c = newComp;
                }

                ncd.songs[i++] = c.ToString();
            }

            string root = System.IO.Path.GetDirectoryName(savepath);
            if (!System.IO.Directory.Exists(root))
                System.IO.Directory.CreateDirectory(root);
            ncd.Serialize(savepath);

            return ncd;
        }
 public AccompanimentGeneratorANNFF(CompositionCategory category, PatchNames instrument)
 {
     this.Epochs = 50000;
     this.category = category;
     this.instrument = instrument;
 }
 public AccompanimentGenerator(CompositionCategory category, PatchNames instrument)
 {
     this.category = category;
     this.instrument = instrument;
 }
        public TrackGenerator(CompositionCategory category, Composition comp)
        {
            this.category = category;
            InitializeComponent();

            // Load data for accompany instruments
            string path = "save/" + category.CategoryName;
            if(System.IO.Directory.Exists(path))
            {
                var files = System.IO.Directory.GetFiles(path);
                foreach(var file in files)
                {
                    try
                    {
                        string filename = System.IO.Path.GetFileNameWithoutExtension(file);
                        if (!filename.ToLower().Contains("accomp_ann_ff_"))
                            continue;
                        var sub = filename.Substring(14);

                        PatchNames instrument = (PatchNames)(int.Parse(sub));

                        if(!accompInstruBox.Items.Contains(instrument))
                            accompInstruBox.Items.Add(instrument);
                    }
                    catch(Exception E)
                    {
                        Console.WriteLine(E);
                    }
                }
            }

            // Load data for accompany tracks
            if(comp != null)
                for(int i = 0; i < comp.Tracks.Count; i++)
                {
                    var track = comp.Tracks[i];

                    ComboBoxItem item = new ComboBoxItem();
                    item.Content = string.Format("Track {0} - {1}", i.ToString(), track.Instrument.ToString());
                    item.Tag = track;

                    accompTrackBox.Items.Add(item);
                }

            randomScale.Items.Clear();
            foreach(var s in Scales.ScaleTypes)
            {
                //ListBoxItem item = new ListBoxItem();
                randomScale.Items.Add(s);
            }

            randomInstrument.Items.Clear();
            var popularInstruments = new PatchNames[]{PatchNames.Acoustic_Grand,PatchNames.String_Ensemble_1,
                PatchNames.Acoustic_Bass,PatchNames.Trumpet,PatchNames.Violin,PatchNames.Electric_Grand,
                PatchNames.French_Horn,PatchNames.Flute,PatchNames.Trombone,PatchNames.Acoustic_Guitarnylon, PatchNames.Orchestral_Strings};
            foreach (var d in popularInstruments)
            {
                loadInstrument.Items.Add(d);
                geneticInstrumentBox.Items.Add(d);
                randomInstrument.Items.Add(d);
            }

            StopSpinner();

            LoadInstrumentalData();
        }
 public AccompanyGeneratorMarkov(CompositionCategory cat, PatchNames instrument)
 {
     this.instrument = instrument;
     Add(cat);
 }
        void Add(CompositionCategory category)
        {
            table = new MarkovTable<Note>(ORDER);
            foreach (var c in category.Compositions)
            {
                if (c.Tracks.Count < 2)
                    continue;
                var mainSeq = NormalizeSequence((c.Tracks[0].GetMainSequence() as MelodySequence));

                for (int i = 1; i < c.Tracks.Count; i++)
                {
                    var accompSeq = NormalizeSequence(c.Tracks[i].GetMainSequence() as MelodySequence);
                    if (accompSeq.TotalRestDuration() > accompSeq.TotalNoteDuration())
                        continue;
                    Console.WriteLine("Adding comp {0}:{1}", c.NameTag,i);
                    table.Add(mainSeq.ToArray(), accompSeq.ToArray());
                   // Console.WriteLine("Adding track");
                }

            }
        }
Beispiel #12
0
        public void Train(CompositionCategory cat)
        {
            Accord.Math.Tools.SetupGenerator(42);
            List<int[]> inputSequences = new List<int[]>();
            List<int[]> outputSequences = new List<int[]>();
            foreach(Composition comp in cat.Compositions)
            {
                if (comp.Tracks.Count < 2)
                    continue;

                var melInput = comp.Tracks[0].GetMainSequence() as MelodySequence; melInput.Trim(100); melInput.NormalizeNotes(4);
                var melOutput = comp.Tracks[1].GetMainSequence() as MelodySequence; melOutput.Trim(100); melOutput.NormalizeNotes(4);
                if (melInput.Length > melOutput.Length)
                    melInput.Trim(melOutput.Length);
                else if (melOutput.Length > melInput.Length)
                    melOutput.Trim(melInput.Length);

                book.Add(melInput.Notes); book.Add(melOutput.Notes);
                inputSequences.Add(book.ToCodes(melInput.ToArray()));
                outputSequences.Add(book.ToCodes(melOutput.ToArray()));
            }

            if (outputSequences.Count != inputSequences.Count)
                throw new Exception("MSP");
            for(int i = 0; i < outputSequences.Count; i++)
            {
                if (outputSequences[i].Length != inputSequences[i].Length)
                    throw new Exception("MSP 2");
            }

            var topology = new Forward(states: 50);

            hmm = new HiddenMarkovModel(20, book.TotalUniqueSymbols + 1);
            var teacher = new Accord.Statistics.Models.Markov.Learning.MaximumLikelihoodLearning(hmm) {UseLaplaceRule=false /*Tolerance = 0.1, Iterations=0*/};
            //var teacher = new ViterbiLearning(hmm);

                double ll = teacher.Run(outputSequences.ToArray(), inputSequences.ToArray());
                Console.WriteLine("Error: {0}", ll);
        }
 public InstrumentalGenerator(CompositionCategory category)
 {
     this.category = category;
     this.MaxNotes = 100;
 }
 public void AssignCategory(CompositionCategory category)
 {
     this.category = category;
 }
Beispiel #15
0
 static string GetSavePath(CompositionCategory category)
 {
     return "save/" + category.CategoryName + "/ncdm.dat";
 }