|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | +using Sys = Cosmos.System; |
| 5 | +using System.Threading; |
| 6 | + |
| 7 | +namespace BeepDemo |
| 8 | +{ |
| 9 | + public class Kernel: Sys.Kernel |
| 10 | + { |
| 11 | + protected override void BeforeRun() |
| 12 | + { |
| 13 | + Console.WriteLine("Cosmos booted successfully. Type a line of text to get it echoed back."); |
| 14 | + } |
| 15 | + protected override void Run() |
| 16 | + { |
| 17 | + Console.WriteLine("Run 'Mary Had a Little Lamb'? "); |
| 18 | + string ans = Console.ReadLine(); |
| 19 | + if (ans.ToLower() == "y" || ans.ToLower() == "yes") |
| 20 | + { |
| 21 | + BeepTest.Main(); |
| 22 | + } |
| 23 | + else |
| 24 | + { |
| 25 | + Console.WriteLine("Default beep:"); |
| 26 | + Console.Beep(); |
| 27 | + // Does the follwing: Console.Beep((int)Sys.Notes.Default (800 hertz), (int)Sys.Durations.Default (200 milliseconds) ); |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + class BeepTest |
| 32 | + { |
| 33 | + public static void Main() |
| 34 | + { |
| 35 | + // Declare the first few notes of the song, "Mary Had A Little Lamb". |
| 36 | + Note[] Mary = |
| 37 | + { |
| 38 | + new Note(Tone.B, Duration.QUARTER), |
| 39 | + new Note(Tone.A, Duration.QUARTER), |
| 40 | + new Note(Tone.GbelowC, Duration.QUARTER), |
| 41 | + new Note(Tone.A, Duration.QUARTER), |
| 42 | + new Note(Tone.B, Duration.QUARTER), |
| 43 | + new Note(Tone.B, Duration.QUARTER), |
| 44 | + new Note(Tone.B, Duration.HALF), |
| 45 | + new Note(Tone.A, Duration.QUARTER), |
| 46 | + new Note(Tone.A, Duration.QUARTER), |
| 47 | + new Note(Tone.A, Duration.HALF), |
| 48 | + new Note(Tone.B, Duration.QUARTER), |
| 49 | + new Note(Tone.D, Duration.QUARTER), |
| 50 | + new Note(Tone.D, Duration.HALF) |
| 51 | + }; |
| 52 | + // Play the song |
| 53 | + Play(Mary); |
| 54 | + } |
| 55 | + |
| 56 | + // Play the notes in a song. |
| 57 | + protected static void Play(Note[] tune) |
| 58 | + { |
| 59 | + foreach (Note n in tune) |
| 60 | + { |
| 61 | + if (n.NoteTone == Tone.REST) |
| 62 | + { |
| 63 | + Thread.Sleep((int)n.NoteDuration); |
| 64 | + } |
| 65 | + else |
| 66 | + { |
| 67 | + Console.Beep((int)n.NoteTone, (int)n.NoteDuration); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // Define the frequencies of notes in an octave, as well as |
| 73 | + // silence (rest). |
| 74 | + protected enum Tone |
| 75 | + { |
| 76 | + REST = 0, |
| 77 | + GbelowC = 196, |
| 78 | + A = 220, |
| 79 | + Asharp = 233, |
| 80 | + B = 247, |
| 81 | + C = 262, |
| 82 | + Csharp = 277, |
| 83 | + D = 294, |
| 84 | + Dsharp = 311, |
| 85 | + E = 330, |
| 86 | + F = 349, |
| 87 | + Fsharp = 370, |
| 88 | + G = 392, |
| 89 | + Gsharp = 415, |
| 90 | + } |
| 91 | + |
| 92 | + // Define the duration of a note in units of milliseconds. |
| 93 | + protected enum Duration |
| 94 | + { |
| 95 | + WHOLE = 1600, |
| 96 | + HALF = WHOLE / 2, |
| 97 | + QUARTER = HALF / 2, |
| 98 | + EIGHTH = QUARTER / 2, |
| 99 | + SIXTEENTH = EIGHTH / 2, |
| 100 | + } |
| 101 | + |
| 102 | + // Define a note as a frequency (tone) and the amount of |
| 103 | + // time (duration) the note plays. |
| 104 | + protected struct Note |
| 105 | + { |
| 106 | + Tone toneVal; |
| 107 | + Duration durVal; |
| 108 | + |
| 109 | + // Define a constructor to create a specific note. |
| 110 | + public Note(Tone frequency, Duration time) |
| 111 | + { |
| 112 | + toneVal = frequency; |
| 113 | + durVal = time; |
| 114 | + } |
| 115 | + |
| 116 | + // Define properties to return the note's tone and duration. |
| 117 | + public Tone NoteTone { get { return toneVal; } } |
| 118 | + public Duration NoteDuration { get { return durVal; } } |
| 119 | + } |
| 120 | + } |
| 121 | + /* |
| 122 | + This example produces the following results: |
| 123 | +
|
| 124 | + This example plays the first few notes of "Mary Had A Little Lamb" |
| 125 | + through the computer PC Speaker. |
| 126 | + */ |
| 127 | +} |
0 commit comments