-
Notifications
You must be signed in to change notification settings - Fork 0
/
SoundEmu.cs
105 lines (100 loc) · 2.44 KB
/
SoundEmu.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System;
using System.Threading;
using System.Collections.Generic;
using System.Text;
namespace CPUTing
{
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,
}
public enum Duration
{
WHOLE = 1600,
HALF = WHOLE / 2,
QUARTER = HALF / 2,
EIGHTH = QUARTER / 2,
SIXTEENTH = EIGHTH / 2,
}
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 class SoundEmu
{
bool[] Voices = new bool[4];
Thread[] VoiceThreads = new Thread[4];
public SoundEmu()
{
for (int i = 0; i < VoiceThreads.Length; i++)
{
Voices[i] = false;
VoiceThreads[i] = new Thread(new ThreadStart(Play));
}
}
public void UpdateVoices()
{
for (int i = 0; i < Voices.Length; i++)
{
if (Voices[i] == true)
{
VoiceThreads[i].Start();
}
}
}
public void SetVoice(int index)
{
Voices[index] = !Voices[index];
}
void Play()
{
for (int i = 0; i < Voices.Length; i++)
{
if (Voices[i] == true)
{
Console.Beep(800, 200);
}
}
}
public void Play(int index)
{
if (Voices[index] == true)
{
Console.Beep(800, 200);
}
}
public void PlayNotes(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);
}
}
}
}