在android中将16位pcm音频下采样为8位

发布于 2024-08-23 08:08:04 字数 706 浏览 3 评论 0原文

我想将 16 位 pcm 音频下采样到 8 位,然后在 android 中将相同的音频从 8 位上采样回 16 位。我正在使用这个似乎有效:

int tempint;
for (int i=1, j=0; i<tempBuffer.length; i+=2, j++)
{
    tempint = ((int) tempBuffer[i]) ^ 0x00000080;
    tempBuffer[j] = (byte) tempint; 
    Log.e("test","------------* COUNTER -> "+j+" BUFFER VALUE -> "+tempBuffer[j]+"*-----------");
} 

其中 tempbuffer 是一个短 [] ,而 tempint 是一个 int 。 谁能告诉我这是否工作正常,因为我是初学者,我也使用它将字节 [] 转换回短 [],

for (int x=1, j=0; x<music.length; x+=2, j++)
{
    tempint = ((int) music[x])& 0xFF;
    music[j] = tempint.shortValue();
    Log.e("maxsap","------------*"+ music[j]+"*-----------");
}

我不确定它是否有效。

I would like to downsample 16 bit pcm audio to 8 bit and then upsample the same from 8 bit back to 16 bit in android. I am using this which seems to work:

int tempint;
for (int i=1, j=0; i<tempBuffer.length; i+=2, j++)
{
    tempint = ((int) tempBuffer[i]) ^ 0x00000080;
    tempBuffer[j] = (byte) tempint; 
    Log.e("test","------------* COUNTER -> "+j+" BUFFER VALUE -> "+tempBuffer[j]+"*-----------");
} 

where tempbuffer is a short [] and tempint is an int.
Can anyone tell me if this works fine because I am a starter, also I am using this to convert the byte [] back to a short []

for (int x=1, j=0; x<music.length; x+=2, j++)
{
    tempint = ((int) music[x])& 0xFF;
    music[j] = tempint.shortValue();
    Log.e("maxsap","------------*"+ music[j]+"*-----------");
}

which I am not sure it is working.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终止放荡 2024-08-30 08:08:04

假设 8 位和 16 位音频都是带符号的 PCM:

16 位到 8 位:

sample_8 = sample_16 >> 8;

8 位到 16 位:

sample_16 = sample_8 << 8;

如果 8 位 PCM 是“偏移二进制”(具有隐式偏差的无符号格式),则转换将为:

16 位到 8 位:

sample_8 = (sample_16 >> 8) + 128;

8 位到 16 位:

sample_16 = (sample_8 - 128) << 8;

理想情况下,您会在从 8 位转换为 16 位时添加抖动,但您可能无法承担这样做的计算成本。

另请注意,术语“上采样”和“下采样”通常指改变采样率。您在这里所做的不是重新采样,而是更改位深度或重新量化数据。

Assuming both 8 bit and 16 bit audio are signed PCM:

16 bits to 8 bits:

sample_8 = sample_16 >> 8;

8 bits to 16 bits:

sample_16 = sample_8 << 8;

If the 8 bit PCM is "offset binary" however (an unsigned format with an implicit bias) then the conversions would be:

16 bits to 8 bits:

sample_8 = (sample_16 >> 8) + 128;

8 bits to 16 bits:

sample_16 = (sample_8 - 128) << 8;

Ideally you would add dithering when converting from 8 bits up to 16 bits but you may not be able to afford the computational cost of doing this.

Note also that the terms "upsampling" and "downsampling" usually refer to changing sample rates. What you're doing here is not re-resampling, but changing the bit depth or re-quantizing the data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文