- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个OSX应用程序,该应用程序使用音频单元记录音频数据。可以将音频单元的输入设置为任何可用的输入源,包括内置输入。问题是,我从内置输入获得的音频经常被剪切,而在诸如Audacity(甚至Quicktime)之类的程序中,我可以调低输入电平,而不会剪切。
当然,将样本帧乘以一小部分是行不通的,因为我得到的音量较小,但是样本本身在输入时仍会被裁剪。
如何设置该内置输入的输入电平或增益以避免削波问题?
最佳答案
这对我来说可以在MacBook Pro(2011年型号)上设置输入音量。这有点时髦,我不得不尝试设置主声道音量,然后设置每个独立的立体声声道音量,直到找到有效的音量为止。查看代码中的注释,我怀疑判断您的代码是否正常的最佳方法是找到一个可以正常工作的get/set-property组合,然后执行诸如get/set(其他操作)/get之类的操作来验证您的代码是否正确二传手正在工作。
哦,我当然会指出,我不会像在这里那样在getProperty调用中依赖地址中的值保持相同。看来可行,但是在通过引用传递函数时,依靠相同的结构值绝对是不明智的做法。这当然是示例代码,因此请原谅我的懒惰。 ;)
//
// main.c
// testInputVolumeSetter
//
#include <CoreFoundation/CoreFoundation.h>
#include <CoreAudio/CoreAudio.h>
OSStatus setDefaultInputDeviceVolume( Float32 toVolume );
int main(int argc, const char * argv[]) {
OSStatus err;
// Load the Sound system preference, select a default
// input device, set its volume to max. Now set
// breakpoints at each of these lines. As you step over
// them you'll see the input volume change in the Sound
// preference panel.
//
// On my MacBook Pro setting the channel[ 1 ] volume
// on the default microphone input device seems to do
// the trick. channel[ 0 ] reports that it works but
// seems to have no effect and the master channel is
// unsettable.
//
// I do not know how to tell which one will work so
// probably the best thing to do is write your code
// to call getProperty after you call setProperty to
// determine which channel(s) work.
err = setDefaultInputDeviceVolume( 0.0 );
err = setDefaultInputDeviceVolume( 0.5 );
err = setDefaultInputDeviceVolume( 1.0 );
}
// 0.0 == no volume, 1.0 == max volume
OSStatus setDefaultInputDeviceVolume( Float32 toVolume ) {
AudioObjectPropertyAddress address;
AudioDeviceID deviceID;
OSStatus err;
UInt32 size;
UInt32 channels[ 2 ];
Float32 volume;
// get the default input device id
address.mSelector = kAudioHardwarePropertyDefaultInputDevice;
address.mScope = kAudioObjectPropertyScopeGlobal;
address.mElement = kAudioObjectPropertyElementMaster;
size = sizeof(deviceID);
err = AudioObjectGetPropertyData( kAudioObjectSystemObject, &address, 0, nil, &size, &deviceID );
// get the input device stereo channels
if ( ! err ) {
address.mSelector = kAudioDevicePropertyPreferredChannelsForStereo;
address.mScope = kAudioDevicePropertyScopeInput;
address.mElement = kAudioObjectPropertyElementWildcard;
size = sizeof(channels);
err = AudioObjectGetPropertyData( deviceID, &address, 0, nil, &size, &channels );
}
// run some tests to see what channels might respond to volume changes
if ( ! err ) {
Boolean hasProperty;
address.mSelector = kAudioDevicePropertyVolumeScalar;
address.mScope = kAudioDevicePropertyScopeInput;
// On my MacBook Pro using the default microphone input:
address.mElement = kAudioObjectPropertyElementMaster;
// returns false, no VolumeScalar property for the master channel
hasProperty = AudioObjectHasProperty( deviceID, &address );
address.mElement = channels[ 0 ];
// returns true, channel 0 has a VolumeScalar property
hasProperty = AudioObjectHasProperty( deviceID, &address );
address.mElement = channels[ 1 ];
// returns true, channel 1 has a VolumeScalar property
hasProperty = AudioObjectHasProperty( deviceID, &address );
}
// try to get the input volume
if ( ! err ) {
address.mSelector = kAudioDevicePropertyVolumeScalar;
address.mScope = kAudioDevicePropertyScopeInput;
size = sizeof(volume);
address.mElement = kAudioObjectPropertyElementMaster;
// returns an error which we expect since it reported not having the property
err = AudioObjectGetPropertyData( deviceID, &address, 0, nil, &size, &volume );
size = sizeof(volume);
address.mElement = channels[ 0 ];
// returns noErr, but says the volume is always zero (weird)
err = AudioObjectGetPropertyData( deviceID, &address, 0, nil, &size, &volume );
size = sizeof(volume);
address.mElement = channels[ 1 ];
// returns noErr, but returns the correct volume!
err = AudioObjectGetPropertyData( deviceID, &address, 0, nil, &size, &volume );
}
// try to set the input volume
if ( ! err ) {
address.mSelector = kAudioDevicePropertyVolumeScalar;
address.mScope = kAudioDevicePropertyScopeInput;
size = sizeof(volume);
if ( toVolume < 0.0 ) volume = 0.0;
else if ( toVolume > 1.0 ) volume = 1.0;
else volume = toVolume;
address.mElement = kAudioObjectPropertyElementMaster;
// returns an error which we expect since it reported not having the property
err = AudioObjectSetPropertyData( deviceID, &address, 0, nil, size, &volume );
address.mElement = channels[ 0 ];
// returns noErr, but doesn't affect my input gain
err = AudioObjectSetPropertyData( deviceID, &address, 0, nil, size, &volume );
address.mElement = channels[ 1 ];
// success! correctly sets the input device volume.
err = AudioObjectSetPropertyData( deviceID, &address, 0, nil, size, &volume );
}
return err;
}
otool -tV /System/Library/PreferencePanes/Sound.prefPane/Contents/MacOS/Sound | bbedit
Audio
以查看调用了哪些方法(如果您没有bbedit,则每个Mac开发人员都应使用IMO,将其转储到文件中并在其他文本编辑器中打开)。
AudioObjectSetPropertyData
,
kAudioDevicePropertyVolumeScalar
等),以查找示例用法。
关于objective-c - 如何在内置输入(OSX Core音频/音频单元)上设置输入电平(增益)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11041335/
我的音频分析功能在 iPad (2) 上的响应比在 iPhone (4) 上的响应更好。它似乎对 iPad 上更柔和的声音很敏感,而 iPhone 需要更大声的输入才能正确响应。无论这是因为麦克风放置
我试图像这样在 Android 上获取麦克风的振幅电平: MediaRecorder recorder = new MediaRecorder(); recorder.setAudioSource(M
我想知道我的手机的tx level。我有一个 AT 命令终端,我只想要 AT 命令来获取 tx level。 我尝试了这个命令: AT*PSENGI=2 但是调制解调器返回: Error 此命令还返回
我真的很想看到一个独占模式音频应用程序的明确示例,该应用程序在一定的输入电平(音量)下从线路输入(不是麦克风)获取输入,并尽最大努力实现这一目标。 (首选 C++,但有总比没有好) 这听起来应该很简单
我正在构建一个 dB 计作为我正在创建的应用程序的一部分,我已经从我的 iPhone 上的麦克风接收峰值和平均功率(值范围从 -60 到 0.4),现在我需要弄清楚如何将这些功率级别转换为类似于此图表
我是一名优秀的程序员,十分优秀!