Code Analysis in JAVA, javapcm

Source: Internet
Author: User

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 ();}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.