How to Make Music With Machine Learning Using C#

Introduction

 
This article is a full project example of a simple music ML using design patterns for converting music theories into a C# windows form application and creating cusomizable output results by via the windows form user interface. This pianist has learned to create music bar templates and use them to create musical melodies for each bar at run time with fully randomized results. This project used Sanford.Multimedia.Midi library to playing outputs, but it is possible to use this library to convert output results into MIDI file format too. I tried to create a simple music template outputs by writing all needed music theories into design patterns. This project can play many randomized melodies and all converted music theories are customizable in the user interface.
 
Backgrounds
  • Intermediate C#
  • Abstract Factory Design Pattern
  • Decorator Design Pattern
  • Template Method Design Pattern
  • Encapsulation
  • Basic Music Theories (Beginner)
  • Read Sample Project In Git 

Author Introduction

 
There is much artificial intelligence for sound and music that fall into different types. For example, sound analysis, human voice analysis, reading brain signals when hearing sounds, music assistant and writing music notes software, software for music production, and more. Some of them are amazing because they can create a work of art, and for us, the difference may be that we can create art with programming and data, and that's great. But the truth is that there are sophisticated algorithms that have been developed over the years by various researchers in a variety of technologies, including electronics and information technology. Some algorithms may not work with big data, but they have many complexities, most of which are related to analysis. This is because they are supposed to have outputs that excite humans.
 
I have a simple ML example if you like to start art programming. For example, if you want to create a music note writing program, you need to create all basics theories like what I did categorize in this project and include more of yours. I am interested in auto-generating music algorithms so I created this project based on simple auto-generating melodies for 4/4 and 3/4 music bars. But you need to discover patterns of harmony, rhythm, and style in music to create full music to excite humans. The fact is that this requires a professional team that does not fit into an article.
 
Some music applications compose by synthesizer, so you have to work with frequencies, and your data mining maybe have to render big data. Then, you will have a difficult time finding fast algorithms for rendering.
 
For example, you have to listen to a song, and detect the tempo of the music, scale, and rhythm and export all sounds of track separated by vocals, instruments, or drums.
 
Therefore, you need to write your algorithms by frequencies in these steps:
  1. Find an algorithm to divide all instrumental, drums, effects and vocal frequencies from each other
  2. Find an algorithm to detect ticks of tempo and a clean timeline
  3. Find an algorithm to detect all bars of the soundtrack by using detected tempo and timeline of the track by the rhythm
  4. Find an algorithm to detect each beat of the detected bars
  5. Find an algorithm to detect how many notes are in each detected bar by their properties like divided sounds
  6. Find an algorithm to detect which frequency is used for starting note
  7. Find an algorithm to detect the notes of the music scale by detected first note frequency
"Each step need using some ML libraries too, you can not write these algorithms without the help of other AI libraries, for example you have to detect which frequency is for an instrument"
 
Here are some complexities in each step, for example in step 7 (scales), some music has modulation in scales, and you have to discover the modulations happened in the music. Therefore, you have to separate their notes when they have a close relationship together by their notes and you need to explain that modulation happens in music, or some times some scales have descending notes that they are different with the base scale notes and they are close to another scale, so you have a difficult time detecting scale by data mining when it is difficult by human too, but it is possible.
 
Your music application should learn basic music theories, like what I did in the example project:
  • Octaves and notes, by their properties
  • Scales and their types like heptatonic or pentatonic and more
  • Tempo
  • Note Values and Silence (time length for each note by tempo)
  • Rhythm and bars
These basic theories are enough for example project, but I want to explain to you what probability we might have in output results of the example project for 4/4 bars in heptatonic scales.
 
Count of Templates = COT = 6561
Avrage of Note Count in All Templates = AVG = 10
Scale Notes Count = SNC = 7
Scales Count = SC = 14
So the minimum output result count in this project is
Count of output melodies in 4/4 bars in example project =
(COT * (AVG ^ SNC)) * SC =
(6561 * (10 ^ 7)) * 14 =
918540000000000 possible melody bars for play just for 4/4 rhythm bars
 
This ML can generate 5 hours of music templates for each request in a second with a tempo of 120.
 
You can create a table in a database and save all generated melodies in this table to check to identify any melody. This helps you to run a loop until it geta a new melody or you can find out that you need more new randomization methods for generating new melodies. Saving more properties for each melody help you to discover how to generate a piece of music. You need to read many MIDI sample music by categorized titles, like their music genre or properties by their musical theories. Then your machine has learned harmonic fluctuations by your algorithms and has many templates for an infinity full music generator. MIDI format is a standard music data for compting music. MIDI is a small size music file with many properties in detail. If you have discovered cool generating algorithms, you have so many standard music templates for your machine learning to start.
 
I tried to write the project based on the design patterns so that I could explain it to you by C#
 

Topics

  1. Basic Music ML Data Mining: a simple data mining for creating a randomized melody
  2. Notes: Create note models by MIDI note number for usage by Sanford.Multimedia.Midi library
  3. Tempo and Bars: Create tempo interface and models and create a tempo calculator for note values in each rhythm bar
  4. Bar Template Creator Loop: use a simple algorithm for rendering many templates at run time in less than a second by using models and a loop
  5. Decorating Scales: use decorate design pattern for music scales
  6. Generate Method: use template method design pattern for melody generator method
  7. Generator: use the abstract factory design pattern for creating a generator factory
  8. Randomization: needed methods for creating note orders in melody generator with encapsulation
  9. Windows Form: create a user interface by all parameters for listening to the run time generated melodies
Topic 1 - Basic Music ML Data Mining
 
It is simple if you just want to create a melody for a bar of a rhythm. I did play all my templates in a queue without a data mining selection algorithm and just by random selection and sometimes interesting results are heard. Now imagine if you could find a simple algorithm to create a melody with many bars, you could have great results. But to make full music, you have to analyze other music and learn their harmonies based on data and base your program to composing by popular mix and mastering ways. There are many algorithms that can convert a simple one note for the human with exciting frequency modes, while you can't create such a work with any instrument, the basic old algorithm example is Echo for Vocals.
 
This project needs a simple data mining for generating a melody for a bar. If we define how a melody is created in a bar, then we can reach the melody with a few simple choices. Each bar has a queue of notes with a playtime length. Each length is calculated by tempo number and the sum of the note value lengths are standard in each bar for each rhythm by tempo.
 
In each place of the bar note values, we can put a note selected from a requested scale. Now if your scale has 7 notes and your template is created by 4 notes, then you have 4^7 possible melodies for your template.
 
So you just need to do the simple steps below: 
  • Step 1: Create a list of notes from requested or randomized octave
  • Step 2: Create a separate note list by requested scale from the created list in step 1
  • Step 3: Choose a template by requested tempo and rhythm bar, length of each note value must calculate by tempo number
  • Step 4: Choose a note for starting a melody and select notes with a count of your template to create an int array of note numbers for usage in next step
  • Step 5: Using a randomization method by the requested randomize type and use note numbers array for selection in the randomize method
  • Step 6: Create a queue of randomized results with the length of tempo note values, in this step you have to put each index of randomized note numbers into the template note index places in each beat of the bar
The usage of these steps is in topic 6.
 
Topic 2 - Notes
 
Create a note model like this with the number of MIDI note data and create a list from your notes with a loop.
  1. public class Note  
  2. {  
  3.     private string name = "";  
  4.     private double frequence = 0;  
  5.     private int octave = 0;  
  6.     private int number = 0;  
  7.   
  8.     public string Name { get { return name; } }  
  9.     public int Number { get { return number; } }  
  10.     public double Frequence { get { return frequence; } }  
  11.     public int Octave { get { return octave; } }  
  12. }  
  13.   
  14.   
  15. public static class NoteList  
  16. {  
  17.     public static List<Note> GetList(int? ftOcv = null ,int? flOcv = null )  
  18.     {  
  19.         List<Note> result = new List<Note>();  
  20.   
  21.         for (int i = 12; i < 128; i++)  
  22.         {  
  23.             Note n = new Note(i);  
  24.             result.Add(n);  
  25.         }  
  26.   
  27.         if(ftOcv != null)  
  28.         {  
  29.             result = result.Where(a => a.Octave >= ftOcv).ToList();  
  30.         }  
  31.   
  32.         if(flOcv != null)  
  33.         {  
  34.             result = result.Where(a => a.Octave <= flOcv).ToList();  
  35.         }  
  36.   
  37.         return result;  
  38.     }  
  39. }  
Topic 3 – Tempo and Bars
 
Create a tempo calculator for each rhythm bar by tempo number for note values. Create your model by inherited from an interface for usage in template method design pattern in topic 6
  1. public class Tempo  
  2. {  
  3.     private int _tempo = 120;  
  4.   
  5.     public Tempo()  
  6.     {  
  7.   
  8.     }  
  9.   
  10.     public Tempo(int tempo)  
  11.     {  
  12.         _tempo = tempo;  
  13.     }  
  14.   
  15.     public TempoFor4_4Bars Tempo4_4  
  16.     {  
  17.         get  
  18.         {  
  19.             return new TempoFor4_4Bars(_tempo);  
  20.         }  
  21.     }  
  22.   
  23.     public TempoFor3_4Bars Tempo3_4  
  24.     {  
  25.         get  
  26.         {  
  27.             return new TempoFor3_4Bars(_tempo);  
  28.         }  
  29.     }  
  30. }  
  31.   
  32. public interface ITempoForBars  
  33. {  
  34.     double ComputeBarLength { get; }  
  35.     double CountOfBeats { get; }  
  36.     double BarLength { get; }  
  37.     double QuarterLength { get; }  
  38.     double DottedQuarterLength { get; }  
  39.     double TripletQuarterLength { get; }  
  40.     bool SupportingHalfLength { get; }  
  41.     double HalfLength { get; }  
  42.     double EighthLength { get; }  
  43.     double DottedEighthLength { get; }  
  44.     double TripletEighthLength { get; }  
  45.     double SixteenthLength { get; }  
  46.     double DottedSixteenthLength { get; }  
  47.     double TripletSixteenthLength { get; }  
  48.     double ThirtySecondLength { get; }  
  49.     double DottedThirtySecondLength { get; }  
  50.     double TripletThirtySecondLength { get; }  
  51.     double SixtyFourthLength { get; }  
  52.     double DottedSixtyFourthLength { get; }  
  53.     double TripletSixtyFourthLength { get; }  
  54.     double HundredTwentyEighthLength { get; }  
  55. }  
  56.   
  57. public class TempoFor4_4Bars : ITempoForBars  
  58. {  
  59.     private readonly short _binary = 2;  
  60.     private readonly int _tempoBaseNumber = 120;  
  61.     private readonly int _minute = 60000;  
  62.     private int _tempo = 120;  
  63.     private int _distance = 0;  
  64.   
  65.     public TempoFor4_4Bars(int tempo)  
  66.     {  
  67.         _tempo = tempo;  
  68.         _distance = _tempoBaseNumber - tempo;  
  69.     }  
  70.       
  71.     public double ComputeBarLength  
  72.     {  
  73.         get  
  74.        {  
  75.             return QuarterLength * CountOfBeats;  
  76.         }  
  77.     }  
  78.     public double CountOfBeats { get { return 4; } }  
  79.     public double BarLength { get { return ComputeBarLength; } }  
  80.     public double QuarterLength { get { return _minute / _tempo; } }  
  81.     public double DottedQuarterLength { get { return QuarterLength + HalfLength; } }  
  82.     public double TripletQuarterLength { get { return QuarterLength / 3; } }  
  83.     public bool SupportingHalfLength { get { return CountOfBeats >= 2; } }  
  84.     public double HalfLength { get { return SupportingHalfLength ? QuarterLength * 2 : 0; } }  
  85.     public double EighthLength { get { return QuarterLength / _binary; } }  
  86.     public double DottedEighthLength { get { return EighthLength + SixteenthLength; } }  
  87.     public double TripletEighthLength { get { return EighthLength / 3; } }  
  88.     public double SixteenthLength { get { return EighthLength / _binary; } }  
  89.     public double DottedSixteenthLength { get { return SixteenthLength + ThirtySecondLength; } }  
  90.     public double TripletSixteenthLength { get { return SixteenthLength / 3; } }  
  91.     public double ThirtySecondLength { get { return SixteenthLength / _binary; } }  
  92.     public double DottedThirtySecondLength { get { return ThirtySecondLength + SixtyFourthLength; } }  
  93.     public double TripletThirtySecondLength { get { return ThirtySecondLength / 3; } }  
  94.     public double SixtyFourthLength { get { return ThirtySecondLength / _binary; } }  
  95.     public double DottedSixtyFourthLength { get { return SixtyFourthLength + HundredTwentyEighthLength; } }  
  96.     public double TripletSixtyFourthLength { get { return SixtyFourthLength / 3; } }  
  97.     public double HundredTwentyEighthLength { get { return SixtyFourthLength / _binary; } }  
  98. }  
Topic 4 – Bar Template Creator Loop
 
First, create a model for beats then create beat types for each bar model and run a loop to create your templates.
  1. public class BeatModel  
  2. {  
  3.     public int OrderIndex { getset; }  
  4.     public List<NoteValueLengthIdentity> NoteValues { getset; }  
  5. }  
  6.   
  7. public class NoteValueLengthIdentity  
  8. {  
  9.     public int OrderIndex { getset; }  
  10.     public NoteValue NoteValue { getset; }  
  11.     public double NormalLength  
  12.     {  
  13.         get  
  14.         {  
  15.             TempoFor4_4Bars barModel = new TempoFor4_4Bars(120);  
  16.             switch (NoteValue)  
  17.             {  
  18.                 case NoteValue.Quarter:  
  19.                     return barModel.QuarterLength;  
  20.                 case NoteValue.DottedQuarter:  
  21.                     return barModel.DottedQuarterLength;  
  22.                 case NoteValue.Half:  
  23.                     return barModel.HalfLength;  
  24.                 case NoteValue.Eighth:  
  25.                     return barModel.EighthLength;  
  26.                 case NoteValue.DottedEighth:  
  27.                     return barModel.DottedEighthLength;  
  28.                 case NoteValue.Sixteenth:  
  29.                     return barModel.SixteenthLength;  
  30.                 case NoteValue.DottedSixteenth:  
  31.                     return barModel.DottedSixteenthLength;  
  32.                 case NoteValue.ThirtySecond:  
  33.                     return barModel.ThirtySecondLength;  
  34.                 case NoteValue.DottedThirtySecond:  
  35.                     return barModel.DottedThirtySecondLength;  
  36.                 case NoteValue.SixtyFourth:  
  37.                     return barModel.SixtyFourthLength;  
  38.                 case NoteValue.DottedSixtyFourth:  
  39.                     return barModel.DottedSixtyFourthLength;  
  40.                 case NoteValue.HundredTwentyEighth:  
  41.                     return barModel.HundredTwentyEighthLength;  
  42.                 default:  
  43.                     return 0;  
  44.             }  
  45.         }  
  46.     }  
  47.     public bool IsPulledFromPrevious { getset; }  
  48. }  
  49.   
  50. public enum NoteValue  
  51. {  
  52.     Quarter = 0,  
  53.     DottedQuarter = 1,  
  54.     Half = 2,  
  55.     Eighth = 3,  
  56.     DottedEighth = 4,  
  57.     Sixteenth = 5,  
  58.     DottedSixteenth = 6,  
  59.     ThirtySecond = 7,  
  60.     DottedThirtySecond = 8,  
  61.     SixtyFourth = 9,  
  62.     DottedSixtyFourth = 10,  
  63.     HundredTwentyEighth = 11  
  64. }  
  65.   
  66. public interface IBarTemplateModel  
  67. {  
  68.     List<NoteValueLengthIdentity> AllNoteValues { get; }  
  69.     int NoteCount { get; }  
  70.     bool HaveError { get; }  
  71. }  
  72.   
  73. public class BarTemplateModel : IBarTemplateModel  
  74.     {  
  75.         public BeatModel Beat1 { getset; }  
  76.         public BeatModel Beat2 { getset; }  
  77.         public BeatModel Beat3 { getset; }  
  78.         public BeatModel Beat4 { getset; }  
  79.         public List<NoteValueLengthIdentity> AllNoteValues  
  80.         {  
  81.             get  
  82.             {  
  83.                 List<NoteValueLengthIdentity> result = new List<NoteValueLengthIdentity>();  
  84.   
  85.                 result.AddRange(Beat1.NoteValues);  
  86.                 result.AddRange(Beat2.NoteValues);  
  87.                 result.AddRange(Beat3.NoteValues);  
  88.                 result.AddRange(Beat4.NoteValues);  
  89.   
  90.                 return result;  
  91.             }  
  92.         }  
  93.         public int NoteCount  
  94.         {  
  95.             get  
  96.             {  
  97.                 return AllNoteValues.Count;  
  98.             }  
  99.         }  
  100.         public bool HaveError  
  101.         {  
  102.             get  
  103.             {  
  104.                 var sum1 = Beat1.NoteValues.Sum(a => a.NormalLength);  
  105.                 var sum2 = Beat2.NoteValues.Sum(a => a.NormalLength);  
  106.                 var sum3 = Beat3.NoteValues.Sum(a => a.NormalLength);  
  107.                 var sum4 = Beat4.NoteValues.Sum(a => a.NormalLength);  
  108.                 return sum1 + sum2 + sum3 + sum4 != 2000;  
  109.             }  
  110.         }  
  111.     }  
  112.   
  113.     public static IEnumerable<IBarTemplateModel> GetAllAvailableTemplates()  
  114.     {  
  115.         for (int i = 1; i < 10; i++)  
  116.         {  
  117.             for (int j = 1; j < 10; j++)  
  118.             {  
  119.                 for (int k = 1; k < 10; k++)  
  120.                 {  
  121.                     for (int l = 1; l < 10; l++)  
  122.                     {  
  123.                         yield return new BarTemplateModel() { Beat1 = GetBeatModel1(i), Beat2 = GetBeatModel2(j), Beat3 = GetBeatModel3(k), Beat4 = GetBeatModel4(l) };  
  124.                     }  
  125.                 }  
  126.             }  
  127.         }  
  128.     }  
  129.   
  130.     public static BeatModel GetBeatModel1(int swichMethod)  
  131.     {  
  132.         switch (swichMethod)  
  133.         {  
  134.             case 1:  
  135.                 return SimpleBeat1(1);  
  136.             case 2:  
  137.                 return SimpleBeat2(1);  
  138.             case 3:  
  139.                 return SimpleBeat3(1);  
  140.             case 4:  
  141.                 return SimpleBeat4(1);  
  142.             case 5:  
  143.                 return SimpleBeat5(1);  
  144.             case 6:  
  145.                 return SimpleBeat6(1);  
  146.             case 7:  
  147.                 return SimpleBeat7(1);  
  148.             case 8:  
  149.                 return SimpleBeat8(1);  
  150.             case 9:  
  151.                 return SimpleBeat9(1);  
  152.             default:  
  153.                 return SimpleBeat1(1);  
  154.         }  
  155.     }  
  156.   
  157. private static BeatModel SimpleBeat1(int i)  
  158.     {  
  159.         return new BeatModel()  
  160.         {  
  161.             OrderIndex = i,  
  162.             NoteValues = SimpleNoteValueTemp1()  
  163.         };  
  164.     }  
  165. private static List<NoteValueLengthIdentity> SimpleNoteValueTemp2()  
  166.     {  
  167.         return new List<NoteValueLengthIdentity>()  
  168.         {  
  169.             new NoteValueLengthIdentity()  
  170.             {  
  171.                 OrderIndex = 1,  
  172.                 NoteValue = NoteValue.Eighth,  
  173.                 IsPulledFromPrevious = false  
  174.             },  
  175.             new NoteValueLengthIdentity()  
  176.             {  
  177.                 OrderIndex = 2,  
  178.                 NoteValue = NoteValue.Eighth,  
  179.                 IsPulledFromPrevious = false  
  180.             },  
  181.         };  
  182.     }  
Topic 5 – Decorating Scales
 
