Example #1
0
        public static Motif MakeChordal(this Motif motif)
        {
            var chordalMotif = new Motif();

            chordalMotif.Pitches = motif.Pitches.Select(i => i == -1 ? i : i * 2).ToList();
            chordalMotif.Rhythm  = motif.Rhythm.Select(i => i).ToList();

            return(chordalMotif);
        }
Example #2
0
        public static Motif Concatenate(this Motif motif1, Motif motif2)
        {
            var newMotif = new Motif();

            newMotif.Pitches.AddRange(motif1.Pitches);
            newMotif.Rhythm.AddRange(motif1.Rhythm);
            newMotif.Pitches.AddRange(motif2.Pitches);
            newMotif.Rhythm.AddRange(motif2.Rhythm);
            return(newMotif);
        }
Example #3
0
        public static List <Tone> DevelopMotif(this List <Note> key, Motif motif, List <int> startIndexes, TimeSignature timeSignature, int maxBars = 8, int startOctave = 4, double alterChance = 0.5)
        {
            var phrase             = new List <Tone>();
            var randomIntGenerator = new Random();

            foreach (var startIndex in startIndexes)
            {
                var max = (int)Math.Round((decimal)(1 / alterChance));
                var randomAlterChance = randomIntGenerator.Next(1, max);
                var altered           = new Motif();
                if (randomAlterChance == 1)
                {
                    altered = motif.ModifyMotif();
                }
                else
                {
                    altered = motif;
                }

                var addition         = key.ApplyMotif(altered, startIndex, startOctave);
                var timeWithAddition = phrase.TotalTime() + addition.TotalTime();
                var maxTime          = maxBars * timeSignature.BarTime;
                if (timeWithAddition < maxTime)
                {
                    phrase.AddRange(addition);
                }
            }

            for (int i = 0; i < phrase.Count; i++)
            {
                var timeUpToThisTone = phrase.Take(i).ToList().TotalTime();
                var singleBarTime    = timeSignature.Beats * (double)timeSignature.BeatType;
                if (timeUpToThisTone % singleBarTime == 0)
                {
                    phrase[i].Volume = 0.3;
                }
                else
                {
                    phrase[i].Volume = 0.2;
                }
            }
            phrase.PadWithRests(timeSignature);

            return(phrase);
        }
Example #4
0
        public static List <Tone> ApplyMotif(this List <Note> key, Motif motif, int?startIndex = null, int startOctave = 4)
        {
            int start             = startIndex ?? motif.Pitches[0];
            var translatedMotif   = new Motif();
            var translationAmount = start + startOctave * key.Count - motif.Pitches[0];

            translatedMotif.Pitches = motif.Pitches.Select(i => i == -1 || i + translationAmount < 0 ? i : i + translationAmount).ToList();
            var appliedMotif = new List <Tone>();
            var octaves      = 100;
            var keyRange     = key.KeyRange(octaves);

            for (int i = 0; i < translatedMotif.Pitches.Count; i++)
            {
                var tone = new Tone();
                if (translatedMotif.Pitches[i] == -1)
                {
                    tone.Note   = Note.Rest;
                    tone.Octave = null;
                }
                else
                {
                    var octave = keyRange[translatedMotif.Pitches[i]].Octave;
                    int?octaveToUse;
                    if (octave < Sound.MinOctave)
                    {
                        octaveToUse = Sound.MinOctave;
                    }
                    else if (octave > Sound.MaxOctave)
                    {
                        octaveToUse = Sound.MaxOctave;
                    }
                    else
                    {
                        octaveToUse = octave;
                    }
                    tone.Note   = keyRange[translatedMotif.Pitches[i]].Note;
                    tone.Octave = octaveToUse;
                }
                tone.Length = motif.Rhythm[i];
                appliedMotif.Add(tone);
            }

            return(appliedMotif);
        }
