美文网首页
IOS Audio Unit播放PCM数据

IOS Audio Unit播放PCM数据

作者: huisedediao | 来源:发表于2018-07-05 10:19 被阅读0次

    示例代码: XBVoiceTool

    步骤:

    1.设置AVAudioSession
    2.初始化audioUnit
    3.设置输出流格式
    4.设置回调
    5.在回调中提供数据

    设置AVAudioSession
        //设置session
        NSError *error = nil;
        AVAudioSession* session = [AVAudioSession sharedInstance];
        [session setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:&error];
        [session setActive:YES error:nil];
    
    初始化audioUnit
        //初始化audioUnit
        AudioComponentDescription outputDesc;
        outputDesc.componentType = kAudioUnitType_Output;
        outputDesc.componentSubType = kAudioUnitSubType_VoiceProcessingIO;
        outputDesc.componentManufacturer = kAudioUnitManufacturer_Apple;
        outputDesc.componentFlags = 0;
        outputDesc.componentFlagsMask = 0;
        AudioComponent outputComponent = AudioComponentFindNext(NULL, &outputDesc);
        AudioComponentInstanceNew(outputComponent, &audioUnit);
    
    设置输出流格式
        //设置输出格式
        int mFramesPerPacket = 1;
        int mBytesPerFrame = channel * bit / 8;
        
        AudioStreamBasicDescription streamDesc;
        streamDesc.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsNonInterleaved;
        streamDesc.mFormatID = kAudioFormatLinearPCM;
        streamDesc.mSampleRate = rate;
        streamDesc.mFramesPerPacket = mFramesPerPacket;
        streamDesc.mChannelsPerFrame = channel;
        streamDesc.mBitsPerChannel = bit;
        streamDesc.mBytesPerFrame = mBytesPerFrame;
        streamDesc.mBytesPerPacket = mBytesPerFrame * mFramesPerPacket;
        
        OSStatus status = AudioUnitSetProperty(audioUnit,
                                               kAudioUnitProperty_StreamFormat,
                                               kAudioUnitScope_Input,
                                               kOutputBus,
                                               &streamDesc,
                                               sizeof(streamDesc));
        CheckError(status, "SetProperty StreamFormat failure");
    
    设置回调
        //设置回调
        AURenderCallbackStruct outputCallBackStruct;
        outputCallBackStruct.inputProc = outputCallBackFun;
        outputCallBackStruct.inputProcRefCon = (__bridge void * _Nullable)(self);
        status = AudioUnitSetProperty(audioUnit,
                                      kAudioUnitProperty_SetRenderCallback,
                                      kAudioUnitScope_Input,
                                      kOutputBus,
                                      &outputCallBackStruct,
                                      sizeof(outputCallBackStruct));
        CheckError(status, "SetProperty EnableIO failure");
    
    在回调中提供数据

    这里设置成在player 的 bl_input回调中提供数据

    static OSStatus outputCallBackFun(    void *                            inRefCon,
                        AudioUnitRenderActionFlags *    ioActionFlags,
                        const AudioTimeStamp *            inTimeStamp,
                        UInt32                            inBusNumber,
                        UInt32                            inNumberFrames,
                        AudioBufferList * __nullable    ioData)
    {
        memset(ioData->mBuffers[0].mData, 0, ioData->mBuffers[0].mDataByteSize);
    //    memset(ioData->mBuffers[1].mData, 0, ioData->mBuffers[1].mDataByteSize);
        
        XBAudioUnitPlayer *player = (__bridge XBAudioUnitPlayer *)(inRefCon);
        if (player.bl_input)
        {
            player.bl_input(ioData);
        }
        return noErr;
    }
    

    相关文章

      网友评论

          本文标题:IOS Audio Unit播放PCM数据

          本文链接:https://www.haomeiwen.com/subject/wgpuuftx.html