gpt4 book ai didi

Java 使用样本创建波形

转载 作者:行者123 更新时间:2023-12-02 00:08:51 27 4
gpt4 key购买 nike

我已将声音文件加载到字节数组中。我对下一步必须做什么来获取绘制波形点的信息感到困惑。根据我在网上找到的信息,我必须创建另一个数组吗?有人可以向我解释这是如何工作的,因为我不太明白如何收集样本。

最佳答案

样本是根据文件格式收集的。这么说吧,您的音频格式是 8 位单声道音频文件。

这是最简单的情况。您只需迭代 byte[] 并将存储的值绘制为幅度。

当您的文件是 16 位音频文件时,每个样本由 2 个字节组成,因此您必须查看每个样本的两个字节。您可以通过调用以下方法来完成此操作:

private int getSixteenBitSample(int high, int low) {
return (high << 8) + (low & 0x00ff);
}

它将连接每个样本的字节数组的第一个和第二个字节。所以你的循环看起来像这样:

int sampleArray[] = new int[numSamples];

for(int i = 0, j=0;i < bytearray.length;)
{
int iLow = bytearray[i];
i++;
int iHigh = bytearray[i];
i++;

sampleArray[j] = getSixteenBitSample(iHigh, iLow);
j++;
}

第三种情况可能是,您的文件是 16 位立体声音频文件。在这种情况下,每个样本都有两个字节,每个字节之后 channel 都会发生变化。

例如:

First read sample 1 from byte 0 and byte 1. -> First sample of channel 1
Second read sample 2 from byte 2 and byte 3 -> First sample of channel 2
Third read sample 3 from byte 4 and byte 5 -> Second sample of channel 1
Forth read sample 4 from byte 6 and byte 7 -> Second sample of channel 2

有关更详细的描述,请查看this page

关于Java 使用样本创建波形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13289881/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com