Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Song.Play(Song.Mary);
- public static class Song
- {
- public static Note[] Mary =
- {
- new Note(Tone.B, Duration.QUARTER),
- new Note(Tone.A, Duration.QUARTER),
- new Note(Tone.C, Duration.QUARTER),
- new Note(Tone.A, Duration.QUARTER),
- new Note(Tone.B, Duration.QUARTER),
- new Note(Tone.B, Duration.QUARTER),
- new Note(Tone.B, Duration.HALF),
- new Note(Tone.A, Duration.QUARTER),
- new Note(Tone.A, Duration.QUARTER),
- new Note(Tone.A, Duration.HALF),
- new Note(Tone.B, Duration.QUARTER),
- new Note(Tone.D, Duration.QUARTER),
- new Note(Tone.D, Duration.HALF)
- };
- public static void Play(Note[] tune)
- {
- foreach (Note n in tune)
- {
- if (n.NoteTone == Tone.REST)
- Thread.Sleep((int)n.NoteDuration);
- else
- {
- Console.Beep((int)n.NoteTone, (int)n.NoteDuration);
- Console.WriteLine(n);
- }
- }
- }
- // Define the frequencies of notes in an octave, as well as
- // silence (rest).
- public enum Tone
- {
- REST = 0,
- GbelowC = 196,
- A = 220,
- Asharp = 233,
- B = 247,
- C = 262,
- Csharp = 277,
- D = 294,
- Dsharp = 311,
- E = 330,
- F = 349,
- Fsharp = 370,
- G = 392,
- Gsharp = 415,
- }
- // Define the duration of a note in units of milliseconds.
- public enum Duration
- {
- WHOLE = 1600,
- HALF = WHOLE >> 1,
- QUARTER = HALF >> 1,
- EIGHTH = QUARTER >> 1,
- SIXTEENTH = EIGHTH >> 1,
- }
- // Define a note as a frequency (tone) and the amount of
- // time (duration) the note plays.
- public struct Note
- {
- Tone toneVal;
- Duration durVal;
- // Define a constructor to create a specific note.
- public Note(Tone frequency, Duration time)
- {
- toneVal = frequency;
- durVal = time;
- }
- // Define properties to return the note's tone and duration.
- public Tone NoteTone { get { return toneVal; } }
- public Duration NoteDuration { get { return durVal; } }
- public override string ToString()
- {
- return $"Tone: {toneVal} Durration: {durVal}";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement