16Bit Little Endian字节数组到整数数组到Decibel值

Dom*_*ber 0 c audio type-conversion pulseaudio decibel

我正在使用PulseAudio API"实时"获取当前的麦克风输入.缓冲区数据作为16位小端字节数组传送.我想要做的是找出缓冲区中的最大峰值水平并将其转换为分贝值.为此,我必须将每个两个字节的数组值转换为一个整数值.在同一个循环过程中,我也在寻找最大值.之后,我将最大值转换为分贝值.这是C代码:

static ssize_t loop_write(int fd, const uint8_t *data, size_t size) 
{
int newsize = size / 2;
uint16_t max_value = 0;
int i = 0;

for (i = 0; i < size; i += 2)
{
    // put two bytes into one integer
    uint16_t val = data[i] + ((uint32_t)data[i+1] << 8);

    // find max value
    if(val > max_value)
       max_value = val;
}

// convert to decibel
float decibel = max_value / pow(2, 15);

if(decibel != 0)
    decibel = 20 * log(decibel);

// print result
printf("%f, ", decibel);

return size;
}
Run Code Online (Sandbox Code Playgroud)

据我所知,PA_SAMPLE_S16LE的幅度值应在0到32768之间.但是我在分贝转换之前得到0到65536之间的值.我的转换有什么问题吗?

为了完整起见,我还发布了我的pulseaudio设置:

int main(int argc, char*argv[]) 
{
char *device = "alsa_input.usb-041e_30d3_121023000184-00-U0x41e0x30d3.analog-mono";

// The sample type to use
static const pa_sample_spec ss = {
    .format = PA_SAMPLE_S16LE,
    .rate = 44100,
    .channels = 1
};
pa_simple *s = NULL;
int ret = 1;
int error;

// Create the recording stream 
if (!(s = pa_simple_new(NULL, argv[0], PA_STREAM_RECORD, device, "record", &ss, NULL, NULL, &error))) {
    fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error));
    goto finish;
}

for (;;) {
    uint8_t buf[BUFSIZE];

    // Record some data ...
    if (pa_simple_read(s, buf, sizeof(buf), &error) < 0) {
        fprintf(stderr, __FILE__": pa_simple_read() failed: %s\n", pa_strerror(error));
        goto finish;
    }

    // And write it to STDOUT
    if (loop_write(STDOUT_FILENO, buf, sizeof(buf)) != sizeof(buf)) {
        fprintf(stderr, __FILE__": write() failed: %s\n", strerror(errno));
        goto finish;
    }
}

ret = 0;

finish:

if (s)
    pa_simple_free(s);

return 0;
}
Run Code Online (Sandbox Code Playgroud)

dat*_*olf 7

我想要做的是找出缓冲区中的最大峰值水平并将其转换为分贝值.

从物理角度来看,这种方法没有意义.虽然可以指定与完整动态范围相关的单个样本值,但您可能对声音级别(即信号的功率)更感兴趣.一个单峰,即使它满量程也只带来很少的能量; 由于谐波失真和有限的带宽,它可能会产生非常大的爆音,但从技术上讲,它的功率密度分布在整个频带限制频谱上.

你真正需要的是确定RMS值(均方根).即

RMS = sqrt( sum( square(samples) )/n_samples )
Run Code Online (Sandbox Code Playgroud)

编辑: 请注意,上述内容仅适用于没有DC部分的信号.大多数模拟声音接口都是交流耦合的,所以这不是问题.但如果还有DC部件,则必须先从样本中减去平均值,即

RMS_DC_reject = sqrt( sum( square(samples - mean_sample) )/n_samples )
Run Code Online (Sandbox Code Playgroud)

我将把它作为练习让读者将其添加到下面的代码中.

这为您提供了处理样本的强大功能,这正是您真正想要的.你问过deciBels.现在我要问你dB(什么)?您需要参考值,因为Bels(或deciBels)是一个相对(即比较)度量.对于数字信号,满量程将为0 dB(FS),零线将为-20 log10( 2^B ),其中B = sampling bit depth.对于约为-96 dB(FS)的16位信号.

如果我们在谈论线路上的信号,则共同参考是1 mW的功率,在这种情况下,标度是dB(m).对于音频线路电平,已经定义满量程等于1 mW的信号功率,即1 V RMS在1 kOhm电阻上消耗的电压(再次有RMS).

现在,由于我们的满量程由输入电路立即确定,输入电路以dB(m)为单位定义,您可以稍后将dB(FS)显示为dB(m)(或dBm).

说到实际的声级,这取决于您的输入放大器增益和所用麦克风的转换效率.


据我所知,PA_SAMPLE_S16LE的幅度值应在0到32768之间.但是我在分贝转换之前得到0到65536之间的值.我的转换有什么问题吗?

您询问了有符号整数格式.但是您将值转换为unsigned int.由于dB_FS相对于满量程,因此不要将其除以位数.对于16位的零信号,结果应该是大约-96dB.这种划分无论如何都没有意义,因为它只是将你的RMS扩展到范围[0; 1],但log(0)偏离-infinity.因此你的if陈述.但请记住,这是物理学,物理学是连续的,这里应该没有if语句.

你应该这样写

// even for signed values this should be 2^N
// we're going to deal with signed later
double const MAX_SIGNAL = 1 << SAMPLE_BITS;

// using double here, because float offers only 25 bits of
// distortion free dynamic range.
double accum = 0;
int const n_samples = size/2;
for (i = 0; i < size; i += 2)
{
    // put two bytes into one __signed__ integer
    int16_t val = data[i] + ((int16_t)data[i+1] << 8);

    accum += val*val;
}
accum /= n_samples;

// Since we're using signed values we need to
// double the accumulation; of course this could be
// contracted into the statement above
accum *= 2.;

float const dB_FS = -20 * log10( MAX_SIGNAL - sqrt(accum) );
Run Code Online (Sandbox Code Playgroud)