【发布时间】:2011-09-14 20:17:17
【问题描述】:
我有二维整数数组。第一个索引表示通道数。第二个表示通道中的样本数。如何将此数组保存到音频文件中?我知道,我必须将它转换为字节数组,但我不知道该怎么做。
// 编辑
更多信息。我已经有一个绘制波形的类。在这里:
http://javafaq.nu/java-example-code-716.html
现在我想剪掉这波的一部分并将其保存到新文件中。所以我要切掉一部分int[][] samplesContainer,把它转换成字节数组(我不知道怎么做),然后保存到和audioInputStream格式相同的文件中。
// 编辑
好的。所以最大的问题是给这个写倒函数:
protected int[][] getSampleArray(byte[] eightBitByteArray) {
int[][] toReturn = new int[getNumberOfChannels()][eightBitByteArray.length / (2 * getNumberOfChannels())];
int index = 0;
//loop through the byte[]
for (int t = 0; t < eightBitByteArray.length;) {
//for each iteration, loop through the channels
for (int a = 0; a < getNumberOfChannels(); a++) {
//do the byte to sample conversion
//see AmplitudeEditor for more info
int low = (int) eightBitByteArray[t];
t++;
int high = (int) eightBitByteArray[t];
t++;
int sample = (high << 8) + (low & 0x00ff);
if (sample < sampleMin) {
sampleMin = sample;
} else if (sample > sampleMax) {
sampleMax = sample;
}
//set the value.
toReturn[a][index] = sample;
}
index++;
}
return toReturn;
}
我不明白为什么 t 在高之后会有第二次递增。我也不知道如何从样本中得到高低。
【问题讨论】:
-
您能否更具体地说明您要编写的音频文件类型?如果您询问如何将一堆整数写入二进制文件,您可能需要查看
ByteBuffer和IntBuffer类,更一般的是java.nio -
不知道有什么不清楚的地方,请告诉我。
标签: java file scala audio bytearray