-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecThd.pas
More file actions
360 lines (341 loc) · 11.9 KB
/
RecThd.pas
File metadata and controls
360 lines (341 loc) · 11.9 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
unit RecThd;
interface
uses
Classes, SysUtils, Windows, Messages, MMSystem, WaveUtil, RecordFrm, Dialogs,
syncobjs;
const
WM_RQUIT = WM_USER + 111;
WM_RDATA = WM_USER + 112;
WM_RSTARTED = WM_USER + 113;
ifSignal = $01;
ifLoud = $02;
ifClip = $04;
ifStop = $08;
ifCanWrite = $10;
ifError = $80;
INITIAL_SECTORSTATSSIZE = 300 * 75; // 5 minutes
GROW_SECTORSTATSSIZE = INITIAL_SECTORSTATSSIZE;
type
TWaveRecordThd = class(TThread)
private
hWnd: THandle;
FSectorStatsList: PSectorStatsList;
FSectorStatsHandle: HGLOBAL;
FSectorStatsSize: Cardinal;
SectorStatsCapacity: Cardinal;
FSectorSize: Cardinal;
WaveFile: HMMIO;
RecOffset: Cardinal;
RecSilent: Cardinal;
Recorded: Cardinal;
SectorStatsCarry: Cardinal;
IsReady: TEvent;
ckOutRIFF, ckOut: TMMCKInfo;
FWaveFormat: PWaveFormatEx;
RecSettings:TRecSettings;
FStopped: Boolean;
CanWrite: Boolean;
procedure MMWimData(wParam: HWaveIn; lParam: PWaveHdr);
procedure SetSectorStatsCapacity(NewCapacity: Cardinal);
protected
procedure Execute; override;
public
constructor Create(AWindow: THandle; AFileName: string;
const AFormat: PWaveFormatEx; const ASettings: TRecSettings;
ASectorStatsHandle: HGLOBAL; ASectorStatsSize: Cardinal);
destructor Destroy; override;
procedure Quit;
property Stopped: Boolean write FStopped;
property RecordMode: TRecordMode read RecSettings.RecordMode;
property SectorStatsHandle: HGLOBAL read FSectorStatsHandle;
property SectorStatsSize: Cardinal read FSectorStatsSize;
property SectorSize: Cardinal read FSectorSize;
property WaveFormat: PWaveFormatEx read FWaveFormat;
property RecordedBytes: Cardinal read Recorded;
end;
procedure WaveInCallback(WaveIn: HWAVEIN; wMsg: UINT;
dwInstance, dwParam1, dwParam2: DWORD); stdcall;
implementation
resourcestring
NoFileStr = 'Cannot start recording (blank filename).';
NoRiffStr = 'Cannot create RIFF chunk.';
NoCrFmtStr = 'Cannot create fmt chunk.';
NoWriteFmtStr = 'Cannot write to fmt chunk';
NoAscWriteStr = 'Cannot ascend/write.';
NoDataStr = 'Cannot create data chunk.';
NoRiffStr2 = 'Cannot open RIFF chunk, file is not a valid WAVE file.';
NoDescStr = 'Cannot descend into data part.';
NoAllocMemStr = 'Unable to allocate memory, code: ';
NoSeekStr = 'Cannot seek on file.';
TimeOutStr = 'Semaphore timeout. Recording thread not ready?';
ZombieStr = 'PostThreadMessage() failed. May have created zombie thread.';
procedure WaveInCallback(WaveIn: HWAVEIN; wMsg: UINT;
dwInstance, dwParam1, dwParam2: DWORD); stdcall;
begin
PostThreadMessage(dwInstance, wMsg, WaveIn, dwParam1);
end;
{ TWaveRecordThd }
procedure TWaveRecordThd.Execute;
var Msg: TMSG;
{$IFDEF LOGGING}
f: TextFile;
{$ENDIF}
begin
try
{$IFDEF LOGGING}
Assign(f, 'c:\cdwavrec.log');
ReWrite(f);
Writeln(f, 'Init');
{$ENDIF}
IsReady.SetEvent;
while not Terminated and GetMessage(Msg, 0, 0, 0) do
begin
case Msg.message of
MM_WIM_DATA: begin
{$IFDEF LOGGING}
Writeln(f, 'MM_WIM_DATA ', msg.wParam);
{$ENDIF}
MMWimData(msg.wParam, PWaveHdr(msg.lParam));
end;
{$IFDEF LOGGING}
MM_WIM_OPEN: begin
Writeln(f, 'MM_WIM_OPEN ', msg.wParam);
// PostMessage(hWnd, Msg.Message, msg.wParam, msg.lParam);
end;
{$ENDIF}
MM_WIM_CLOSE: begin
{$IFDEF LOGGING}
Writeln(f, 'MM_WIM_CLOSE ', msg.wParam);
{$ENDIF}
Terminate;
end;
WM_RQUIT: begin
{$IFDEF LOGGING}
Writeln(f, 'WM_RQUIT');
{$ENDIF}
Terminate;
end;
end{case};
end;
{$IFDEF LOGGING}
WriteLn(f, 'done');
{$ENDIF}
finally
{$IFDEF LOGGING}
close(f);
{$ENDIF}
if Wavefile <> HMMIO(INVALID_HANDLE_VALUE) then
begin
mmioAscendSafe(WaveFile, ckOut, Recorded);
mmioAscendSafe(WaveFile, ckOutRIFF, ckOut.dwDataOffset + Recorded - ckOutRIFF.dwDataOffset);
mmioClose(WaveFile, 0);
WaveFile := HMMIO(INVALID_HANDLE_VALUE);
end;
if (FSectorStatsList <> nil) then
begin
GlobalUnlock(FSectorStatsHandle);
FSectorStatsList := nil;
FSectorStatsHandle := GlobalReAlloc(
FSectorStatsHandle,
((Recorded - 1) div SectorSize + 1) * SizeOf(TSectorStats),
GMEM_MOVEABLE);
end;
end;
end;
constructor TWaveRecordThd.Create;
var
Res: MMRESULT;
begin
inherited Create(True);
IsReady := TEvent.Create(nil, True, False, '');
hWnd := AWindow;
FWaveFormat := AFormat;
FSectorSize := (AFormat.nAvgBytesPerSec div (75 * AFormat.nBlockAlign)) * AFormat.nBlockAlign;
RecSettings := ASettings;
WaveFile := HMMIO(INVALID_HANDLE_VALUE);
Priority := tpHighest;
if (RecSettings.RecordMode in [rmFile, rmAppend]) then
begin
if (AFileName = '') then Raise Exception.Create(NoFileStr);
end;
try
case RecSettings.RecordMode of
rmFile: begin
WaveFile := OpenWaveFile(AFileName, nil, GENERIC_WRITE, CREATE_ALWAYS);
ckOutRIFF.fccType := mmioStringToFOURCC('WAVE', 0);
if mmioCreateChunk(Wavefile, @ckOutRIFF, MMIO_CREATERIFF) <> 0 then
raise Exception.Create(NoRiffStr);
ckOut.ckid := mmioStringToFOURCC('fmt ',0);
if AFormat.wFormatTag = WAVE_FORMAT_PCM then
ckOut.ckSize := sizeof(TWaveFormatEx) - 2
else
ckOut.ckSize := sizeof(TWaveFormatEx) + AFormat.cbSize;
if mmioCreateChunk(WaveFile, @ckOut, 0) <> 0 then
raise Exception.Create(NoCrFmtStr);
if mmioWrite(WaveFile, PChar(AFormat), ckOut.ckSize) <> Integer(ckOut.ckSize) then
raise Exception.Create(NoWriteFmtStr);
if mmioAscend(WaveFile, @ckOut, 0) <> 0 then
raise Exception.Create(NoAscWriteStr);
ckOut.ckid := mmioStringToFOURCC('data',0);
if mmioCreateChunk(Wavefile, @ckOut, 0) <> 0 then
raise Exception.Create(NoDataStr);
end;
rmAppend: begin
WaveFile := OpenWaveFile(AFileName, nil, GENERIC_READ or GENERIC_WRITE, OPEN_EXISTING);
ckOutRIFF.fccType := mmioStringToFOURCC('WAVE', 0);
res := mmioDescend(WaveFile, @ckOutRIFF, nil, MMIO_FINDRIFF);
if (res <> 0) then
raise Exception.Create(NoRiffStr2);
ckOut.ckid := mmioStringToFOURCC('data',0);
if mmioDescend(WaveFile, @ckOut, @ckOutRIFF, MMIO_FINDCHUNK)<>0 then
raise Exception.Create(NoDescStr);
ckOutRIFF.dwFlags := ckOutRIFF.dwFlags or MMIO_DIRTY;
ckOut.dwFlags := ckOut.dwFlags or MMIO_DIRTY;
end;
rmTest: begin
end;
end;
SectorStatsCarry := 0;
FSectorStatsSize := 0;
CanWrite := not RecSettings.StartOnVoice;
RecOffset:= 0;
RecSilent:= 0;
Recorded := 0;
if (ASectorStatsHandle = 0) then
begin
SectorStatsCapacity := INITIAL_SECTORSTATSSIZE;
FSectorStatsHandle := GlobalAlloc(GHND, SectorStatsCapacity * SizeOf(TSectorStats));
if SectorStatsHandle = 0 then
raise Exception.Create(NoAllocMemStr + IntToStr(GetLastError()));
end else begin
FSectorStatsHandle := ASectorStatsHandle;
SectorStatsCapacity := GlobalSize(ASectorStatsHandle) div SizeOf(TSectorStats);
FSectorStatsSize := ASectorStatsSize;
Recorded := ASectorStatsSize * SectorSize;
if mmioSeek(WaveFile, Recorded, SEEK_CUR) = -1 then
raise Exception.Create(NoSeekStr); // jump to end...
end;
FSectorStatsList := PSectorStatsList(GlobalLock(SectorStatsHandle));
Resume;
try
if IsReady.WaitFor(10240) <> wrSignaled then
raise Exception.Create(TimeOutStr);
except
GlobalUnlock(FSectorStatsHandle);
FSectorStatsList := nil;
FSectorStatsHandle := GlobalFree(FSectorStatsHandle);
raise;
end;
except
if WaveFile <> 0 then mmioClose(WaveFile, 0);
WaveFile := HMMIO(INVALID_HANDLE_VALUE);
raise;
end;
end;
destructor TWaveRecordThd.Destroy;
begin
if Wavefile <> HMMIO(INVALID_HANDLE_VALUE) then
begin
mmioAscendSafe(WaveFile, ckOut, Recorded);
mmioAscendSafe(WaveFile, ckOutRIFF, ckOut.dwDataOffset + Recorded - ckOutRIFF.dwDataOffset);
mmioClose(WaveFile, 0);
WaveFile := HMMIO(INVALID_HANDLE_VALUE);
end;
if FSectorStatsList <> nil then
begin
GlobalUnlock(FSectorStatsHandle);
FSectorStatsList := nil;
end;
inherited;
end;
procedure TWaveRecordThd.MMWimData(wParam: HWaveIn; lParam: PWaveHdr);
var RecLevel: TSectorStats;
i, n, oldSize: Cardinal;
Info: Integer;
HasSignal: Boolean;
begin
Info := 0;
HasSignal := false; // avoid warning //
with lParam^ do begin
dwFlags := dwFlags and not WHDR_DONE;
if (not CanWrite) or (WaveFile = HMMIO(INVALID_HANDLE_VALUE)) then
begin
case WaveFormat.wBitsPerSample of
8: RecLevel := SectorLevel8(lpData, dwBytesRecorded shr 1);
16: RecLevel := SectorLevel16(lpData, dwBytesRecorded shr 2);
24: RecLevel := SectorLevel24(lpData, dwBytesRecorded div 3);
end;
Hassignal :=
(RecLevel.Min < -RecSettings.Silence) or
(RecLevel.Max > RecSettings.Silence);
if HasSignal then
begin
CanWrite := True;
PostMessage(hWnd, WM_RSTARTED, Info, RecOffset);
end;
end;
if Canwrite then
begin
if (dwBytesRecorded > 0) and (WaveFile <> HMMIO(INVALID_HANDLE_VALUE)) then
begin
if mmioWrite(WaveFile, lpData, dwBytesRecorded) = Integer(dwBytesRecorded) then
begin
inc(Recorded, dwBytesRecorded);
OldSize := SectorStatsSize;
n := OldSize + (dwBytesRecorded - 1) div SectorSize + 1;
if (SectorStatsCapacity < n) then
SetSectorStatsCapacity(n + GROW_SECTORSTATSSIZE);
inc(FSectorStatsSize,
FillSectorStats(
FSectorStatsList, OldSize, SectorStatsCarry,
lpdata, dwBytesRecorded, SectorSize, WaveFormat.wBitsPerSample));
RecLevel.Min := 0;
RecLevel.Max := 0;
for i := OldSize to SectorStatsSize - 1 do
begin
if FSectorStatsList[i].Min < RecLevel.Min then RecLevel.Min := FSectorStatsList[i].Min;
if FSectorStatsList[i].Max > RecLevel.Max then RecLevel.Max := FSectorStatsList[i].Max;
end;
Hassignal :=
(RecLevel.Min < -RecSettings.Silence) or
(RecLevel.Max > RecSettings.Silence);
end else begin
Info := ifStop or ifError;
end;
end;
Info := Info or ifCanWrite;
end else begin
inc(RecOffset, dwBytesRecorded);
end;
if HasSignal then
begin
RecSilent := 0;
Info := Info or ifSignal;
end else begin
if CanWrite then inc(RecSilent, dwBytesRecorded);
end;
if not FStopped then
begin
WaveInAddBuffer(wParam, lParam, sizeof(TWaveHdr));
if (Recorded > RecSettings.StopTime) or
(RecSilent > RecSettings.StopSilence) then Info := Info or ifStop;
PostMessage(hWnd, WM_RDATA, Info, Integer(RecLevel));
end;
end;
end;
procedure TWaveRecordThd.Quit;
begin
if not PostThreadMessage(ThreadID, WM_RQUIT, 0, 0) then
MessageDlg(ZombieStr, mtError, [mbOk], 0);
end;
procedure TWaveRecordThd.SetSectorStatsCapacity;
begin
GlobalUnlock(FSectorStatsHandle);
FSectorStatsHandle := GlobalReAlloc(
SectorStatsHandle,
NewCapacity * SizeOf(TSectorStats),
GMEM_MOVEABLE or GMEM_ZEROINIT);
SectorStatsCapacity := NewCapacity;
FSectorStatsList := PSectorStatsList(GlobalLock(SectorStatsHandle));
end;
end.