Commit 3f632ed9 authored by 赵啸非's avatar 赵啸非

修改音频识别结果

parent ecb902b7
...@@ -97,18 +97,17 @@ public class ApiSendMsgController { ...@@ -97,18 +97,17 @@ public class ApiSendMsgController {
String jsonStr = ""; String jsonStr = "";
try { try {
if (file == null || file.getSize() == 0L) throw new AppException("文件为空!"); if (file == null || file.getSize() == 0L) throw new AppException("文件为空!");
/* if (!"pcm".equalsIgnoreCase(FileUtil.getSuffix(file.getOriginalFilename()))&& if (!"pcm".equalsIgnoreCase(FileUtil.getSuffix(file.getOriginalFilename())))
!"mp3".equalsIgnoreCase(FileUtil.getSuffix(file.getOriginalFilename()))) throw new AppException("只支持pcm!");
throw new AppException("只支持pcm或mp3文件!");*/
//if (file.getOriginalFilename()) //if (file.getOriginalFilename())
//String filePath = uploadService.saveFileUpload(file, prePath, null); String filePath = uploadService.saveFileUpload(file, prePath, null);
//filePath = uploadService.getFilePath(filePath); filePath = uploadService.getFilePath(filePath);
//log.info("filePath==>" + filePath); //log.info("filePath==>" + filePath);
byte[] bytes = file.getBytes(); // byte[] bytes = file.getBytes();
IatModelMulUtil iatModelMulUtil = new IatModelMulUtil(bytes, appid); IatModelMulUtil iatModelMulUtil = new IatModelMulUtil(filePath, appid);
String authUrl = IatModelMulUtil.getAuthUrl(hostUrl, apiKey, apiSecret); String authUrl = IatModelMulUtil.getAuthUrl(hostUrl, apiKey, apiSecret);
//log.info("authUrl==>" + authUrl); //log.info("authUrl==>" + authUrl);
......
...@@ -24,7 +24,7 @@ public class IatModelMulMain extends WebSocketListener { ...@@ -24,7 +24,7 @@ public class IatModelMulMain extends WebSocketListener {
private static final String appid = "3cc52607"; //在控制台-我的应用获取 private static final String appid = "3cc52607"; //在控制台-我的应用获取
private static final String apiSecret = "ZTdmMjFjMGYxYmJhN2VmYjFlMTg3N2Rk"; // 在控制台-我的应用获取 private static final String apiSecret = "ZTdmMjFjMGYxYmJhN2VmYjFlMTg3N2Rk"; // 在控制台-我的应用获取
private static final String apiKey = "d0f73d44e996c2da9924c4476c578a30"; // 在控制台-我的应用获取 private static final String apiKey = "d0f73d44e996c2da9924c4476c578a30"; // 在控制台-我的应用获取
private static final String file = "E://test1.mp3"; // 识别音频位置 private static final String file = "F://recording.pcm"; // 识别音频位置
public static final int StatusFirstFrame = 0; public static final int StatusFirstFrame = 0;
public static final int StatusContinueFrame = 1; public static final int StatusContinueFrame = 1;
public static final int StatusLastFrame = 2; public static final int StatusLastFrame = 2;
......
...@@ -12,6 +12,7 @@ import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; ...@@ -12,6 +12,7 @@ import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import javax.crypto.Mac; import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec; import javax.crypto.spec.SecretKeySpec;
import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
...@@ -62,6 +63,11 @@ public class IatModelMulUtil extends WebSocketListener { ...@@ -62,6 +63,11 @@ public class IatModelMulUtil extends WebSocketListener {
this.appid = appid; this.appid = appid;
} }
public IatModelMulUtil(String file, String appid) {
this.file = file;
this.appid = appid;
}
@Override @Override
public void onOpen(WebSocket webSocket, Response response) { public void onOpen(WebSocket webSocket, Response response) {
super.onOpen(webSocket, response); super.onOpen(webSocket, response);
...@@ -72,15 +78,18 @@ public class IatModelMulUtil extends WebSocketListener { ...@@ -72,15 +78,18 @@ public class IatModelMulUtil extends WebSocketListener {
int intervel = 40; int intervel = 40;
int status = 0; // 音频的状态 int status = 0; // 音频的状态
int seq = 0; //数据序号 int seq = 0; //数据序号
// try (FileInputStream fs = new FileInputStream(file)) { try (FileInputStream fs = new FileInputStream(file)) {
try{ // try{
// byte[] buffer = new byte[frameSize]; byte[] buffer = new byte[frameSize];
// 发送音频 // 发送音频
end: end:
while (true) { while (true) {
seq++; // 每次循环更新下seq seq++; // 每次循环更新下seq
// int len = fs.read(buffer); //int len = bytes.length;
int len = bytes.length;
int len = fs.read(buffer);
//int len = bytes.length;
if (len == -1) { if (len == -1) {
status = StatusLastFrame; //文件读完,改变status 为 2 status = StatusLastFrame; //文件读完,改变status 为 2
} }
...@@ -116,7 +125,7 @@ public class IatModelMulUtil extends WebSocketListener { ...@@ -116,7 +125,7 @@ public class IatModelMulUtil extends WebSocketListener {
audio.setBitDepth(16); audio.setBitDepth(16);
audio.setSeq(seq); audio.setSeq(seq);
audio.setStatus(status); audio.setStatus(status);
audio.setAudio(Base64.getEncoder().encodeToString(Arrays.copyOf(bytes, len))); audio.setAudio(Base64.getEncoder().encodeToString(Arrays.copyOf(buffer, len)));
payload.setAudio(audio); payload.setAudio(audio);
audioRoot.setPayload(payload); audioRoot.setPayload(payload);
...@@ -172,7 +181,7 @@ public class IatModelMulUtil extends WebSocketListener { ...@@ -172,7 +181,7 @@ public class IatModelMulUtil extends WebSocketListener {
audio.setBitDepth(16); audio.setBitDepth(16);
audio.setSeq(seq); audio.setSeq(seq);
audio.setStatus(status); audio.setStatus(status);
audio.setAudio(Base64.getEncoder().encodeToString(Arrays.copyOf(bytes, len))); audio.setAudio(Base64.getEncoder().encodeToString(Arrays.copyOf(buffer, len)));
payload.setAudio(audio); payload.setAudio(audio);
audioRoot.setPayload(payload); audioRoot.setPayload(payload);
...@@ -251,7 +260,7 @@ public class IatModelMulUtil extends WebSocketListener { ...@@ -251,7 +260,7 @@ public class IatModelMulUtil extends WebSocketListener {
@Override @Override
public void onMessage(WebSocket webSocket, String text) { public void onMessage(WebSocket webSocket, String text) {
super.onMessage(webSocket, text); super.onMessage(webSocket, text);
log.info("onMessage==>" + text); // log.info("onMessage==>" + text);
JsonParse jsonParse = gson.fromJson(text, JsonParse.class); JsonParse jsonParse = gson.fromJson(text, JsonParse.class);
if (jsonParse != null) { if (jsonParse != null) {
if (jsonParse.header.code != 0) { if (jsonParse.header.code != 0) {
...@@ -269,7 +278,7 @@ public class IatModelMulUtil extends WebSocketListener { ...@@ -269,7 +278,7 @@ public class IatModelMulUtil extends WebSocketListener {
for (Ws ws : wsList) { for (Ws ws : wsList) {
List<Cw> cwList = ws.cw; List<Cw> cwList = ws.cw;
for (Cw cw : cwList) { for (Cw cw : cwList) {
// log.info("识别结果==》{}", cw.w); log.info("识别结果==》{}", cw.w);
responseBuilder.append(cw.w); responseBuilder.append(cw.w);
/* try { /* try {
......
...@@ -34,24 +34,15 @@ Content-Disposition: form-data; name="file"; filename="file.zip" ...@@ -34,24 +34,15 @@ Content-Disposition: form-data; name="file"; filename="file.zip"
--WebAppBoundary-- --WebAppBoundary--
###上传音频文件
POST {{baseUrl}}/audio/upload
Content-Type: multipart/form-data; boundary=WebAppBoundary
--WebAppBoundary
Content-Disposition: form-data; name="file"; filename="new.opus"
< ./new.opus
--WebAppBoundary--
###上传音频文件1 ###上传音频文件1
POST {{baseUrl}}/audio/upload POST {{baseUrl}}/audio/upload
Content-Type: multipart/form-data; boundary=WebAppBoundary Content-Type: multipart/form-data; boundary=WebAppBoundary
--WebAppBoundary --WebAppBoundary
Content-Disposition: form-data; name="file"; filename="test4.pcm" Content-Disposition: form-data; name="file"; filename="recording.pcm"
< ./test4.pcm < ./recording.pcm
--WebAppBoundary-- --WebAppBoundary--
###测试链接数据库 ###测试链接数据库
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment