項目中需要用到對PCM人聲音頻數據進行變聲處理。苦苦掙扎了一周終于找到了純Java實現的一套框架——TarsosDSP。功能非常強大!可以實時音頻處理!當然我只用到了對文件處理。實際上邏輯是一樣的
TarsosDSP的GitHub地址:https://github.com/JorenSix/TarsosDSP 將它整合至自己的項目工程。
具體Java工具類代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
/** * 變聲 * @param rawPcmInputStream 原始PCM數據輸入流 * @param speedFactor 變速率 (0,2) 大于1為加快語速,小于1為放慢語速 * @param rateFactor 音調變化率 (0,2) 大于1為降低音調(深沉),小于1為提升音調(尖銳) * @return 變聲后的PCM數據輸入流 */ 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()); } |
其中數據轉錄器(AudioOutputToByteArray)代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
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; } } |
可以通過這個工具方法播放音頻:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
/** * 播放PCM * * 不要在非桌面環境調用。。。鬼知道會發生什么 * @param rawPcmInputStream 原始PCM數據輸入流 * @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(); } |
原文鏈接:https://segmentfault.com/a/1190000012834216