代碼分析JAVA中PCM人聲音頻變聲處理
項(xiàng)目中需要用到對(duì)PCM人聲音頻數(shù)據(jù)進(jìn)行變聲處理??嗫鄴暝艘恢芙K于找到了純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)文章
IDEA導(dǎo)入jar包的完整實(shí)現(xiàn)步驟
由于導(dǎo)入jar包項(xiàng)目存在很多不確定的問(wèn)題,導(dǎo)致每次都需要調(diào)試、配置好多遍,對(duì)此特意記錄下來(lái),這篇文章主要給大家介紹了關(guān)于IDEA導(dǎo)入jar包的相關(guān)資料,需要的朋友可以參考下2024-01-01
Spring Security permitAll()不允許匿名訪問(wèn)的操作
這篇文章主要介紹了Spring Security permitAll()不允許匿名訪問(wèn)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
Spring-boot oauth2使用RestTemplate進(jìn)行后臺(tái)自動(dòng)登錄的實(shí)現(xiàn)
這篇文章主要介紹了Spring-boot oauth2使用RestTemplate進(jìn)行后臺(tái)自動(dòng)登錄的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
SpringBoot整合MOTT動(dòng)態(tài)讀取數(shù)據(jù)庫(kù)連接信息并連接MQTT服務(wù)端
MQTT是一種輕量級(jí)的消息傳輸協(xié)議(Message Queuing Telemetry Transport),旨在實(shí)現(xiàn)設(shè)備之間的低帶寬和高延遲的通信,本文給大家介紹了SpringBoot整合MOTT動(dòng)態(tài)讀取數(shù)據(jù)庫(kù)連接信息并連接MQTT服務(wù)端,需要的朋友可以參考下2024-04-04

