/
PCSpeaker.cs
122 lines (110 loc) · 3.73 KB
/
PCSpeaker.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cosmos.Core;
namespace Cosmos.HAL
{
public static class SpeakerExtensions
{
public static uint MsToHz(this int ms)
{
return (uint)(1000 / ms);
}
public static uint MsToHz(this uint ms)
{
return (uint)(1000 / ms);
}
}
public class PCSpeaker
{
protected static Core.IOGroup.PCSpeaker IO = BaseIOGroups.PCSpeaker;
private static PIT SpeakerPIT = new PIT();
/// <summary>
/// Enable sound.
/// </summary>
private static void EnableSound()
{
IO.Gate.Byte = (byte)(IO.Gate.Byte | 0x03);
}
/// <summary>
/// Disable sound.
/// </summary>
private static void DisableSound()
{
IO.Gate.Byte = (byte)(IO.Gate.Byte & ~3);
//IO.Port61.Byte = (byte)(IO.Port61.Byte | 0xFC);
}
/// <summary>
/// Play beep sound, at a specified frequency for a specified duration.
/// </summary>
/// <param name="frequency">Audio frequency in Hz, must be between 37 and 32767Hz.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown if frequency is invalid.</exception>
public static void Beep(uint frequency)
{
if (frequency < 37 || frequency > 32767)
{
throw new ArgumentOutOfRangeException("Frequency must be between 37 and 32767Hz");
}
uint divisor = 1193180 / frequency;
byte temp;
IO.CommandRegister.Byte = 0xB6;
IO.Channel2Data.Byte = (byte)(divisor & 0xFF);
IO.Channel2Data.Byte = (byte)((divisor >> 8) & 0xFF);
temp = IO.Gate.Byte;
if (temp != (temp | 3))
{
IO.Gate.Byte = (byte)(temp | 3);
}
EnableSound();
}
// TODO: continue exception list, once HAL is documented.
/// <summary>
/// Play beep sound, at a specified frequency for a specified duration.
/// </summary>
/// <param name="frequency">Audio frequency in Hz, must be between 37 and 32767Hz.</param>
/// <param name="duration">Beep duration, must be > 0.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown if duration or frequency invalid.</exception>
public static void Beep(uint frequency, uint duration)
{
if (duration <= 0)
{
throw new ArgumentOutOfRangeException("Duration must be more than 0");
}
Beep(frequency);
SpeakerPIT.Wait(duration);
DisableSound();
}
}
/*
public class PCSpeaker
{
public void playSound(UInt32 nFrequence)
{
UInt32 Div;
UInt16 tmp;
//Set the PIT to the desired frequency
Div = 1193180 / nFrequence;
BaseIOGroups.PCSpeaker.Port43.Byte = (byte)0xB6;
BaseIOGroups.PCSpeaker.Port42.Byte = (byte)Div;
BaseIOGroups.PCSpeaker.Port42.Byte = (byte)(Div >> 8);
tmp = BaseIOGroups.PCSpeaker.Port61.Byte;
if (tmp != (tmp | 3))
{
BaseIOGroups.PCSpeaker.Port61.Byte = (byte)(tmp | 3);
}
}
public void nosound()
{
byte tmp = (byte)(BaseIOGroups.PCSpeaker.Port61.Byte & 0xFC);
BaseIOGroups.PCSpeaker.Port61.Byte = tmp;
}
public void beep()
{
playSound(1000);
Int32 div = 1193180;
BaseIOGroups.PCSpeaker.Port42.Byte = (byte)div;
//PIT.PITFrequency = 1193180;
}
}*/
}