java wave 文件头

  1. package com.wujn;  
  2.   
  3. import java.io.ByteArrayOutputStream;  
  4. import java.io.IOException;  
  5.   
  6. public class WaveHeader {  
  7.   
  8.     public final char fileID[] = { 'R''I''F''F' };  
  9.     public int fileLength;  
  10.     public char wavTag[] = { 'W''A''V''E' };;  
  11.     public char FmtHdrID[] = { 'f''m''t'' ' };  
  12.     public int FmtHdrLeth;  
  13.     public short FormatTag;  
  14.     public short Channels;  
  15.     public int SamplesPerSec;  
  16.     public int AvgBytesPerSec;  
  17.     public short BlockAlign;  
  18.     public short BitsPerSample;  
  19.     public char DataHdrID[] = { 'd''a''t''a' };  
  20.     public int DataHdrLeth;  
  21.   
  22.     public byte[] getHeader() throws IOException {  
  23.         ByteArrayOutputStream bos = new ByteArrayOutputStream();  
  24.         writeChar(bos, fileID);  
  25.         writeInt(bos, fileLength);  
  26.         writeChar(bos, wavTag);  
  27.         writeChar(bos, FmtHdrID);  
  28.         writeInt(bos, FmtHdrLeth);  
  29.         writeShort(bos, FormatTag);  
  30.         writeShort(bos, Channels);  
  31.         writeInt(bos, SamplesPerSec);  
  32.         writeInt(bos, AvgBytesPerSec);  
  33.         writeShort(bos, BlockAlign);  
  34.         writeShort(bos, BitsPerSample);  
  35.         writeChar(bos, DataHdrID);  
  36.         writeInt(bos, DataHdrLeth);  
  37.         bos.flush();  
  38.         byte[] r = bos.toByteArray();  
  39.         bos.close();  
  40.         return r;  
  41.     }  
  42.   
  43.     private void writeShort(ByteArrayOutputStream bos, int s) throws IOException {  
  44.         byte[] mybyte = new byte[2];  
  45.         mybyte[1] = (byte) ((s << 16) >> 24);  
  46.         mybyte[0] = (byte) ((s << 24) >> 24);  
  47.         bos.write(mybyte);  
  48.     }  
  49.   
  50.     private void writeInt(ByteArrayOutputStream bos, int n) throws IOException {  
  51.         byte[] buf = new byte[4];  
  52.         buf[3] = (byte) (n >> 24);  
  53.         buf[2] = (byte) ((n << 8) >> 24);  
  54.         buf[1] = (byte) ((n << 16) >> 24);  
  55.         buf[0] = (byte) ((n << 24) >> 24);  
  56.         bos.write(buf);  
  57.     }  
  58.   
  59.     private void writeChar(ByteArrayOutputStream bos, char[] id) {  
  60.         for (int i = 0; i < id.length; i++) {  
  61.             char c = id[i];  
  62.             bos.write(c);  
  63.         }  
  64.     }  
  65. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值