Java Implementation of Multiple wav File Synthesis Method Example

  • 2021-07-26 07:47:15
  • OfStack

In this paper, an example is given to describe the method of synthesizing multiple wav files into one by Java. Share it for your reference, as follows:

In the previous article, the method of cutting wav audio files by java was introduced, and the method of merging multiple wav audio files was given here.


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;
/**
 *  Get wav The header files are then merged into a single wav
 * @author zcf
 * @date 2017-10-17
 */
public class WavMergeUtil {
  /**
   * meger Multiple wav
   * @param inputs  Multiple wav
   * @param output  To generate 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);
      }
    }
  }
  /**
   *  Parse the header and get the file pointer pointing to the starting position of the data InputStreram Remember to close it after use 
   */
  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)) {//  Include fmt Segment 
        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;
    }
  }
  /**
   *  Will int Convert to byte[]
   */
  private static byte[] intToByteArray(int data) {
    return ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(data).array();
  }
  /**
   *  Will short Convert to byte[]
   */
  private static byte[] shortToByteArray(short data) {
    return ByteBuffer.allocate(2).order(ByteOrder.LITTLE_ENDIAN).putShort(data).array();
  }
  /**
   *  Will byte[] Convert to short
   */
  private static short byteArrayToShort(byte[] b) {
    return ByteBuffer.wrap(b).order(ByteOrder.LITTLE_ENDIAN).getShort();
  }
  /**
   *  Will byte[] Convert to int
   */
  private static int byteArrayToInt(byte[] b) {
    return ByteBuffer.wrap(b).order(ByteOrder.LITTLE_ENDIAN).getInt();
  }
  /**
   *  Head part information 
   */
  static class Header {
    public int fileSize;
    public int fileSizeOffset;
    public int dataSize;
    public int dataSizeOffset;
    public int dataOffset;
    public FileInputStream dataInputStream;
  }
}

More readers interested in java algorithm can check the topics of this site: "Summary of Java File and Directory Operation Skills", "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Skills" and "Summary of Java Cache Operation Skills"

I hope this article is helpful to everyone's java programming.


Related articles: