简体   繁体   中英

Java Convert AudioFormat

what I'm trying to do is that I am capturing the audio from microphone via TargetDataLine class, send it with socket and play on another side via SourceDataLine. The problem is that microphone I'm capturing with, only supports:

AudioFormat format = new AudioFormat(48000, 16, 1, true, true);

and the speaker I'm playing it to, supports only:

AudioFormat format = new AudioFormat(32000, 16, 2, true, true);

When I directly get 48khz and push it to 32khz audioformat, the sound is pitched, is there any way to convert the 48khz mono sound to 32khz stereo?

If Java has some built in audio file format conversion, you can learn about it from the " Using Files and Format Converters " post on the audio tutorials thread.

If it has to be done manually, check out the first code example on that page, especially noting the point with the comment

  // Here, do something useful with the audio data that's // now in the audioBytes array... 

If you run the input and output lines in parallel, you can use linear interpolation to take data from the 48000 line and post to the 32000 line. The ratio is 3:2.

Thus, for 3 PCM data points in the input, calculate 2 PCM data points for the output.

For the input, you would send in[0] to out[0] and the sum of in 1 /2 and in[2]/2 to out 1 .

Duplicate the value for converting from mono to stereo.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM