代碼分析JAVA中PCM人聲音頻變聲處理
項目中需要用到對PCM人聲音頻數(shù)據(jù)進行變聲處理??嗫鄴暝艘恢芙K于找到了純Java實現(xiàn)的一套框架——TarsosDSP。功能非常強大!可以實時音頻處理!當然我只用到了對文件處理。實際上邏輯是一樣的
TarsosDSP的GitHub地址:https://github.com/JorenSix/TarsosDSP 將它整合至自己的項目工程。
具體Java工具類代碼:
/** * 變聲 * @param rawPcmInputStream 原始PCM數(shù)據(jù)輸入流 * @param speedFactor 變速率 (0,2) 大于1為加快語速,小于1為放慢語速 * @param rateFactor 音調變化率 (0,2) 大于1為降低音調(深沉),小于1為提升音調(尖銳) * @return 變聲后的PCM數(shù)據(jù)輸入流 */ public static InputStream speechPitchShift(final InputStream rawPcmInputStream,double speedFactor,double rateFactor) { TarsosDSPAudioFormat format = new TarsosDSPAudioFormat(16000,16,1,true,false); AudioInputStream inputStream = new AudioInputStream(rawPcmInputStream, JVMAudioInputStream.toAudioFormat(format),AudioSystem.NOT_SPECIFIED); JVMAudioInputStream stream = new JVMAudioInputStream(inputStream); WaveformSimilarityBasedOverlapAdd w = new WaveformSimilarityBasedOverlapAdd(WaveformSimilarityBasedOverlapAdd.Parameters.speechDefaults(speedFactor, 16000)); int inputBufferSize = w.getInputBufferSize(); int overlap = w.getOverlap(); AudioDispatcher dispatcher = new AudioDispatcher(stream, inputBufferSize ,overlap); w.setDispatcher(dispatcher); AudioOutputToByteArray out = new AudioOutputToByteArray(); dispatcher.addAudioProcessor(w); dispatcher.addAudioProcessor(new RateTransposer(rateFactor)); dispatcher.addAudioProcessor(out); dispatcher.run(); return new ByteArrayInputStream(out.getData()); }
其中數(shù)據(jù)轉錄器(AudioOutputToByteArray)代碼如下:
public class AudioOutputToByteArray implements AudioProcessor { private boolean isDone = false; private byte[] out = null; private ByteArrayOutputStream bos; public AudioOutputToByteArray() { bos = new ByteArrayOutputStream(); } public byte[] getData() { while (!isDone && out == null) { try { Thread.sleep(10); } catch (InterruptedException ignored) {} } return out; } @Override public boolean process(AudioEvent audioEvent) { bos.write(audioEvent.getByteBuffer(),0,audioEvent.getByteBuffer().length); return true; } @Override public void processingFinished() { out = bos.toByteArray().clone(); bos = null; isDone = true; } }
可以通過這個工具方法播放音頻:
/** * 播放PCM * * 不要在非桌面環(huán)境調用。。。鬼知道會發(fā)生什么 * @param rawPcmInputStream 原始PCM數(shù)據(jù)輸入流 * @throws LineUnavailableException */ public static void play(final InputStream rawPcmInputStream) throws LineUnavailableException { TarsosDSPAudioFormat format = new TarsosDSPAudioFormat(16000,16,1,true,false); AudioInputStream inputStream = new AudioInputStream(rawPcmInputStream, JVMAudioInputStream.toAudioFormat(format),AudioSystem.NOT_SPECIFIED); JVMAudioInputStream stream = new JVMAudioInputStream(inputStream); AudioDispatcher dispatcher = new AudioDispatcher(stream, 1024 ,0); dispatcher.addAudioProcessor(new AudioPlayer(format,1024)); dispatcher.run(); }
相關文章
如何解決Mybatis-plus中@TableLogic注解失效問題
這篇文章主要介紹了如何解決Mybatis-plus中@TableLogic注解失效問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05Spring中InitializingBean的使用詳細解析
這篇文章主要介紹了Spring中InitializingBean的使用詳細解析,InitializingBean是Spring提供的拓展性接口,提供了屬性初始化后的處理方法,它只有一個afterPropertiesSet方法,凡是繼承該接口的類,在bean的屬性初始化后都會執(zhí)行該方法,需要的朋友可以參考下2024-02-02SpringBoot結合ElasticSearch實現(xiàn)模糊查詢的項目實踐
本文主要介紹了SpringBoot結合ElasticSearch實現(xiàn)模糊查詢的項目實踐,主要實現(xiàn)模糊查詢、批量CRUD、排序、分頁和高亮功能,具有一定的參考價值,感興趣的可以了解一下2024-03-03Java在指定路徑上創(chuàng)建文件提示不存在解決方法
在本篇文章里小編給大家整理的是一篇關于Java在指定路徑上創(chuàng)建文件提示不存在解決方法,有需要的朋友們可以參考下。2020-02-02SpringBoot啟動失敗的解決方法:A component required a&nb
這篇文章主要介紹了解決SpringBoot啟動失?。篈 component required a bean of type ‘xxxxxxx‘ that could not be found.,目前解決方法有兩種,一種是不注入bean的方式,另一種是使用@Component的方式,本文給大家詳細講解,需要的朋友可以參考下2023-02-02