百度語音識別

pom引入jar

<!--mp3轉pcm-->
    <dependency>
      <groupId>com.googlecode.soundlibs</groupId>
      <artifactId>mp3spi</artifactId>
      <version>1.9.5.4</version>
    </dependency>
<!--百度語音識別-->
    <dependency>
      <groupId>com.baidu.aip</groupId>
      <artifactId>java-sdk</artifactId>
      <version>4.9.0</version>
    </dependency>

工具類

import com.baidu.aip.speech.AipSpeech;
import com.baidu.aip.speech.TtsResponse;
import com.baidu.aip.util.Util;
import javazoom.spi.mpeg.sampled.file.MpegAudioFileReader;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import java.io.File;
import java.io.IOException;

/**
 * @ProjectName: testPcm
 * @Package: com.XXX.util
 * @Author: huat
 * @Date: 2019/8/10 12:20
 * @Version: 1.0
 */
@Slf4j
public class chinese {

        //設置APPID/AK/SK
        public static final String APP_ID = "XXXXXXXXX";
        public static final String API_KEY = "XXXXXXXXXXXXXx";
        public static final String SECRET_KEY = "XXXXXXXXXXXXXXXXXXXx";
        public static AipSpeech client = null;
    /**
     * 單例 懶加載模式 返回實例
     * @return
     */
    public static AipSpeech getInstance(){

        if (client==null){
            synchronized (AipSpeech.class){
                if (client==null) {
                    client = new AipSpeech(APP_ID, API_KEY, SECRET_KEY);
                }
            }
        }
        return client;
    }

    /**
     * 語音合成
     * @param word 文字內容
     * @param outputPath 合成語音生成路徑
     * @return
     */
    public static boolean SpeechSynthesizer(String word, String outputPath) {
        /*
        最長的長度
         */
        int maxLength = 1024;
        if (word.getBytes().length >= maxLength) {
            return false;
        }
        // 初始化一個AipSpeech
        client = getInstance();

        // 可選:設置網絡鏈接參數
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // 可選:設置代理服務器地址, http和socket二選一,或者均不設置
//        client.setHttpProxy("proxy_host", proxy_port);  // 設置http代理
//        client.setSocketProxy("proxy_host", proxy_port);  // 設置socket代理

        // 調用接口
        TtsResponse res = client.synthesis(word, "zh", 1, null);
        byte[] data = res.getData();
        org.json.JSONObject res1 = res.getResult();
        if (data != null) {
            try {
                Util.writeBytesToFileSystem(data, outputPath);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return true;
        }
        if (res1 != null) {
            log.info(" result : " + res1.toString());
        }
        return false;

    }
    /**
     * 語音識別
     * @param videoPath
     * @param videoType
     * @return
     */
    public static String SpeechRecognition(String videoPath, String videoType) {
        // 初始化一個AipSpeech
        client = getInstance();

        // 可選:設置網絡鏈接參數
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // 可選:設置代理服務器地址, http和socket二選一,或者均不設置
//        client.setHttpProxy("proxy_host", proxy_port);  // 設置http代理
//        client.setSocketProxy("proxy_host", proxy_port);  // 設置socket代理


        // 調用接口
        JSONObject res = client.asr(videoPath, videoType, 16000, null);
        log.info(" SpeechRecognition : " + res.toString());
        return res.toString(2);
    }


    /**
     *  mp3轉pcm
     * @param mp3filepath MP3文件存放路徑
     * @param pcmfilepath pcm文件保存路徑
     * @return
     */
    public static boolean convertMP32Pcm(String mp3filepath, String pcmfilepath){
        try {
            //獲取文件的音頻流,pcm的格式
            AudioInputStream audioInputStream = getPcmAudioInputStream(mp3filepath);
            //將音頻轉化爲  pcm的格式保存下來
            AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE, new File(pcmfilepath));
            return true;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 得到pcm文件的音頻流
     * @param mp3filepath
     * @return
     */
    private static AudioInputStream getPcmAudioInputStream(String mp3filepath) {
        File mp3 = new File(mp3filepath);
        AudioInputStream audioInputStream = null;
        AudioFormat targetFormat = null;
        try {
            AudioInputStream in = null;
            MpegAudioFileReader mp = new MpegAudioFileReader();
            in = mp.getAudioInputStream(mp3);
            AudioFormat baseFormat = in.getFormat();
            targetFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), 16,
                    baseFormat.getChannels(), baseFormat.getChannels()*2, baseFormat.getSampleRate(), false);
            audioInputStream = AudioSystem.getAudioInputStream(targetFormat, in);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return audioInputStream;
    }

    public static void main(String[] args) throws IOException {
//        SpeechSynthesizer("簡單測試百度語音合成", "d:/SpeechSynthesizer.mp3");
        //mp3轉pcm格式
        convertMP32Pcm("E:/KwDownload/song/許嵩-有何不可 - 鈴聲.mp3","d:/16k.pcm");
        //第二個參數爲文件後綴
        SpeechRecognition("d:/16k.pcm","pcm");
    }

}
相關文章
相關標籤/搜索