Ejemplo n.º 1
0
        public MidiBuilder(Section section)
        {
            var bpm = section.Bpm;

            if (bpm == 0M)
            {
                bpm = 120M;
            }

            _trackChunks = new List <TrackChunk>();

            foreach (var phrase in section.Phrases)
            {
                var trackChunk = new TrackChunk();
                AddBpmEvent(trackChunk, bpm);
                AddTimeSignatureEvent(trackChunk);
                AddNameEvent(trackChunk, phrase.Description);
                SetInstrument(trackChunk, phrase);
                SetPan(trackChunk, phrase);

                _trackChunks.Add(trackChunk);
            }

            foreach (var sourcePhrase in section.Phrases)
            {
                var index  = section.Phrases.IndexOf(sourcePhrase);
                var phrase = sourcePhrase.Clone();

                PhraseHelper.UnmergeRepeatedNotes(phrase);
                PhraseHelper.UnmergeChords(phrase);

                using (var notesManager = _trackChunks[index].ManageNotes())
                {
                    var notes = phrase.Elements
                                .Select(x => new Note
                                        (
                                            GetMidiNoteNumber(x.Note),
                                            GetMidiNoteLength(x.Duration),
                                            GetMidiNoteLength(x.Position)
                                        )
                    {
                        Velocity = (SevenBitNumber)x.Velocity
                    }).ToList();

                    notesManager.Notes.Add(notes);
                }

                var notesOn = _trackChunks[index].Events.OfType <NoteOnEvent>().ToList();
                foreach (var noteOn in notesOn)
                {
                    noteOn.Channel = GetChannel(index, sourcePhrase);
                }

                var notesOff = _trackChunks[index].Events.OfType <NoteOffEvent>().ToList();
                foreach (var noteOff in notesOff)
                {
                    noteOff.Channel = GetChannel(index, sourcePhrase);
                }
            }
        }
Ejemplo n.º 2
0
        public static string GenerateTab(Phrase phrase, string tuning = "E,B,G,D,A,E", bool oneLineIfPossible = false)
        {
            phrase = phrase.Clone();
            while (PhraseHelper.IsPhraseDuplicated(phrase))
            {
                PhraseHelper.TrimPhrase(phrase, phrase.PhraseLength / 2);
            }

            var tabParser = new TabParser(tuning);

            while (phrase.Elements.Min(x => x.Note) > tabParser.TabLines.Last().Number)
            {
                NoteHelper.ShiftNotesDirect(phrase, 1, Interval.Octave, Direction.Down);
            }

            while (phrase.Elements.Min(x => x.Note) < tabParser.TabLines.Last().Number)
            {
                NoteHelper.ShiftNotesDirect(phrase, 1, Interval.Octave);
            }



            tabParser.LoadTabFromPhrase(phrase, oneLineIfPossible);


            return(BuildTab(tabParser));
        }
Ejemplo n.º 3
0
 void Awake()
 {
     PhraseHelper.Init();
     ReproductionHelper.Init();
     MutationHelper.Init();
     UIManager.Init();
     PopulationManager.Init();
 }
Ejemplo n.º 4
0
    private static void MutateIndividual(Individual individual)
    {
        string currentPhrase = individual.dna.phrase;
        string newPhrase     = "";

        for (int i = 0; i < currentPhrase.Length; i++)
        {
            newPhrase += (rand.Next(0, 10000) < Config.mutationRate * 100) ? PhraseHelper.GetRandomLetter() : currentPhrase[i];
        }

        individual.dna.phrase = newPhrase;
    }
