转换成16KHz采样率
void reSampling(byte[] data) throws IOException, UnsupportedAudioFileException { WaveFileReader reader = new WaveFileReader(); AudioInputStream audioIn = reader.getAudioInputStream(new ByteArrayInputStream(data)); AudioFormat srcFormat = audioIn.getFormat(); int targetSampleRate = 16000; AudioFormat dstFormat = new AudioFormat(srcFormat.getEncoding(), targetSampleRate, srcFormat.getSampleSizeInBits(), srcFormat.getChannels(), srcFormat.getFrameSize(), srcFormat.getFrameRate(), srcFormat.isBigEndian()); System.out.println(audioIn.getFrameLength()); AudioInputStream convertedIn = AudioSystem.getAudioInputStream(dstFormat, audioIn); String fileName = System.getenv("TEMP").concat(File.separator).concat(System.currentTimeMillis()+".wav"); File file= new File(fileName); WaveFileWriter writer = new WaveFileWriter(); writer.write(convertedIn, AudioFileFormat.Type.WAVE, file); }
使用方法:
byte[] bytes = Files.readAllBytes(Paths.get("e:\\asset\\nZLWKJjQ.wav"));
参考来源:
https://stackoverflow.com/questions/15410725/java-resample-wav-soundfile-without-third-party-library
https://www.codota.com/web/assistant/code/rs/5c7689a149efcb00014e68b2#L54
笔者一直想把重采样率的数据放在内存中,以二进制数据方式进行交互,也就是不使用磁盘作为交互,返回byte[]。暂时没想到办法