使用SampleRateConverter对音频采样率进行转换
SampleRateconverter.java(接近于官方源码)
输入目标采样率,输入文件,输出文件。食用方便;p
比如
SampleRateConverter.main(new String[]{"44100","f:\\temp\\32_bit_float.wav","f:\\temp\\32_bit_float_44.1K.wav"});
SampleRateConverter源码:
/*
* SampleRateConverter.java
*
* This file is part of jsresources.org
*/ /*
* Copyright (c) 1999 - 2003 by Matthias Pfisterer
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/ /*
|<--- this code is formatted to fit into 80 columns --->|
*/ import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.File;
import java.io.IOException; /** <titleabbrev>SampleRateConverter</titleabbrev>
<title>Converting the sample rate of audio files</title> <formalpara><title>Purpose</title>
<para>Converts audio files, changing the sample rate of the
audio data.</para>
</formalpara> <formalpara><title>Usage</title>
<para>
<cmdsynopsis>
<command>java SampleRateConverter</command>
<arg choice="plain"><option>-h</option></arg>
</cmdsynopsis>
<cmdsynopsis>
<command>java SampleRateConverter</command>
<arg choice="plain"><replaceable class="parameter">targetsamplerate</replaceable></arg>
<arg choice="plain"><replaceable class="parameter">sourcefile</replaceable></arg>
<arg choice="plain"><replaceable class="parameter">targetfile</replaceable></arg>
</cmdsynopsis>
</para></formalpara> <formalpara><title>Parameters</title>
<variablelist>
<varlistentry>
<term><option>-h</option></term>
<listitem><para>prints usage information</para></listitem>
</varlistentry>
<varlistentry>
<term><replaceable class="parameter">targetsamplerate</replaceable></term>
<listitem><para>the sample rate that should be converted to</para></listitem>
</varlistentry>
<varlistentry>
<term><replaceable class="parameter">sourcefile</replaceable></term>
<listitem><para>the file name of the audio file that should be read to get the audio data to convert</para></listitem>
</varlistentry>
<varlistentry>
<term><replaceable class="parameter">targetfile</replaceable></term>
<listitem><para>the file name of the audio file that the converted audio data should be written to</para></listitem>
</varlistentry>
</variablelist>
</formalpara> <formalpara><title>Bugs, limitations</title>
<para>Sample rate conversion can only be handled natively
by <ulink url="http://www.tritonus.org/">Tritonus</ulink>.
If you want to do sample rate conversion with the
Sun jdk1.3/1.4, you have to install Tritonus' sample rate converter.
It is part of the 'Tritonus miscellaneous' plug-in. See <ulink url
="http://www.tritonus.org/plugins.html">Tritonus Plug-ins</ulink>.
</para>
</formalpara> <formalpara><title>Source code</title>
<para>
<ulink url="SampleRateConverter.java.html">SampleRateConverter.java</ulink>,
<ulink url="AudioCommon.java.html">AudioCommon.java</ulink>
</para>
</formalpara> */
public class SampleRateConverter
{
/** Flag for debugging messages.
* If true, some messages are dumped to the console
* during operation.
*/
private static boolean DEBUG = true; public static void main(String[] args)
throws UnsupportedAudioFileException, IOException
{
if (args.length == 1)
{
if (args[0].equals("-h"))
{
printUsageAndExit();
}
else
{
printUsageAndExit();
}
}
else if (args.length != 3)
{
printUsageAndExit();
}
float fTargetSampleRate = Float.parseFloat(args[0]);
if (DEBUG) { out("target sample rate: " + fTargetSampleRate); }
File sourceFile = new File(args[1]);
File targetFile = new File(args[2]); /* We try to use the same audio file type for the target
file as the source file. So we first have to find
out about the source file's properties.
*/
AudioFileFormat sourceFileFormat = AudioSystem.getAudioFileFormat(sourceFile);
AudioFileFormat.Type targetFileType = sourceFileFormat.getType(); /* Here, we are reading the source file.
*/
AudioInputStream sourceStream = null;
sourceStream = AudioSystem.getAudioInputStream(sourceFile);
if (sourceStream == null)
{
out("cannot open source audio file: " + sourceFile);
System.exit(1);
}
AudioFormat sourceFormat = sourceStream.getFormat();
if (DEBUG) { out("source format: " + sourceFormat); } /* Currently, the only known and working sample rate
converter for Java Sound requires that the encoding
of the source stream is PCM (signed or unsigned).
So as a measure of convenience, we check if this
holds here.
*/
AudioFormat.Encoding encoding = sourceFormat.getEncoding();
//此处与官方不同
if (! (encoding.equals(AudioFormat.Encoding.PCM_SIGNED)
|| encoding.equals(AudioFormat.Encoding.PCM_UNSIGNED)
||encoding.equals(AudioFormat.Encoding.PCM_FLOAT)))
{
out("encoding of source audio data is not PCM; conversion not possible");
System.exit(1);
} /* Since we now know that we are dealing with PCM, we know
that the frame rate is the same as the sample rate.
*/
float fTargetFrameRate = fTargetSampleRate; /* Here, we are constructing the desired format of the
audio data (as the result of the conversion should be).
We take over all values besides the sample/frame rate.
*/ AudioFormat targetFormat = new AudioFormat(
sourceFormat.getEncoding(),
fTargetSampleRate,
sourceFormat.getSampleSizeInBits(),
sourceFormat.getChannels(),
sourceFormat.getFrameSize(),
fTargetFrameRate,
sourceFormat.isBigEndian()); if (DEBUG) { out("desired target format: " + targetFormat); } /* 开始音频转换.
*/
AudioInputStream targetStream = AudioSystem.getAudioInputStream(targetFormat, sourceStream);
if (DEBUG) { out("targetStream: " + targetStream); } /* And finally, we are trying to write the converted audio
data to a new file.
*/
int nWrittenBytes = 0;
nWrittenBytes = AudioSystem.write(targetStream, targetFileType, targetFile);
if (DEBUG) { out("Written bytes: " + nWrittenBytes); }
} private static void printUsageAndExit()
{
out("SampleRateConverter: usage:");
out("\tjava SampleRateConverter -h");
out("\tjava SampleRateConverter <targetsamplerate> <sourcefile> <targetfile>");
System.exit(1);
} private static void out(String strMessage)
{
System.out.println(strMessage);
}
}
笔者在JDK1.8下面测试发现,目标编码还支持PCM_FLOAT(即pcm_f32le) 32位浮点型音频
使用SampleRateConverter对音频采样率进行转换的更多相关文章
- 纯java代码对音频采样率进行转换
转换成16KHz采样率(含文件头) void reSamplingAndSave(byte[] data) throws IOException, UnsupportedAudioFileExcept ...
- 为什么需要超出48K的音频采样率,以及PCM到DSD的演进
网上很多观点说,根据采样定理,48K的音频采样率即可无损的表示音频模拟信号(人耳最多可以听到20K的音频),为何还需要96K, 192K等更高的采样率呢?最先我也有这样的疑问,毕竟采样定理是经过数学家 ...
- javaCV开发详解之7:让音频转换更加简单,实现通用音频编码格式转换、重采样等音频参数的转换功能(以pcm16le编码的wav转mp3为例)
javaCV系列文章: javacv开发详解之1:调用本机摄像头视频 javaCV开发详解之2:推流器实现,推本地摄像头视频到流媒体服务器以及摄像头录制视频功能实现(基于javaCV-FFMPEG.j ...
- 太赞了!Python竟可以轻松实现音频格式无损转换
大家好,我是辰哥 辰哥在平时处理音频格式的时候,需要去下载各种音频处理软件(专业一点的软件还要收费),掌握Python技术的我们,知道Python是万能的(哈哈哈,开个玩笑).今天辰哥就来教大家用Py ...
- 将任意音频格式文件转换成16K采样率16bit的wav文件
此转换需要使用ffmpeg 假设有目录 d:\录音 目录有 张三.m4a, 李四.m4a xxx.m4a(其他任意格式音频触类旁通可以把 *.m4a改成*.*).批量转换成采样率16K,有符号,16b ...
- Unity 利用FFmpeg实现录屏、直播推流、音频视频格式转换、剪裁等功能
目录 一.FFmpeg简介. 二.FFmpeg常用参数及命令. 三.FFmpeg在Unity 3D中的使用. 1.FFmpeg 录屏. 2.FFmpeg 推流. 3.FFmpeg 其他功能简述. 一. ...
- FFmpeg学习4:音频格式转换
前段时间,在学习试用FFmpeg播放音频的时候总是有杂音,网上的很多教程是基于之前版本的FFmpeg的,而新的FFmepg3中audio增加了平面(planar)格式,而SDL播放音频是不支持平面格式 ...
- (原创)speex与wav格式音频文件的互相转换
我们的司信项目又有了新的需求,就是要做会议室.然而需求却很纠结,要继续按照原来发语音消息那样的形式来实现这个会议的功能,还要实现语音播放的计时,暂停,语音的拼接,还要绘制频谱图等等. 如果是wav,m ...
- C# 使用ffmpeg.exe进行音频转换完整demo
今天在处理微信的开发接口时候,发现微信多媒体上传接口中返回的音频格式是amr.坑人的是现在大部分的web 播放器,不支持amr的格式播放.试了很多方法都不行. 没办法,只要找一个妥协的解决方案:将am ...
随机推荐
- HTTP头部
10-URI的基本格式以及与URL的区别 HTTP连接的常见流程 从TCP编程上看HTTP请求处理 长连接与短连接 补充一下代理的知识 什么是正向代理,什么是反向代理? 想在外部公网访问公司内部局域网 ...
- LA 3704细胞自动机——循环矩阵&&矩阵快速幂
题目 一个细胞自动机包含 $n$ 个格子,每个格子的取值为 $0 \sim m-1$.给定距离 $d$,则每次操作是将每个格子的值变为到它的距离不超过 $d$ 的所有格子的在操作之前的值的和除以 $m ...
- LightOJ - 1282 - Leading and Trailing(数学技巧,快速幂取余)
链接: https://vjudge.net/problem/LightOJ-1282 题意: You are given two integers: n and k, your task is to ...
- javaweb学习笔记(三)
一.javaweb高级(Filter和Listener)的简单介绍 1.过滤器Filter (https://www.cnblogs.com/vanl/p/5742501.html) ①定义 Filt ...
- windows错误代码摘录
Windows API 错误代码定义在winerror.h里,当我们得到一个Error Code不知其意时,可以查阅这个文件 这里定义了绝大部分的错误,摘录翻译如下 [0]-操作成功完成. [1]-功 ...
- loj #10131
抽离题意 求删除一条树边和一条非树边后将图分成不连通的两部分的方案数 对于一棵树,再加入一条边就会产生环.若只有一个环,说明只加入了一条非树边 (x, y),记 lca 为 l, 那么 对于任意一条 ...
- 数组splay ------ luogu P3369 【模板】普通平衡树(Treap/SBT)
二次联通门 : luogu P3369 [模板]普通平衡树(Treap/SBT) #include <cstdio> #define Max 100005 #define Inline _ ...
- 笔记-读官方Git教程(2)~安装与配置
小书匠 版本管理 教程内容基本来自git官方教程,认真都了系列的文章,然后对一些重点的记录下来,做了简单的归纳并写上自己的思考. 1.安装 在基于 Debian 的发行版上,使用 apt-get安装 ...
- deepin 深度Linux系统 15.11 链接蓝牙鼠标问题
不知道为毛就是搜索不到,好吧只能用老方法,那就是不使用deepin系统自带的面板进行管理 用下面的命令进行安装配置即可 sudo apt install bluetooth blueman bluem ...
- Alpha冲刺(1/6)
队名:無駄無駄 组长博客 作业博客(5分) 以下内容一个小组共55分,看完之后对此部分整体打分 张越洋 过去两天完成了哪些任务 如何进行团队代码的版本管理 如何使用微信云开发 如何使用管理微信开发团队 ...