Usage of scales is by some parameters of their properties in music theories. So decorating of scales helps for simple access of scales. Here is an example for C# decorating design pattern codes into music C# scales
  1.  public interface IScale  
  2. {  
  3.     ScaleType Type { get; }  
  4.     ScalePos Pos { get; }  
  5. }  
  6.   
  7. public interface IHeptaScaleIdentity  
  8. {  
  9.     string Name { get; }  
  10.     int ID { get; }  
  11.     string[] AscendingNotes { get; }  
  12.     string[] DescendingNotes { get; }  
  13.     string StartNote { get; }  
  14.     string DescendingStartNote { get; }  
  15.     string FinishNote { get; }  
  16.     string DescendingFinishNote { get; }  
  17. }  
  18.   
  19. public interface IPentaScaleIdentity  
  20. {  
  21.     string Name { get; }  
  22.     int ID { get; }  
  23.     string[] AscendingNotes { get; }  
  24.     string[] DescendingNotes { get; }  
  25.     string StartNote { get; }  
  26.     string DescendingStartNote { get; }  
  27.     string FinishNote { get; }  
  28.     string DescendingFinishNote { get; }  
  29. }  
  30.   
  31. public interface IMajorHeptatonicScale : IScale  
  32. {  
  33.     bool AcceptModelation { get; }  
  34.     int[] ModelationSupportScalesId { get; }  
  35. }  
  36.   
  37. public interface IMinorHeptatonicScale : IScale  
  38. {  
  39.     bool AcceptModelation { get; }  
  40.     int[] ModelationSupportScalesId { get; }  
  41. }  
  42.   
  43. public interface IMajorPentatonicScale : IScale  
  44. {  
  45.     bool AcceptModelation { get; }  
  46.     int[] ModelationSupportScalesId { get; }  
  47. }  
  48.   
  49. public interface IMinorPentatonicScale : IScale  
  50. {  
  51.     bool AcceptModelation { get; }  
  52.     int[] ModelationSupportScalesId { get; }  
  53. }  
  54.   
  55. public class Major_Scale_Hepta : IMajorHeptatonicScale  
  56. {  
  57.     public ScaleType Type { get { return ScaleType.Heptatonic; } }  
  58.     public ScalePos Pos { get { return ScalePos.Major; } }  
  59.     public bool AcceptModelation { get { return false; } }  
  60.     public int[] ModelationSupportScalesId { get { return null; } }  
  61. }  
  62.   
  63. public class Minor_Scale_Hepta : IMinorHeptatonicScale  
  64. {  
  65.     public ScaleType Type { get { return ScaleType.Heptatonic; } }  
  66.     public ScalePos Pos { get { return ScalePos.Minor; } }  
  67.     public bool AcceptModelation { get { return false; } }  
  68.     public int[] ModelationSupportScalesId { get { return null; } }  
  69. }  
  70.   
  71. public class Major_Scale_Penta : IMajorPentatonicScale  
  72. {  
  73.     public ScaleType Type { get { return ScaleType.Pentatonic; } }  
  74.     public ScalePos Pos { get { return ScalePos.Major; } }  
  75.     public bool AcceptModelation { get { return false; } }  
  76.     public int[] ModelationSupportScalesId { get { return null; } }  
  77. }  
  78.   
  79. public class Minor_Scale_Penta : IMinorPentatonicScale  
  80. {  
  81.     public ScaleType Type { get { return ScaleType.Pentatonic; } }  
  82.     public ScalePos Pos { get { return ScalePos.Minor; } }  
  83.     public bool AcceptModelation { get { return false; } }  
  84.     public int[] ModelationSupportScalesId { get { return null; } }  
  85. }  
  86.   
  87.   
  88. public class C_Sharp_Major_Scale_Hepta : IMajorHeptatonicScale, IHeptaScaleIdentity  
  89. {  
  90.     IMajorHeptatonicScale Heptatonic;  
  91.     public C_Sharp_Major_Scale_Hepta(IMajorHeptatonicScale heptaScale)  
  92.     {  
  93.         this.Heptatonic = heptaScale;  
  94.     }  
  95.     public ScaleType Type { get { return this.Heptatonic.Type; } }  
  96.     public ScalePos Pos { get { return this.Heptatonic.Pos; } }  
  97.     public string Name { get { return "Heptatonic C# Major"; } }  
  98.     public int ID { get { return 1; } }  
  99.     public virtual bool AcceptModelation { get { return true; } }  
  100.     public virtual int[] ModelationSupportScalesId { get { return new int[] { 2 }; } }  
  101.     public virtual string[] AscendingNotes { get { return new string[] { "C#""D#""F""F#""G#""A#""C""C#" }; } }  
  102.     public virtual string[] DescendingNotes { get { return new string[] { "C#""D#""F""F#""G#""A#""C""C#" }; } }  
  103.     public virtual string StartNote { get { return "C#"; } }  
  104.     public virtual string DescendingStartNote { get { return "G#"; } }  
  105.     public virtual string FinishNote { get { return "F#"; } }  
  106.     public virtual string DescendingFinishNote { get { return "C#"; } }  
  107. }  
  108.   
  109. public class C_Sharp_Minor_Scale_Hepta : IMinorHeptatonicScale, IHeptaScaleIdentity  
  110. {  
  111.     IMinorHeptatonicScale Heptatonic;  
  112.     public C_Sharp_Minor_Scale_Hepta(IMinorHeptatonicScale heptaScale)  
  113.     {  
  114.         this.Heptatonic = heptaScale;  
  115.     }  
  116.     public ScaleType Type { get { return this.Heptatonic.Type; } }  
  117.     public ScalePos Pos { get { return this.Heptatonic.Pos; } }  
  118.     public string Name { get { return "Heptatonic C# Minor"; } }  
  119.     public int ID { get { return 2; } }  
  120.     public virtual bool AcceptModelation { get { return true; } }  
  121.     public virtual int[] ModelationSupportScalesId { get { return new int[] { 1 }; } }  
  122.     public virtual string[] AscendingNotes { get { return new string[] { "C#""D#""E""F#""G#""A#""B#""C#" }; } }  
  123.     public virtual string[] DescendingNotes { get { return new string[] { "C#""D#""E""F#""G#""A#""B#""C#" }; } }  
  124.     public virtual string StartNote { get { return "C#"; } }  
  125.     public virtual string DescendingStartNote { get { return "G#"; } }  
  126.     public virtual string FinishNote { get { return "F#"; } }  
  127.     public virtual string DescendingFinishNote { get { return "C#"; } }  
  128. }  
Topic 6 – Generate Mehod
 
Create a generate method for using template method design pattern for run steps in topic 1
  1. public abstract class GenerateTemplateCreator  
  2. {  
  3.     public static PlayLineHarmony GetResult(ITempoForBars tempo, IBarTemplateModel template, List<Note.Note> noteList, PlayOrderType orderType)  
  4.     {  
  5.         PlayLineHarmony playLine = new PlayLineHarmony();  
  6.         playLine.Instrument = Sanford.Multimedia.Midi.GeneralMidiInstrument.Accordion;  
  7.         playLine.Order = 1;  
  8.         playLine.Vol = 127;  
  9.         playLine.NoteQueue = new List<PlayLineNotes>();  
  10.         Random r = new Random();  
  11.         var repeatRandom = r.Next(0, 19);  
  12.         var minOctav = r.Next(3, 5);  
  13.         var maxOctav = r.Next(5, 7);  
  14.         var skipRandom = r.Next(1, 8);  
  15.         int[] notes = noteList.Where(a => a.Octave >= minOctav && a.Octave <= maxOctav).Skip(skipRandom).Take(template.NoteCount).OrderByDescending(a => a.Number).Select(a => a.Number).ToArray();  
  16.         if (orderType == PlayOrderType.Ascending)  
  17.             notes = notes.OrderBy(a => a).ToArray();  
  18.         else if (orderType == PlayOrderType.Randomize)  
  19.             notes = notes.Randomize().Take(notes.Length).ToArray();  
  20.         for (int j = 0; j < notes.Length; j++)  
  21.         {  
  22.             var newNote = GetNote(playLine.NoteQueue.Count + 1, tempo, template.AllNoteValues[j].NoteValue, noteList, notes[j]);  
  23.             playLine.NoteQueue.Add(newNote);  
  24.         }  
  25.   
  26.         return playLine;  
  27.     }  
  28.   
  29.     private static PlayLineNotes GetNote(int order, ITempoForBars tempo, NoteValue value, List<Note.Note> noteList, int noteNumber)  
  30.     {  
  31.         return new PlayLineNotes()  
  32.         {  
  33.             IsPlayed = false,  
  34.             Length = tempo.LengthByTempo(value),  
  35.             Note = noteNumber != 0 ? noteList.FirstOrDefault(a => a.Number == noteNumber) : null,  
  36.             Order = order,  
  37.             Silence = false  
  38.         };  
  39.     }  
  40. }  
  41.   
  42. public enum PlayOrderType  
  43. {  
  44.     Ascending = 0,  
  45.     Descending = 1,  
  46.     Randomize = 2  
  47. }  
Topic 7 – Generator
 
