300分酬谢!!! 请帮忙把以下C++代码翻译成DELPHI(300分)

  • 主题发起人 TDA2822M
  • 开始时间
T

TDA2822M

Unregistered / Unconfirmed
GUEST, unregistred user!
////////////ADPCM/////////////////////

short adpcmValprev; // Previous output value;
char adpcmIndex; // Index into stepsize table
short adpcmStep; // Stepsize
short adpcmValprevp; // Previous output value;
char adpcmIndexp; // Index into stepsize table
short adpcmStepp; // Stepsize

BOOL adpcmLsbMsb; //
unsigned short adpcmNibble;


// Intel ADPCM step variation table
static short indexTable[16] = { -1, -1, -1, -1, 2, 4, 6, 8};
static short stepsizeTable[89] = {
7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767 };

void adpcm_ini(void)
{
adpcmValprev=0;
adpcmIndex=0;
adpcmStep=stepsizeTable[adpcmIndex];
adpcmValprevp=0;
adpcmIndexp=0;
adpcmStepp=stepsizeTable[adpcmIndexp];
adpcmLsbMsb=FALSE;
} // adpcm_ini()

unsigned short adpcm_code_1_sample(short diff)
{
// short diff; // Difference between val and valprev
BOOL sign;
unsigned char delta; // Current adpcm output value
short vpdiff; // Current change to valpred
//short step; // Stepsize
long aux;

// Step 1 - compute difference with previous value

// diff=val-adpcmValprev;

aux=(long)diff;
aux-=(long)adpcmValprev;
//if (aux>32767) aux=32767; else if (aux<-32768) aux=-32768;
if (aux>32767) aux=32767; else if (aux<-32767) aux=-32767;
diff=(short)aux;

if (diff<0) {sign=TRUE;diff=-diff;} else sign=FALSE;

// Step 2 - Divide and clamp
// Note: This code *approximately* computes: delta = diff*4/step; vpdiff = (delta+0.5)*step/4; but in shift step bits are dropped.
// The net result of this is that even if you have fast mul/div hardware you cannot put it to good use since the fixup would be too expensive.
delta=0; vpdiff=adpcmStep>>3;
if (diff>=adpcmStep) {delta =4; diff-=adpcmStep; vpdiff+=adpcmStep;} adpcmStep>>=1;
if (diff>=adpcmStep) {delta|=2; diff-=adpcmStep; vpdiff+=adpcmStep;} adpcmStep>>=1;
if (diff>=adpcmStep) {delta|=1; vpdiff+=adpcmStep;}

// Step 3 and 4 - Update previous value and clamp
//if (sign) adpcmValprev-=vpdiff; else adpcmValprev+=vpdiff;
//if (adpcmValprev>32767) adpcmValprev=32767; else if (adpcmValprev<-32768) adpcmValprev=-32768;

aux=adpcmValprev;
if (sign) aux-=(long)vpdiff; else aux+=(long)vpdiff;
// // if (aux>32767) aux=32767; else if (aux<-32768) aux=-32768;
if (aux>32767) aux=32767; else if (aux<-32767) aux=-32767;
adpcmValprev=(short)aux;

// Step 5 - Assemble value, update index and step values
adpcmIndex+=indexTable[delta];
if (adpcmIndex<0) adpcmIndex=0; if (adpcmIndex>88) adpcmIndex=88;

adpcmStep=stepsizeTable[adpcmIndex];

if (sign) delta|=0x08;

// Step 6 - Output value
adpcmLsbMsb=!adpcmLsbMsb;
if (adpcmLsbMsb) {adpcmNibble=((unsigned short)delta)<<4; return(0xffff);}
else return(adpcmNibble|((unsigned short)delta));
} // adpcm_code_1_sample()


void adpcm_compress(signed short *buffin,unsigned int len,unsigned char *buffout)
{
int i;
adpcm_ini();
for(i=0;i<len;++i)
{
unsigned short sample=adpcm_code_1_sample(buffin);
if(sample!=0xffff)
buffout[i/2]=sample&amp;0xff;
}
}
 
PROGRAM SAMPLE;


Integer adpcmValprev ;
{char} Byte adpcmIndex ;
Integer adpcmStep ;
Integer adpcmValprevp ;
{char} Byte adpcmIndexp ;
Integer adpcmStepp ;

BOOL adpcmLsbMsb ;
WORD Integer adpcmNibble ;



