ios - 核心音频 : file playback render callback function

标签 ios callback core-audio audiounit

我正在使用 RemoteIO Audio Unit 在我的应用程序中通过 kAudioUnitProperty_ScheduledFileIDs 播放音频。 音频文件为 PCM 格式。我如何为这种情况实现渲染回调函数,以便我可以手动修改缓冲区样本?
这是我的代码:

static AudioComponentInstance audioUnit;

AudioComponentDescription desc;

desc.componentType = kAudioUnitType_Output;
desc.componentSubType = kAudioUnitSubType_RemoteIO;
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
desc.componentFlags = 0;
desc.componentFlagsMask = 0;

AudioComponent comp = AudioComponentFindNext(NULL, &desc);

CheckError(AudioComponentInstanceNew(comp, &audioUnit), "error AudioComponentInstanceNew");


NSURL *playerFile = [[NSBundle mainBundle] URLForResource:@"short" withExtension:@"wav"];

AudioFileID audioFileID;

CheckError(AudioFileOpenURL((__bridge CFURLRef)playerFile, kAudioFileReadPermission, 0, &audioFileID), "error AudioFileOpenURL");

// Determine file properties
UInt64 packetCount;
UInt32 size = sizeof(packetCount);
CheckError(AudioFileGetProperty(audioFileID, kAudioFilePropertyAudioDataPacketCount, &size, &packetCount),
                "AudioFileGetProperty(kAudioFilePropertyAudioDataPacketCount)");

AudioStreamBasicDescription dataFormat;
size = sizeof(dataFormat);
CheckError(AudioFileGetProperty(audioFileID, kAudioFilePropertyDataFormat, &size, &dataFormat),
                "AudioFileGetProperty(kAudioFilePropertyDataFormat)");

// Assign the region to play
ScheduledAudioFileRegion region;
memset (&region.mTimeStamp, 0, sizeof(region.mTimeStamp));
region.mTimeStamp.mFlags = kAudioTimeStampSampleTimeValid;
region.mTimeStamp.mSampleTime = 0;
region.mCompletionProc = NULL;
region.mCompletionProcUserData = NULL;
region.mAudioFile = audioFileID;
region.mLoopCount = 0;
region.mStartFrame = 0;
region.mFramesToPlay = (UInt32)packetCount * dataFormat.mFramesPerPacket;
CheckError(AudioUnitSetProperty(audioUnit, kAudioUnitProperty_ScheduledFileRegion, kAudioUnitScope_Global, 0, &region, sizeof(region)),
                "AudioUnitSetProperty(kAudioUnitProperty_ScheduledFileRegion)");

// Prime the player by reading some frames from disk
UInt32 defaultNumberOfFrames = 0;
CheckError(AudioUnitSetProperty(audioUnit, kAudioUnitProperty_ScheduledFilePrime, kAudioUnitScope_Global, 0, &defaultNumberOfFrames, sizeof(defaultNumberOfFrames)),
                "AudioUnitSetProperty(kAudioUnitProperty_ScheduledFilePrime)");

AURenderCallbackStruct callbackStruct;
callbackStruct.inputProc = MyCallback;
callbackStruct.inputProcRefCon = (__bridge void * _Nullable)(self);

CheckError(AudioUnitSetProperty(audioUnit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &callbackStruct, sizeof(callbackStruct)), "error AudioUnitSetProperty[kAudioUnitProperty_setRenderCallback]");

CheckError(AudioUnitInitialize(audioUnit), "error AudioUnitInitialize");

回调函数:

static OSStatus MyCallback(void *inRefCon,
                             AudioUnitRenderActionFlags *ioFlags,
                             const AudioTimeStamp *inTimeStamp,
                             UInt32 inBusNumber,
                             UInt32 inNumberFrames,
                             AudioBufferList *ioData){

    printf("my callback");  
    return noErr;
}

音频单元在按下按钮时开始播放:

- (IBAction)playSound:(id)sender {

 CheckError(AudioOutputUnitStart(audioUnit), "error AudioOutputUnitStart");

}

此代码在编译期间失败并出现 kAudioUnitErr_InvalidProperty(-10879) 错误。目标是修改从 AudioFileID 读取的缓冲区样本,并将结果发送给扬声器。

最佳答案

鉴于您刚刚熟悉核心音频,我建议您首先让 RemoteIO 回调独立于文件播放器工作。只需删除所有与文件播放器相关的代码,然后尝试先使其正常工作即可。

然后,一旦您完成了该工作,就可以继续合并您的文件播放器。

据我所知,这是错误的,我认为您将音频文件服务 API 与音频单元混淆了。该 API 用于将文件读入缓冲区,您可以手动将其提供给 RemoteIO,如果您确实想走这条路,请使用扩展音频文件服务 API,这要容易得多。 kAudioUnitProperty_ScheduledFileRegion 属性应该在文件播放器音频单元上调用。要获得其中之一,您需要以与 remmoteIO 相同的方式创建它,但 AudioComponentDescription 的 componentSubType 和 componentType 分别是 kAudioUnitSubType_AudioFilePlayerkAudioUnitType_Generator 。然后,一旦您拥有该设备,您需要使用 kAudioUnitProperty_MakeConnection 属性将其连接到 RemoteIO。

但是说真的,首先让您的remoteIO回调正常工作,然后尝试制作一个文件播放器音频单元并连接它(没有回调),然后从那里开始。

独立询问有关每个步骤的非常具体的问题,发布您尝试过但不起作用的代码,您将获得大量帮助。

关于ios - 核心音频 : file playback render callback function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37773506/

相关文章:

ios - 在 Xcode 上归档时按顺序使用 max() 和 min() 的 Swift 问题

node.js - 如何从异步调用返回值

c - Printf - 控制 %.2f (float32) 的输出

ios - 过程中的 UIProgressView 栏

ios - 如何创建合适的 iOS CoreData 数据库模型?

Linux 中的 C++ : In what forked-task context should a timer callback execute?

php - 服务器到客户端回调 - PHP/JavaScript

ios - 为什么插入耳机时 iPhone 底部麦克风的音量与不插入时不同?

ios - 读取音频文件,执行过滤器(即混响),然后写入音频文件而不在 iOS 上播放

ios - 在 iOS 上加密和解密 Cookie