我需要使用 pa_s> pa_s> pa_s> pa_s> pastreamcallbackdelegate 将回调函数传递给 pa_s> pa_s> pa_s> pa_s>我像您一样在互联网上查看,但也如所述,它给了我这个错误:
与委托portaudio.pastreamcallbackdelegate
相对应的streamcallback没有超载我不知道如何解决它。我对 c#语言一无所知,而我才开始使用库 portaudio 。
在这里我发布我的代码:
unsafe public PortAudio.PaStreamCallbackResult StreamCallback(
IntPtr* input,
IntPtr* output,
uint frameCount,
ref PortAudio.PaStreamCallbackTimeInfo timeInfo,
PortAudio.PaStreamCallbackFlags statusFlags,
IntPtr* userData)
{
//Aggiungi roba
int i;
for (i = 0; i < frameCount; i++)
{
output++; //LEFT
output = input++;
output++; //RIGHT
output = input++;
}
return PortAudio.PaStreamCallbackResult.paContinue;
}
unsafe private void btnStream_Click(object sender, EventArgs e)
{
//Dichiarazioni
IntPtr* input;
IntPtr* output;
uint frameCount;
PortAudio.PaStreamCallbackTimeInfo timeInfo;
PortAudio.PaStreamCallbackFlags statusFlags;
IntPtr* userData;
IntPtr stream;
PortAudio.PaError err = PortAudio.Pa_Initialize();
if (err != PortAudio.PaError.paNoError)
PortAudio.Pa_Terminate();
err = PortAudio.Pa_OpenDefaultStream(out stream,
1,
1,
(uint)PortAudio.PaSampleFormat.paFloat32,
44100,
PortAudio.paFramesPerBufferUnspecified,
new PortAudio.PaStreamCallbackDelegate(StreamCallback), //In this row it gives me the error
(IntPtr)null);
如果有人知道如何帮助我,我会非常感激,谢谢。
您的输入,输出和用户数据参数应为IntPtr
类型,而不是IntPtr*
我也认为您并没有在回调方法中制作副本。我真的认为您应该使用一些IntPtr
方法。我从未在.net中使用不安全的类型,但是查看副本样本,我相信您的算法将是:
unsafe public PortAudio.PaStreamCallbackResult StreamCallback(
IntPtr input, IntPtr output, uint frameCount,
ref PortAudio.PaStreamCallbackTimeInfo timeInfo,
PortAudio.PaStreamCallbackFlags statusFlags,
IntPtr userData)
{
byte *src = (byte *)input.ToPointer();
byte *dst = (byte *)output.ToPointer();
for (int i = 0; i < frameCount; i++)
{
*dst++ = *src++;//in c this could be written dst[i] = src[i] maybe you can try that.
}
return PortAudio.PaStreamCallbackResult.paContinue;
}
编辑:根据Hans Passant,您还应该将您的委托保存在一个字段中,以免收集垃圾。但是 afaik 只要垃圾收集至少有一个引用,它就不会清洁对象,这足以将其存储在变量中。仅当回调以定义的相同方法发生时,这仅适用,否则您应该按照他所说的来存储在字段中。
unsafe private void btnStream_Click(object sender, EventArgs e)
{
//Dichiarazioni
IntPtr* input;
IntPtr* output;
uint frameCount;
PortAudio.PaStreamCallbackTimeInfo timeInfo;
PortAudio.PaStreamCallbackFlags statusFlags;
IntPtr* userData;
IntPtr stream;
PortAudio.PaError err = PortAudio.Pa_Initialize();
var callback = new PortAudio.PaStreamCallbackDelegate(StreamCallback);
if (err != PortAudio.PaError.paNoError)
PortAudio.Pa_Terminate();
err = PortAudio.Pa_OpenDefaultStream(out stream, 1, 1,
(uint)PortAudio.PaSampleFormat.paFloat32,
44100,
PortAudio.paFramesPerBufferUnspecified,
callback,
(IntPtr)null);