0
我有整數的二維數組。第一個索引表示通道的數量。第二個表示通道中的採樣數。我如何將這個數組保存到音頻文件中?我知道,我必須將其轉換爲字節數組,但我不知道如何做到這一點。將樣本轉換爲字節數組
//編輯
更多的信息。我已經有了一個繪製波形的類。正是在這裏:
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'](http://download.oracle.com/javase/1,5.0/docs/api/java/ nio/ByteBuffer.html)和['IntBuffer'](http://download.oracle.com/javase/1,5.0/docs/api/java/nio/IntBuffer.html)類,更一般的是['java。 nio'](http://en.wikipedia.org/wiki/New_I/O) –
我不知道有什麼不清楚的地方,請讓我知道。 – ciembor