代碼分析JAVA中PCM人聲音頻變聲處理
項目中需要用到對PCM人聲音頻數(shù)據(jù)進(jìn)行變聲處理。苦苦掙扎了一周終于找到了純Java實現(xiàn)的一套框架——TarsosDSP。功能非常強(qiáng)大!可以實時音頻處理!當(dāng)然我只用到了對文件處理。實際上邏輯是一樣的
TarsosDSP的GitHub地址:https://github.com/JorenSix/TarsosDSP 將它整合至自己的項目工程。
具體Java工具類代碼:
/**
* 變聲
* @param rawPcmInputStream 原始PCM數(shù)據(jù)輸入流
* @param speedFactor 變速率 (0,2) 大于1為加快語速,小于1為放慢語速
* @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;
}
}
可以通過這個工具方法播放音頻:
/**
* 播放PCM
*
* 不要在非桌面環(huán)境調(diào)用。。。鬼知道會發(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)文章
Spring Security permitAll()不允許匿名訪問的操作
這篇文章主要介紹了Spring Security permitAll()不允許匿名訪問的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
Spring-boot oauth2使用RestTemplate進(jìn)行后臺自動登錄的實現(xiàn)
這篇文章主要介紹了Spring-boot oauth2使用RestTemplate進(jìn)行后臺自動登錄的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
SpringBoot整合MOTT動態(tài)讀取數(shù)據(jù)庫連接信息并連接MQTT服務(wù)端
MQTT是一種輕量級的消息傳輸協(xié)議(Message Queuing Telemetry Transport),旨在實現(xiàn)設(shè)備之間的低帶寬和高延遲的通信,本文給大家介紹了SpringBoot整合MOTT動態(tài)讀取數(shù)據(jù)庫連接信息并連接MQTT服務(wù)端,需要的朋友可以參考下2024-04-04

