【发布时间】:2018-11-30 09:57:54
【问题描述】:
我在渲染回调函数中使用 AudioUnitRender() 函数在 iphone 中实时从麦克风获取音频数据
err = AudioUnitRender(player->outputUnit, ioActioanFlags, inTimeStamp, 1, inNumberFrames, ioData);
自动调用回调函数时音频数据进入ioData。我正在使用ioData中返回的音频数据,如下所示:
for(frame = 0; frame<inNumberFrames; ++frame){
Float32 *data = (Float32*)ioData->mBuffers[0].mData;
myvar[k++] = (data)[frame];
.
.
.
}
这里的myvar 是Float32 类型的数组。我猜想输入音频在 +1.0/-1.0 范围内,因为 myvar[] 中的值总是在该范围内。我最近发现,如果我在麦克风附近发出响亮的声音,有时我会在 myvar[] 中得到超出 +1.0/-1.0 范围的值。
AudioUnitRender() 作为麦克风音频数据返回的 Float32 类型数据的具体范围是多少?
是否可以将 AudioUnitRender() 返回的任何原始音频作为整数获取? android 中的AudioRecord 类将原始麦克风音频作为有符号短数字(16 位)提供给我。我在目标 C 中寻找它在 ios 中的等价物。
--- 编辑 1 ---
当前用于音频的配置如下:
// Configure the audio session
AVAudioSession *sessionInstance = [AVAudioSession sharedInstance];
// we are going to play and record so we pick that category
NSError *error = nil;
[sessionInstance setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
// set the buffer duration to 5 ms
NSTimeInterval bufferDuration = .004; // with setPreferredSampleRate:16000 gives inNumberFrames = 64 in SineWaveRenderProc()
// NSTimeInterval bufferDuration = .016; // with setPreferredSampleRate:16000 gives inNumberFrames = 256 in SineWaveRenderProc() ;; NOTE: 0.004*4 = 0.016
[sessionInstance setPreferredIOBufferDuration:bufferDuration error:&error];
// set the session's sample rate
// [sessionInstance setPreferredSampleRate:44100 error:&error]; // ORIGINAL // inNumberFrames = 256 in SineWaveRenderProc() with bufferDuration = .005; above
[sessionInstance setPreferredSampleRate:16000 error:&error]; // inNumberFrames = 64 in SineWaveRenderProc() with bufferDuration = .005; above
// activate the audio session
[[AVAudioSession sharedInstance] setActive:YES error:&error];
// XThrowIfError((OSStatus)error.code, "couldn't set session active");
// NOTE: looks like this is necessary
UInt32 one = 1;
AudioUnitSetProperty(player->outputUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, &one, sizeof(one) );
AudioUnitSetProperty(player->outputUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, 0, &one, sizeof(one) );
【问题讨论】:
-
范围应为 -1.0/1.0,但如果您使用的采样率与数据源或内置 iPhone 硬件不同,重采样滤波器可能会在该范围之外产生振铃/波纹。
-
我使用的是 16000 Hz,但我认为这是标准值
-
@hotpaw 我在用于设置采样频率的配置中进行了编辑
-
较新的 iOS 设备上的音频硬件似乎以 48ks/s 的速度运行。所有其他采样率可能会从 48k(或更高?)过滤重新采样转换。
标签: ios objective-c iphone core-audio