{static} Integer indexTable[16] := BEGIN -1,-1,-1,-1,2,4,6,8END; ;
{static} Integer stepsizeTable[89] := BEGIN
7,8,9,10,11,12,13,14,16,17,
19,21,23,25,28,31,34,37,41,45,
50,55,60,66,73,80,88,97,107,118,
130,143,157,173,190,209,230,253,279,307,
337,371,408,449,494,544,598,658,724,796,
876,963,1060,1166,1282,1411,1552,1707,1878,2066,
2272,2499,2749,3024,3327,3660,4026,4428,4871,5358,
5894,6484,7132,7845,8630,9493,10442,11487,12635,13899,
15289,16818,18500,20350,22385,24623,27086,29794,32767END; ;

{void} adpcm_ini ({void} )
BEGIN
adpcmValprev := 0;
adpcmIndex := 0;
adpcmStep := stepsizeTable[adpcmIndex] ;
adpcmValprevp := 0;
adpcmIndexp := 0;
adpcmStepp := stepsizeTable[adpcmIndexp] ;
adpcmLsbMsb := FALSE ;
END;

WORD Integer adpcm_code_1_sample (Integer diff )
BEGIN

BOOL sign ;
WORD {char} Byte delta ;
Integer vpdiff ;

Longint aux ;





aux := (Longint )diff ;
aux -:= (Longint )adpcmValprev ;

if (aux >32767)aux := 32767;else if (aux <-32767)aux := -32767;
diff := (Integer )aux ;

if (diff <0) BEGIN sign := TRUE ;diff := -diff ;END; else sign := FALSE ;


delta := 0;vpdiff := adpcmStep >>3;
if (diff >:= adpcmStep ) BEGIN delta := 4;diff -:= adpcmStep ;vpdiff +:= adpcmStep ;END; adpcmStep >>:= 1;
if (diff >:= adpcmStep ) BEGIN delta OR := 2;diff -:= adpcmStep ;vpdiff +:= adpcmStep ;END; adpcmStep >>:= 1;
if (diff >:= adpcmStep ) BEGIN delta OR := 1;vpdiff +:= adpcmStep ;END;



aux := adpcmValprev ;
if (sign )aux -:= (Longint )vpdiff ;else aux +:= (Longint )vpdiff ;
if (aux >32767)aux := 32767;else if (aux <-32767)aux := -32767;
adpcmValprev := (Integer )aux ;

adpcmIndex +:= indexTable[delta] ;
if (adpcmIndex <0)adpcmIndex := 0;if (adpcmIndex >88)adpcmIndex := 88;

adpcmStep := stepsizeTable[adpcmIndex] ;

if (sign )delta OR := 0x08;

adpcmLsbMsb := NOT adpcmLsbMsb ;
if (adpcmLsbMsb ) BEGIN adpcmNibble := ((WORD Integer )delta )<<4;{return} (0xffff);END;
else {return} (adpcmNibble OR ((WORD Integer )delta ));
END;


