ios - Remote I/O 音频单元是否设置缓冲区中的 channel 数?

标签 ios audio ffmpeg core-audio

我正在使用 kxmovie (它是一个基于 ffmpeg 的视频播放器)作为应用程序的基础,当连接到设备的唯一东西是耳机并且我们正在播放轨道时,我试图弄清楚 RemoteI/O 单元如何在 iOS 上工作超过 2 个 channel (比如环绕 6 轨道 channel )。似乎它与输出 channel 设置一起使用,缓冲区只有 2 个 channel 。这是因为 Core Audio 的拉式结构吗?如果是这样,轨道中的其他 channel 会发生什么?它们是被下混还是被忽略了?

连接到 remoteio 单元的渲染回调的代码在这里:

- (BOOL) renderFrames: (UInt32) numFrames
               ioData: (AudioBufferList *) ioData
{
    NSLog(@"Number of channels in buffer: %lu",ioData->mNumberBuffers);

    for (int iBuffer=0; iBuffer < ioData->mNumberBuffers; ++iBuffer) {
        memset(ioData->mBuffers[iBuffer].mData, 0, ioData->mBuffers[iBuffer].mDataByteSize);
    }


    if (_playing && _outputBlock ) {

        // Collect data to render from the callbacks
        _outputBlock(_outData, numFrames, _numOutputChannels);

        // Put the rendered data into the output buffer
        if (_numBytesPerSample == 4) // then we've already got floats
        {
            float zero = 0.0;

            for (int iBuffer=0; iBuffer < ioData->mNumberBuffers; ++iBuffer) {

                int thisNumChannels = ioData->mBuffers[iBuffer].mNumberChannels;

                for (int iChannel = 0; iChannel < thisNumChannels; ++iChannel) {
                    vDSP_vsadd(_outData+iChannel, _numOutputChannels, &zero, (float *)ioData->mBuffers[iBuffer].mData, thisNumChannels, numFrames);
                }
            }
        }
        else if (_numBytesPerSample == 2) // then we need to convert SInt16 -> Float (and also scale)
        {
            float scale = (float)INT16_MAX;
            vDSP_vsmul(_outData, 1, &scale, _outData, 1, numFrames*_numOutputChannels);

            for (int iBuffer=0; iBuffer < ioData->mNumberBuffers; ++iBuffer) {

                int thisNumChannels = ioData->mBuffers[iBuffer].mNumberChannels;

                for (int iChannel = 0; iChannel < thisNumChannels; ++iChannel) {
                    vDSP_vfix16(_outData+iChannel, _numOutputChannels, (SInt16 *)ioData->mBuffers[iBuffer].mData+iChannel, thisNumChannels, numFrames);
                }
            }

        }        
    }

    return noErr;
}

谢谢!

编辑:这是 ASBD (_ouputFormat) 的代码。它直接从远程获取它的值。您也可以查看整个方法文件here .
if (checkError(AudioUnitGetProperty(_audioUnit,
                                    kAudioUnitProperty_StreamFormat,
                                    kAudioUnitScope_Input,
                                    0,
                                    &_outputFormat,
                                    &size),
               "Couldn't get the hardware output stream format"))
    return NO;


_outputFormat.mSampleRate = _samplingRate;
if (checkError(AudioUnitSetProperty(_audioUnit,
                                    kAudioUnitProperty_StreamFormat,
                                    kAudioUnitScope_Input,
                                    0,
                                    &_outputFormat,
                                    size),
               "Couldn't set the hardware output stream format")) {

    // just warning
}

_numBytesPerSample = _outputFormat.mBitsPerChannel / 8;
_numOutputChannels = _outputFormat.mChannelsPerFrame;

NSLog(@"Current output bytes per sample: %ld", _numBytesPerSample);
NSLog(@"Current output num channels: %ld", _numOutputChannels);

// Slap a render callback on the unit
AURenderCallbackStruct callbackStruct;
callbackStruct.inputProc = renderCallback;
callbackStruct.inputProcRefCon = (__bridge void *)(self);

if (checkError(AudioUnitSetProperty(_audioUnit,
                                    kAudioUnitProperty_SetRenderCallback,
                                    kAudioUnitScope_Input,
                                    0,
                                    &callbackStruct,
                                    sizeof(callbackStruct)),
               "Couldn't set the render callback on the audio unit"))
    return NO;

最佳答案

我终于找到了将 channel 重新混合为立体声的代码。它使用 RIO 的 ASBD 在 KxAudioManager 中设置一个属性。然后,在 KxMovieDecoder.m 中,它使用相同的变量设置 ffmpeg 选项。这是代码:

id<KxAudioManager> audioManager = [KxAudioManager audioManager];
swrContext = swr_alloc_set_opts(NULL,
                                av_get_default_channel_layout(audioManager.numOutputChannels),
                                AV_SAMPLE_FMT_S16,
                                audioManager.samplingRate,
                                av_get_default_channel_layout(codecCtx->channels),
                                codecCtx->sample_fmt,
                                codecCtx->sample_rate,
                                0,
                                NULL);

现在开始阅读 ffmpeg 是如何进行解码的。娱乐时间。

关于ios - Remote I/O 音频单元是否设置缓冲区中的 channel 数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19961734/

相关文章:

iphone - 如何将两个数组加载到同一个 TableView 中?

c++ - QML多媒体播放文件夹中的所有mp3

java - 安卓 ffmpeg : Path with spaces

video - 使用 ffmpeg/ffprobe 检测视频中 alpha channel 的好方法

audio - 将 5.1 视频缩混为立体声的更快方法?

ios - 如何使用 ffmpeg/sdl 在 UIView 中播放视频

ios - 如何创建一个导航栏,在快速滚动后固定在顶部

ios - 即使有其他标记,Xcode 在 ARC 项目中仍然存在非 ARC 文件问题

matlab - 在Matlab中读取和播放PCM音频

java - 声音数据格式