本文主要是介绍【语音处理】wav转pcm mp3转pcm Java示例代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
【语音处理】wav转pcmJava示例代码
都是作者亲测的代码哦。因各个音频之间存在差异导致转换会存在问题。建议大家自己有习惯看源码去了解音频相关知识的能力。
代码地址:https://gitee.com/xshuai/ai/blob/master/AIDemo/src/main/java/com/xs/audio/tns/WAVConvertPCM.java
- Wav转PCM
package com.xs.audio.tns;import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;import org.apache.commons.io.IOUtils;
/*** WAV转PCM(截掉前44个字节数据)* @author Administrator**/
public class WAVConvertPCM {/*** WAV转PCM文件* @param wavfilepath wav文件路径* @param pcmfilepath pcm要保存的文件路径及文件名* @return*/public static String convertAudioFiles(String wavfilepath,String pcmfilepath){FileInputStream fileInputStream;FileOutputStream fileOutputStream;try {fileInputStream = new FileInputStream(wavfilepath);fileOutputStream = new FileOutputStream(pcmfilepath);byte[] wavbyte = InputStreamToByte(fileInputStream);byte[] pcmbyte = Arrays.copyOfRange(wavbyte, 44, wavbyte.length);fileOutputStream.write(pcmbyte);IOUtils.closeQuietly(fileInputStream);IOUtils.closeQuietly(fileOutputStream);} catch (Exception e) {System.out.println(e.getMessage());}return pcmfilepath;}/*** 输入流转byte二进制数据* @param fis* @return* @throws IOException*/private static byte[] InputStreamToByte(FileInputStream fis) throws IOException {ByteArrayOutputStream byteStream = new ByteArrayOutputStream();long size = fis.getChannel().size();byte[] buffer = null;if (size <= Integer.MAX_VALUE) {buffer = new byte[(int) size];} else {buffer = new byte[8];for (int ix = 0; ix < 8; ++ix) {int offset = 64 - (ix + 1) * 8;buffer[ix] = (byte) ((size >> offset) & 0xff);}}int len;while ((len = fis.read(buffer)) != -1) {byteStream.write(buffer, 0, len);}byte[] data = byteStream.toByteArray();IOUtils.closeQuietly(byteStream);return data;}
}
- MP3转PCM
代码地址:https://gitee.com/xshuai/ai/blob/master/AIDemo/src/main/java/com/xs/audio/tns/MP3ConvertPCM.java
package com.xs.audio.tns;import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
/*** MP3转PCM Java方式实现* @author 小帅丶* @date 2017年12月6日*/
public class MP3ConvertPCM {/*** MP3转换PCM文件方法* @param mp3filepath 原始文件路径* @param pcmfilepath 转换文件的保存路径* @throws Exception */public static void mp3Convertpcm(String mp3filepath,String pcmfilepath) throws Exception{File mp3 = new File(mp3filepath);File pcm = new File(pcmfilepath);//原MP3文件转AudioInputStreamAudioInputStream mp3audioStream = AudioSystem.getAudioInputStream(mp3);//将AudioInputStream MP3文件 转换为PCM AudioInputStreamAudioInputStream pcmaudioStream = AudioSystem.getAudioInputStream(AudioFormat.Encoding.PCM_SIGNED, mp3audioStream);//准备转换的流输出到OutputStreamOutputStream os = new FileOutputStream(pcm);int bytesRead = 0;byte[] buffer = new byte[8192];while ((bytesRead=pcmaudioStream.read(buffer, 0, 8192))!=-1) {os.write(buffer, 0, bytesRead);}os.close();pcmaudioStream.close();}
}
需要用到的jar不支持maven引入
- 注意:需要下载MP3插件jar。才可以进行MP3CONVERTPCM
- 下载地址一链接: https://pan.baidu.com/s/1nwmC4Xz 密码: fzqd
- 下载地址二链接: https://gitee.com/xshuai/ai/attach_files
或者用谷歌的jar支持maven引入
<dependency><groupId>com.googlecode.soundlibs</groupId><artifactId>mp3spi</artifactId><version>1.9.5.4</version></dependency>
这篇关于【语音处理】wav转pcm mp3转pcm Java示例代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!