代碼分析JAVA中PCM人聲音頻變聲處理
項(xiàng)目中需要用到對(duì)PCM人聲音頻數(shù)據(jù)進(jìn)行變聲處理。苦苦掙扎了一周終于找到了純Java實(shí)現(xiàn)的一套框架——TarsosDSP。功能非常強(qiáng)大!可以實(shí)時(shí)音頻處理!當(dāng)然我只用到了對(duì)文件處理。實(shí)際上邏輯是一樣的
TarsosDSP的GitHub地址:https://github.com/JorenSix/TarsosDSP 將它整合至自己的項(xiàng)目工程。
具體Java工具類(lèi)代碼:
/** * 變聲 * @param rawPcmInputStream 原始PCM數(shù)據(jù)輸入流 * @param speedFactor 變速率 (0,2) 大于1為加快語(yǔ)速,小于1為放慢語(yǔ)速 * @param rateFactor 音調(diào)變化率 (0,2) 大于1為降低音調(diào)(深沉),小于1為提升音調(diào)(尖銳) * @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ù)轉(zhuǎn)錄器(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; } }
可以通過(guò)這個(gè)工具方法播放音頻:
/** * 播放PCM * * 不要在非桌面環(huán)境調(diào)用。。。鬼知道會(huì)發(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(); }
相關(guān)文章
如何解決Mybatis-plus中@TableLogic注解失效問(wèn)題
這篇文章主要介紹了如何解決Mybatis-plus中@TableLogic注解失效問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05Spring中InitializingBean的使用詳細(xì)解析
這篇文章主要介紹了Spring中InitializingBean的使用詳細(xì)解析,InitializingBean是Spring提供的拓展性接口,提供了屬性初始化后的處理方法,它只有一個(gè)afterPropertiesSet方法,凡是繼承該接口的類(lèi),在bean的屬性初始化后都會(huì)執(zhí)行該方法,需要的朋友可以參考下2024-02-02Java泛型extends及super區(qū)別實(shí)例解析
這篇文章主要介紹了Java泛型extends及super區(qū)別實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08SpringBoot結(jié)合ElasticSearch實(shí)現(xiàn)模糊查詢(xún)的項(xiàng)目實(shí)踐
本文主要介紹了SpringBoot結(jié)合ElasticSearch實(shí)現(xiàn)模糊查詢(xún)的項(xiàng)目實(shí)踐,主要實(shí)現(xiàn)模糊查詢(xún)、批量CRUD、排序、分頁(yè)和高亮功能,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03Java在指定路徑上創(chuàng)建文件提示不存在解決方法
在本篇文章里小編給大家整理的是一篇關(guān)于Java在指定路徑上創(chuàng)建文件提示不存在解決方法,有需要的朋友們可以參考下。2020-02-02java 數(shù)據(jù)結(jié)構(gòu)中棧和隊(duì)列的實(shí)例詳解
這篇文章主要介紹了java 數(shù)據(jù)結(jié)構(gòu)中棧和隊(duì)列的實(shí)例詳解的相關(guān)資料,主要使用數(shù)組與線性表的方法來(lái)實(shí)現(xiàn),需要的朋友可以參考下2017-09-09SpringBoot啟動(dòng)失敗的解決方法:A component required a&nb
這篇文章主要介紹了解決SpringBoot啟動(dòng)失?。篈 component required a bean of type ‘xxxxxxx‘ that could not be found.,目前解決方法有兩種,一種是不注入bean的方式,另一種是使用@Component的方式,本文給大家詳細(xì)講解,需要的朋友可以參考下2023-02-02