-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathACMConvertor.pas
373 lines (338 loc) · 9.29 KB
/
ACMConvertor.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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
unit ACMConvertor;
interface
uses
Windows, Messages, MSACM, MMSystem;
type
TNotifyEvent = procedure(Sender: TObject) of object;
TSeekOrigin = (soBeginning, soCurrent, soEnd);
TStream = class(TObject)
private
function GetPosition: Int64;
procedure SetPosition(const Pos: Int64);
function GetSize: Int64;
procedure SetSize64(const NewSize: Int64);
protected
procedure SetSize(NewSize: Longint); overload; virtual;
procedure SetSize(const NewSize: Int64); overload; virtual;
public
function Read(var Buffer; Count: Longint): Longint; virtual; abstract;
function Write(const Buffer; Count: Longint): Longint; virtual; abstract;
function Seek(Offset: Longint; Origin: Word): Longint; overload; virtual;
function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; overload; virtual;
procedure ReadBuffer(var Buffer; Count: Longint);
procedure WriteBuffer(const Buffer; Count: Longint);
function CopyFrom(Source: TStream; Count: Int64): Int64;
property Position: Int64 read GetPosition write SetPosition;
property Size: Int64 read GetSize write SetSize64;
end;
TACMWaveFormat = packed record
case integer of
0: (Format: TWaveFormatEx);
1: (RawData: Array[0..128] of byte);
end;
TACMConvertor = Class(TObject)
private
FChooseData: TACMFORMATCHOOSEA;
FActive: Boolean;
FBufferIn: Pointer;
FBufferOut: Pointer;
FInputBufferSize: DWord;
FOutputBufferSize: DWord;
FStartOfStream: Boolean;
FStreamHandle: HACMStream;
FStreamHeader: TACMStreamHeader;
procedure SetActive(const Value: Boolean);
procedure SetInputBufferSize(const Value: DWord);
protected
procedure CloseStream;
procedure OpenStream;
procedure ReadFormat(var Format: TACMWaveFormat; Stream: TStream);
procedure WriteFormat(var Format: TACMWaveFormat; Stream: TStream);
public
FormatIn, FormatOut: TACMWaveFormat;
constructor Create;
destructor Destroy; override;
function ChooseFormat(var Format: TACMWaveFormat; const UseDefault: Boolean): Boolean;
function ChooseFormatIn(const UseDefault: Boolean): Boolean;
function ChooseFormatOut(const UseDefault: Boolean): Boolean;
function Convert: DWord;
function SuggestFormat(Format: TACMWaveFormat): TACMWaveFormat;
property Active: Boolean read FActive write SetActive;
property BufferIn: Pointer read FBufferIn;
property BufferOut: Pointer read FBufferOut;
property OutputBufferSize: DWord read FOutputBufferSize;
published
property InputBufferSize: DWord read FInputBufferSize write SetInputBufferSize;
end;
implementation
{TStream}
function TStream.GetPosition: Int64;
begin
Result := Seek(0, soCurrent);
end;
procedure TStream.SetPosition(const Pos: Int64);
begin
Seek(Pos, soBeginning);
end;
function TStream.GetSize: Int64;
var
Pos: Int64;
begin
Pos := Seek(0, soCurrent);
Result := Seek(0, soEnd);
Seek(Pos, soBeginning);
end;
procedure TStream.SetSize(NewSize: Longint);
begin
WriteLn(NewSize);
end;
procedure TStream.SetSize64(const NewSize: Int64);
begin
SetSize(NewSize);
end;
procedure TStream.SetSize(const NewSize: Int64);
begin
if (NewSize < Low(Longint)) or (NewSize > High(Longint)) then
Exit;
SetSize(Longint(NewSize));
end;
function TStream.Seek(Offset: Longint; Origin: Word): Longint;
type
TSeek64 = function (const Offset: Int64; Origin: TSeekOrigin): Int64 of object;
var
Impl: TSeek64;
Base: TSeek64;
ClassTStream: TClass;
begin
Impl := Seek;
ClassTStream := Self.ClassType;
while (ClassTStream <> nil) and (ClassTStream <> TStream) do
ClassTStream := ClassTStream.ClassParent;
Base := TStream(@ClassTStream).Seek;
Result := Seek(Int64(Offset), TSeekOrigin(Origin));
end;
function TStream.Seek(const Offset: Int64; Origin: TSeekOrigin): Int64;
begin
Result := 0;
if (Offset < Low(Longint)) or (Offset > High(Longint)) then
Exit;
Result := Seek(Longint(Offset), Ord(Origin));
end;
procedure TStream.ReadBuffer(var Buffer; Count: Longint);
begin
if (Count <> 0) and (Read(Buffer, Count) <> Count) then
Exit;
end;
procedure TStream.WriteBuffer(const Buffer; Count: Longint);
begin
if (Count <> 0) and (Write(Buffer, Count) <> Count) then
Exit;
end;
function TStream.CopyFrom(Source: TStream; Count: Int64): Int64;
const
MaxBufSize = $F000;
var
BufSize, N: Integer;
Buffer: PChar;
begin
if Count = 0 then
begin
Source.Position := 0;
Count := Source.Size;
end;
Result := Count;
if Count > MaxBufSize then BufSize := MaxBufSize else BufSize := Count;
GetMem(Buffer, BufSize);
try
while Count <> 0 do
begin
if Count > BufSize then N := BufSize else N := Count;
Source.ReadBuffer(Buffer^, N);
WriteBuffer(Buffer^, N);
Dec(Count, N);
end;
finally
FreeMem(Buffer, BufSize);
end;
end;
{TACMConvertor}
function TACMConvertor.ChooseFormat(var Format: TACMWaveFormat; const UseDefault: Boolean): Boolean;
var
OriginalFormat: PWaveFormatEX;
FormatSelection: Longint;
begin
Result := False;
GetMem(OriginalFormat, Sizeof(TACMWaveFormat));
try
if UseDefault then
begin
Move(Format, OriginalFormat^, SizeOf(TACMWaveFormat))
end
else
begin
with OriginalFormat^ do
begin
wFormatTag := 49;
nChannels := 1;
nSamplesPerSec := 8000;
nAvgBytesPerSec:= 8000;
nBlockAlign:=1;
wbitspersample := 8;
cbSize := SizeOf(TACMWaveFormat);
end;
end;
with FChooseData do begin
pwfx := OriginalFormat;
cbStruct := SizeOf(FChooseData);
cbwfx := SizeOf(TACMWaveFormat);
fdwStyle := ACMFORMATCHOOSE_STYLEF_INITTOWFXSTRUCT
end;
FormatSelection := ACMFormatChoose(FChooseData);
if FormatSelection = MMSYSERR_NOERROR then
begin
Move(FChooseData.pwfx^, Format, SizeOf(TACMWaveFormat));
Result := True;
end;
finally
FreeMem(OriginalFormat);
end;
end;
function TACMConvertor.ChooseFormatIn(const UseDefault: Boolean): Boolean;
begin
Result := ChooseFormat(FormatIn, UseDefault);
end;
function TACMConvertor.ChooseFormatOut(const UseDefault: Boolean): Boolean;
begin
Result := ChooseFormat(FormatOut, UseDefault);
end;
procedure TACMConvertor.CloseStream;
begin
ACMStreamUnPrepareHeader(FStreamHandle, FStreamHeader, 0);
ACMStreamClose(FStreamHandle, 0);
FreeMem(FBufferIn);
FreeMem(FBufferOut);
FActive := False;
FStartOfStream := False;
end;
function TACMConvertor.Convert: dword;
var
Start: dword;
begin
if FStartOfStream then
begin
Start := ACM_STREAMCONVERTF_BLOCKALIGN
end
else
begin
Start := 0;
end;
ZeroMemory(BufferOut, OutputBufferSize);
ACMStreamConvert(FStreamHandle, FStreamHeader, ACM_STREAMCONVERTF_BLOCKALIGN or Start);
ACMStreamReset(FStreamHandle,0);
Result := FStreamHeader.cbDstLengthUsed;
FStartOfStream := False;
end;
constructor TACMConvertor.Create;
begin
inherited;
FStreamHandle := nil;
InputBufferSize := 2048;
with FormatIn.Format do begin
wFormatTag := 1;
nChannels := 1;
nSamplesPerSec := 22050;
nAvgBytesPerSec:= 22050;
nBlockAlign:=1;
wbitspersample := 8;
cbSize := SizeOf(TACMWaveFormat);
end;
with FormatOut.Format do begin
wFormatTag := 1;
nChannels := 1;
nSamplesPerSec := 22050;
nAvgBytesPerSec:= 22050;
nBlockAlign:=1;
wbitspersample := 8;
cbSize := SizeOf(TACMWaveFormat);
end;
end;
destructor TACMConvertor.Destroy;
begin
Active := False;
inherited;
end;
procedure TACMConvertor.OpenStream;
procedure BuildHeader;
begin
with FStreamHeader do begin
cbStruct := SizeOf(TACMStreamHeader);
fdwStatus := 0;
dwUser := 0;
pbSrc := FBufferIn;
cbSrcLength := InputBufferSize;
cbSrcLengthUsed := 0;
dwSrcUser := 0;
pbDst := FBufferOut;
cbDstLength := OutputBufferSize;
cbDstLengthUsed := 0;
dwDstUser := 0;
end;
end;
begin
FStartOfStream := True;
ACMStreamOpen(FStreamhandle, nil, FormatIn.Format, FormatOut.Format, nil, 0, 0, 0);
ACMStreamSize(FStreamHandle, InputBufferSize, FOutputBufferSize, ACM_STREAMSIZEF_SOURCE);
GetMem(FBufferIn, InputBufferSize);
Getmem(FBufferOut, OutputBufferSize);
try
BuildHeader;
ACMStreamPrepareHeader(FStreamHandle, FStreamHeader, 0);
except
Freemem(FBufferIn);
Freemem(FBufferOut);
Exit;
end;
FActive := True;
end;
procedure TACMConvertor.ReadFormat(var Format: TACMWaveFormat; Stream: TStream);
var
Size: integer;
begin
Stream.Read(Size, SizeOf(integer));
Stream.Read(Format, Size);
end;
procedure TACMConvertor.SetActive(const Value: Boolean);
begin
if Value = FActive then Exit;
if Value then
begin
OpenStream
end
else
begin
CloseStream;
end;
end;
procedure TACMConvertor.SetInputBufferSize(const Value: DWord);
begin
if Active then Exit;
FInputBufferSize := Value;
end;
function TACMConvertor.SuggestFormat(Format: TACMWaveFormat): TACMWaveFormat;
var
WaveFormatEx: TWaveFormatEx;
ValidItems: dword;
begin
ValidItems := 0;
if ACMFormatSuggest(nil, Format.Format, WaveFormatEx, SizeOf(TACMWaveFormat), ValidItems) = 0 then Exit;
Move(WaveFormatEx, Result, SizeOf(TACMWaveFormat));
end;
procedure TACMConvertor.WriteFormat(var Format: TACMWaveFormat; Stream: TStream);
var
Size: integer;
begin
Size := SizeOf(Format);
Stream.Write(Size, SizeOf(integer));
Stream.Write(Format, Size);
end;
end.