c++ - 什么是样本格式?

标签 c++ audio core-audio portaudio

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

在看起来过时的预处理器指令中,有一个 typedef PaSampleType,它采用 portaudio.h 中定义的 PaSampleFormat 值/p>

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

在头文件中,定义为

  /** 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.
    */

但这并没有让我更清楚。

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

谢谢,

最佳答案

来自 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 作为代表不同样本格式的位域。所以如果你想使用交错的 float ,你会这样做:

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

关于c++ - 什么是样本格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47961954/

相关文章:

c++ - 查找在螺旋矩阵中行进的 N 个正方形的坐标

c# - 如何在For循环中的控制台应用程序中播放声音,听起来好像有人在打字?

iphone - 修改SpeakHere以录制设备上播放的音频

objective-c - 音频图输出单元回调函数-无法读取数据

c++ - 如何捕获部分屏幕并将其保存为 BMP?

c++ - 如何将 Qt GUI 应用程序的版本打印到控制台

JavaSonics ListenUp 的 Javascript 替代品

objective-c - 在 Cocoa 类中使用核心音频

c++ - 使用 masm 编译程序集文件时表达式中缺少运算符

android - Howler JS 2.0-Cordova,Android,设备上未播放声音