纯java代码对音频采样率进行转换(JDK rt包)

 

转换成指定采样率(含文件头)

    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

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值