简体   繁体   English

将带符号的16位PCM采样转换为无符号8位PCM采样时的削波

[英]Clipping when converting signed 16-bit PCM samples to unsigned 8-bit PCM samples

I have signed mono 16-bit PCM audio samples stored in a SInt16 buffer and I am trying to convert them to unsigned mono 8-bit PCM samples stored in a UInt8 buffer. 我签署了存储在SInt16缓冲区中的单声道16位PCM音频样本,我试图将它们转换为存储在UInt8缓冲区中的无符号单声道8位PCM样本。 I've written the following basically-working code: 我写了以下基本工作代码:

for (int i=0; i < numSamples; i++) { 
    SInt8 tempSigned8Bit = signed16BitBuffer[i]/127; // In 2 passes
    unsigned8BitBuffer[i] = tempSigned8Bit + 127;    // for clarity
}

However, I can hear clipping at the maximum amplitudes in the resulting audio, or at least that is my impression of where the distortion is occurring. 但是,我可以听到结果音频中最大振幅的削波,或者至少是我对失真发生位置的印象。 Is this an artifact of the re-quantization or do I need to include some sort of clamping as described in this question about a similar conversion but without any signedness conversion: 这是重新量化的工件还是我需要包含某种类型的钳位,如此问题中描述的类似转换但没有任何符号转换:

Convert 16 bit pcm to 8 bit 将16位pcm转换为8位

Bitwise optimizations unnecessary but I certainly wouldn't say no to them. 按位优化是不必要的,但我当然不会对它们说不。

This will fail for large values because you need to divide by 256 not 127. Also the offset needs to be 128, not 127. 对于较大的值,这将失败,因为您需要除以256而不是127.此外,偏移量需要为128,而不是127。

for (int i = 0; i < numSamples; i++) { 
    SInt8 tempSigned8Bit = signed16BitBuffer[i] / 256;
    unsigned8BitBuffer[i] = tempSigned8Bit + 128;
}

The conversion for +/- full scale and zero looks like this: +/-满量程和零的转换如下所示:

Signed    Divide    Add
16 bit    by 256    128
sample

 32767 ->  127 ->   255    ; Full scale +
     0 ->    0 ->   128    ; 0
-32768 -> -128 ->     0    ; Full scale -

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM