Code Analysis in JAVA, javapcm
TheAudio Data of PCM voice changes. After struggling for a week, I finally found a framework implemented in pure Java-TarsosDSP. Very powerful! Real-time audio processing! Of course, I only used to process files. Actually, the logic is the same.
GitHub address for TarsosDSP: The https://github.com/JorenSix/TarsosDSP integrates it into its own project.
Java tool code:
/*** Voice change ** @ param rawPcmInputStream raw PCM data input stream * @ param speedFactor variable speed () greater than 1 to speed up the speech, if the value is less than 1, it indicates a slow speech rate * @ param rateFactor. The tone change rate () is greater than 1. If the value is less than 1, it indicates a lowering tone (deep). If the value is less than 1, it indicates an ascending tone (sharp) * @ return: the PCM data input stream after the change */public static InputStream speechPitchShift (final InputStream rawPcmInputStream, double speedFactor, double rateFactor) {TarsosDSPAudioFormat = new partition (, 16, 1, true, false); AudioInputStream inputStream = new AudioInputStream (rawPcmInputStream, JVMAudioInputStream. toAudioFormat (format), AudioSystem. NOT_SPECIFIED); JVMAudioInputStream stream = new JVMAudioInputStream (inputStream); WaveformSimilarityBasedOverlapAdd w = new WaveformSimilarityBasedOverlapAdd (signature. 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 ());}
The code of the Data Recorder (AudioOutputToByteArray) is as follows:
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; }}
You can use this tool to play the audio:
/*** Play PCM **. Do not call it in a non-desktop environment... Ghost knows what will happen * @ param rawPcmInputStream raw PCM data input stream * @ throws LineUnavailableException */public static void play (final InputStream rawPcmInputStream) throws LineUnavailableException {partition format = new partition (, 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 ();}