欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java实现多个wav文件合成一个的方法示例

程序员文章站 2024-02-24 13:17:58
本文实例讲述了java实现多个wav文件合成一个的方法。分享给大家供大家参考,具体如下: 前面一篇介绍了,这里再给出合并多个wav音频文件的方法。 pack...

本文实例讲述了java实现多个wav文件合成一个的方法。分享给大家供大家参考,具体如下:

前面一篇介绍了,这里再给出合并多个wav音频文件的方法。

package com.cmos.nomsapp.utils.wavmeger;
import java.io.file;
import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.randomaccessfile;
import java.nio.bytebuffer;
import java.nio.byteorder;
import java.util.arraylist;
import java.util.list;
/**
 * 获取wav头文件然后合并成单个wav
 * @author zcf
 * @date 2017-10-17
 */
public class wavmergeutil {
  /**
   * meger多个wav
   * @param inputs 多个wav
   * @param output 要生成的wav
   * @throws ioexception
   */
  public static void mergewav(file[] inputs, string output) throws ioexception {
    if (inputs.length < 1) {
      return;
    }
    try(fileinputstream fis = new fileinputstream(inputs[0]);
      fileoutputstream fos = new fileoutputstream(new file(output))){
      byte[] buffer = new byte[1024 * 4];
      int total = 0;
      int count;
      while ((count = fis.read(buffer)) > -1) {
        fos.write(buffer, 0, count);
        total += count;
      }
      fis.close();
      for (int i = 1; i < inputs.length; i++) {
        file file = inputs[i];
        try(fileinputstream fish = new fileinputstream(file)){
          header header = resolveheader(fish);
          fileinputstream datainputstream = header.datainputstream;
          while ((count = datainputstream.read(buffer)) > -1) {
            fos.write(buffer, 0, count);
            total += count;
          }
        }
      }
      fos.flush();
      fos.close();
      fileinputstream fisho = new fileinputstream(new file(output));
      header outputheader = resolveheader(fisho);
      outputheader.datainputstream.close();
      try(randomaccessfile res = new randomaccessfile(output, "rw")){
        res.seek(4);
        byte[] filelen = inttobytearray(total + outputheader.dataoffset - 8);
        res.write(filelen, 0, 4);
        res.seek(outputheader.datasizeoffset);
        byte[] datalen = inttobytearray(total);
        res.write(datalen, 0, 4);
      }
    }
  }
  /**
   * 解析头部,并获得文件指针指向数据开始位置的inputstreram,记得使用后需要关闭
   */
  private static header resolveheader(fileinputstream fis) throws ioexception {
      byte[] byte4 = new byte[4];
      byte[] buffer = new byte[2048];
      int readcount = 0;
      header header = new header();
      fis.read(byte4);// riff
      fis.read(byte4);
      readcount += 8;
      header.filesizeoffset = 4;
      header.filesize = bytearraytoint(byte4);
      fis.read(byte4);// wave
      fis.read(byte4);// fmt
      fis.read(byte4);
      readcount += 12;
      int fmtlen = bytearraytoint(byte4);
      fis.read(buffer, 0, fmtlen);
      readcount += fmtlen;
      fis.read(byte4);// data or fact
      readcount += 4;
      if (isfmt(byte4, 0)) {// 包含fmt段
        fis.read(byte4);
        int factlen = bytearraytoint(byte4);
        fis.read(buffer, 0, factlen);
        fis.read(byte4);// data
        readcount += 8 + factlen;
      }
      fis.read(byte4);// data size
      int datalen = bytearraytoint(byte4);
      header.datasize = datalen;
      header.datasizeoffset = readcount;
      readcount += 4;
      header.dataoffset = readcount;
      header.datainputstream = fis;
      return header;
  }
  private static boolean isriff(byte[] bytes, int start) {
    if (bytes[start + 0] == 'r' && bytes[start + 1] == 'i' && bytes[start + 2] == 'f' && bytes[start + 3] == 'f') {
      return true;
    } else {
      return false;
    }
  }
  private static boolean isfmt(byte[] bytes, int start) {
    if (bytes[start + 0] == 'f' && bytes[start + 1] == 'm' && bytes[start + 2] == 't' && bytes[start + 3] == ' ') {
      return true;
    } else {
      return false;
    }
  }
  private static boolean isdata(byte[] bytes, int start) {
    if (bytes[start + 0] == 'd' && bytes[start + 1] == 'a' && bytes[start + 2] == 't' && bytes[start + 3] == 'a') {
      return true;
    } else {
      return false;
    }
  }
  /**
   * 将int转化为byte[]
   */
  private static byte[] inttobytearray(int data) {
    return bytebuffer.allocate(4).order(byteorder.little_endian).putint(data).array();
  }
  /**
   * 将short转化为byte[]
   */
  private static byte[] shorttobytearray(short data) {
    return bytebuffer.allocate(2).order(byteorder.little_endian).putshort(data).array();
  }
  /**
   * 将byte[]转化为short
   */
  private static short bytearraytoshort(byte[] b) {
    return bytebuffer.wrap(b).order(byteorder.little_endian).getshort();
  }
  /**
   * 将byte[]转化为int
   */
  private static int bytearraytoint(byte[] b) {
    return bytebuffer.wrap(b).order(byteorder.little_endian).getint();
  }
  /**
   * 头部部分信息
   */
  static class header {
    public int filesize;
    public int filesizeoffset;
    public int datasize;
    public int datasizeoffset;
    public int dataoffset;
    public fileinputstream datainputstream;
  }
}

更多关于java算法相关内容感兴趣的读者可查看本站专题:《java文件与目录操作技巧汇总》、《java数据结构与算法教程》、《java操作dom节点技巧总结》和《java缓存操作技巧汇总

希望本文所述对大家java程序设计有所帮助。