{void} adpcm_compress (signed Integer *buffin ,WORD Integer len ,WORD {char} Byte *buffout )
BEGIN
Integer i ;
adpcm_ini ();
for (i := 0;i <len ;INC(i )
BEGIN
WORD Integer sample := adpcm_code_1_sample (buffin );
if (sample <> := 0xffff)
buffout[i / ] := sample AND 0xff;
END;
END;
END.
 
基本如此
 
哎呀! 上面的翻译是错误的,喔!要翻译那么多,感却我告诉你方法!
只是举些例子:

short adpcmValprev; --> adpcmValprev:shortint;
char adpcmIndex; --> adpcmIndex:shortint;

unsigned char --> byte;
unsigned short --> byte;
char --> shortint;
int --> integer;
unsigned int --> word;
long --> longint;
unsigned long --> DWord;
BOOL --> Boolean(或LongBool);

a-=b --> a:=a-b;
a+=b --> a:=a+b;
a|=b --> a:=a or b;
a<<=b --> a:=a shl b;
a>>=b --> a:=a shr b;
a&amp;=b --> a:=a and b;

void aa(void() --> Procedure aa;
int aa(...) --> Function aa(...):Integer;

static short indexTable[16] = { -1, -1, -1, -1, 2, 4, 6, 8};为:
const
indexTable : packed array [0..15] of shortint = (-1,-1,....);

static short stepsizeTable[89] = {

const
stepsizeTable : packed array [0..88] of shortint = (
7, 8, 9, 10, 11, 12, 13, 14, 16, 17,...);

其他的自己搞定算了!!!
 
第一个回答的有错误,基本上是象wql所说的。但还有一些没说出。我转化如下:
var
adpcmValprev: Smallint;
adpcmIndex: byte;
adpcmStep: smallint;
adpcmValprevp: smallint;
adpcmIndexp: byte;
adpcmStepp: smallint;

adpcmLsbMsb: boolean;
adpcmNibble: word;

const
indexTable: array[0..15] of smallint = ( -1, -1, -1, -1, 2, 4, 6, 8, 0, 0, 0, 0, 0, 0, 0, 0);
stepsizeTable: array[0..88] of smallint =(
7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767 );

procedure adpcm_ini;
begin
adpcmValprev := 0;
adpcmIndex := 0;
adpcmStep := stepsizeTable[adpcmIndex];
adpcmValprevp := 0;
adpcmIndexp := 0;
adpcmStepp := stepsizeTable[adpcmIndexp];
adpcmLsbMsb := FALSE;
end;

function adpcm_code_1_sample(diff: smallint): word;
var
sign: boolean;
delta: byte;
vpdiff: word;
aux: Integer;
adpcmLabMsb: boolean;
begin
aux := word(diff);
aux := aux - word(adpcmValprev);
if (aux > 32767) then
aux := 32767
else if (aux < -32767) then
aux := -32767;
diff := smallint(aux);

if (diff < 0) then
begin
sign := True;
diff := -diff;
end
else
sign := False;

delta := 0;
vpdiff := adpcmStep shr 3;
if (diff >= adpcmStep) then
begin
delta := 4;
diff := diff - adpcmStep;
vpdiff := vpdiff + adpcmStep;
end;
adpcmStep := adpcmStep shr 1;
if (diff >= adpcmStep) then
begin
delta := delta or 2;
diff := diff - adpcmStep;
vpdiff := vpdiff + adpcmStep;
end;
adpcmStep := adpcmStep shr 1;
if (diff >= adpcmStep) then
begin
delta := delta or 1;
vpdiff := vpdiff + adpcmStep;
end;
aux := adpcmValprev;
if sign then
aux := aux - integer(vpdiff)
else
aux := aux + integer(vpdiff);
if (aux > 32767) then
aux := 32767
else if (aux < -32767) then
aux := -32767;
adpcmValprev := smallint(aux);

adpcmIndex := adpcmIndex + indexTable[delta];
if (adpcmIndex < 0) then
adpcmIndex := 0;
if (adpcmIndex > 88) then
adpcmIndex := 88;

adpcmStep := stepsizeTable[adpcmIndex];
if sign then
delta := delta or $08;

adpcmLsbMsb := not adpcmLsbMsb;
if adpcmLabMsb then
begin
adpcmNibble := word(delta) shl 4;
Result := $ffff;
end
else
Result := adpcmNibble or word(delta);
end;

procedure adpcm_compress(buffin: array of smallint; len: word; buffout: array of byte);
var
I: integer;
sample: smallint;
begin
adpcm_ini;
for i := 0 to len do
begin
sample := adpcm_code_1_sample(buffin);
if (sample <> $ffff) then
buffout[i div 2] := sample and $ff;
end;
end;

以上在Delphi6+win2k上调试通过!
 
哥们,您的 名字不错 TDA2822M 学电子吧!有空谈论以下,我想设计一款功率放大器!
可否给个建议!
QQ:78672855
webmaster@iligia.com
欢迎访问 www.iligia.com
 
to ligia:
不要意思。学计算机的。高中的时候玩过电子技术。那时候常买《电子报》,《北京电子
报》,《电子制作》。呵呵。。。
 
现在设计功放不费事了,找块好的厚膜电路就成了,其他电路一概都省了,很多功能都集成了,音质还不错的,有空你可以试试。
 
好象以前有一个还不错。似乎是STK6153(功率放大),STK3048A(前置放大)?
你说的是这些?

我还是喜欢集成电路的。LM1875,LM3886之类的。不过那都是很久以前的东西了。
我已经有2年没碰过电子了。那东西太贵,尤其是玩音响。等有了钱再说吧。

如果有机会的话,我还是喜欢分立元件做的功放。可惜水平有限。尤其是做电路板,
我一直都在用万用板。那东西好象不适合玩音响。
 
A率波形文件的解码器?
 
to leizengzheng:
TDA2822M,小功率的功率放大电路。额定输入功率:1W*2.工作电压范围:1.8--15V
其他的参数我都忘记了。很久没看这些东西了。

大家还是多关注一下C++代码翻译成DELphi的问题吧。
[:)]
 
to TDA2822M:
这些对学计算机编程有用吗?我是个电子盲,不过很喜欢编程的,不会那些对学习计算机编程
有引响吗?
 
to 加菲:
我已经两年多没翻过电子书了。请大家关注一下本来的问题吧:C++-》Delphi
谢谢。
 
接受答案了.
 
顶部