我无法使用MixerUnit和OutputUnit从AUGraph获取音频。
我已经建立了一个混合2个音频文件的图表。这是我创建图形的代码。我没有任何错误。
NSLog(@"initializeAUGraph");
mAudioFormat = [[AVAudioFormat alloc] initWithCommonFormat:AVAudioPCMFormatFloat32
sampleRate:kSampleRate
channels:2
interleaved:NO];
CheckError(NewAUGraph(&mGraph),"Error creating new graph");
AUNode outputNode;
AudioComponentDescription outputcd = {0};
outputcd.componentType = kAudioUnitType_Output;
outputcd.componentSubType = kAudioUnitSubType_DefaultOutput;
outputcd.componentManufacturer = kAudioUnitManufacturer_Apple;
AudioComponent comp = AudioComponentFindNext(NULL, &outputcd);
if (comp == NULL) {
printf ("can't get output unit"); exit (-1);
}
AUNode mixerNode;
AudioComponentDescription mixerDescription = {0};
mixerDescription.componentType = kAudioUnitType_Mixer;
mixerDescription.componentSubType = kAudioUnitSubType_MultiChannelMixer;
mixerDescription.componentManufacturer = kAudioUnitManufacturer_Apple;
CheckError(AUGraphAddNode(mGraph, &outputcd, &outputNode), "Error adding Output node to graph");
CheckError(AUGraphAddNode(mGraph, &mixerDescription, &mixerNode), "Error adding Mixer mode to graph");
CheckError(AUGraphOpen(mGraph), "AUGraphOpen failed");
CheckError(AUGraphConnectNodeInput(mGraph, mixerNode, 0, outputNode, 0),"AUGraphConnectModeInput");
CheckError(AUGraphNodeInfo(mGraph, mixerNode, NULL, &mMixer), "Error loading mixer node info");
CheckError(AUGraphNodeInfo(mGraph, outputNode, NULL, &mOutput), "Error loading output node info");
UInt32 numbuses = 2;
CheckError(AudioUnitSetProperty(mMixer, kAudioUnitProperty_ElementCount, kAudioUnitScope_Input, 0, &numbuses, sizeof(numbuses)) , "Error setting prop to mixer");
for (int i = 0; i < numbuses; ++i) {
// setup render callback struct
AURenderCallbackStruct renderCallbackStruct;
renderCallbackStruct.inputProc = &renderAudioInput;
renderCallbackStruct.inputProcRefCon = mSoundBuffer;
// Set a callback for the specified node's specified input
CheckError((AUGraphSetNodeInputCallback(mGraph, mixerNode, i, &renderCallbackStruct))," AUGraphSetNodeInputCallback failed in cycle");
//Set the input stream format property
CheckError(AudioUnitSetProperty(mMixer, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, i, mAudioFormat.streamDescription, sizeof(AudioStreamBasicDescription)),"Set proprety in cycle error");
}
CheckError(AudioUnitSetProperty(mMixer, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 0, mAudioFormat.streamDescription, sizeof(AudioStreamBasicDescription)), "AudioUnitSetProperty mixer stream format failed");
/*CheckError(AudioUnitSetProperty(mOutput, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 0, mAudioFormat.streamDescription, sizeof(AudioStreamBasicDescription)), "AudioUnitSetProperty output stream format failed");*/
CheckError(AUGraphInitialize(mGraph),"AUGraphInitialize failed");
控制台输出:
2018-05-23 01:19:22.676281+0300 Mixer2Mac[37249:1285521] loadAudioFiles
2018-05-23 01:19:22.676918+0300 Mixer2Mac[37249:1285521] 2017569 frames in GuitarMonoSTP.aif
2018-05-23 01:19:22.676939+0300 Mixer2Mac[37249:1285521] 2017569 frames after sample ratio multiplied in GuitarMonoSTP.aif
2018-05-23 01:19:22.682277+0300 Mixer2Mac[37249:1285521] 2017569 frames in DrumsMonoSTP.aif
2018-05-23 01:19:22.682303+0300 Mixer2Mac[37249:1285521] 2017569 frames after sample ratio multiplied in DrumsMonoSTP.aif
2018-05-23 01:19:22.687415+0300 Mixer2Mac[37249:1285521] initializeAUGraph
2018-05-23 01:19:23.860541+0300 Mixer2Mac[37249:1285521] startPlaying
2018-05-23 01:19:24.830917+0300 Mixer2Mac[37249:1285521] stopPlaying
2018-05-23 01:19:28.218856+0300 Mixer2Mac[37249:1285521] startPlaying
2018-05-23 01:20:13.009934+0300 Mixer2Mac[37249:1285693] Starting over at frame 0 for bus 0
2018-05-23 01:20:13.010091+0300 Mixer2Mac[37249:1285693] Starting over at frame 0 for bus 1
我看到文件正在控制台中播放,但是听不到声音。该应用程序适用于MacO,不适用于iOS。可能是什么问题?
编辑1
这是回调函数。它被称为。我可以在控制台中看到它。打印“LOLLOL”
static OSStatus renderAudioInput(void *inRefCon, AudioUnitRenderActionFlags *actionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberOfFrames, AudioBufferList *ioData)
{
NSLog(@"LOLLOLO");
SoundBufferPtr soundBuffer = (SoundBufferPtr)inRefCon;
//NSLog(@"numberOfFrames: %d", inNumberOfFrames);
//Get the frame to start at and total number of samples
UInt32 sample = soundBuffer[inBusNumber].sampleNumber;
UInt32 startSample = sample;
UInt32 bufferTotalSamples = soundBuffer[inBusNumber].numberOfFrames;
//Get a reference to the input data buffer
Float32 *inputData = soundBuffer[inBusNumber].data; // audio data buffer
//Get references to the channel buffers
Float32 *outLeft = (Float32 *)ioData->mBuffers[0].mData; // output audio buffer for Left channel
Float32 *outRight = (Float32 *)ioData->mBuffers[1].mData; // output audio buffer for Right channel
//Loop thru the number of frames and set the output data from the input data.
//Use the left channel for bus 0 (guitar) and right channel for bus 1 (drums) to distiguish for example
for (UInt32 i = 0; i < inNumberOfFrames; ++i) {
if (inBusNumber == 0) {
outLeft[i] = inputData[sample++];
outRight[i] = 0;
} else { //inBusNumber == 1
outLeft[i] = 0;
outRight[i] = inputData[sample++];
}
//If the sample is beyond the total number of samples in the loop, start over at the beginning
if (sample > bufferTotalSamples) {
// start over from the beginning of the data, our audio simply loops
sample = 0;
NSLog(@"Starting over at frame 0 for bus %d", (int)inBusNumber);
}
}
//Set the sample number in the sound buffer struct so we know which frame playback is on
soundBuffer[inBusNumber].sampleNumber = sample;
performFFT(&inputData[startSample], inNumberOfFrames, soundBuffer, inBusNumber);
return noErr;
}
NSLog(“LOLLOL”)之后的输出:
2018-05-23 12:56:49.297251+0300 Mixer2Mac[39034:1368659] loadAudioFiles
2018-05-23 12:56:49.298417+0300 Mixer2Mac[39034:1368659] 2017569 frames in GuitarMonoSTP.aif
2018-05-23 12:56:49.298445+0300 Mixer2Mac[39034:1368659] 2017569 frames after sample ratio multiplied in GuitarMonoSTP.aif
2018-05-23 12:56:49.309126+0300 Mixer2Mac[39034:1368659] 2017569 frames in DrumsMonoSTP.aif
2018-05-23 12:56:49.309156+0300 Mixer2Mac[39034:1368659] 2017569 frames after sample ratio multiplied in DrumsMonoSTP.aif
2018-05-23 12:56:49.316572+0300 Mixer2Mac[39034:1368659] initializeAUGraph
2018-05-23 12:56:51.206301+0300 Mixer2Mac[39034:1368659] startPlaying
2018-05-23 12:56:51.229420+0300 Mixer2Mac[39034:1368816] LOLLOLO
2018-05-23 12:56:51.229692+0300 Mixer2Mac[39034:1368816] LOLLOLO
2018-05-23 12:56:51.240977+0300 Mixer2Mac[39034:1368816] LOLLOLO
2018-05-23 12:56:51.241162+0300 Mixer2Mac[39034:1368816] LOLLOLO
最佳答案
新的,改进的答案
This answer并不是完全重复的,因为您有两个总线而不是一个,它指出在macOS上混音器的输入量默认为零,并且注释添加了输出量也是如此,因此请修改您的代码,如下所示:
for (int i = 0; i < numbuses; ++i) {
// setup render callback struct
AURenderCallbackStruct renderCallbackStruct;
renderCallbackStruct.inputProc = &renderAudioInput;
renderCallbackStruct.inputProcRefCon = mSoundBuffer;
// Set a callback for the specified node's specified input
CheckError((AUGraphSetNodeInputCallback(mGraph, mixerNode, i, &renderCallbackStruct))," AUGraphSetNodeInputCallback failed in cycle");
//Set the input stream format property
CheckError(AudioUnitSetProperty(mMixer, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, i, mAudioFormat.streamDescription, sizeof(AudioStreamBasicDescription)),"Set proprety in cycle error");
// NEW! mixer input volume defaults to zero on macOS. Why!?!?
CheckError(AudioUnitSetParameter(mMixer, kMultiChannelMixerParam_Volume, kAudioUnitScope_Input, i, 1.0, 0), "AudioUnitSetParameter failed");
}
// NEW! Output defaults to zero too!
CheckError(AudioUnitSetParameter(mMixer, kMultiChannelMixerParam_Volume, kAudioUnitScope_Output, 0, 1.0, 0), "AudioUnitSetParameter failed");
The documentation似乎错误地指出
kMultiChannelMixerParam_Volume
默认为1.0:全球范围。该值应为0.0到1.0之间的数字。默认值为1.0。
但是,这是针对全球范围的。
哦,您是在启动它
初始化后,您需要启动音频图:
OSStatus status = AUGraphStart(mGraph);
关于xcode - CoreAudio AUGraph没有声音,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50477242/