关于java:输出wav格式文件多通道

29次阅读

共计 1825 个字符,预计需要花费 5 分钟才能阅读完成。

输入 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<bytes.length;i+2){out.write(byte1,i,i+2);// 1 号通道  数据为 2 个字节数据
    out.write(byte1,i,i+2);// 2 号通道
    out.write(byte1,i,i+2);// 3 号通道
}
// 顺次轮巡输入数据可造成多个通道为 1 个 wav 文件 

多通道数据格式

单通道数据格式为

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();

正文完
 0