Ejemplo n.º 5
0
        private Phrase GenratePhraseBasic(int noteCount)
        {
            var phrase = new Phrase();

            var selectedNotes =
                (from onoffProbability in _probabilities
                 let noteOn = GetRandomBool(onoffProbability.OnOffChance)
                              where noteOn
                              select onoffProbability).ToList();

            while (selectedNotes.Count > noteCount)
            {
                var leastPopularNote = selectedNotes.OrderBy(x => x.OnOffChance).FirstOrDefault();
                selectedNotes.Remove(leastPopularNote);
            }
            while (selectedNotes.Count < noteCount)
            {
                var mostPopularNote = _probabilities.Except(selectedNotes)
                                      .OrderByDescending(x => x.OnOffChance)
                                      .FirstOrDefault();
                selectedNotes.Add(mostPopularNote);
            }

            selectedNotes = selectedNotes.Where(x => x != null).ToList();

            selectedNotes = selectedNotes.OrderBy(x => x.Position).ToList();

            foreach (var note in selectedNotes)
            {
                var randomNote = GetRandomNote(note.Notes);

                phrase.Elements.Add(new PhraseElement
                {
                    Position = note.Position,
                    Duration = 1,
                    Note     = randomNote
                });
            }
            phrase.Elements.RemoveAll(x => x.Position >= _riffLength);

            PhraseHelper.UpdateDurationsFromPositions(phrase, _riffLength);
            return(phrase);
        }
Ejemplo n.º 6
0
        public static Phrase ParseTab(string tabText)
        {
            var tab = new TabParser();

            tab.LoadTabFromTabText(tabText);

            var phrase = new Phrase
            {
                Elements = tab
                           .TabNotes
                           .Select(x => new PhraseElement {
                    Note = x.Number, Duration = x.Length
                })
                           .ToList()
            };

            PhraseHelper.UpdatePositionsFromDurations(phrase);

            return(phrase);
        }
Ejemplo n.º 7
0
        private void BindDetailView(Meal meal)
        {
            MyMealLikesNumber.Text = "--";

            if (meal.GradesCount > 0)
            {
                MyMealLikesNumber.Text        = PhraseHelper.GetGradePhrase(Convert.ToInt32(meal.Grade));
                MyMealsGradesImage.Visibility = ViewStates.Visible;
            }

            MyMealDetailPostedOn.Text    = meal.CreatedOn.ToShortDateString();
            MyMealDetailDescription.Text = meal.Description;

            FetchMealRatingByUser();

            if (!String.IsNullOrEmpty(meal.ImageUrl))
            {
                RenderImage(meal.ImageUrl);
            }
        }
Ejemplo n.º 8
0
        private async void FetchMealRatingByUser()
        {
            try
            {
                MealRating mealRating = await service.GetMealRatingByUser(UserCache.user.Id, MealId);

                if (mealRating != null && mealRating.RatingId > 0)
                {
                    //This user has already submitted a rating or a grade.
                    MyMealsDetailGradeUserSubmittedValue.Text      = PhraseHelper.GetGradePhrase(Convert.ToInt32(mealRating.Grade));
                    MyMealsDetailsSubmittedGradeSection.Visibility = ViewStates.Visible;
                    MyMealsDetailsGradingSection.Visibility        = ViewStates.Gone;
                }
                else
                {
                    MyMealsDetailsSubmittedGradeSection.Visibility = ViewStates.Gone;
                    MyMealsDetailsGradingSection.Visibility        = ViewStates.Visible;
                }
            }
            catch (Exception ex)
            {
                string err = ex.Message;
            }
        }
Ejemplo n.º 9
0
 public DNA()
 {
     phrase = PhraseHelper.GetRandomPhrase(Config.nLettersPerPhrase);
 }
Ejemplo n.º 10
0
        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            var item = items[position];

            if (convertView == null)
            {
                convertView = context.LayoutInflater.Inflate(Resource.Layout.GradesRow, null);
            }

            convertView.FindViewById <TextView>(Resource.Id.GradesRowPostedBy).Text = String.Format("{0} ", item.SubmittedByName);
            convertView.FindViewById <TextView>(Resource.Id.GradesRowPostedOn).Text = item.SubmittedOn.ToShortDateString();
            convertView.FindViewById <TextView>(Resource.Id.GradesRowGrade).Text    = PhraseHelper.GetGradePhrase(Convert.ToInt32(item.Grade));

            //ImageView imgView = convertView.FindViewById<ImageView>(Resource.Id.GradesListImageView);

            //Set the image of the view according to the grade the user gave.

            return(convertView);
        }
