gpt4 book ai didi

c++ - 零延迟麦克风环回、侧音

转载 作者:可可西里 更新时间:2023-11-01 10:44:44 26 4
gpt4 key购买 nike

我正在尝试以编程方式创建到扬声器或输出的零延迟麦克风环回。这用于为耳机生成侧音。我相信任何读者都知道,侧音必须是零延迟,否则,延迟听到自己的声音会导致您失去大部分连贯的说话能力。

我尝试使用 C# 中的 Naudio 和 C++ 中的 portaudio 创建解决方案。我在 PortAudio 上运气最好,但是我无法实现我需要的零延迟侧音。 Portaudio 产生了 5 毫秒左右的延迟,这是可以检测到的,并导致我的讲话不断放慢速度。

我确实知道 Windows 提供了麦克风环回,我已经对此进行了测试,但即使是 Windows 环回也有足够的延迟作为侧音令人讨厌。

我的问题分为两部分

1.) 这是硬件/软件的局限性吗?实现零音频延迟实际上是无法克服的吗?

2.) 这是我使用 portaudio 的 C++ 代码。有什么方法可以比我现有的更能减少延迟吗? (请原谅我可能草率的代码,我是 C++ 的新手而且我还在学习)

class vC_sidetone {
public:
void enable(int in_ch, int out_ch);
void disable();

vC_sidetone(int inputDevice, int outputDevice){
st_inputDevice = inputDevice;
st_outputDevice = outputDevice;
}

int st_instanceCallBack(const void *inputBuffer,
void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo *timeInfo,
PaStreamCallbackFlags statusFlags);

private:
int st_inputDevice;
int st_outputDevice;
PaError st_error;
PaStream *st_stream = NULL;
};

PaStreamParameters vC_getParam(PaDeviceIndex dev, int ch, PaSampleFormat smplFormat){

PaStreamParameters param;
param.device = dev;
param.channelCount = ch;
param.sampleFormat = smplFormat;
//param.suggestedLatency = Pa_GetDeviceInfo(dev)->defaultLowInputLatency;
param.suggestedLatency = 0.000; //here is a good place to tweak latency
param.hostApiSpecificStreamInfo = NULL;

return param;
}

void vC_sidetone::enable(int in_ch, int out_ch){
int framesPerBuffer = 1;
PaSampleFormat smplFormat = paFloat32;
int smplRate = 44100;

PaStreamParameters inParam = vC_getParam(st_inputDevice, in_ch, smplFormat);
PaStreamParameters outParam = vC_getParam(st_outputDevice, out_ch, smplFormat);

st_error = Pa_Initialize();

// Open and start stream using callback:
st_error = Pa_OpenStream(
&st_stream,
&inParam,
&outParam,
smplRate,
framesPerBuffer,
paClipOff,
st_gblCallBack,
this
);

st_error = Pa_StartStream(st_stream);
}

void vC_sidetone::disable(){

st_error = Pa_StopStream(st_stream);
Pa_AbortStream(st_stream);
Pa_CloseStream(st_stream);
Pa_Terminate();
}

int vC_sidetone::st_instanceCallBack(const void *inputBuffer,
void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo *timeInfo,
PaStreamCallbackFlags statusFlags){

(void)timeInfo; // Prevent unused variable warnings.
(void)statusFlags;


// Cast data to floats:
float *out = (float*)outputBuffer;
float *in = (float*)inputBuffer;
unsigned long i;

//for (i = 0; i < framesPerBuffer*NUM_CHANNELS; i++)
// out[i] = in[i];
for (i = 0; i < framesPerBuffer; i++) //another good place for latency
out[i] = in[i];

return paContinue;
}

//this is actually not part of the vC_sidetone class, I was getting linker errors
static int st_gblCallBack(const void *inputBuffer,
void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo *timeInfo,
PaStreamCallbackFlags statusFlags,
void *userData){

return ((vC_sidetone*)userData)->st_instanceCallBack(inputBuffer, outputBuffer,
framesPerBuffer,
timeInfo,
statusFlags);
}

最佳答案

抽象层只能增加延迟,而不能减少延迟。很明显,时间旅行是不可能的。 PulseAudio 不是 Windows 上的原生 API,它建立在原生音频架构之上。

当前的原生 Windows API 是 WASAPI。它以低延迟而享有盛誉。但它也是底层硬件之上的抽象层。例如 USB 也有延迟。没有比这更好的了。

关于c++ - 零延迟麦克风环回、侧音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31011619/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com