急!! Delphi 代码转换为 C 代码.... ( 我写的有错?? )
C 代码编译成 DLL 文件后, 执行结果与 Delphi 的 DLL 结果不一样.
代码如下:
///////////////////////////////////////////////
//
// Delphi 代码
//
///////////////////////////////////////////////
const
constACMAdptive: array[0..15] of SmallInt = (230, 230, 230, 230, 307, 409,
512, 614, 768, 614, 512, 409, 307, 230, 230, 230);
constACMCoef: array[0..1, 0..6] of SmallInt = (
(256, 512, 0, 192, 240, 460, 392),
(0, -256, 0, 64, 0, -208, -232));
procedure CreateACMData(Source: array of Byte; var Dest: array of SmallInt);
var
i, j, Prec: Integer;
Sample, Delta: LongInt;
Samp1, Samp2: SmallInt;
Code: Byte;
begin
Prec := Source[0];
Delta := (Source[2] shl 8) or Source[1];
Samp1 := (Source[4] shl 8) or Source[3];
Samp2 := (Source[6] shl 8) or Source[5];
Dest[0] := Samp2;
Dest[1] := Samp1;
for i := 0 to 248 do
begin
for j := 0 to 1 do
begin
if j = 0 then
begin
Code :=(Source[i + 7] and $0F0);
Code := Code shr 4;
end
else begin
Code := Source[i + 7] and $0F;
end;
Sample := (Samp1 * constACMCoef[0, Prec] + Samp2 *
constACMCoef[1, Prec]) div 256;
if (Code > 7) then
begin
Sample := Sample + Delta * (Code - $10);
end
else begin
Sample := Sample + Delta * Code;
end;
if Sample > 32767 then
begin
Sample := 32767;
end;
if Sample < -32768 then
begin
Sample := -32768;
end;
Dest[i * 2 + j + 2] := Sample;
Delta := (Delta * constACMAdptive[Code]) div 256;
if Delta < 16 then
begin
Delta := 16;
end;
Samp2 := Samp1;
Samp1 := Sample;
end;
end;
end;
///////////////////////////////////////////////
//
// C 代码
//
///////////////////////////////////////////////
short constACMAdptive[16] = {230, 230, 230, 230, 307, 409,
512, 614, 768, 614, 512, 409, 307, 230, 230, 230};
short constACMCoef[2][7] = {
{256, 512, 0, 192, 240, 460, 392},
{0, -256, 0, 64, 0, -208, -232}};
void WINAPI CreateACMData(byte *Source, short *Dest)
{
int i, j, Prec;
LONG Sample, Delta;
short Samp1, Samp2;
byte Code;
Prec = Source[0];
Delta = (Source[2] << 8) | Source[1];
Samp1 = (Source[4] << 8) | Source[3];
Samp2 = (Source[6] << 8) | Source[5];
Dest[0] = Samp2;
Dest[1] = Samp1;
for (i = 0; i <= 248; i++)
{
for (j = 0; j <= 1; j++)
{
Code = (j==0) ? ((Source[i + 7] & 0xF0) >> 4) : (Source[i + 7] & 0x0F);
Sample = (Samp1 * constACMCoef[0][Prec] + Samp2 * constACMCoef[1][Prec]) / 256;
Sample = (Code > 7) ? (Sample + Delta * (Code - 0x10)) : (Sample + Delta * Code);
Sample = (Sample > 32767) ? 32767 : Sample;
Sample = (Sample < -32768) ? -32768 : Sample;
Dest[i * 2 + j + 2] = (byte)Sample;
Delta = (Delta * constACMAdptive[Code]) / 256;
Delta = (Delta < 16) ? 16 : Delta;
Samp2 = Samp1;
Samp1 = (short)Sample;
}
}
}