I created the generator classes by the abstract factory design pattern to using divided by scales and keys or rhythm generate.
  1. public class GenerateTemplate  
  2. {  
  3.     private HeptatonicGenerator _heptatonicGenerator;  
  4.     private PentatonicGenerator _pentatonicGenerator;  
  5.   
  6.     // Constructor  
  7.     public GenerateTemplate(TemplateGeneratorFactory factory)  
  8.     {  
  9.         _heptatonicGenerator = factory.HeptatonicGenerator();  
  10.         _pentatonicGenerator = factory.PentatonicGenerator();  
  11.     }  
  12.   
  13.     public HeptatonicGenerator GenerateHeptatonic()  
  14.     {  
  15.         return _heptatonicGenerator;  
  16.     }  
  17.   
  18.     public PentatonicGenerator GeneratePentatonic()  
  19.     {  
  20.         return _pentatonicGenerator;  
  21.     }  
  22. }  
  23.   
  24. public class DescendingFactory : TemplateGeneratorFactory  
  25. {  
  26.     public override PentatonicGenerator PentatonicGenerator()  
  27.     {  
  28.         return new PentatonicDescendingTemplate();  
  29.     }  
  30.     public override HeptatonicGenerator HeptatonicGenerator()  
  31.     {  
  32.         return new HeptatonicDescendingTemplate();  
  33.     }  
  34. }  
  35.   
  36. public class AccendingFactory : TemplateGeneratorFactory  
  37. {  
  38.     public override PentatonicGenerator PentatonicGenerator()  
  39.     {  
  40.         return new PentatonicAscendingTemplate();  
  41.     }  
  42.     public override HeptatonicGenerator HeptatonicGenerator()  
  43.     {  
  44.         return new HeptatonicAscendingTemplate();  
  45.     }  
  46. }  
  47.   
  48. public abstract class TemplateGeneratorFactory  
  49. {  
  50.     public abstract PentatonicGenerator PentatonicGenerator();  
  51.     public abstract HeptatonicGenerator HeptatonicGenerator();  
  52. }  
  53.   
  54. public abstract class HeptatonicGenerator  
  55. {  
  56.     public abstract PlayLineHarmony Get4_4Templates(ITempoForBars tempo, IBarTemplateModel template, List<Note.Note> noteList, PlayOrderType orderType);  
  57.     public abstract PlayLineHarmony Get3_4Templates(ITempoForBars tempo, IBarTemplateModel template, List<Note.Note> noteList, PlayOrderType orderType);  
  58. }  
  59.   
  60. public abstract class PentatonicGenerator  
  61. {  
  62.     public abstract PlayLineHarmony Get4_4Templates(ITempoForBars tempo, IBarTemplateModel template, List<Note.Note> noteList, PlayOrderType orderType);  
  63.     public abstract PlayLineHarmony Get3_4Templates(ITempoForBars tempo, IBarTemplateModel template, List<Note.Note> noteList, PlayOrderType orderType);  
  64. }  
  65.   
  66. public class HeptatonicAscendingTemplate : HeptatonicGenerator  
  67. {  
  68.     public override PlayLineHarmony Get4_4Templates(ITempoForBars tempo, IBarTemplateModel template, List<Note.Note> noteList, PlayOrderType orderType)  
  69.     {  
  70.         return GenerateTemplateCreator.GetResult(tempo,template,noteList,orderType);  
  71.     }  
  72.     public override PlayLineHarmony Get3_4Templates(ITempoForBars tempo, IBarTemplateModel template, List<Note.Note> noteList, PlayOrderType orderType)  
  73.     {  
  74.         return GenerateTemplateCreator.GetResult(tempo, template, noteList, orderType);  
  75.     }  
  76. }  
  77.   
  78. public class PentatonicAscendingTemplate : PentatonicGenerator  
  79. {  
  80.     public override PlayLineHarmony Get4_4Templates(ITempoForBars tempo, IBarTemplateModel template, List<Note.Note> noteList, PlayOrderType orderType)  
  81.     {  
  82.         return GenerateTemplateCreator.GetResult(tempo, template, noteList, orderType);  
  83.     }  
  84.     public override PlayLineHarmony Get3_4Templates(ITempoForBars tempo, IBarTemplateModel template, List<Note.Note> noteList, PlayOrderType orderType)  
  85.     {  
  86.         return GenerateTemplateCreator.GetResult(tempo, template, noteList, orderType);  
  87.     }  
  88. }  
  89.   
  90. public class HeptatonicDescendingTemplate : HeptatonicGenerator  
  91. {  
  92.     public override PlayLineHarmony Get4_4Templates(ITempoForBars tempo, IBarTemplateModel template, List<Note.Note> noteList, PlayOrderType orderType)  
  93.     {  
  94.         return GenerateTemplateCreator.GetResult(tempo, template, noteList, orderType);  
  95.     }  
  96.     public override PlayLineHarmony Get3_4Templates(ITempoForBars tempo, IBarTemplateModel template, List<Note.Note> noteList, PlayOrderType orderType)  
  97.     {  
  98.         return GenerateTemplateCreator.GetResult(tempo, template, noteList, orderType);  
  99.     }  
  100. }  
  101.   
  102. public class PentatonicDescendingTemplate : PentatonicGenerator  
  103. {  
  104.     public override PlayLineHarmony Get4_4Templates(ITempoForBars tempo, IBarTemplateModel template, List<Note.Note> noteList, PlayOrderType orderType)  
  105.     {  
  106.         return GenerateTemplateCreator.GetResult(tempo, template, noteList, orderType);  
  107.     }  
  108.     public override PlayLineHarmony Get3_4Templates(ITempoForBars tempo, IBarTemplateModel template, List<Note.Note> noteList, PlayOrderType orderType)  
  109.     {  
  110.         return GenerateTemplateCreator.GetResult(tempo, template, noteList, orderType);  
  111.     }  
  112. }  
Topic 8 – Randomization
 
