什么是样本格式?

问题描述 投票:0回答:2

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

在看起来过时的预处理器指令中,有一个typedef PaSampleType,它取PaSampleFormat中定义的portaudio.h

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

在头文件中,它被定义为

  /** 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
2个回答
2
投票

来自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;
}

1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.