Chinaunix首页 | 论坛 | 博客
  • 博客访问: 604392
  • 博文数量: 298
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3077
  • 用 户 组: 普通用户
  • 注册时间: 2019-06-17 10:57
文章分类

全部博文(298)

文章存档

2022年(96)

2021年(201)

2019年(1)

我的朋友

分类: Java

2022-01-27 16:32:13


点击(此处)折叠或打开


  1. 前言
  2. wav音频添加自定义时长静音的工具类

  3. Maven依赖
  4.         <dependency>
  5.             <groupId>com.google.guava</groupId>
  6.             <artifactId>guava</artifactId>
  7.             <version>30.1.1-jre</version>
  8.         </dependency>
  9.         <dependency>
  10.             <groupId>cn.hutool</groupId>
  11.             <artifactId>hutool-all</artifactId>
  12.             <version>5.5.2</version>
  13.         </dependency>
  14. 代码
  15. package ai.guiji.csdn.tools;

  16. import cn.hutool.core.io.FileUtil;
  17. import cn.hutool.core.io.IoUtil;
  18. import cn.hutool.core.util.IdUtil;
  19. import cn.hutool.core.util.URLUtil;
  20. import com.google.common.base.Joiner;
  21. import com.google.common.primitives.Bytes;

  22. import java.io.BufferedOutputStream;
  23. import java.io.File;
  24. import java.io.FileOutputStream;
  25. import java.io.IOException;
  26. import java.net.URL;
  27. import java.util.ArrayList;
  28. import java.util.Arrays;
  29. import java.util.List;
  30. import java.util.stream.LongStream;

  31. /** java项目 fhadmin.cn **/
  32. public class WavAddSilenceUtils {
  33.   /**
  34.    * 根据PCM文件构建wav的header字段
  35.    *
  36.    * @param srate Sample rate - 8000, 16000, etc.
  37.    * @param channel Number of channels - Mono = 1, Stereo = 2, etc..
  38.    * @param format Number of bits per sample (16 here)
  39.    * @throws IOException
  40.    */
  41.   public static byte[] buildWavHeader(int dataLength, int srate, int channel, int format)
  42.       throws IOException {
  43.     byte[] header = new byte[44];

  44.     long totalDataLen = dataLength + 36;
  45.     long bitrate = srate * channel * format;

  46.     header[0] = 'R';
  47.     header[1] = 'I';
  48.     header[2] = 'F';
  49.     header[3] = 'F';
  50.     header[4] = (byte) (totalDataLen & 0xff);
  51.     header[5] = (byte) ((totalDataLen >> 8) & 0xff);
  52.     header[6] = (byte) ((totalDataLen >> 16) & 0xff);
  53.     header[7] = (byte) ((totalDataLen >> 24) & 0xff);
  54.     header[8] = 'W';
  55.     header[9] = 'A';
  56.     header[10] = 'V';
  57.     header[11] = 'E';
  58.     header[12] = 'f';
  59.     header[13] = 'm';
  60.     header[14] = 't';
  61.     header[15] = ' ';
  62.     header[16] = (byte) format;
  63.     header[17] = 0;
  64.     header[18] = 0;
  65.     header[19] = 0;
  66.     header[20] = 1;
  67.     header[21] = 0;
  68.     header[22] = (byte) channel;
  69.     header[23] = 0;
  70.     header[24] = (byte) (srate & 0xff);
  71.     header[25] = (byte) ((srate >> 8) & 0xff);
  72.     header[26] = (byte) ((srate >> 16) & 0xff);
  73.     header[27] = (byte) ((srate >> 24) & 0xff);
  74.     header[28] = (byte) ((bitrate / 8) & 0xff);
  75.     header[29] = (byte) (((bitrate / 8) >> 8) & 0xff);
  76.     header[30] = (byte) (((bitrate / 8) >> 16) & 0xff);
  77.     header[31] = (byte) (((bitrate / 8) >> 24) & 0xff);
  78.     header[32] = (byte) ((channel * format) / 8);
  79.     header[33] = 0;
  80.     header[34] = 16;
  81.     header[35] = 0;
  82.     header[36] = 'd';
  83.     header[37] = 'a';
  84.     header[38] = 't';
  85.     header[39] = 'a';
  86.     header[40] = (byte) (dataLength & 0xff);
  87.     header[41] = (byte) ((dataLength >> 8) & 0xff);
  88.     header[42] = (byte) ((dataLength >> 16) & 0xff);
  89.     header[43] = (byte) ((dataLength >> 24) & 0xff);

  90.     return header;
  91.   }

  92.   /**
  93.    * 默认写入的pcm数据是16000采样率,16bit,可以按照需要修改
  94.    *
  95.    * @param filePath
  96.    * @param pcmData
  97.    */
  98.   public static boolean writeToFile(String filePath, byte[] pcmData) {
  99.     BufferedOutputStream bos = null;
  100.     try {
  101.       bos = new BufferedOutputStream(new FileOutputStream(filePath));
  102.       byte[] header = buildWavHeader(pcmData.length, 16000, 1, 16);
  103.       bos.write(header, 0, 44);
  104.       bos.write(pcmData);
  105.       bos.close();
  106.       return true;
  107.     } catch (Exception e) {
  108.       e.printStackTrace();
  109.     } finally {
  110.       if (bos != null) {
  111.         try {
  112.           bos.close();
  113.         } catch (IOException e) {
  114.           e.printStackTrace();
  115.         }
  116.       }
  117.     }

  118.     return false;
  119.   }

  120.   /**
  121.    * 增加延时静音
  122.    *
  123.    * @param filePath 音频地址
  124.    * @param tmpDirPath 临时目录地址
  125.    * @param delayTime 延时时长,单位毫秒
  126.    * @return 最终文件地址
  127.    * @throws Exception 异常
  128.    */
  129.   public static String delayWav(String filePath, String tmpDirPath, Long delayTime)
  130.       throws Exception {
  131.     byte[] bytes;
  132.     if (filePath.startsWith("http")) {
  133.       bytes = IoUtil.readBytes(URLUtil.getStream(new URL(filePath)));
  134.     } else {
  135.       bytes = FileUtil.readBytes(filePath);
  136.     }
  137.     List<Byte> resultByte =
  138.         new ArrayList<>(Bytes.asList(Arrays.copyOfRange(bytes, 44, bytes.length)));
  139.     LongStream.range(0, (long) (delayTime * 32)).forEach(x -> resultByte.add((byte) 0));
  140.     String resultPath =
  141.         Joiner.on(File.separator).join(Arrays.asList(tmpDirPath, IdUtil.simpleUUID() + ".wav"));
  142.     writeToFile(resultPath, Bytes.toArray(resultByte));
  143.     return resultPath;
  144.   }

  145.   public static void main(String[] args) throws Exception {
  146.     System.out.println(delayWav("", "C:\\Users\\huyi\\Desktop\\", 10000L));
  147.   }
  148. }


  149. 代码说明:

  150. 1、delayWav方法参数分别为wav音频文件地址(可以支持http的url地址)、临时文件目录地址、延时静音时长(单位毫秒)

  151. 2、对wav的要求默认为:采样率16K、单声道。

  152. 对需要处理的音频参数调整。

  153. 3、生成uuid的随机文件名,避免重复。


阅读(675) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~