转换成16KHz采样率(含文件头)
void reSamplingAndSave(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()); 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"));
重采样,不保留文件头(通常用于语音识别):
byte[] reSampling(byte[] data) throws IOException, UnsupportedAudioFileException { AudioInputStream audioIn = AudioSystem.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()); AudioInputStream convertedIn = AudioSystem.getAudioInputStream(dstFormat, audioIn); int numReads = -1; int BUFF_SIZE = targetSampleRate/2; byte [] buff = new byte[BUFF_SIZE]; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); while ((numReads = convertedIn.read(buff)) !=-1) { System.out.println("读入字节数:"+ numReads); outputStream.write(buff); } return outputStream.toByteArray(); }
重采样2(不含文件头):
public static final int SAMPLE_RATE = 16000; // 16-bit audio private static final int BYTES_PER_SAMPLE = 2; // 16-bit audio private static final int BITS_PER_SAMPLE = 16; private static final double MAX_16_BIT = 32768; private static final int SAMPLE_BUFFER_SIZE = 4096; private static final int MONO = 1; private static final int STEREO = 2; private static final boolean LITTLE_ENDIAN = false; private static final boolean BIG_ENDIAN = true; private static final boolean SIGNED = true; private static final boolean UNSIGNED = false; private static AudioFormat dstFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, SAMPLE_RATE, BITS_PER_SAMPLE, MONO, BYTES_PER_SAMPLE, 8000, LITTLE_ENDIAN); public static byte[] reSamplingPCM(byte[] data) { try(AudioInputStream audioIn = AudioSystem.getAudioInputStream(new ByteArrayInputStream(data)); AudioInputStream convertedStream = AudioSystem.getAudioInputStream(dstFormat, audioIn); ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { if (audioIn.getFormat().matches(dstFormat)) { return data; } int numReads = -1; int BUFF_SIZE = SAMPLE_RATE / 2; byte[] buff = new byte[BUFF_SIZE]; while ((numReads = convertedStream.read(buff)) != -1) { log.info("read {} byte(s)", numReads); outputStream.write(buff); } return outputStream.toByteArray(); } catch (UnsupportedAudioFileException |IOException e) { log.error("occurs errors when re-sampling the audio stream:{}",e); throw new RuntimeException("occurs errors when re-sampling the audio stream:{}",e); } }
参考来源:
https://stackoverflow.com/questions/15410725/java-resample-wav-soundfile-without-third-party-library
https://www.codota.com/web/assistant/code/rs/5c7689a149efcb00014e68b2#L54