java实现wav的重采样

原因是之前写的TTS文件,需要指定采样率和单声道

但是TTS是用的Jacob调用COM+sapi实现的

java+WNI10+JACOB方式

SAPI底层支持的是C,C#【官方文档】

SpAudioFormat SetWaveFormatEx method (SAPI 5.4) | Microsoft Learn

用C实现的方式【可指定输出的WAV格式】

【基于微软 SAPI 的 TTS 程序实现_sapi.h配置-CSDN博客】

懒得看C,直接写个重采样接口好了,参考如下方法

java实现wav的重采样_java wav采样转换-CSDN博客

代码如下:

java 复制代码
package com.qf.tdboot02.service;

import org.springframework.stereotype.Service;

import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;
@Service
public class WavMonoConverter {
    public static void main(String[] args) {
        File inputFile = new File("path_to_input.wav");
        File outputFile = new File("path_to_output.wav");
        convertStereoToMono(inputFile, outputFile);
    }

    public static void convertStereoToMono(File inputFile, File outputFile) {
        try {
            AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(inputFile);
            AudioFormat format = audioInputStream.getFormat();
            int channels = format.getChannels();

            AudioFormat newFormat = new AudioFormat(
                    format.getEncoding(),
                    8000,// 采样率为8000 Hz
                    format.getSampleSizeInBits(),// 采样点
                    1, // 单声道
                    format.getFrameSize(), // 帧大小,对于单声道16位PCM,每帧大小是采样率 * 声道数 * 采样点位数/8 = 8000 * 1 * 2 = 16000,但这里通常设置为采样率,因为每帧通常包含一个采样点
                    8000, // 帧率,通常与采样率相同
                    format.isBigEndian() // 使用小端字节序,这是Java音频系统通常使用的
            );


            // 转换音频流为单声道
            AudioInputStream monoAudioInputStream = AudioSystem.getAudioInputStream(newFormat, audioInputStream);

            // 写入新的WAV文件
            AudioSystem.write(monoAudioInputStream, AudioFileFormat.Type.WAVE, outputFile);

            // 关闭流
            monoAudioInputStream.close();
            audioInputStream.close();
        } catch (UnsupportedAudioFileException | IOException e) {
            e.printStackTrace();
        }
    }
}
java 复制代码
package com.qf.tdboot02.controller;

import com.qf.tdboot02.constant.WavConstant;
import com.qf.tdboot02.service.WavMonoConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.File;

/**
 * @author: 作者
 * @create: 2024-04-17 10:03
 * @Description:
 */
@Component
@RequestMapping("/wav")
public class WavConvertController {

    private final WavMonoConverter wavMonoConverter;

    @Autowired
    public WavConvertController(WavMonoConverter wavMonoConverter) {
        this.wavMonoConverter = wavMonoConverter;
    }

    @GetMapping("/convert")
    public void convert(String in,String out) {
        File inputFile = new File(in);
        File outputFile = new File(out);
        wavMonoConverter.convertStereoToMono(inputFile, outputFile);
    }

}

接口调用:

http://localhost:8090/wav/convert?in=D://in.wav&out=D://out.wav

生成文件:

相关推荐
CRMEB系统商城22 分钟前
CRMEB标准版系统(Java)v3.1正式发布
java·spring·微信小程序·php·教育电商
写后端的胖头鱼29 分钟前
【高频面试题】Java 基础类型 + 包装类 + String 互相转换
java·开发语言
weixin_4604435630 分钟前
企业考试系统从.NET迁移到Java+Linux:数据库迁移、接口兼容与灰度切换实践
java·煤矿培训考试系统·煤矿在线考试系统·煤矿安全培训·煤矿考试系统·一人一档
matlab代码1 小时前
基于matlab自助水果超市水果识别收费系统(GUI界面)【源码67期】
开发语言·matlab
香菜TTT1 小时前
Redis的哨兵机制
java·数据库·redis
hui-梦苑1 小时前
[PHP]轻量主题函数WordPress 集成 KaTeX 数学公式渲染
开发语言·php·wordpress·katex
withoutfear1 小时前
IntelliJ IDEA卡顿内存分配不足和索引被频繁触发问题解决办法
java·ide·intellij-idea
大圣编蚕1 小时前
Java StringWriter 详解:从入门到实战
java·开发语言·python
码行山野赴时序归途1 小时前
C语言预处理指令:编译前的“幕后导演“
c语言·开发语言
golang学习记2 小时前
Go 1.27新特性: json/v2使用有趣指南
开发语言·golang·json