Generator needs an encapsulation class to return a list of randomized note numbers for replace in template note values queue. Create loop methods like this:
  1. public class Randomization  
  2. {  
  3.     private int[] Notes;  
  4.     private int length = 0;  
  5.     private Random r = new Random();  
  6.     private List<int> Result = new List<int>();  
  7.   
  8.     public Randomization(PlayTypeSign sign, int[] notes)  
  9.     {  
  10.         Notes = notes;  
  11.         length = Notes.Length;  
  12.   
  13.         switch (sign)  
  14.         {  
  15.             case PlayTypeSign.OrderNotes:  
  16.                 Result = OrderNotes();  
  17.                 break;  
  18.             case PlayTypeSign.Melody:  
  19.                 Result = Melody();  
  20.                 break;  
  21.             case PlayTypeSign.Melody2:  
  22.                 Result = Melody2();  
  23.                 break;  
  24.             case PlayTypeSign.Shuffle1:  
  25.                 Result = Shuffle1();  
  26.                 break;  
  27.             case PlayTypeSign.Shuffle2:  
  28.                 Result = Shuffle2();  
  29.                 break;  
  30.             case PlayTypeSign.Shuffle3:  
  31.                 Result = Shuffle3();  
  32.                 break;  
  33.             case PlayTypeSign.OrderAsc:  
  34.                 Result = OrderAsc();  
  35.                 break;  
  36.             case PlayTypeSign.OrderDes:  
  37.                 Result = OrderDes();  
  38.                 break;  
  39.             case PlayTypeSign.OrderAscBy2Step:  
  40.                 Result = OrderAscBy2Step();  
  41.                 break;  
  42.             case PlayTypeSign.OrderDesBy2Step:  
  43.                 Result = OrderDesBy2Step();  
  44.                 break;  
  45.             case PlayTypeSign.OrderAscBy3Step:  
  46.                 Result = OrderAscBy3Step();  
  47.                 break;  
  48.             case PlayTypeSign.OrderDesBy3Step:  
  49.                 Result = OrderDesBy3Step();  
  50.                 break;  
  51.             case PlayTypeSign.OrderingStep3:  
  52.                 Result = OrderingStep3();  
  53.                 break;  
  54.             case PlayTypeSign.OrderingStep3Backward:  
  55.                 Result = OrderingStep3Backward();  
  56.                 break;  
  57.             case PlayTypeSign.GetByBase:  
  58.                 Result = GetByBase();  
  59.                 break;  
  60.             default:  
  61.                 break;  
  62.         }  
  63.     }  
  64.   
  65.     public List<int> GetList()  
  66.     {  
  67.         return Result;  
  68.     }  
  69.   
  70.     public List<int> GetByBase()  
  71.     {  
  72.         List<int> result = new List<int>();  
  73.   
  74.         for (int i = 0; i < length && result.Count < length; i++)  
  75.         {  
  76.             var randomPlace = r.Next(0, length);  
  77.             result.Add(Notes[i]);  
  78.             if (randomPlace % 2 == 0)  
  79.                 result.Add(0);  
  80.             result.Add(Notes[randomPlace]);  
  81.         }  
  82.   
  83.         return result;  
  84.     }  
  85.   
  86.     public List<int> OrderAsc()  
  87.     {  
  88.         List<int> result = new List<int>();  
  89.         Notes = Notes.OrderBy(a => Guid.NewGuid()).ToArray();  
  90.         for (int i = 0; i < length && result.Count < length; i++)  
  91.         {  
  92.             result.Add(Notes[i + 1 < length ? i + 1 : i]);  
  93.             result.Add(Notes[i + 2 < length ? i + 2 : i]);  
  94.         }  
  95.   
  96.         return result;  
  97.     }  
  98.   
  99.     public List<int> OrderDes()  
  100.     {  
  101.         List<int> result = new List<int>();  
  102.         Notes = Notes.OrderBy(a => Guid.NewGuid()).ToArray();  
  103.         for (int i = length; i >= 0 && result.Count < length; i--)  
  104.         {  
  105.             result.Add(Notes[i - 1 > 0 ? i - 1 : i + 1]);  
  106.             result.Add(Notes[i - 2 > 0 ? i - 2 : i]);  
  107.         }  
  108.   
  109.         return result;  
  110.     }  
  111.   
  112.     public List<int> OrderingStep3()  
  113.     {  
  114.         List<int> result = new List<int>();  
  115.         Notes = Notes.OrderBy(a => a).ToArray();  
  116.         int last = 0;  
  117.   
  118.         for (int i = 0; i < length && result.Count < length; i++)  
  119.         {  
  120.             last = last + (i + 3);  
  121.             result.Add(Notes[last < length ? last : i]);  
  122.         }  
  123.   
  124.         return result;  
  125.     }  
  126.   
  127.     public List<int> OrderingStep3Backward()  
  128.     {  
  129.         List<int> result = new List<int>();  
  130.         Notes = Notes.OrderByDescending(a => a).ToArray();  
  131.         int last = 0;  
  132.   
  133.         for (int i = 0; i < length && result.Count < length; i++)  
  134.         {  
  135.             last = last + (i - 3);  
  136.             result.Add(Notes[last >= 0 && last < length? last : i]);  
  137.         }  
  138.   
  139.         return result;  
  140.     }  
  141.   
  142.     public List<int> OrderAscBy2Step()  
  143.     {  
  144.         List<int> result = new List<int>();  
  145.         Notes = Notes.OrderBy(a => a).ToArray();  
  146.         for (int i = 0; i < length && result.Count < length; i++)  
  147.         {  
  148.             result.Add(Notes[i + 1 < length ? i + 1 : i]);  
  149.             result.Add(Notes[i + 3 < length ? i + 3 : i]);  
  150.         }  
  151.   
  152.         return result;  
  153.     }  
  154.   
  155.     public List<int> OrderDesBy2Step()  
  156.     {  
  157.         List<int> result = new List<int>();  
  158.         Notes = Notes.OrderByDescending(a => a).ToArray();  
  159.         for (int i = length; i >= 0 && result.Count < length; i--)  
  160.         {  
  161.             result.Add(Notes[i - 1 > 0 ? i - 1 : i + 1]);  
  162.             result.Add(Notes[i - 3 > 0 ? i - 3 : i]);  
  163.         }  
  164.   
  165.         return result;  
  166.     }  
  167.   
  168.     public List<int> OrderAscBy3Step()  
  169.     {  
  170.         List<int> result = new List<int>();  
  171.         Notes = Notes.OrderBy(a => a).ToArray();  
  172.         for (int i = 0; i < length && result.Count < length; i++)  
  173.         {  
  174.             result.Add(Notes[i - 1 > 0 && i - 1 < length ? i - 1 : i]);  
  175.             result.Add(Notes[i + 4 < length ? i + 4 : 0]);  
  176.             result.Add(Notes[i + 2 < length ? i + 2 : i]);  
  177.         }  
  178.   
  179.         return result;  
  180.     }  
  181.   
  182.     public List<int> OrderDesBy3Step()  
  183.     {  
  184.         List<int> result = new List<int>();  
  185.         Notes = Notes.OrderByDescending(a => a).ToArray();  
  186.         for (int i = length; i >= 0 && result.Count < length; i--)  
  187.         {  
  188.             result.Add(Notes[i - 1 > 0 ? i - 1 : i + 1]);  
  189.             result.Add(Notes[i - 4 > 0 ? i - 4 : 0]);  
  190.             result.Add(Notes[i - 2 > 0 ? i - 2 : i]);  
  191.         }  
  192.   
  193.         return result;  
  194.     }  
  195.   
  196.     private List<int> OrderNotes()  
  197.     {  
  198.         List<int> result = new List<int>();  
  199.         Notes = Notes.OrderBy(a => Guid.NewGuid()).ToArray();  
  200.         int n = r.Next(1, 3);  
  201.         int mod = r.Next(3, 6);  
  202.         if (n == 1)  
  203.             for (int i = 1; i < length; i += 2)  
  204.             {  
  205.                 result.Add(Notes[i]);  
  206.                 result.Add(Notes[i - 1]);  
  207.                 if (i % mod == 1)  
  208.                     result.Add(0);  
  209.             }  
  210.   
  211.         if (n == 2)  
  212.             for (int i = 0; i < length; i++)  
  213.             {  
  214.                 if (i < length - 1)  
  215.                 {  
  216.                     result.Add(Notes[i + 1]);  
  217.                     result.Add(Notes[i]);  
  218.                     if (i % mod == 1)  
  219.                         result.Add(0);  
  220.                 }  
  221.             }  
  222.   
  223.         if (n == 3)  
  224.             for (int i = length - 1; i >= 0; i--)  
  225.             {  
  226.                 if (i > 1)  
  227.                 {  
  228.                     result.Add(Notes[i + 1]);  
  229.                     result.Add(Notes[i]);  
  230.                     if (i % mod == 1)  
  231.                         result.Add(0);  
  232.                 }  
  233.             }  
  234.   
  235.         return result;  
  236.     }  
  237.   
  238.     private List<int> Melody()  
  239.     {  
  240.         List<int> result = new List<int>();  
  241.         int n = r.Next(1, 4);  
  242.         int mod = r.Next(2, 6);  
  243.         for (int i = length - 1; i >= 0; i--)  
  244.         {  
  245.             for (int j = 0; j < i; j += 2)  
  246.             {  
  247.                 int r1 = r.Next(1, 2);  
  248.                 int r2 = r.Next(2, 3);  
  249.                 int r3 = r.Next(3, 4);  
  250.                 int r4 = r.Next(4, 5);  
  251.                 if (i + r2 < length && i + r2 > 0)  
  252.                     result.Add(Notes[i + r2]);  
  253.                 result.Add(Notes[i]);  
  254.                 if (i - r1 < length && i - r1 > 0)  
  255.                     result.Add(Notes[i - r1]);  
  256.                 if (i + r3 < length && i + r3 > 0)  
  257.                     result.Add(Notes[i + r3]);  
  258.                 if (i - 1 < length && i > 0)  
  259.                     result.Add(Notes[i - 1]);  
  260.                 if (i % mod == 0)  
  261.                     result.Add(0);  
  262.             }  
  263.         }  
  264.         return result;  
  265.     }  
  266.   
  267.     private List<int> Melody2()  
  268.     {  
  269.         List<int> result = new List<int>();  
  270.         Notes = Notes.OrderBy(a => Guid.NewGuid()).ToArray();  
  271.         int n = r.Next(1, 5);  
  272.         int mod = r.Next(2, 6);  
  273.         if (n == 1)  
  274.             for (int i = 0; i < length; i += 2)  
  275.             {  
  276.                 if (i - 2 > 0)  
  277.                     result.Add(Notes[i - 2]);  
  278.                 if (i + 1 < length)  
  279.                     result.Add(Notes[i + 1]);  
  280.                 result.Add(Notes[i]);  
  281.                 if (i + 2 < length)  
  282.                     result.Add(Notes[i + 2]);  
  283.                 if (i % mod == 1)  
  284.                     result.Add(0);  
  285.             }  
  286.   
  287.         if (n == 2)  
  288.             for (int i = 0; i < length; i += 3)  
  289.             {  
  290.                 if (i - 2 > 0)  
  291.                     result.Add(Notes[i - 2]);  
  292.                 if (i + 1 < length)  
  293.                     result.Add(Notes[i + 1]);  
  294.                 if (i + 3 < length)  
  295.                     result.Add(Notes[i + 3]);  
  296.                 result.Add(Notes[i]);  
  297.                 if (i + 2 < length)  
  298.                     result.Add(Notes[i + 2]);  
  299.                 if (i % mod == 1)  
  300.                     result.Add(0);  
  301.             }  
  302.         if (n == 3)  
  303.             for (int i = 0; i < length; i++)  
  304.             {  
  305.                 if (i + 3 < length)  
  306.                     result.Add(Notes[i + 3]);  
  307.                 result.Add(Notes[i]);  
  308.                 if (i + 2 < length)  
  309.                     result.Add(Notes[i + 2]);  
  310.                 if (i + 4 < length)  
  311.                     result.Add(Notes[i + 4]);  
  312.                 if (i + 1 < length)  
  313.                     result.Add(Notes[i + 1]);  
  314.                 if (i % mod == 1)  
  315.                     result.Add(0);  
  316.             }  
  317.         if (n == 4)  
  318.             for (int i = length - 1; i >= 0; i--)  
  319.             {  
  320.                 if (i - 3 > 0)  
  321.                     result.Add(Notes[i - 3]);  
  322.                 result.Add(Notes[i]);  
  323.                 if (i - 2 > 0)  
  324.                     result.Add(Notes[i - 2]);  
  325.                 if (i - 4 > 0)  
  326.                     result.Add(Notes[i - 4]);  
  327.                 if (i - 1 > 0)  
  328.                     result.Add(Notes[i - 1]);  
  329.                 if (i + 2 < length)  
  330.                     result.Add(Notes[i + 2]);  
  331.                 if (i % mod == 1)  
  332.                     result.Add(0);  
  333.             }  
  334.         return result;  
  335.     }  
  336.   
  337.     private List<int> Shuffle1()  
  338.     {  
  339.         List<int> result = new List<int>();  
  340.         Notes = Notes.OrderBy(a => Guid.NewGuid()).ToArray();  
  341.         int n = r.Next(1, 4);  
  342.         int mod = r.Next(2, 6);  
  343.         for (int i = 0; i < length; i++)  
  344.         {  
  345.             if (mod % 2 == 1)  
  346.                 for (int j = 0; j < i; j += 2)  
  347.                 {  
  348.                     int r1 = r.Next(1, 3);  
  349.                     int r2 = r.Next(1, 4);  
  350.                     int r3 = r.Next(3, 5);  
  351.                     int r4 = r.Next(4, 6);  
  352.                     if (i + r2 < length)  
  353.                         result.Add(Notes[i + r2]);  
  354.                     result.Add(Notes[i]);  
  355.                     if (i + r1 < length)  
  356.                         result.Add(Notes[i + r1]);  
  357.                     if (i + r3 < length)  
  358.                         result.Add(Notes[i + r3]);  
  359.                     if (i + 1 < length)  
  360.                         result.Add(Notes[i + 1]);  
  361.                     if (i % mod == 0)  
  362.                         result.Add(0);  
  363.                 }  
  364.             else  
  365.                 for (int j = 0; j < i; j++)  
  366.                 {  
  367.                     int r1 = r.Next(2, 3);  
  368.                     int r2 = r.Next(2, 4);  
  369.                     int r3 = r.Next(4, 5);  
  370.                     int r4 = r.Next(3, 6);  
  371.                     if (i + r2 < length)  
  372.                         result.Add(Notes[i + r2]);  
  373.                     result.Add(Notes[i]);  
  374.                     if (i + r1 < length)  
  375.                         result.Add(Notes[i + r1]);  
  376.                     if (i + r3 < length)  
  377.                         result.Add(Notes[i + r3]);  
  378.                     if (i + 1 < length)  
  379.                         result.Add(Notes[i + 1]);  
  380.                     if (i % mod == 1)  
  381.                         result.Add(0);  
  382.                 }  
  383.         }  
  384.         return result;  
  385.     }  
  386.   
  387.     private List<int> Shuffle2()  
  388.     {  
  389.         List<int> result = new List<int>();  
  390.         Notes = Notes.OrderBy(a => Guid.NewGuid()).ToArray();  
  391.         int n = r.Next(1, 4);  
  392.         int mod = r.Next(2, 6);  
  393.         for (int i = length - 1; i >= 0; i--)  
  394.         {  
  395.             for (int j = 0; j < i; j += 2)  
  396.             {  
  397.                 int r1 = r.Next(2, 3);  
  398.                 int r2 = r.Next(1, 3);  
  399.                 int r3 = r.Next(3, 6);  
  400.                 int r4 = r.Next(2, 5);  
  401.                 if (i - r2 < length && i - r2 > 0)  
  402.                     result.Add(Notes[i - r2]);  
  403.                 result.Add(Notes[i]);  
  404.                 if (i - r1 < length && i - r1 > 0)  
  405.                     result.Add(Notes[i - r1]);  
  406.                 if (i - r3 < length && i - r3 > 0)  
  407.                     result.Add(Notes[i - r3]);  
  408.                 if (i - 1 < length && i > 0)  
  409.                     result.Add(Notes[i - 1]);  
  410.                 if (i % mod == 0)  
  411.                     result.Add(0);  
  412.             }  
  413.         }  
  414.         return result;  
  415.     }  
  416.   
  417.     private List<int> Shuffle3()  
  418.     {  
  419.         List<int> result = new List<int>();  
  420.         int n = r.Next(1, 4);  
  421.         int mod = r.Next(2, 6);  
  422.         for (int i = 0; i < length; i++)  
  423.         {  
  424.             for (int j = i; j >= 0; j--)  
  425.             {  
  426.                 int r1 = r.Next(1, 4);  
  427.                 int r2 = r.Next(1, 3);  
  428.                 int r3 = r.Next(3, 6);  
  429.                 int r4 = r.Next(3, 5);  
  430.                 if (i + r2 < length)  
  431.                     result.Add(Notes[i + r2]);  
  432.                 result.Add(Notes[i]);  
  433.                 if (i + r1 < length)  
  434.                     result.Add(Notes[i + r1]);  
  435.                 if (i + r3 < length)  
  436.                     result.Add(Notes[i + r3]);  
  437.                 if (i + 1 < length)  
  438.                     result.Add(Notes[i + 1]);  
  439.                 if (i % mod == 1)  
  440.                     result.Add(0);  
  441.             }  
  442.         }  
  443.         return result;  
  444.     }  
  445. }  
  446.   
  447. public enum PlayTypeSign  
  448. {  
  449.     OrderNotes = 0,  
  450.     Melody = 1,  
  451.     Shuffle1 = 2,  
  452.     Shuffle2 = 3,  
  453.     Shuffle3 = 4,  
  454.     OrderAsc = 5,  
  455.     OrderDes = 6,  
  456.     OrderAscBy2Step = 7,  
  457.     OrderDesBy2Step = 8,  
  458.     OrderAscBy3Step = 9,  
  459.     OrderDesBy3Step = 10,  
  460.     OrderingStep3 = 11,  
  461.     OrderingStep3Backward = 12,  
  462.     GetByBase = 13,  
  463.     Melody2 = 14  
  464. }  