Ejemplo n.º 11
0
        private void LoadTrainingData(List <Phrase> sourceRiffs = null)
        {
            if (sourceRiffs == null)
            {
                _riffs = Directory.GetFiles(_rootFolder, "*.mid", SearchOption.AllDirectories)
                         .Select(x => MidiHelper.ReadMidi(x).Phrases[0])
                         .ToList();
            }
            else
            {
                _riffs = sourceRiffs.ToList();
            }



            var largeRiffs = _riffs.Where(riff => riff.PhraseLength > _riffLength).ToList();

            foreach (var largeRiff in largeRiffs)
            {
                PhraseHelper.TrimPhrase(largeRiff, _riffLength);
            }

            foreach (var halfSizeRiff in _riffs.Where(riff => riff.PhraseLength == _riffLength / 2M))
            {
                PhraseHelper.DuplicatePhrase(halfSizeRiff);
            }

            foreach (var quarterSizeRiff in _riffs.Where(riff => riff.PhraseLength == _riffLength / 4M))
            {
                PhraseHelper.DuplicatePhrase(quarterSizeRiff);
                PhraseHelper.DuplicatePhrase(quarterSizeRiff);
            }

            foreach (var wrongSizeRiff in _riffs.Where(riff => riff.PhraseLength != _riffLength))
            {
                Console.WriteLine("Riff " + wrongSizeRiff.Description + " is not " + _riffLength + " steps long - it's " + wrongSizeRiff.PhraseLength);
            }

            _riffs = _riffs.Where(riff => riff.PhraseLength == _riffLength).ToList();

            var wrongScaleRiffs = new List <Phrase>();

            foreach (var scaleRiff in _riffs)
            {
                var matchingScales = ScaleHelper.FindMatchingScales(scaleRiff).Where(x => x.DistanceFromScale == 0).Select(x => x.Scale.Name).ToList();
                if (!matchingScales.Contains(_baseScale))
                {
                    Console.WriteLine("Riff " + scaleRiff.Description + " DOES NOT contain base scale - is possibly " + (matchingScales.FirstOrDefault() ?? "??"));
                    wrongScaleRiffs.Add(scaleRiff);
                }
            }
            _riffs = _riffs.Except(wrongScaleRiffs).ToList();


            var wrongOctaveRiffs = new List <Phrase>();
            var lowestNote       = NoteHelper.NoteToNumber("C2");
            var highestNote      = NoteHelper.NoteToNumber("C4");

            foreach (var octaveRiff in _riffs)
            {
                var lowNote  = octaveRiff.Elements.Min(x => x.Note);
                var highNote = octaveRiff.Elements.Max(x => x.Note);

                if (lowNote >= NoteHelper.NoteToNumber("C3") && lowNote < NoteHelper.NoteToNumber("C4"))
                {
                    NoteHelper.ShiftNotesDirect(octaveRiff, 1, Interval.Octave, Direction.Down);
                    lowNote  = octaveRiff.Elements.Min(x => x.Note);
                    highNote = octaveRiff.Elements.Max(x => x.Note);
                }
                if (lowNote >= NoteHelper.NoteToNumber("C1") && lowNote < NoteHelper.NoteToNumber("C2"))
                {
                    NoteHelper.ShiftNotesDirect(octaveRiff, 1, Interval.Octave, Direction.Up);
                    lowNote  = octaveRiff.Elements.Min(x => x.Note);
                    highNote = octaveRiff.Elements.Max(x => x.Note);
                }

                if (lowNote < lowestNote || highNote > highestNote)
                {
                    Console.WriteLine("Riff "
                                      + octaveRiff.Description
                                      + " (" + NoteHelper.NumberToNote(lowNote)
                                      + "-" + NoteHelper.NumberToNote(highNote)
                                      + ") is outside the correct note range ("
                                      + NoteHelper.NumberToNote(lowestNote)
                                      + "-" + NoteHelper.NumberToNote(highestNote)
                                      + ").");
                    wrongOctaveRiffs.Add(octaveRiff);
                }
            }
            _riffs = _riffs.Except(wrongOctaveRiffs).ToList();
        }
