我尝试使用以下方法将带有22050 randomNumbers的PCM文件从-1转换为1:
private void rawToWave(final File rawFile, final File waveFile) throws IOException {
byte[] rawData = new byte[(int) rawFile.length()];
DataInputStream input = null;
try {
input = new DataInputStream(new FileInputStream(rawFile));
input.read(rawData);
} finally {
if (input != null) {
input.close();
}
}
DataOutputStream output = null;
try {
int myBitsPerSample= 16;
int myFormat = 1;
long myChannels = 1;
long mySampleRate = 22050;
long myByteRate = mySampleRate * myChannels * myBitsPerSample/8;
int myBlockAlign = (int) (myChannels * myBitsPerSample/8);
output = new DataOutputStream(new FileOutputStream(waveFile));
// WAVE header
// see http://ccrma.stanford.edu/courses/422/projects/WaveFormat/
writeString(output, "RIFF"); // chunk id
writeInt(output, 36 + rawData.length); // chunk size
writeString(output, "WAVE"); // format
writeString(output, "fmt "); // subchunk 1 id
writeInt(output, 16); // subchunk 1 size
writeShort(output, (short) 1); // audio format (1 = PCM)
writeShort(output, (short) 1); // number of channels
writeInt(output, (int)mySampleRate ); // sample ratemySampleRate
writeInt(output, (int) (myByteRate)); // byte rate
writeShort(output, (short) myBlockAlign); // block align
writeShort(output, (short) 16); // bits per sample
writeString(output, "data"); // subchunk 2 id
writeInt(output, rawData.length); // subchunk 2 size
// Audio data (conversion big endian -> little endian)
short[] shorts = new short[rawData.length /2];
ByteBuffer.wrap(rawData).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shorts);
ByteBuffer bytes = ByteBuffer.allocate(shorts.length * 2);
for (short s : shorts) {
bytes.putShort(s);
}
output.write(bytes.array());
} finally {
if (output != null) {
output.close();
}
}
}
public void writeShortLE(DataOutputStream out, short value) throws IOException {
out.writeByte(value & 0xFF);
out.writeByte((value >> 8) & 0xFF);
}
private void writeInt(final DataOutputStream output, final int value) throws IOException {
output.write(value >> 0);
output.write(value >> 8);
output.write(value >> 16);
output.write(value >> 24);
}
private void writeShort(final DataOutputStream output, final short value) throws IOException {
output.write(value >> 0);
output.write(value >> 8);
}
private void writeString(final DataOutputStream output, final String value) throws IOException {
for (int i = 0; i < value.length(); i++) {
output.write(value.charAt(i));
}
}
我有一个双数组,从第一个元素到特定截止值的值为0,然后随机数直到第二个点,从此到文件的末尾再次为0。我在这个数组上做了一个反向fft并将值放入PCM文件中。 PCM-File应该输出一个whitenoise,但整个频谱上的噪声不仅高于截止频率。它适用于带有audiowrite的Matlab,但不适用于Java / Android。
从pcm文件中读取字节数组是否存在问题? 我试图再次将两个wav文件导入到matlab中(在android中转换的一个和matlab中的一个)。来自matlab的那个具有与pcm文件相同的值和相同的值。在android中转换的那个具有超过229000个值和随机值,这些值与pcm文件中的不同。这两种转换方法之间的区别是什么?为什么它在matlab中正常工作而不是在android中使用这种方法?