Topic 9 – Windows Form
 
Create a Windows form to hear the results by usage of design patterns like these void methods.
  1. private void BuildPlay()  
  2. {  
  3.     switch (comboBoxScaleNoteOrder.SelectedIndex)  
  4.     {  
  5.         case 0:  
  6.             TemplateGeneratorFactory desGnerator = new DescendingFactory();  
  7.             GenerateTemplate generatorDes = new GenerateTemplate(desGnerator);  
  8.             BuildScaleType(generatorDes);  
  9.             break;  
  10.         case 1:  
  11.             TemplateGeneratorFactory ascGnerator = new DescendingFactory();  
  12.             GenerateTemplate generatorAsc = new GenerateTemplate(ascGnerator);  
  13.             BuildScaleType(generatorAsc);  
  14.             break;  
  15.         default:  
  16.             break;  
  17.     }  
  18. }  
  19.   
  20. public void BuildScaleType(GenerateTemplate generator)  
  21. {  
  22.     switch (comboBoxScaleType.SelectedIndex)  
  23.     {  
  24.         case 0:  
  25.             GenerateByHeptaFactory(generator.GenerateHeptatonic());  
  26.             break;  
  27.         case 1:  
  28.             GenerateByPentaFactory(generator.GeneratePentatonic());  
  29.             break;  
  30.         default:  
  31.             break;  
  32.     }  
  33.   
  34. }  
  35.   
  36. public void GenerateByPentaFactory(PentatonicGenerator pentaGenerator)  
  37. {  
  38.     switch (comboBoxScalePos.SelectedIndex)  
  39.     {  
  40.         case 0:  
  41.             var scaleMinor = PentaMinor();  
  42.             List<Note.Note> noteListMinor = Note.NoteList.GetList(0, 9).Where(n => scaleMinor.Any(a => a == n.Name)).ToList();  
  43.             _currentPlay = pentaGenerator.Get4_4Templates(_tempoForBars, _templates[(int)numericTemplates.Value], noteListMinor, PlayOrderType());  
  44.             break;  
  45.         case 1:  
  46.             var scaleMajor = PentaMajor();  
  47.             List<Note.Note> noteListMajor = Note.NoteList.GetList(0, 9).Where(n => scaleMajor.Any(a => a == n.Name)).ToList();  
  48.             _currentPlay = pentaGenerator.Get3_4Templates(_tempoForBars, _templates[(int)numericTemplates.Value], noteListMajor, PlayOrderType());  
  49.             break;  
  50.         default:  
  51.             break;  
  52.     }  
  53. }  
  54.   
  55. public void GenerateByHeptaFactory(HeptatonicGenerator heptaGenerator)  
  56. {  
  57.     switch (comboBoxScalePos.SelectedIndex)  
  58.     {  
  59.         case 0:  
  60.             var scaleMinor = HeptaMinor();  
  61.             List<Note.Note> noteListMinor = Note.NoteList.GetList(0, 9).Where(n => scaleMinor.Any(a => a == n.Name)).ToList();  
  62.             _currentPlay = heptaGenerator.Get4_4Templates(_tempoForBars, _templates[(int)numericTemplates.Value], noteListMinor, PlayOrderType());  
  63.             break;  
  64.         case 1:  
  65.             var scaleMajor = HeptaMajor();  
  66.             List<Note.Note> noteListMajor = Note.NoteList.GetList(0, 9).Where(n => scaleMajor.Any(a => a == n.Name)).ToList();  
  67.             _currentPlay = heptaGenerator.Get3_4Templates(_tempoForBars, _templates[(int)numericTemplates.Value], noteListMajor, PlayOrderType());  
  68.             break;  
  69.         default:  
  70.             break;  
  71.     }  
  72. }  
  73.   
  74. public string[] HeptaMinor()  
  75. {  
  76.     switch (_scales[listBoxScale.SelectedIndex].Name)  
  77.     {  
  78.         case "C#":  
  79.             IMinorHeptatonicScale iMinorHeptaScaleC = new Minor_Scale_Hepta();  
  80.             C_Sharp_Minor_Scale_Hepta scaleC = new C_Sharp_Minor_Scale_Hepta(iMinorHeptaScaleC);  
  81.             if (comboBoxScaleNoteOrder.SelectedIndex == 0)  
  82.                 return scaleC.AscendingNotes;  
  83.             else  
  84.                 return scaleC.DescendingNotes;  
  85.         case "Hungarian":  
  86.             IMinorHeptatonicScale iMinorHeptaScaleHungarian = new Minor_Scale_Hepta();  
  87.             Hungarian_Minor_Scale_Hepta scaleHungarian = new Hungarian_Minor_Scale_Hepta(iMinorHeptaScaleHungarian);  
  88.             if (comboBoxScaleNoteOrder.SelectedIndex == 0)  
  89.                 return scaleHungarian.AscendingNotes;  
  90.             else  
  91.                 return scaleHungarian.DescendingNotes;  
  92.         case "Natural":  
  93.             IMinorHeptatonicScale iMinorHeptaScaleNatural = new Minor_Scale_Hepta();  
  94.             Natural_Minor_Scale_Hepta scaleNatural = new Natural_Minor_Scale_Hepta(iMinorHeptaScaleNatural);  
  95.             if (comboBoxScaleNoteOrder.SelectedIndex == 0)  
  96.                 return scaleNatural.AscendingNotes;  
  97.             else  
  98.                 return scaleNatural.DescendingNotes;  
  99.         case "Double Harmonic":  
  100.             IMinorHeptatonicScale iMinorHeptaScaleDouble = new Minor_Scale_Hepta();  
  101.             Double_Harmonic_Minor_Scale_Hepta scaleDouble = new Double_Harmonic_Minor_Scale_Hepta(iMinorHeptaScaleDouble);  
  102.             if (comboBoxScaleNoteOrder.SelectedIndex == 0)  
  103.                 return scaleDouble.AscendingNotes;  
  104.             else  
  105.                 return scaleDouble.DescendingNotes;  
  106.   
  107.         case "Persian":  
  108.             IMinorHeptatonicScale iMinorHeptaScalePersian = new Minor_Scale_Hepta();  
  109.             Persian_Minor_Scale_Hepta scalePersian = new Persian_Minor_Scale_Hepta(iMinorHeptaScalePersian);  
  110.             if (comboBoxScaleNoteOrder.SelectedIndex == 0)  
  111.                 return scalePersian.AscendingNotes;  
  112.             else  
  113.                 return scalePersian.DescendingNotes;  
  114.         case "":  
  115.   
  116.             break;  
  117.         default:  
  118.             break;  
  119.     }  
  120.     return null;  
  121. }  

Summary

 
In this article, we learned how to write music theories into C# using design patterns and how to create a create rhythm bars melodies generator at run time.