转换成16KHz采样率(含文件头)

    void reSamplingAndSave(byte[] data) throws IOException, UnsupportedAudioFileException {
WaveFileReader reader = new WaveFileReader();
AudioInputStream audioIn = reader.getAudioInputStream(new ByteArrayInputStream(data)); AudioFormat srcFormat = audioIn.getFormat();
int targetSampleRate = 16000; AudioFormat dstFormat = new AudioFormat(srcFormat.getEncoding(),
targetSampleRate,
srcFormat.getSampleSizeInBits(),
srcFormat.getChannels(),
srcFormat.getFrameSize(),
srcFormat.getFrameRate(),
srcFormat.isBigEndian()); AudioInputStream convertedIn = AudioSystem.getAudioInputStream(dstFormat, audioIn); String fileName = System.getenv("TEMP").concat(File.separator).concat(System.currentTimeMillis()+".wav");
File file= new File(fileName);
WaveFileWriter writer = new WaveFileWriter();
writer.write(convertedIn, AudioFileFormat.Type.WAVE, file);
}

使用方法:

 byte[] bytes = Files.readAllBytes(Paths.get("e:\\asset\\nZLWKJjQ.wav"));

重采样,不保留文件头(通常用于语音识别):

    byte[] reSampling(byte[] data) throws IOException, UnsupportedAudioFileException {

        AudioInputStream audioIn = AudioSystem.getAudioInputStream(new ByteArrayInputStream(data));

        AudioFormat srcFormat = audioIn.getFormat();
int targetSampleRate = 16000; AudioFormat dstFormat = new AudioFormat(srcFormat.getEncoding(),
targetSampleRate,
srcFormat.getSampleSizeInBits(),
srcFormat.getChannels(),
srcFormat.getFrameSize(),
srcFormat.getFrameRate(),
srcFormat.isBigEndian()); AudioInputStream convertedIn = AudioSystem.getAudioInputStream(dstFormat, audioIn); int numReads = -1; int BUFF_SIZE = targetSampleRate/2; byte [] buff = new byte[BUFF_SIZE]; ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
while ((numReads = convertedIn.read(buff)) !=-1)
{
System.out.println("读入字节数:"+ numReads);
outputStream.write(buff);
}
return outputStream.toByteArray();
}

重采样2(不含文件头):

public static final int SAMPLE_RATE = 16000;

