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

修改音频识别结果

parent ecb902b7
......@@ -97,18 +97,17 @@ public class ApiSendMsgController {
String jsonStr = "";
try {
if (file == null || file.getSize() == 0L) throw new AppException("文件为空!");
/* if (!"pcm".equalsIgnoreCase(FileUtil.getSuffix(file.getOriginalFilename()))&&
!"mp3".equalsIgnoreCase(FileUtil.getSuffix(file.getOriginalFilename())))
throw new AppException("只支持pcm或mp3文件!");*/
if (!"pcm".equalsIgnoreCase(FileUtil.getSuffix(file.getOriginalFilename())))
throw new AppException("只支持pcm!");
//if (file.getOriginalFilename())
//String filePath = uploadService.saveFileUpload(file, prePath, null);
//filePath = uploadService.getFilePath(filePath);
String filePath = uploadService.saveFileUpload(file, prePath, null);
filePath = uploadService.getFilePath(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);
//log.info("authUrl==>" + authUrl);
......
......@@ -24,7 +24,7 @@ public class IatModelMulMain extends WebSocketListener {
private static final String appid = "3cc52607"; //在控制台-我的应用获取
private static final String apiSecret = "ZTdmMjFjMGYxYmJhN2VmYjFlMTg3N2Rk"; // 在控制台-我的应用获取
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 StatusContinueFrame = 1;
public static final int StatusLastFrame = 2;
......
......@@ -12,6 +12,7 @@ import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
......@@ -57,11 +58,16 @@ public class IatModelMulUtil extends WebSocketListener {
public IatModelMulUtil(byte[] bytes, String appid) {
// this.file = file;
// this.file = file;
this.bytes = bytes;
this.appid = appid;
}
public IatModelMulUtil(String file, String appid) {
this.file = file;
this.appid = appid;
}
@Override
public void onOpen(WebSocket webSocket, Response response) {
super.onOpen(webSocket, response);
......@@ -72,15 +78,18 @@ public class IatModelMulUtil extends WebSocketListener {
int intervel = 40;
int status = 0; // 音频的状态
int seq = 0; //数据序号
// try (FileInputStream fs = new FileInputStream(file)) {
try{
// byte[] buffer = new byte[frameSize];
try (FileInputStream fs = new FileInputStream(file)) {
// try{
byte[] buffer = new byte[frameSize];
// 发送音频
end:
while (true) {
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) {
status = StatusLastFrame; //文件读完,改变status 为 2
}
......@@ -116,7 +125,7 @@ public class IatModelMulUtil extends WebSocketListener {
audio.setBitDepth(16);
audio.setSeq(seq);
audio.setStatus(status);
audio.setAudio(Base64.getEncoder().encodeToString(Arrays.copyOf(bytes, len)));
audio.setAudio(Base64.getEncoder().encodeToString(Arrays.copyOf(buffer, len)));
payload.setAudio(audio);
audioRoot.setPayload(payload);
......@@ -172,7 +181,7 @@ public class IatModelMulUtil extends WebSocketListener {
audio.setBitDepth(16);
audio.setSeq(seq);
audio.setStatus(status);
audio.setAudio(Base64.getEncoder().encodeToString(Arrays.copyOf(bytes, len)));
audio.setAudio(Base64.getEncoder().encodeToString(Arrays.copyOf(buffer, len)));
payload.setAudio(audio);
audioRoot.setPayload(payload);
......@@ -195,7 +204,7 @@ public class IatModelMulUtil extends WebSocketListener {
" }\n" +
" }\n" +
"}";*/
// log.info("mid json==>" + json);
// log.info("mid json==>" + json);
webSocket.send(json);
break;
case StatusLastFrame: // 最后一帧音频status = 2 ,标志音频发送结束
......@@ -236,7 +245,7 @@ public class IatModelMulUtil extends WebSocketListener {
" }\n" +
" }\n" +
"}";*/
// log.info("last json==>" + json);
// log.info("last json==>" + json);
webSocket.send(json);
break end;
}
......@@ -251,7 +260,7 @@ public class IatModelMulUtil extends WebSocketListener {
@Override
public void onMessage(WebSocket webSocket, String text) {
super.onMessage(webSocket, text);
log.info("onMessage==>" + text);
// log.info("onMessage==>" + text);
JsonParse jsonParse = gson.fromJson(text, JsonParse.class);
if (jsonParse != null) {
if (jsonParse.header.code != 0) {
......@@ -269,7 +278,7 @@ public class IatModelMulUtil extends WebSocketListener {
for (Ws ws : wsList) {
List<Cw> cwList = ws.cw;
for (Cw cw : cwList) {
// log.info("识别结果==》{}", cw.w);
log.info("识别结果==》{}", cw.w);
responseBuilder.append(cw.w);
/* try {
......
......@@ -34,24 +34,15 @@ Content-Disposition: form-data; name="file"; filename="file.zip"
--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
POST {{baseUrl}}/audio/upload
Content-Type: multipart/form-data; boundary=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--
###测试链接数据库
......
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