【語音處理】wav轉pcm mp3轉pcm Java示例程式碼
阿新 • • 發佈:2019-01-23
【語音處理】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
需要用到的jar不支援maven引入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檔案轉AudioInputStream AudioInputStream mp3audioStream = AudioSystem.getAudioInputStream(mp3); //將AudioInputStream MP3檔案 轉換為PCM AudioInputStream AudioInputStream pcmaudioStream = AudioSystem.getAudioInputStream(AudioFormat.Encoding.PCM_SIGNED, mp3audioStream); //準備轉換的流輸出到OutputStream OutputStream 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(); } }
- 注意:需要下載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>