Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使用特效音频单元?_Ios_Core Audio_Audiounit - Fatal编程技术网

Ios 如何使用特效音频单元?

Ios 如何使用特效音频单元?,ios,core-audio,audiounit,Ios,Core Audio,Audiounit,我试着播放8000hz的样本并通过一个特效传递它们。 我的目标是提高音频音量(示例代码还不能做到这一点)。 我想我需要一个特效音频单元,链接到远程音频单元。 我还了解到效果单元对其可处理的格式要求非常严格,主要要求44.1khz采样、浮点和32位采样(. 因此,我添加了一个转换器单元。 另外,由于我认为(虽然不确定)iOS无法播放32位样本(感谢@hotpaw2!),我添加了另一个转换回16位。 问题是,在初始化音频图时,我总是得到错误-10868。 我在没有最后一个转换单元的情况下也得到了它。

我试着播放8000hz的样本并通过一个特效传递它们。
我的目标是提高音频音量(示例代码还不能做到这一点)。
我想我需要一个特效音频单元,链接到远程音频单元。
我还了解到效果单元对其可处理的格式要求非常严格,主要要求44.1khz采样、浮点和32位采样(.
因此,我添加了一个转换器单元。
另外,由于我认为(虽然不确定)iOS无法播放32位样本(感谢@hotpaw2!),我添加了另一个转换回16位。 问题是,在初始化音频图时,我总是得到错误-10868。
我在没有最后一个转换单元的情况下也得到了它。
如果我将转换单元连接到输出(无效果单元),一切正常(8k样本播放正常)。
发生什么事了

    /* Must use play & record category, for reasons beyond the scope of this question */
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDuckOthers | AVAudioSessionCategoryOptionDefaultToSpeaker | AVAudioSessionCategoryOptionAllowBluetooth error:nil];
    NSError* err = nil;
    if (![[AVAudioSession sharedInstance] setPreferredSampleRate:44100 error:&err]){
        NSLog(@"%@",err);
    }
    AudioUnit effect,convert,output,oconvert;
    AUNode neffect,nconvert,noutput,noconvert;
    AUGraph graph;
    AudioComponentDescription deffect,dconvert,doutput;
    AudioStreamBasicDescription in_format,out_format,effect_format;
    // Formats
    memset(&in_format,0,sizeof(in_format));
    memset(&out_format, 0, sizeof(out_format));
    memset(&effect_format, 0, sizeof(effect_format));
    in_format.mSampleRate = 8000;
    in_format.mChannelsPerFrame = 1;
    in_format.mFormatID = kAudioFormatLinearPCM;
    in_format.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked;
    in_format.mBitsPerChannel = 16;
    in_format.mFramesPerPacket = 1;
    in_format.mBytesPerFrame = in_format.mChannelsPerFrame * (in_format.mBitsPerChannel / 8);
    in_format.mBytesPerPacket = in_format.mBytesPerFrame * in_format.mFramesPerPacket;
    out_format.mSampleRate = 44100;
    out_format.mChannelsPerFrame = 1;
    out_format.mFormatID = kAudioFormatLinearPCM;
    out_format.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked;
    out_format.mBitsPerChannel = 16;
    out_format.mFramesPerPacket = 1;
    out_format.mBytesPerFrame = out_format.mChannelsPerFrame * (out_format.mBitsPerChannel / 8);
    out_format.mBytesPerPacket = out_format.mBytesPerFrame * out_format.mFramesPerPacket;
    effect_format.mSampleRate = 44100;
    effect_format.mChannelsPerFrame = 1;
    effect_format.mFormatID = kAudioFormatLinearPCM;
    effect_format.mFormatFlags = kAudioFormatFlagsNativeFloatPacked;
    effect_format.mBitsPerChannel = 32;
    effect_format.mFramesPerPacket = 1;
    effect_format.mBytesPerFrame = effect_format.mChannelsPerFrame * (effect_format.mBitsPerChannel / 8);
    effect_format.mBytesPerPacket = effect_format.mBytesPerFrame * effect_format.mFramesPerPacket;

    // Descriptions
    memset(&doutput, 0, sizeof(doutput));
    memset(&deffect, 0, sizeof(deffect));
    memset(&dconvert, 0, sizeof(dconvert));
    doutput.componentType = kAudioUnitType_Output;
    doutput.componentSubType = kAudioUnitSubType_RemoteIO;
    doutput.componentManufacturer = deffect.componentManufacturer = dconvert.componentManufacturer = kAudioUnitManufacturer_Apple;
    dconvert.componentType = kAudioUnitType_FormatConverter;
    dconvert.componentSubType = kAudioUnitSubType_AUConverter;
    deffect.componentType = kAudioUnitType_Effect;
    deffect.componentSubType = kAudioUnitSubType_DynamicsProcessor;
    // Create graph
    SdCheck(NewAUGraph(&graph));

    // Create nodes;
    SdCheck(AUGraphAddNode(graph, &deffect, &neffect));
    SdCheck(AUGraphAddNode(graph, &doutput, &noutput));
    SdCheck(AUGraphAddNode(graph, &dconvert, &nconvert));
    SdCheck(AUGraphAddNode(graph, &dconvert, &noconvert));
    // Open graph
    SdCheck(AUGraphOpen(graph));
    // Get units
    SdCheck(AUGraphNodeInfo(graph, neffect,NULL, &effect));
    SdCheck(AUGraphNodeInfo(graph, noutput,NULL, &output));
    SdCheck(AUGraphNodeInfo(graph, nconvert,NULL, &convert));
    SdCheck(AUGraphNodeInfo(graph, noconvert,NULL, &oconvert));
    // Set formats
    SdCheck(AudioUnitSetProperty (output,
                                  kAudioUnitProperty_StreamFormat,
                                  kAudioUnitScope_Input,
                                  0,
                                  &out_format,
                                  sizeof(out_format)));
    SdCheck(AudioUnitSetProperty (convert,
                                  kAudioUnitProperty_StreamFormat,
                                  kAudioUnitScope_Input,
                                  0,
                                  &effect_format,
                                  sizeof(effect_format)));
    SdCheck(AudioUnitSetProperty (convert,
                                  kAudioUnitProperty_StreamFormat,
                                  kAudioUnitScope_Output,
                                  0,
                                  &in_format,
                                  sizeof(in_format)));
    SdCheck(AudioUnitSetProperty (effect,
                                  kAudioUnitProperty_StreamFormat,
                                  kAudioUnitScope_Input,
                                  0,
                                  &effect_format,
                                  sizeof(effect_format)));
    SdCheck(AudioUnitSetProperty (effect,
                                  kAudioUnitProperty_StreamFormat,
                                  kAudioUnitScope_Output,
                                  0,
                                  &effect_format,
                                  sizeof(effect_format)));
    SdCheck(AudioUnitSetProperty (oconvert,
                                  kAudioUnitProperty_StreamFormat,
                                  kAudioUnitScope_Input,
                                  0,
                                  &out_format,
                                  sizeof(out_format)));
    SdCheck(AudioUnitSetProperty (oconvert,
                                  kAudioUnitProperty_StreamFormat,
                                  kAudioUnitScope_Output,
                                  0,
                                  &effect_format,
                                  sizeof(effect_format)));

    // Connect nodes
    SdCheck(AUGraphConnectNodeInput(graph, nconvert, 0, neffect, 0));
    SdCheck(AUGraphConnectNodeInput(graph, neffect, 0, noconvert, 0));
    SdCheck(AUGraphConnectNodeInput(graph, noconvert, 0, noutput, 0));
    // Set render callback
    AURenderCallbackStruct input;
    memset(&input, 0, sizeof(input));
    input.inputProc = SdInputProc;
    input.inputProcRefCon = (__bridge void*)self;
    SdCheck(AUGraphSetNodeInputCallback(graph, nconvert, 0, &input));
    // Initialize graph
    /*** The following fails with error -10868 (unsupported format) ***/
    SdCheck(AUGraphInitialize(graph));

iOS上的大多数效果音频单元要求在其输入和输出连接上使用32位浮点格式。您的示例代码试图将效果单元配置为16位整数I/O,但这不起作用。

iOS上的大多数效果音频单元要求在其输入和输出连接上使用32位浮点格式s、 您的示例代码试图使用16位整数I/O配置效果单元,但这不起作用。

谢谢您的回答!我已尝试将效果单元格式设置为32位浮点(尝试了
kAudioFormatFlagsNativeFloatPacked
)。这次在初始化图形时出现相同错误(设置格式有效)。尝试在效果和输出之间插入另一个转换器,以更改为16位采样,得到了相同的结果。是否将连接到效果单元的单元的范围设置为相同的格式(32位浮点)?是的,我做了,它现在像:8k样本->转换->32位样本->效果->另一个转换->16位->远程io,请参阅修订后的代码。顺便说一句-up投票了你的答案,因为它确实有帮助-32位fp在设置效果单位的格式时似乎有帮助,但是初始化仍然失败:-(感谢您的回答!我已尝试将效果单位格式设置为32位浮点(尝试了
kAudioFormatFlagsNativeFloatPacked
)。这次在初始化图形时遇到了相同的错误(设置格式有效)。尝试在效果和输出之间插入另一个转换器,以更改为16位采样,得到了相同的结果。是否将连接到效果单元的单元的范围设置为相同的格式(32位浮点)?是的,我做了,它现在像:8k样本->转换->32位样本->效果->另一个转换->16位->远程io,请参阅修订后的代码。顺便说一句-up投票了你的答案,因为它确实有帮助-32位fp在设置效果单位的格式时似乎有帮助,但是初始化仍然失败:-(