输出wav格式文件-多通道

输出wav文件主要格式在于文件头

WaveHeader header = new WaveHeader(sun);//sun为文件字节数
//length field = size of the content (PCMSize) + size of the header field (excluding the first 4 bytes of the identifier RIFF and the fileLength itself 4 bytes)
header.fileLength = sun + (44 - 8);
header.FmtHdrLeth = 16;
header.BitsPerSample = 16;
header.Channels = 6;//通道数
header.FormatTag = 0x0001;
header.SamplesPerSec = 4000000; //采样率
header.BlockAlign = (short)(header.Channels * header.BitsPerSample / 8);
header.AvgBytesPerSec = header.BlockAlign * header.SamplesPerSec;
header.DataHdrLeth = sun;
byte[] h = header.getHeader();
assert h.length == 44; //WAV standard, the header should be 44 bytes 去掉44就是pcm

其次输出多个通道数据数据格式

for(int i=0;i

多通道数据格式

输出wav格式文件-多通道_第1张图片

单通道数据格式为

InputStream fis = new FileInputStream(src[0]);
// Calculate the length
 byte[] buf = new byte[1024 * 4];
 int size = fis.read(buf);
 int PCMSize = 0;
 while (size != -1) {
      PCMSize += size;
 size = fis.read(buf);
 }
  fis.close();
 // Fill in the parameters, bit rate and so on. Here is the 16-bit mono 8000 hz
 WaveHeader header = new WaveHeader(PCMSize);
 //length field = size of the content (PCMSize) + size of the header field (excluding the first 4 bytes of the identifier RIFF and the fileLength itself 4 bytes)
 header.fileLength = PCMSize + (44 - 8);
 header.FmtHdrLeth = 16;
 header.BitsPerSample = 16;
 header.Channels = 1;
 header.FormatTag = 0x0001;
 header.SamplesPerSec = 4000000; //采样率
 header.BlockAlign = (short)(header.Channels * header.BitsPerSample / 8);
 header.AvgBytesPerSec = header.BlockAlign * header.SamplesPerSec;
 header.DataHdrLeth = PCMSize;
 byte[] h = header.getHeader();
 assert h.length == 44; //WAV standard, the header should be 44 bytes 去掉44就是pcm
 byte[] b = new byte[10];
 FileOutputStream fs = new FileOutputStream(src[1]);
 fs.write(h);
 FileInputStream fiss = new FileInputStream(src[0]);
 byte[] bb = new byte[10];
 int len = -1;
 while((len = fiss.read(bb))>0) {
      fs.write(bb, 0, len);
 }
  fs.close();
 fiss.close();

你可能感兴趣的:(java)