Example #1
0
    private List <int> GenerateMelody(int beatsPerTab)
    {
        // Create a probability array
        const int             joint = 1; const int withinTierce = 2; const int outsideTierce = 3; const int repeat = 4;
        Dictionary <int, int> cases = new Dictionary <int, int>();

        cases.Add(joint, JointNoteProb);
        cases.Add(withinTierce, NoteWithinTierceProb);
        cases.Add(outsideTierce, NoteOutsideTierceProb);
        cases.Add(repeat, RepeatNoteProb);
        int[] probArray = Probability.CreateProbabilityArray(cases);

        // Creating the melody by adding a first random note
        int        firstNote = RandomGenerator.Next(MelodyNotes.Min, MelodyNotes.Max);
        List <int> melody    = new List <int>();

        melody.Add(firstNote);

        // Add notes to all beats using given probabilities
        for (int i = 1; i < NumberTabMelody * beatsPerTab; i++)
        {
            int choice   = probArray[RandomGenerator.Next(0, 100)];
            int note     = -1;
            int lastNote = melody[i - 1];
            switch (choice)
            {
            case joint:
                List <int> jointNotes = MusicCreator2.GetJointNotes(lastNote, MelodyNotes.Min, MelodyNotes.Max);
                note = jointNotes[RandomGenerator.Next(0, jointNotes.Count)];
                break;

            case withinTierce:
                List <int> withinTierceNotes = MusicCreator2.GetNotesWithinInterval(lastNote, 3, MelodyNotes.Min, MelodyNotes.Max);
                List <int> withinOneNote     = MusicCreator2.GetNotesWithinInterval(lastNote, 1, MelodyNotes.Min, MelodyNotes.Max);
                withinTierceNotes.RemoveAll(n => withinOneNote.Contains(n));
                note = withinTierceNotes[RandomGenerator.Next(0, withinTierceNotes.Count)];
                break;

            case outsideTierce:
                List <int> withinTierceN = MusicCreator2.GetNotesWithinInterval(lastNote, 3, MelodyNotes.Min, MelodyNotes.Max);
                List <int> withinOctave  = MusicCreator2.GetNotesWithinInterval(lastNote, 7, MelodyNotes.Min, MelodyNotes.Max);
                withinOctave.RemoveAll(n => withinTierceN.Contains(n));
                note = withinOctave[RandomGenerator.Next(0, withinOctave.Count)];
                break;

            case repeat:
                note = lastNote;
                break;

            default:
                Debug.LogError("Problem with the choice : " + choice);
                break;
            }
            melody.Add(note);
        }

        // TODO add silences and continue by avoiding making too large silence or too large notes

        // Add some silences randomly by replacing notes
        int numberSilence = RandomGenerator.Next(NumberSilenceInMelody.Min, NumberSilenceInMelody.Max);

        PutNoteRandomlyIn(MusicPlayer.SILENCE, melody, numberSilence);

        // Add some continue randomly by replacing notes
        int numberContinues = RandomGenerator.Next(NumberContinueInMelody.Min, NumberContinueInMelody.Max);

        PutNoteRandomlyIn(MusicPlayer.CONTINUE, melody, numberContinues);

        return(melody);
    }