0

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

4

1 回答 1

3

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

这是最简单的情况。您可以遍历您的 byte[] 并将存储的值绘制为幅度。

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

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

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

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 位立体声音频文件。在这种情况下,每个样本都有两个字节,并且在每个字节之后通道都会改变。

例如:

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

有关进一步和更详细的描述,请查看此页面

于 2012-11-08T14:19:43.727 回答