-
Notifications
You must be signed in to change notification settings - Fork 44
/
ACMOut.pas
193 lines (167 loc) · 4.7 KB
/
ACMOut.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
unit ACMOut;
interface
uses
Windows, Messages, Classes, Sysutils, ACMConvertor, MMSystem, MSACM;
type
EACMOut = class(Exception);
TACMOut = class(TComponent)
private
{ Private declarations }
FActive : Boolean;
FNumBuffersLeft : Byte;
FBackBufferList : TList;
FNumBuffers : Byte;
FBufferList : TList;
FFormat : TACMWaveFormat;
FOnBufferPlayed : TNotifyEvent;
FWaveOutHandle : HWaveOut;
FWindowHandle : HWnd;
protected
{ Protected declarations }
function NewHeader : PWaveHDR;
procedure DisposeHeader(Header : PWaveHDR);
procedure DoWaveDone(Header : PWaveHdr);
procedure WndProc(var Message : TMessage);
public
{ Public declarations }
constructor Create(AOwner : TComponent); override;
destructor Destroy; override;
procedure Close;
procedure Open(aFormat : TACMWaveFormat);
procedure Play(var Buffer; Size : Integer);
procedure RaiseException(const aMessage : String; Result : Integer);
property Active : Boolean
read FActive;
property Format : TACMWaveFormat
read FFormat;
property WindowHandle : HWnd
read FWindowHandle;
published
{ Published declarations }
property NumBuffers : Byte
read FNumBuffers
write FNumBuffers;
property OnBufferPlayed : TNotifyEvent
read FOnBufferPlayed
write FOnBufferPlayed;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Sound', [TACMOut]);
end;
{ TACMOut }
procedure TACMOut.Close;
var
X : Integer;
begin
if not Active then exit;
FActive := False;
WaveOutReset(FWaveOutHandle);
WaveOutClose(FWaveOutHandle);
FBackBufferList.Clear;
FWaveOutHandle := 0;
For X:=FBufferList.Count-1 downto 0 do DisposeHeader(PWaveHDR(FBufferList[X]));
end;
constructor TACMOut.Create(AOwner: TComponent);
begin
inherited;
FBufferList := TList.Create;
FBackBufferList := TList.Create;
FActive := False;
FWindowHandle := AllocateHWND(WndProc);
FWaveOutHandle := 0;
FNumBuffers := 4;
end;
destructor TACMOut.Destroy;
begin
if Active then Close;
FBufferList.Free;
DeAllocateHWND(FWindowHandle);
FBackBufferList.Free;
inherited;
end;
procedure TACMOut.DisposeHeader(Header: PWaveHDR);
var
X : Integer;
begin
X := FBufferList.IndexOf(Header);
if X < 0 then exit;
Freemem(header.lpData);
Freemem(header);
FBufferList.Delete(X);
end;
procedure TACMOut.DoWaveDone(Header : PWaveHdr);
var
Res : Integer;
begin
if not Active then exit;
Res := WaveOutUnPrepareHeader(FWaveOutHandle, Header, SizeOf(TWaveHDR));
if Res <> 0 then RaiseException('WaveOut-UnprepareHeader',Res);
DisposeHeader(Header);
if Assigned(FOnBufferPlayed) then FOnBufferPlayed(Self);
end;
function TACMOut.NewHeader: PWaveHDR;
begin
GetMem(Result, SizeOf(TWaveHDR));
FBufferList.Add(Result);
end;
procedure TACMOut.Open(aFormat: TACMWaveFormat);
var
Res : Integer;
begin
if Active then exit;
FNumBuffersLeft := FNumBuffers;
FFormat := aFormat;
Res := WaveOutOpen(@FWaveOutHandle,0,@FFormat.Format,WindowHandle,0,CALLBACK_WINDOW or WAVE_MAPPED);
if Res <> 0 then RaiseException('WaveOutOpen',Res);
FActive := True;
end;
procedure TACMOut.Play(var Buffer; Size: Integer);
var
TempHeader : PWaveHdr;
Data : Pointer;
Res : Integer;
X : Integer;
procedure PlayHeader(Header : PWaveHDR);
begin
Res := WaveOutPrepareHeader(FWaveOutHandle,Header,SizeOf(TWaveHDR));
if Res <> 0 then RaiseException('WaveOut-PrepareHeader',Res);
Res := WaveOutWrite(FWaveOutHandle, Header, SizeOf(TWaveHDR));
if Res <> 0 then RaiseException('WaveOut-Write',Res);
end;
begin
if Size = 0 then exit;
if not active then exit;
TempHeader := NewHeader;
GetMem(Data, Size);
Move(Buffer,Data^,Size);
with TempHeader^ do begin
lpData := Data;
dwBufferLength := Size;
dwBytesRecorded := Size;
dwUser := 0;
dwFlags := 0;
dwLoops := 0;
end;
if FNumBuffersLeft > 0 then begin
FBackBufferList.Add(TempHeader);
Dec(FNumBuffersLeft);
end else begin
for X:=0 to FBackBufferList.Count-1 do
PlayHeader(PWaveHDR(FBackBufferList[X]));
FBackBufferList.Clear;
PlayHeader(TempHeader);
end;
end;
procedure TACMOut.RaiseException(const aMessage: String; Result: Integer);
begin
end;
procedure TACMOut.WndProc(var Message: TMessage);
begin
case Message.Msg of
MM_WOM_DONE : DoWaveDone(PWaveHDR(Message.LParam));
end;
end;
end.