所以我根据这里的一些帖子拼凑了一些录制音频的例程。我引用的帖子在这里和这里,以及阅读他们引用的网站。
我的设置:我有一个现有的 AUGraph:(几个 AUSamplers -> Mixer -> RemoteIO)。AUSampler 连接到 MusicPlayer 实例中的轨道。一切正常,但我想添加录音。
录音工作正常,但生成的 .caf 音调/速度变慢 + 音质差。我指定的格式一定有问题吗?
有人可以关注这个并告诉我我在哪里设置格式不正确吗?
编辑:这可能是立体声/单声道问题吗?我的意思是单声道录音。
我将 RemoteIO 实例上的流格式设置为:
AudioStreamBasicDescription audioFormat;
audioFormat.mSampleRate = 44100.00;
audioFormat.mFormatID = kAudioFormatLinearPCM;
audioFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
audioFormat.mFramesPerPacket = 1;
audioFormat.mChannelsPerFrame = 1;
audioFormat.mBitsPerChannel = 16;
audioFormat.mBytesPerPacket = 2;
audioFormat.mBytesPerFrame = 2;
// Apply format
result = AudioUnitSetProperty(ioUnit,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Output,
kInputBus,
&audioFormat,
sizeof(audioFormat));
然后从一个按钮操作我创建一个 fileRef 并将一个 renderCallback 附加到 RemoteIO 实例:
- (void)startRecording
{
OSStatus result;
AudioStreamBasicDescription audioFormat;
audioFormat.mSampleRate = 44100.00;
audioFormat.mFormatID = kAudioFormatLinearPCM;
audioFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
audioFormat.mFramesPerPacket = 1;
audioFormat.mChannelsPerFrame = 1;
audioFormat.mBitsPerChannel = 16;
audioFormat.mBytesPerPacket = 2;
audioFormat.mBytesPerFrame = 2;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *destinationFilePath = [[NSString alloc] initWithFormat: @"%@/output.caf", documentsDirectory];
CFURLRef destinationURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
(__bridge CFStringRef)destinationFilePath,
kCFURLPOSIXPathStyle,
false);
result = ExtAudioFileCreateWithURL(destinationURL,
kAudioFileWAVEType,
&audioFormat,
NULL,
kAudioFileFlags_EraseFile,
&extAudioFileRef);
CFRelease(destinationURL);
NSAssert(result == noErr, @"Couldn't create file for writing");
result = ExtAudioFileSetProperty(extAudioFileRef,
kExtAudioFileProperty_ClientDataFormat,
sizeof(AudioStreamBasicDescription),
&audioFormat);
NSAssert(result == noErr, @"Couldn't create file for format");
result = ExtAudioFileWriteAsync(extAudioFileRef, 0, NULL);
NSAssert(result == noErr, @"Couldn't initialize write buffers for audio file");
printf("Adding render to remoteIO \n");
result = AudioUnitAddRenderNotify(ioUnit, renderCallback, (__bridge void*)self);
if (result) {[self printErrorMessage: @"AudioUnitAddRenderNotify" withStatus: result]; return;}
}
最后,在我的 rendercallback 中,我在 postRender 阶段写出数据:
static OSStatus renderCallback (void * inRefCon,
AudioUnitRenderActionFlags * ioActionFlags,
const AudioTimeStamp * inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList * ioData)
{
OSStatus result;
if (*ioActionFlags == kAudioUnitRenderAction_PostRender){
double timeInSeconds = inTimeStamp->mSampleTime / kSampleRate;
printf("%fs inBusNumber: %lu inNumberFrames: %lu \n", timeInSeconds, inBusNumber, inNumberFrames);
MusicPlayerController* THIS = (__bridge MusicPlayerController *)inRefCon;
result = ExtAudioFileWriteAsync(THIS->extAudioFileRef, inNumberFrames, ioData);
if(result) printf("ExtAudioFileWriteAsync %ld \n", result);
}
return noErr;
}