import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class ByteArrayToWav {
public static void main(String[] args) {
String outputFilePath = "output.wav";
try {
// 示例 byte 数组
byte[] audioData = {0x52, 0x49, 0x46, 0x46, 0x24, 0x08, 0x00, 0x00, /*...*/};
writeWavFile(audioData, outputFilePath);
System.out.println("WAV file generated successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
private static void writeWavFile(byte[] audioData, String outputFilePath) throws IOException {
try (FileOutputStream wavFile = new FileOutputStream(outputFilePath)) {
// 写入WAV文件头
writeWavHeader(wavFile, audioData.length);
// 写入音频数据
wavFile.write(audioData);
}
}
private static void writeWavHeader(FileOutputStream wavFile, int dataSize) throws IOException {
// WAV 文件格式头
wavFile.write("RIFF".getBytes());
wavFile.write(intToByteArray(36 + dataSize)); // Chunk size
wavFile.write("WAVEfmt ".getBytes());
wavFile.write(intToByteArray(16)); // Subchunk1 size
wavFile.write(shortToByteArray((short) 1)); // Audio format (1 for PCM)
wavFile.write(shortToByteArray((short) 1)); // Number of channels
wavFile.write(intToByteArray(44100)); // Sample rate (e.g., 44.1kHz)
wavFile.write(intToByteArray(44100 * 2)); // Byte rate (Sample rate * Number of channels * Bits per sample / 8)
wavFile.write(shortToByteArray((short) 2)); // Block align (Number of channels * Bits per sample / 8)
wavFile.write(shortToByteArray((short) 16)); // Bits per sample
wavFile.write("data".getBytes());
wavFile.write(intToByteArray(dataSize)); // Subchunk2 size
}
private static byte[] intToByteArray(int value) {
return new byte[]{
(byte) (value),
(byte) (value >> 8),
(byte) (value >> 16),
(byte) (value >> 24)
};
}
private static byte[] shortToByteArray(short value) {
return new byte[]{
(byte) (value),
(byte) (value >> 8)
};
}
}
java将 10进制的byte数组生成wav文件
最新推荐文章于 2024-06-22 14:08:45 发布