1

我是核心音频和远程 io 的新手。我需要编码和发送的大小为 320 字节的数据。也至少每秒 50 帧。这是我所做的:

    AudioComponentDescription desc;

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


    // Get component
    AudioComponent inputComponent = AudioComponentFindNext(NULL, &desc);

    // Get audio units
    AudioComponentInstanceNew(inputComponent, &audioUnit);

    // Enable IO for recording
    UInt32 flag = 1;                   AudioUnitSetProperty(audioUnit,  kAudioOutputUnitProperty_EnableIO,  kAudioUnitScope_Input,   kInputBus,  &flag,   sizeof(flag));

    // Enable IO for playback     AudioUnitSetProperty(audioUnit,  kAudioOutputUnitProperty_EnableIO,   kAudioUnitScope_Output,   kOutputBus, &flag,   sizeof(flag));

     UInt32 shouldAllocateBuffer = 1;
     AudioUnitSetProperty(audioUnit, kAudioUnitProperty_ShouldAllocateBuffer,      kAudioUnitScope_Global, 1, &shouldAllocateBuffer, sizeof(shouldAllocateBuffer));

    // Describe format
    audioFormat.mSampleRate = 8000.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   AudioUnitSetProperty(audioUnit,  kAudioUnitProperty_StreamFormat,  kAudioUnitScope_Output, 1,   &audioFormat,  sizeof(audioFormat));

      AudioUnitSetProperty(audioUnit,  kAudioUnitProperty_StreamFormat,  kAudioUnitScope_Input,  0,  &audioFormat,  sizeof(audioFormat));

    // Set input callback
    AURenderCallbackStruct callbackStruct;
    callbackStruct.inputProc = recordingCallback;
    callbackStruct.inputProcRefCon = self;
        AudioUnitSetProperty(audioUnit,   kAudioOutputUnitProperty_SetInputCallback,   kAudioUnitScope_Global,   1,   &callbackStruct,   sizeof(callbackStruct));



    // Set output callback
    callbackStruct.inputProc = playbackCallback;
    callbackStruct.inputProcRefCon = self;
      AudioUnitSetProperty(audioUnit, kAudioUnitProperty_SetRenderCallback,  kAudioUnitScope_Global,   0, &callbackStruct,  sizeof(callbackStruct));


    // Initialise
    AudioUnitInitialize(audioUnit);

    AudioOutputUnitStart(audioUnit);

使用此设置,我在尝试使用设备时在回调方法中获得 186 帧。我已按缓冲区分配:

    bufferList = (AudioBufferList*) malloc(sizeof(AudioBufferList));
    bufferList->mNumberBuffers = 1; //mono input
    for(UInt32 i=0;i<bufferList->mNumberBuffers;i++)
    {
          bufferList->mBuffers[i].mNumberChannels = 1;
          bufferList->mBuffers[i].mDataByteSize = 2*186;
          bufferList->mBuffers[i].mData = malloc(bufferList->mBuffers[i].mDataByteSize);
    } 

从回调中的这 372(2 x 186) 个字节中,我获取了 320 个字节的数据并按照我的要求使用。它正在工作,但非常嘈杂。

有人请帮助我。我有大麻烦了。

4

1 回答 1

1

几个建议——

  1. 使用 AVAudioSession 类设置采样率和缓冲区大小。
  2. 386 是一个不寻常的帧数。您的回调可能要求 512 或 1024。您可以尝试使用环形缓冲区来允许不同的缓冲区大小/帧速率以满足您的需要。

这里有些例子:

MixerHost 时间

于 2011-12-16T17:15:03.697 回答