【问题标题】:What is a sample format?什么是样本格式?
【发布时间】:2017-12-24 15:40:25
【问题描述】:

我目前正在查看 PortAudio 代码示例,尤其是 paex_record.c 代码示例。

在看起来很老式的预处理器指令中,有一个typedef PaSampleType,它采用portaudio.h中定义的PaSampleFormat

我知道什么是采样率,但我不知道什么是采样格式。

在头文件中定义为

  /** The sample format of the buffer provided to the stream callback,
     a_ReadStream() or Pa_WriteStream(). It may be any of the formats described
     by the PaSampleFormat enumeration.
    */

但这并没有让我更清楚。

如果有人能阐明这个概念以及它如何应用于我的案例,我将不胜感激。

谢谢,

【问题讨论】:

    标签: c++ audio core-audio portaudio


    【解决方案1】:

    来自 portaudio.h:

    typedef unsigned long PaSampleFormat;  
    #define paFloat32        ((PaSampleFormat) 0x00000001) 
    #define paInt32          ((PaSampleFormat) 0x00000002) 
    #define paInt24          ((PaSampleFormat) 0x00000004) 
    #define paInt16          ((PaSampleFormat) 0x00000008) 
    #define paInt8           ((PaSampleFormat) 0x00000010) 
    #define paUInt8          ((PaSampleFormat) 0x00000020) 
    #define paCustomFormat   ((PaSampleFormat) 0x00010000) 
    #define paNonInterleaved ((PaSampleFormat) 0x80000000)  
    

    看起来 portaudio 库使用 PaSampleFormat 作为表示不同样本格式的位域。所以如果你想使用交错的浮点数,你可以这样做:

     PaSampleFormat myFormat = paFloat32;
    

    或者,如果您想使用非交错签名短裤,您可以这样做:

     PaSampleFormat myFormat = paInt16 | paNonInterleaved;
    

    然后,该库有许多以 PaSampleFormat 作为参数的函数,以便这些函数知道如何在内部处理样本。这是库中的另一个摘录,它使用此位域来获取样本大小。

    PaError Pa_GetSampleSize( PaSampleFormat format )
    {
        int result;
    
        PA_LOGAPI_ENTER_PARAMS( "Pa_GetSampleSize" );
        PA_LOGAPI(("\tPaSampleFormat format: %d\n", format ));
    
        switch( format & ~paNonInterleaved )
        {
    
        case paUInt8:
        case paInt8:
            result = 1;
            break;
    
        case paInt16:
            result = 2;
            break;
    
        case paInt24:
            result = 3;
            break;
    
        case paFloat32:
        case paInt32:
            result = 4;
            break;
    
        default:
            result = paSampleFormatNotSupported;
            break;
        }
    
        PA_LOGAPI_EXIT_PAERROR_OR_T_RESULT( "Pa_GetSampleSize", "int: %d", result );
    
        return (PaError) result;
    }
    

    【讨论】:

      【解决方案2】:

      PortAudio 提供原始 PCM 格式的样本。这意味着每个样本都是一个幅度,将提供给声卡中的 DAC(数模转换器)。对于paInt16,这是从-32768 到32767 的值。对于paFloat32,这是从-1.0 到1.0 的浮点值。声卡将此值转换为成比例的电压,然后驱动您的音频设备。

      【讨论】:

        猜你喜欢
        • 2013-09-23
        • 2010-09-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-05
        • 1970-01-01
        • 1970-01-01
        • 2010-10-16
        相关资源
        最近更新 更多