Delphi convert from c#
本文关键字:from convert Delphi | 更新日期: 2023-09-27 18:12:22
我真的需要你的帮助来移植这个c#代码到Delphi:
public unsafe byte[] Encode(byte[] inputPcmSamples, int sampleLength, out int encodedLength)
{
if (disposed)
throw new ObjectDisposedException("OpusEncoder");
int frames = FrameCount(inputPcmSamples);
IntPtr encodedPtr;
byte[] encoded =new byte [MaxDataBytes];
int length = 0;
/* How this can be ported to delphi */
fixed (byte* benc = encoded)
{
encodedPtr = new IntPtr((void*)benc);
length = API.opus_encode(_encoder, inputPcmSamples, frames, encodedPtr, sampleLength);
}
encodedLength = length;
if (length < 0)
throw new Exception("Encoding failed - " + ((Errors)length).ToString());
return encoded;
}
我正在寻找的主要代码部分是:
fixed (byte* benc = encoded)
{
encodedPtr = new IntPtr((void*)benc);
/* API.opus_encode = is declared in an other Class */
length = API.opus_encode(_encoder, inputPcmSamples, frames, encodedPtr, sampleLength);
}
多谢
你似乎想知道如何处理c#中的fixed
块。
byte[] encoded =new byte [MaxDataBytes];
....
fixed (byte* benc = encoded)
{
encodedPtr = new IntPtr((void*)benc);
length = API.opus_encode(_encoder, inputPcmSamples, frames, encodedPtr, sampleLength);
}
fixed
的这种用法是固定托管数组以获得要传递给非托管代码的指针。
var
encoded: TBytes;
....
SetLength(encoded, MaxDataBytes);
....
length := opus_encode(..., Pointer(encoded), ...);
或者:
length := opus_encode(..., PByte(encoded), ...);
或者:
length := opus_encode(..., @encoded[0], ...);
取决于您如何声明导入函数opus_encode
和您的首选项。
如果MaxDataBytes
是一个足够小的值,使得缓冲区驻留在堆栈上,并且MaxDataBytes
在编译时是已知的,那么可以使用固定长度的数组