-
Notifications
You must be signed in to change notification settings - Fork 1
/
AsyncBeep.pas
executable file
·170 lines (146 loc) · 4.54 KB
/
AsyncBeep.pas
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
{ **************************************************************************** }
{ AsyncBeep.pas Copyright © 2001 DithoSoft Software Solutions }
{ Version 1.0 http://www.dithosoft.de }
{ support@dithosoft.de }
{ ---------------------------------------------------------------------------- }
{ This component wraps the Windows API beep function so that it can be used }
{ asynchronously. }
{ **************************************************************************** }
unit AsyncBeep;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
type
TAsyncBeep = class;
{ The thread that checks of queued beeps }
TBeepThread = class(TThread)
protected
FAsyncBeep: TAsyncBeep;
procedure Execute; override;
procedure DeleteFirstBeep;
end;
{ The asynchronous beep component }
TAsyncBeep = class(TComponent)
private
FBeepQueue: TList;
FBeepThread: TBeepThread;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure DoBeep(Pitch, Duration: Integer);
end;
procedure Register;
implementation
uses typinfo;
resourcestring
SPitchError = 'Pitch must be between 37 and 32767';
SDurationError = 'Duration must be greater than 0';
type
{ An object that holds the information about the beep }
TBeepObject = class
private
FPitch: Integer;
FDuration: Integer;
public
constructor Create(Pitch, Duration: Integer);
property Pitch: Integer read FPitch;
property Duration: Integer read FDuration;
end;
{
Create
Creates a new beep information object.
}
constructor TBeepObject.Create(Pitch, Duration: Integer);
begin
FPitch := Pitch;
FDuration := Duration;
end;
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
{
Create
Creates a new AsyncBeep component.
}
constructor TAsyncBeep.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FBeepQueue := TList.Create;
FBeepThread := TBeepThread.Create(True);
FBeepThread.FAsyncBeep := self;
FBeepThread.Resume;
end;
{
Destroy
Destroy the AsyncBeep component.
}
destructor TAsyncBeep.Destroy;
var
i: Integer;
begin
// Destroy the thread first (this waits until an active beep is finished)
FBeepThread.Free;
// Now we remove all pending beeps and destroy the list
try
for i := FBeepQueue.Count-1 downto 0 do TBeepObject(FBeepQueue[i]).Free;
finally
FBeepQueue.Free;
inherited;
end;
end;
{
DoBeep
Queues a beep sound and returns.
}
procedure TAsyncBeep.DoBeep(Pitch, Duration: Integer);
begin
// Check if pitch and duration are within valid bounds
if (Pitch < 37) or (Pitch > 32767) then raise EPropertyError.Create(SPitchError);
if (Duration < 0) then raise EPropertyError.Create(SDurationError);
// If so, add a new item to the beep queue
FBeepQueue.Add(TBeepObject.Create(Pitch,Duration));
end;
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
{
Execute
The thread's execution loop.
}
procedure TBeepThread.Execute;
var
aBeep: TBeepObject;
begin
// Loop until the thread is terminated
while not Terminated do
begin
// If there are beep objects in the queue
if Assigned(FAsyncBeep) and (FAsyncBeep.FBeepQueue.Count > 0) then
begin
// Get the object, remove it and do the beep
aBeep := TBeepObject(FAsyncBeep.FBeepQueue[0]);
Synchronize(DeleteFirstBeep);
Windows.Beep(aBeep.Pitch,aBeep.Duration);
aBeep.Free;
end;
end;
end;
{
DeleteFirstBeep
Removes the first object in the BeepQueue
}
procedure TBeepThread.DeleteFirstBeep;
begin
// Remove the first beep object from the queue
if Assigned(FAsyncBeep) and (FAsyncBeep.FBeepQueue.Count > 0) then
FAsyncBeep.FBeepQueue.Delete(0);
end;
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
{
Register
Registers the component with the Delphi IDE.
}
procedure Register;
begin
RegisterComponents('Freeware', [TAsyncBeep]);
end;
end.