Example #5
0
        public static Motif Motif(int length, int maxSize, int stasisInhibitor = 5, double restChance = 0.01, NoteLength mostLikelyNoteLength = NoteLength.Crotchet)
        {
            var motif = new Motif();
            var randomIntGenerator = new Random();
            var addRest            = randomIntGenerator.Next(0, (int)(1 / restChance));
            var randomPitch        = addRest == 1 ? -1 : randomIntGenerator.Next(0, maxSize);
            var previousDirection  = randomIntGenerator.Next(-1, 2);
            var nextIndex          = randomPitch;
            int lastIndex          = randomPitch;

            motif.Pitches.Add(nextIndex);
            motif.Rhythm.Add(RandomNoteLength(mostLikelyNoteLength));
            for (var i = 0; i < length; i++)
            {
                if (nextIndex != -1)
                {
                    lastIndex = nextIndex;
                    var direction = randomIntGenerator.Next(-1, 2);
                    for (var j = 0; j < stasisInhibitor; j++)
                    {
                        if (direction == 0 || direction != previousDirection)
                        {
                            direction = randomIntGenerator.Next(-1, 2);
                        }
                    }
                    var potentialNextIndex = lastIndex + direction;
                    previousDirection = direction;
                    while (potentialNextIndex < 0 || potentialNextIndex > maxSize)
                    {
                        var newDirection = randomIntGenerator.Next(-1, 2);
                        potentialNextIndex = nextIndex + newDirection;
                    }
                    addRest   = randomIntGenerator.Next(0, (int)(1 / restChance));
                    nextIndex = restChance == 1 ? -1 : potentialNextIndex;
                }

                motif.Pitches.Add(nextIndex);
                motif.Rhythm.Add(RandomNoteLength(mostLikelyNoteLength));
            }

            return(motif);
        }
Example #6
0
        public static Motif ModifyMotif(this Motif motif, List <Motif> motifPool = null)
        {
            var noOfTypesOfDevelopment = motifPool == null ? 4 : 5;
            var developedMotif         = new Motif();
            var developedMotifPitches  = motif.Pitches.Select(t => t).ToList();
            var developedMotifRhythm   = motif.Rhythm.Select(t => t).ToList();
            var randomIntGenerator     = new Random();
            var randomInt    = randomIntGenerator.Next(1, noOfTypesOfDevelopment);
            var displacement = randomIntGenerator.Next(-1, 2);

            switch (randomInt)
            {
            case 1:
            {
                developedMotifPitches.Reverse();
                break;
            }

            case 2:
            {
                developedMotifPitches.AddRange(developedMotifPitches.Transpose(displacement));
                developedMotifRhythm.AddRange(developedMotifRhythm);
                break;
            }

            case 3:
            {
                var copyPitches = developedMotifPitches.Select(t => t).ToList();
                var copyRhythm  = developedMotifRhythm.Select(nl => nl).ToList();
                copyPitches.Reverse();
                copyRhythm.Reverse();
                developedMotifPitches.AddRange(copyPitches.Transpose(displacement));
                developedMotifRhythm.AddRange(copyRhythm);
                break;
            }

            case 4:
            {
                var copyPitches = developedMotifPitches.Select(t => t).ToList();
                var copyRhythm  = developedMotifRhythm.Select(nl => nl).ToList();
                developedMotifPitches.Reverse();
                copyRhythm.Reverse();
                developedMotifPitches.AddRange(copyPitches.Transpose(displacement));
                developedMotifRhythm.AddRange(copyRhythm);
                break;
            }

            case 5:
            {
                if (motifPool != null)
                {
                    int poolSelection = randomIntGenerator.Next(1, motifPool.Count);
                    developedMotif = motif.Concatenate(motifPool[poolSelection]);
                }
                break;
            }
            }

            developedMotif.Pitches = developedMotifPitches;
            developedMotif.Rhythm  = developedMotifRhythm;
            return(developedMotif);
        }