Advertisement
TimmyChannel

C# Console Song

Jun 23rd, 2022
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.38 KB | None | 0 0
  1. Song.Play(Song.Mary);
  2. public static class Song
  3.     {
  4.       public static Note[] Mary =
  5.        {
  6.         new Note(Tone.B, Duration.QUARTER),
  7.         new Note(Tone.A, Duration.QUARTER),
  8.         new Note(Tone.C, Duration.QUARTER),
  9.         new Note(Tone.A, Duration.QUARTER),
  10.         new Note(Tone.B, Duration.QUARTER),
  11.         new Note(Tone.B, Duration.QUARTER),
  12.         new Note(Tone.B, Duration.HALF),
  13.         new Note(Tone.A, Duration.QUARTER),
  14.         new Note(Tone.A, Duration.QUARTER),
  15.         new Note(Tone.A, Duration.HALF),
  16.         new Note(Tone.B, Duration.QUARTER),
  17.         new Note(Tone.D, Duration.QUARTER),
  18.         new Note(Tone.D, Duration.HALF)
  19.         };
  20.       public static void Play(Note[] tune)
  21.       {
  22.         foreach (Note n in tune)
  23.         {
  24.           if (n.NoteTone == Tone.REST)
  25.             Thread.Sleep((int)n.NoteDuration);
  26.           else
  27.           {
  28.             Console.Beep((int)n.NoteTone, (int)n.NoteDuration);
  29.             Console.WriteLine(n);
  30.           }
  31.         }
  32.       }
  33.  
  34.       // Define the frequencies of notes in an octave, as well as
  35.       // silence (rest).
  36.       public enum Tone
  37.       {
  38.         REST = 0,
  39.         GbelowC = 196,
  40.         A = 220,
  41.         Asharp = 233,
  42.         B = 247,
  43.         C = 262,
  44.         Csharp = 277,
  45.         D = 294,
  46.         Dsharp = 311,
  47.         E = 330,
  48.         F = 349,
  49.         Fsharp = 370,
  50.         G = 392,
  51.         Gsharp = 415,
  52.       }
  53.  
  54.       // Define the duration of a note in units of milliseconds.
  55.       public enum Duration
  56.       {
  57.         WHOLE = 1600,
  58.         HALF = WHOLE >> 1,
  59.         QUARTER = HALF >> 1,
  60.         EIGHTH = QUARTER >> 1,
  61.         SIXTEENTH = EIGHTH >> 1,
  62.       }
  63.  
  64.       // Define a note as a frequency (tone) and the amount of
  65.       // time (duration) the note plays.
  66.       public struct Note
  67.       {
  68.         Tone toneVal;
  69.         Duration durVal;
  70.  
  71.         // Define a constructor to create a specific note.
  72.         public Note(Tone frequency, Duration time)
  73.         {
  74.           toneVal = frequency;
  75.           durVal = time;
  76.         }
  77.  
  78.         // Define properties to return the note's tone and duration.
  79.         public Tone NoteTone { get { return toneVal; } }
  80.         public Duration NoteDuration { get { return durVal; } }
  81.         public override string ToString()
  82.         {
  83.           return $"Tone: {toneVal} Durration: {durVal}";
  84.         }
  85.       }
  86.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement