我在 OSX 10.8
在我的应用程序中,我必须更改麦克风增益,我使用 AudioQueue 来捕获缓冲区,但没有获得任何指针来更改麦克风增益,
Apple HAL 文档也有,但没有得到任何东西,
我在 OSX 10.8
在我的应用程序中,我必须更改麦克风增益,我使用 AudioQueue 来捕获缓冲区,但没有获得任何指针来更改麦克风增益,
Apple HAL 文档也有,但没有得到任何东西,
首先,向您的队列询问它的kAudioQueueProperty_CurrentDevice
,这是它正在读取的设备的标识符字符串。
接下来,您需要打开该设备。这比应有的工作要多,因为 Core Audio 的设计者相信通过通用的“GetProperty”和“SetProperty”函数可以完成几乎所有事情。开始:
AudioValueTranslation
结构,其中包含指向包含设备标识符的变量的指针和指向要在其中的AudioDeviceID
.AudioHardwareGetProperty
or 未弃用但更通用AudioObjectGetProperty
的 get kAudioHardwarePropertyDeviceForUID
,将指针传递给结构。HAL 将查找设备并通过您放入结构中的指针将其返回给您。如果这没有返回错误,那么您现在拥有了一个设备。
最后一步是设置它的增益。我认为这体现kAudioDevicePropertyVolumeScalar
在输入范围上,但我不是 100% 确定。无论如何,您将一直在修补AudioDeviceSetProperty
和/或AudioObjectSetProperty
直到找到正确的组合。
看来,在运行 AudioQueue 时无法即时更改音量增益,有些如何能够在缓冲区中添加麦克风增益,发布代码,
void AQRecorder::setGain(void *data, int bytes, float gain){
SInt16 *editBuffer = (SInt16 *)data;
// loop over every packet
for (int nb = 0; nb < (bytes / 2); nb++) {
// we check if the gain has been modified to save resoures
if (gain != 0) {
// we need more accuracy in our calculation so we calculate with doubles
double gainSample = ((double)editBuffer[nb]) / 32767.0;
/*
at this point we multiply with our gain factor
we dont make a addition to prevent generation of sound where no sound is.
no noise
0*10=0
noise if zero
0+10=10
*/
gainSample *= gain;
/**
our signal range cant be higher or lesser -1.0/1.0
we prevent that the signal got outside our range
*/
gainSample = (gainSample < -1.0) ? -1.0 : (gainSample > 1.0) ? 1.0 : gainSample;
/*
This thing here is a little helper to shape our incoming wave.
The sound gets pretty warm and better and the noise is reduced a lot.
Feel free to outcomment this line and here again.
You can see here what happens here http://silentmatt.com/javascript-function-plotter/
Copy this to the command line and hit enter: plot y=(1.5*x)-0.5*x*x*x
*/
gainSample = (1.5 * gainSample) - 0.5 * gainSample * gainSample * gainSample;
// multiply the new signal back to short
gainSample = gainSample * 32767.0;
// write calculate sample back to the buffer
editBuffer[nb] = (SInt16)gainSample;
}
}
}
记住这个函数只有在有增益变化时才应该调用,否则会节省 CPU 资源。