// 16-bit audio
private static final int BYTES_PER_SAMPLE = 2;
// 16-bit audio
private static final int BITS_PER_SAMPLE = 16;
private static final double MAX_16_BIT = 32768;
private static final int SAMPLE_BUFFER_SIZE = 4096; private static final int MONO = 1;
private static final int STEREO = 2;
private static final boolean LITTLE_ENDIAN = false;
private static final boolean BIG_ENDIAN = true;
private static final boolean SIGNED = true;
private static final boolean UNSIGNED = false;
private static AudioFormat dstFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
SAMPLE_RATE,
BITS_PER_SAMPLE,
MONO,
BYTES_PER_SAMPLE,
8000,
LITTLE_ENDIAN);
public static byte[] reSamplingPCM(byte[] data) { try(AudioInputStream audioIn = AudioSystem.getAudioInputStream(new ByteArrayInputStream(data));
AudioInputStream convertedStream = AudioSystem.getAudioInputStream(dstFormat, audioIn);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { if (audioIn.getFormat().matches(dstFormat)) {
return data;
} int numReads = -1; int BUFF_SIZE = SAMPLE_RATE / 2; byte[] buff = new byte[BUFF_SIZE]; while ((numReads = convertedStream.read(buff)) != -1) {
log.info("read {} byte(s)", numReads);
outputStream.write(buff);
}
return outputStream.toByteArray();
} catch (UnsupportedAudioFileException |IOException e) {
log.error("occurs errors when re-sampling the audio stream:{}",e);
throw new RuntimeException("occurs errors when re-sampling the audio stream:{}",e);
}
}

参考来源:

https://stackoverflow.com/questions/15410725/java-resample-wav-soundfile-without-third-party-library

https://www.codota.com/web/assistant/code/rs/5c7689a149efcb00014e68b2#L54

纯java代码对音频采样率进行转换的更多相关文章

  1. DataX通过纯Java代码启动

    DataX是阿里巴巴团队开发的一个很好开源项目,但是他们对如何使用只提供了python命令启动方式,这种方式对于只是想简单的用下DataX的人来说很是友好,仅仅需要几行代码就可以运行,但是如果你需要在 ...

  2. 基于纯Java代码的Spring容器和Web容器零配置的思考和实现(3) - 使用配置

    经过<基于纯Java代码的Spring容器和Web容器零配置的思考和实现(1) - 数据源与事务管理>和<基于纯Java代码的Spring容器和Web容器零配置的思考和实现(2) - ...

  3. Android 使用纯Java代码布局

    java布局 java代码布局和xml布局的区别 1.Java纯布局更加的灵活,比如自定义控件或一些特殊要求时,使用java代码布局 2.常用的xml布局是所见即所得的编写方式,以及xml本身拥有一些 ...

  4. 帧动画的创建方式 - 纯Java代码方式

    废话不多说,先看东西 帧动画的创建方式主要以下2种: * 用xml创建动画: * 纯Java代码创建动画:   本文内容主要关注 纯java代码创建帧动画 的方式: 用xml创建帧动画:http:// ...

  5. 如何使用java代码进行视频格式的转换(FLV)

    如何使用java代码进行视频格式的转换(FLV) 一,前言 在给网页添加视频播放功能后,发现上传的视频有各种格式,那么就需要将他么转换成FLV,以很好的支持在线视频播放. 公司一直在使用中,配合使用, ...

  6. 在Android中用纯Java代码布局

    感谢大佬:https://www.jianshu.com/p/7aedea560f16 在Android中用纯Java代码布局 本文的完成了参考了一篇国外的教程,在此表示感谢. Android中的界面 ...

  7. Android Studio编写运行测试纯java代码可带main()函数

    问题 小伙伴们在做安卓项目的时候,是不是有时候会忘记某些api的使用方法,不太确定他们的结果是怎样的,需要写一些测试代码,验证看看我们的写法是否正确.刚开始的时候我是在页面上写一个Button,添加点 ...

  8. 使用SampleRateConverter对音频采样率进行转换

    java sound resource SampleRateconverter.java(接近于官方源码) 输入目标采样率,输入文件,输出文件.食用方便;p 比如 SampleRateConverte ...

  9. [改善Java代码]避开基本类型数组转换列表陷阱

    开发中经常用到Arrays和Collections这两个工具类. 在数组和列表之间进行切换.非常方便.但是也会遇到一些问题. 看代码: import java.util.Arrays; import ...

随机推荐

  1. kth-largest-element

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  2. git如何统计代码行数

    1.根据用户名时间段统计 git log --author="username" --since=2018-01-01 --until=2019-12-31 --pretty=tf ...

  3. YAML_03 用playbook安装Apache,修改端口,配置ServerName,修改主页,设置开机自启

    ansible]# vim http.yml --- - hosts: cache   remote_user: root   tasks:     - name: install one speci ...

  4. 用provide/inject来实现简单的vuex状态管理功能

    在开发的时候,经常会涉及到组件之间的通信.简单的有父子组件的通信,兄弟组件的通信通常可以借助Bus来进行.当然也可以用vuex来进行状态管理,但是,有时候用vuex未免有把简单的问题复杂化. 如果要进 ...

  5. Linux工具[转]

    ref: https://github.com/linw7/Skill-Tree/blob/master/Linux%E5%B7%A5%E5%85%B7.md Linux工具 Linux下还是有很多超 ...

  6. 《挑战30天C++入门极限》C++运算符重载函数基础及其值返回状态

        C++运算符重载函数基础及其值返回状态 运算符重载是C++的重要组成部分,它可以让程序更加的简单易懂,简单的运算符使用可以使复杂函数的理解更直观. 对于普通对象来说我们很自然的会频繁使用算数运 ...

  7. C语言scanf函数返回值小记

    scanf scanf是C标准库stdio里面定义的用于获取用户输入的函数,具体的介绍可以在CppReference上看到.scanf的返回值是已经成功赋值的变量个数,也就是说在 scanf(&quo ...

  8. 什么是amcl

    amcl是一种机器人在2D中移动的概率定位系统. 它实现了自适应(或KLD采样)蒙特卡罗定位方法(如Dieter Fox所述),该方法使用粒子滤波器来针对已知地图跟踪机器人的位姿. 参考: https ...

  9. HTML meta pragma no-cache 页面缓存

    HTML meta pragma no-cache 页面缓存不缓存页面(为了提高速度一些浏览器会缓存浏览者浏览过的页面,通过下面的定义,浏览器一般不会缓存页面,而且浏览器无法脱机浏览.) <me ...

  10. docker本地仓库&镜像

    镜像的命名规则: 1.[冷数据]/[base镜像]例如:ansible,centos 2. lastest{最新的意思}  不是真的(随便命名) 3. [image name]=[repository ...