ios - 解码AudioStreamBasicDescription的mFormatFlags编号(ASBD.mFormatFlags)

标签 ios ios-simulator core-audio audio-recording audiounit

我正在编写一个使用 CoreAudio 的音频单元 API 的 iOS 应用程序,在某个时刻我会执行 AudioUnitGetProperty(audioUnit, kAudioUnitProperty_StreamFormat, ...)称呼。此时我设置了一个断点来查看 ASBD,我找到了 mFormatFlags字段是 41 。问题是,我可以从该数字解码实际的标志名称(例如 kAudioFormatFlagIsNonInterleaved | kAudioFormatFlagIsPacked | ... )吗?

非常感谢。

最佳答案

这是一个用于打印 ASBD 的 ostream 重载,主要取自 Apple 示例代码:

std::ostream& operator<<(std::ostream& out, const AudioStreamBasicDescription& format)
{
    unsigned char formatID [5];
    *(UInt32 *)formatID = OSSwapHostToBigInt32(format.mFormatID);
    formatID[4] = '\0';

    // General description
    out << format.mChannelsPerFrame << " ch, " << format.mSampleRate << " Hz, '" << formatID << "' (0x" << std::hex << std::setw(8) << std::setfill('0') << format.mFormatFlags << std::dec << ") ";

    if(kAudioFormatLinearPCM == format.mFormatID) {
        // Bit depth
        UInt32 fractionalBits = ((0x3f << 7)/*kLinearPCMFormatFlagsSampleFractionMask*/ & format.mFormatFlags) >> 7/*kLinearPCMFormatFlagsSampleFractionShift*/;
        if(0 < fractionalBits)
            out << (format.mBitsPerChannel - fractionalBits) << "." << fractionalBits;
        else
            out << format.mBitsPerChannel;

        out << "-bit";

        // Endianness
        bool isInterleaved = !(kAudioFormatFlagIsNonInterleaved & format.mFormatFlags);
        UInt32 interleavedChannelCount = (isInterleaved ? format.mChannelsPerFrame : 1);
        UInt32 sampleSize = (0 < format.mBytesPerFrame && 0 < interleavedChannelCount ? format.mBytesPerFrame / interleavedChannelCount : 0);
        if(1 < sampleSize)
            out << ((kLinearPCMFormatFlagIsBigEndian & format.mFormatFlags) ? " big-endian" : " little-endian");

        // Sign
        bool isInteger = !(kLinearPCMFormatFlagIsFloat & format.mFormatFlags);
        if(isInteger)
            out << ((kLinearPCMFormatFlagIsSignedInteger & format.mFormatFlags) ? " signed" : " unsigned");

        // Integer or floating
        out << (isInteger ? " integer" : " float");

        // Packedness
        if(0 < sampleSize && ((sampleSize << 3) != format.mBitsPerChannel))
            out << ((kLinearPCMFormatFlagIsPacked & format.mFormatFlags) ? ", packed in " : ", unpacked in ") << sampleSize << " bytes";

        // Alignment
        if((0 < sampleSize && ((sampleSize << 3) != format.mBitsPerChannel)) || (0 != (format.mBitsPerChannel & 7)))
            out << ((kLinearPCMFormatFlagIsAlignedHigh & format.mFormatFlags) ? " high-aligned" : " low-aligned");

        if(!isInterleaved)
            out << ", deinterleaved";
    }
    else if(kAudioFormatAppleLossless == format.mFormatID) {
        UInt32 sourceBitDepth = 0;
        switch(format.mFormatFlags) {
            case kAppleLosslessFormatFlag_16BitSourceData:      sourceBitDepth = 16;    break;
            case kAppleLosslessFormatFlag_20BitSourceData:      sourceBitDepth = 20;    break;
            case kAppleLosslessFormatFlag_24BitSourceData:      sourceBitDepth = 24;    break;
            case kAppleLosslessFormatFlag_32BitSourceData:      sourceBitDepth = 32;    break;
        }

        if(0 != sourceBitDepth)
            out << "from " << sourceBitDepth << "-bit source, ";
        else
            out << "from UNKNOWN source bit depth, ";

        out << format.mFramesPerPacket << " frames/packet";
    }
    else
        out << format.mBitsPerChannel << " bits/channel, " << format.mBytesPerPacket << " bytes/packet, " << format.mFramesPerPacket << " frames/packet, " << format.mBytesPerFrame << " bytes/frame";

    return out; 
}

关于ios - 解码AudioStreamBasicDescription的mFormatFlags编号(ASBD.mFormatFlags),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12269295/

相关文章:

ios - 如果推送并呈现多个viewController,如何移动到parentViewController?

ios - 自定义 UILabel、UIButton 类的 viewWillAppear 替代方案

iphone - 在 NSTimer 上重新加载 TableView 部分

ios - 如果在通话过程中插入了麦克风,则无法恢复录制

iphone - 如何在 iOS 中录制语音时以编程方式生成音频波形?

ios - 在 installTapOnBus () 方法中我不能做什么?

javascript - react : onClick on Mobile (iPhone) requires 2 taps?

xcode - 运行 Playground 时出错。无法找到合适的目标设备

ios - 静态库在 iOS 模拟器上出错并在 iOS 设备上工作

ios - 是否可以在 iPhone 模拟器上模拟通话?