先到百度AI开发平台创建一个语音应用
创建成功如下:
pom.xml需要导入百度aip的JavaSDK,以及mp3转pcm的mp3spi
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.accp</groupId>
<artifactId>demo</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--百度语音识别-->
<dependency>
<groupId>com.baidu.aip</groupId>
<artifactId>java-sdk</artifactId>
<version>4.8.0</version>
</dependency>
<!--mp3转pcm-->
<dependency>
<groupId>com.googlecode.soundlibs</groupId>
<artifactId>mp3spi</artifactId>
<version>1.9.5.4</version>
</dependency>
</dependencies>
</project>
百度语音工具类内容如下:
package com.accp;
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 = "15948020";
//你创建的App应用的API Key
public static final String API_KEY = "sPbOLUBOGq5U3WR9fpHOvtcP";
//你创建的App应用的Secret Key
public static final String SECRET_KEY = "ZcyHqnuXHS41vC3SytBwwAPiSAkGW3qa";
public static String pcmPath="D:\\temp\\output.pcm";//pcm文件保存路径
public static String mp3Path="D:\\rap\\output.mp3";//MP3文件存放路径
/**
* 语音合成
* @param text 文字内容
* @param Path 合成语音生成路径
* @return
*/
public static void aSpeechSynthesis(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 {
createFile(Path,"output.mp3");
Util.writeBytesToFileSystem(data, Path);
a=false;
} catch (IOException e) {
e.printStackTrace();
}
}
if (!a) {
convertMP3ToPcm(Path,pcmPath);
}
}
/**
* 语音识别
* @param Path 路径
* @param Path 语音类型
* @return
*/
public static String aSpeechRecognition(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 {
createFile(pcmpath,"output.pcm");
//获取文件的音频流,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;
}
//创建文件夹
public static void createFile(String path,String suffix){
String separator=File.separator+File.separator;
String[] spilt=path.split(separator+suffix);
File file=new File(spilt[0]);
if(!file.exists()){
System.out.println(spilt[0]+"文件夹不存在,正在创建中....");
// mkdir()只会建立一级的文件夹
// mkdirs()可以建立多级文件夹
file.mkdir();
}else{
System.out.println(spilt[0]+"文件夹已存在");
}
}
public static void main(String[] args) throws IOException {
//第一个参数是你输入的文字,第二个参数是你保存到本地的路径
aSpeechSynthesis("靚仔,看什么还看说的就是你", mp3Path);
//这个参数是刚合成的本地路径
String s = aSpeechRecognition(pcmPath);
System.out.println(s);
}
}