我今天有很多关于iOS 和音频单元的有趣的东西,并且发现了很多有用的资源(包括在内)。
首先,我对某些事情感到困惑:真的有必要用混音器单元创建一个音频图来记录应用程序播放的声音吗?
或者使用ObjectAL播放声音(或更简单的 AVAudioPlayer 调用)并创建一个远程 io 单元并使用录音回调在正确的总线上寻址就足够了?
其次,一个更程序化的问题!由于我对Audio Units的概念还不是很满意,所以我尝试调整Apple Mixer Host 项目,使其能够录制结果混音。显然,我尝试使用Michael Tyson RemoteIO 帖子来做到这一点。
我在回调函数上得到一个 EXC_BAD_ACCESS :
static OSStatus recordingCallback (void *inRefCon,
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData) {
AudioBufferList *bufferList; // <- Fill this up with buffers (you will want to malloc it, as it's a dynamic-length list)
EffectState *effectState = (EffectState *)inRefCon;
AudioUnit rioUnit = effectState->rioUnit;
OSStatus status;
// BELOW I GET THE ERROR
status = AudioUnitRender(rioUnit,
ioActionFlags,
inTimeStamp,
inBusNumber,
inNumberFrames,
bufferList);
if (noErr != status) { NSLog(@"AudioUnitRender error"); return noErr;}
// Now, we have the samples we just read sitting in buffers in bufferList
//ExtAudioFileWriteAsync(effectState->audioFileRef, inNumberFrames, bufferList);
return noErr;
}
在使用回调函数之前我在MixerHostAudio.h
typedef struct {
AudioUnit rioUnit;
ExtAudioFileRef audioFileRef;
} EffectState;
并在界面中创建:
AudioUnit iOUnit;
EffectState effectState;
AudioStreamBasicDescription iOStreamFormat;
...
@property AudioUnit iOUnit;
@property (readwrite) AudioStreamBasicDescription iOStreamFormat;
然后在实现文件MixerHostAudio.h 中:
#define kOutputBus 0
#define kInputBus 1
...
@synthesize iOUnit; // the Remote IO unit
...
result = AUGraphNodeInfo (
processingGraph,
iONode,
NULL,
&iOUnit
);
if (noErr != result) {[self printErrorMessage: @"AUGraphNodeInfo" withStatus: result]; return;}
// Enable IO for recording
UInt32 flag = 1;
result = AudioUnitSetProperty(iOUnit,
kAudioOutputUnitProperty_EnableIO,
kAudioUnitScope_Input,
kInputBus,
&flag,
sizeof(flag));
if (noErr != result) {[self printErrorMessage: @"AudioUnitSetProperty" withStatus: result]; return;}
// Describe format
iOStreamFormat.mSampleRate = 44100.00;
iOStreamFormat.mFormatID = kAudioFormatLinearPCM;
iOStreamFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
iOStreamFormat.mFramesPerPacket = 1;
iOStreamFormat.mChannelsPerFrame = 1;
iOStreamFormat.mBitsPerChannel = 16;
iOStreamFormat.mBytesPerPacket = 2;
iOStreamFormat.mBytesPerFrame = 2;
// Apply format
result = AudioUnitSetProperty(iOUnit,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Output,
kInputBus,
&iOStreamFormat,
sizeof(iOStreamFormat));
if (noErr != result) {[self printErrorMessage: @"AudioUnitSetProperty" withStatus: result]; return;}
result = AudioUnitSetProperty(iOUnit,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Input,
kOutputBus,
&iOStreamFormat,
sizeof(iOStreamFormat));
if (noErr != result) {[self printErrorMessage: @"AudioUnitSetProperty" withStatus: result]; return;}
effectState.rioUnit = iOUnit;
// Set input callback ----> RECORDING
AURenderCallbackStruct callbackStruct;
callbackStruct.inputProc = recordingCallback;
callbackStruct.inputProcRefCon = self;
result = AudioUnitSetProperty(iOUnit,
kAudioOutputUnitProperty_SetInputCallback,
kAudioUnitScope_Global,
kInputBus,
&callbackStruct,
sizeof(callbackStruct));
if (noErr != result) {[self printErrorMessage: @"AudioUnitSetProperty" withStatus: result]; return;}
但我不知道出了什么问题,也不知道如何挖掘。注意:存在EffectState结构是因为我还尝试集成BioAudio 项目从缓冲区写入文件的能力。
第三,我想知道是否有更容易的方法来录制我的 iPhone 应用程序播放的声音(即不包括麦克风)?