问题
I need to convert wav file from FORMAT 1 to FORMAT 2
Format 1 : μ-law, 8000Hz, 64 kbps, mono
FORMAT 2 : Container WAV Encoding PCM Rate 16K Sample Format 16 bit Channels Mono
Following is the Code snippet :
File file = new File("audio_before_conversion.wav");
AudioFormat audioFormat = new AudioFormat(16000, 16, 1, true , true);
AudioInputStream audioInputStream1 = new AudioInputStream(
new FileInputStream(file), audioFormat, numFrames);
AudioSystem.write(audioInputStream1, Type.WAVE,
new File("audio_after_conversion.wav"));
Issue : But, this is not working. It playing some noise and also reducing my audio file length.
Edit 1: mu-Law to μ-law
回答1:
Following code worked for me --
File sourceFile = new File("<Source_Path>.wav");
File targetFile = new File("<Destination_Path>.wav");
AudioInputStream sourceAudioInputStream = AudioSystem.getAudioInputStream(sourceFile);
AudioInputStream targetAudioInputStream=AudioSystem.getAudioInputStream(AudioFormat.Encoding.PCM_SIGNED, sourceAudioInputStream);
System.out.println("Sample Rate1 "+targetAudioInputStream.getFormat().getFrameRate());
AudioFormat targetFormat = new AudioFormat(new AudioFormat.Encoding("PCM_SIGNED"), 16000, 16, 1, 2, 8000, false);
AudioInputStream targetAudioInputStream1 = AudioSystem.getAudioInputStream(targetFormat, targetAudioInputStream);
System.out.println("Sample Rate "+targetAudioInputStream1.getFormat().getFrameRate());
try {
AudioSystem.write(targetAudioInputStream1, AudioFileFormat.Type.WAVE, targetFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
回答2:
You need to use the AudioSystem
and split up format conversion and audio writing into two different steps:
final File file = new File("audio_before_conversion.wav");
// open the audio stream
final AudioInputStream pcmStream8k = AudioSystem.getAudioInputStream(file);
// specify target format
final AudioFormat targetFormat = new AudioFormat(16000, 16, 1, true , true);
// this converts your audio stream
final AudioInputStream pcmStream16k = AudioSystem.getAudioInputStream(targetFormat, pcmStream8k);
// this writes the audio stream
AudioSystem.write(pcmStream16k, AudioFileFormat.Type.WAVE, new File("audio_after_conversion.wav"));
来源:https://stackoverflow.com/questions/44954593/i-need-to-convert-audio-file-from-%ce%bc-law-to-pcm