Ejemplo n.º 12
0
        public Phrase GeneratePhrase(string baseRiff = "")
        {
            var riffs = _riffs.OrderBy(x => _random.NextDouble()).Take(_numberOfRiffsToMerge).ToList();

            if (baseRiff != "")
            {
                var riff = _riffs.Where(x => x.Description.ToLower() == baseRiff.ToLower()).ToList();
                if (riff.Count == 1)
                {
                    riffs = riffs.Except(riff).Take(_numberOfRiffsToMerge - 1).ToList();
                    riffs.InsertRange(0, riff);
                }
            }

            GenerateRiffProbabilities(riffs);

            var noteCount = GetNumberOfNotes();
            var phrase    = GenratePhraseBasic(noteCount);

            var perfectRepeats = GetPerfectRepeats();
            var timingRepeats  = GetTimingRepeats(perfectRepeats);
            var repeats        = perfectRepeats.Union(timingRepeats)
                                 .OrderBy(x => x.WindowSize)
                                 .ThenBy(x => x.WindowStart)
                                 .ThenBy(x => x.MatchWindowStart)
                                 .ToList();

            foreach (var repeat in repeats)
            {
                var sectionStartPositions = new List <decimal>
                {
                    repeat.WindowStart,
                    repeat.WindowStart + repeat.WindowSize,
                    repeat.MatchWindowStart,
                    repeat.MatchWindowStart + repeat.WindowSize
                };

                foreach (var position in sectionStartPositions)
                {
                    var element = phrase.Elements.FirstOrDefault(x => x.Position == position);
                    if (element != null)
                    {
                        continue;
                    }

                    element = GetNewRandomElement(position);
                    if (element != null)
                    {
                        phrase.Elements.Add(element);
                    }
                }
            }

            foreach (var repeat in repeats)
            {
                phrase.Elements.RemoveAll(x => x.Position >= repeat.MatchWindowStart &&
                                          x.Position < repeat.MatchWindowStart + repeat.WindowSize);

                var repeatingElements = phrase
                                        .Elements
                                        .Where(x => x.Position >= repeat.WindowStart &&
                                               x.Position < repeat.WindowStart + repeat.WindowSize)
                                        .Select(x => x.Clone())
                                        .ToList();

                foreach (var element in repeatingElements)
                {
                    element.Position = element.Position - repeat.WindowStart + repeat.MatchWindowStart;
                    if (repeat.MatchType != RepeatingElementsFinder.MatchResult.TimingMatch)
                    {
                        continue;
                    }

                    var probability = _probabilities.FirstOrDefault(x => x.Position == element.Position);
                    if (probability != null)
                    {
                        element.Note = GetRandomNote(probability.Notes);
                    }
                }

                phrase.Elements.AddRange(repeatingElements);
            }


            phrase.Elements = phrase.Elements.OrderBy(x => x.Position).ToList();
            phrase.Elements.RemoveAll(x => x.Position >= _riffLength);

            PhraseHelper.UpdateDurationsFromPositions(phrase, _riffLength);

            phrase.Bpm = 60;


            var lowNote = phrase.Elements.Min(x => x.Note);

            if (lowNote <= NoteHelper.NoteToNumber("C2"))
            {
                NoteHelper.ShiftNotesDirect(phrase, 1, Interval.Octave, Direction.Up);
            }

            return(phrase);
        }
 public static void CreatePhraseToGuess()
 {
     phraseToGuess.GetComponent <Text>().text = PhraseHelper.GetRandomPhrase(Config.nLettersPerPhrase);
 }