百度語音識別 +百度文字轉語音

  1. 登錄百度AI開發平臺
    在這裏插入圖片描述

  2. 申請自己的ak
    在這裏插入圖片描述

  3. 開放平臺上有各種語言支持的開發文檔,自學能力差得人,不用擔心,我爲你們封裝了語音合成與識別的utils,複製粘貼即可使用

    package com.czxy.TestSample;
    
    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 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;
    
    /**
     * 百度語音工具類
     */
    public class SpeechUtils {
    
        public static final String APP_ID = "14456779";
    
        public static final String API_KEY = "swit9DZ57kxo4rFjz8sauyuR";
    
        public static final String SECRET_KEY = "CDlpqaGRLoXGHGsedE21ufI3EBU8DCct";
    
    
        /**
         * 語音合成
         * @param text 文字內容
         * @param Path 合成語音生成路徑
         * @return
         */
        public static void SpeechSynthesis(String text, String Path) {
            /*
            最長的長度
             */
            int maxLength = 1024;
            if (text.getBytes().length >= maxLength) {
                return ;
            }
            // 初始化一個AipSpeech
            AipSpeech client = new AipSpeech(APP_ID, API_KEY, SECRET_KEY);
    
            // 可選:設置網絡連接參數
            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(text, "zh", 1, null);
            byte[] data = res.getData();
            //定義變量調用轉換格式
            boolean a = true;
            if (data != null) {
                try {
                    Util.writeBytesToFileSystem(data, "D:\\rap\\output.mp3");
                    a=false;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (!a) {
                convertMP3ToPcm(Path,"D:\\temp\\output.pcm");
            }
    
        }
    
        /**
         * 語音識別
         * @param Path 路徑
         * @param Path 語音類型
         * @return
         */
        public static String SpeechRecognition(String Path) throws IOException {
            // 初始化一個AipSpeech
            AipSpeech client = new AipSpeech(APP_ID, API_KEY, SECRET_KEY);
    
            // 可選:設置網絡連接參數
            client.setConnectionTimeoutInMillis(2000);
            client.setSocketTimeoutInMillis(60000);
    
            // 調用接口
            byte[] data = Util.readFileByBytes(Path);     //readFileByBytes僅爲獲取二進制數據示例
            JSONObject asrRes2 = client.asr(data, "pcm", 16000, null);
            return asrRes2.toString(2);
        }
    
    
        /**
         *  mp3轉pcm
         * @param mp3path MP3文件存放路徑
         * @param pcmpath pcm文件保存路徑
         * @return
         */
        public static boolean convertMP3ToPcm(String mp3path, String pcmpath){
            try {
                //獲取文件的音頻流,pcm的格式
                AudioInputStream audioInputStream = getPcmAudioInputStream(mp3path);
                //將音頻轉化爲  pcm的格式保存下來
                AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE, new File(pcmpath));
                return true;
            } catch (IOException e) {
                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;
        }
    
    }
    
    
    

4 簡單使用

    public static void main(String[] args) throws IOException {
    	//第一個參數是你輸入的文字,第二個參數是你保存到本地的路徑
        SpeechSynthesis("你好,我是dog", "D:\\rap\\output.mp3");
        //這個參數是剛合成的本地路徑
        String s = SpeechRecognition("D:\\temp\\output.pcm");
        System.out.println(s);
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章