Commit 1620e1e7 authored by 赵啸非's avatar 赵啸非

修改音频识别结果

parent 06c2aa48
......@@ -97,8 +97,9 @@ public class ApiSendMsgController {
String jsonStr = "";
try {
if (file == null || file.getSize() == 0L) throw new AppException("文件为空!");
if (!"pcm".equalsIgnoreCase(FileUtil.getSuffix(file.getOriginalFilename())))
throw new AppException("只支持pcm文件!");
if (!"pcm".equalsIgnoreCase(FileUtil.getSuffix(file.getOriginalFilename()))&&
!"mp3".equalsIgnoreCase(FileUtil.getSuffix(file.getOriginalFilename())))
throw new AppException("只支持pcm或mp3文件!");
//if (file.getOriginalFilename())
String filePath = uploadService.saveFileUpload(file, prePath, null);
filePath = uploadService.getFilePath(filePath);
......@@ -124,12 +125,13 @@ public class ApiSendMsgController {
// model.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
jsonStr = JSONObject.toJSONString(model);
} catch (AppException e) {
log.error("异常", e);
model.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
model.put(KEY_RESULT_MSG, e.getMessage());
jsonStr = JSONObject.toJSONString(model);
} catch (Exception e) {
log.error("异常", e);
model.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
model.put(KEY_RESULT_MSG, "文件上传失败");
jsonStr = JSONObject.toJSONString(model);
......
package com.mortals.xhx.common.pdu;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;
@Data
public class Audio{
@JSONField(name = "sample_rate")
private int sampleRate;
private int channels;
private String audio;
private String encoding;
@JSONField(name = "bit_depth")
private int bitDepth;
private int seq;
private int status;
}
\ No newline at end of file
package com.mortals.xhx.common.pdu;
import lombok.Data;
@Data
public class AudioRoot{
private Payload payload;
private Parameter parameter;
private Header header;
}
\ No newline at end of file
package com.mortals.xhx.common.pdu;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;
@Data
public class Header{
@JSONField(name = "app_id")
private String appId;
private int status;
}
\ No newline at end of file
package com.mortals.xhx.common.pdu;
import lombok.Data;
@Data
public class Iat{
private Result result;
private int vinfo;
private String domain;
private int eos;
private String language;
private String accent;
}
\ No newline at end of file
package com.mortals.xhx.common.pdu;
import lombok.Data;
@Data
public class Parameter{
private Iat iat;
}
\ No newline at end of file
package com.mortals.xhx.common.pdu;
import lombok.Data;
@Data
public class Payload{
private Audio audio;
}
\ No newline at end of file
package com.mortals.xhx.common.pdu;
import lombok.Data;
@Data
public class Result{
private String compress;
private String format;
private String encoding;
}
\ No newline at end of file
package com.mortals.xhx.common.utils;
import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.mortals.xhx.common.pdu.*;
import lombok.extern.slf4j.Slf4j;
import okhttp3.HttpUrl;
import okhttp3.Response;
......@@ -34,6 +36,10 @@ public class IatModelMulUtil extends WebSocketListener {
public static final int StatusFirstFrame = 0;
public static final int StatusContinueFrame = 1;
public static final int StatusLastFrame = 2;
public static final String encoding = "raw";
public static final Gson gson = new Gson();
// 开始时间
private static Date dateBegin = new Date();
......@@ -43,32 +49,23 @@ public class IatModelMulUtil extends WebSocketListener {
private final StringBuilder responseBuilder = new StringBuilder();
public String file; // 识别音频位置*/
public String file; // 识别音频位置*/
public String appid;
public String appid;
// public SseEmitter emitter;
public IatModelMulUtil(String file, String appid) {
this.file = file;
this.appid = appid;
// this.emitter = emitter;
}
/*
public IatModelMulUtil(String file, String appid, SseEmitter emitter) {
this.file = file;
this.appid = appid;
this.emitter = emitter;
}
*/
@Override
public void onOpen(WebSocket webSocket, Response response) {
super.onOpen(webSocket, response);
new Thread(() -> {
//连接成功,开始发送数据
int frameSize = 1280; //每一帧音频的大小,建议每 40ms 发送 122B
//int frameSize = 1280; //每一帧音频的大小,建议每 40ms 发送 122B
int frameSize = 8092; //每一帧音频的大小,建议每 40ms 发送 122B
int intervel = 40;
int status = 0; // 音频的状态
int seq = 0; //数据序号
......@@ -84,6 +81,42 @@ public class IatModelMulUtil extends WebSocketListener {
}
switch (status) {
case StatusFirstFrame: // 第一帧音频status = 0
AudioRoot audioRoot = new AudioRoot();
com.mortals.xhx.common.pdu.Header header = new com.mortals.xhx.common.pdu.Header();
header.setAppId(appid);
header.setStatus(StatusFirstFrame);
audioRoot.setHeader(header);
Parameter parameter = new Parameter();
Iat iat = new Iat();
iat.setDomain("slm");
iat.setLanguage("zh_cn");
iat.setAccent("mulacc");
iat.setEos(6000);
iat.setVinfo(1);
com.mortals.xhx.common.pdu.Result result = new com.mortals.xhx.common.pdu.Result();
result.setEncoding("utf8");
result.setCompress("raw");
result.setFormat("json");
iat.setResult(result);
parameter.setIat(iat);
audioRoot.setParameter(parameter);
com.mortals.xhx.common.pdu.Payload payload = new com.mortals.xhx.common.pdu.Payload();
Audio audio = new Audio();
audio.setEncoding(encoding);
audio.setSampleRate(16000);
audio.setChannels(1);
audio.setBitDepth(16);
audio.setSeq(seq);
audio.setStatus(status);
audio.setAudio(Base64.getEncoder().encodeToString(Arrays.copyOf(buffer, len)));
payload.setAudio(audio);
audioRoot.setPayload(payload);
String json = JSON.toJSONString(audioRoot);
/*
String json = "{\n" +
" \"header\": {\n" +
" \"app_id\": \"" + appid + "\",\n" +
......@@ -114,19 +147,40 @@ public class IatModelMulUtil extends WebSocketListener {
" \"audio\": \"" + Base64.getEncoder().encodeToString(Arrays.copyOf(buffer, len)) + "\"\n" +
" }\n" +
" }\n" +
"}";
"}";*/
// log.info("first json==>" + json);
webSocket.send(json);
status = StatusContinueFrame; // 发送完第一帧改变status 为 1
break;
case StatusContinueFrame: //中间帧status = 1
json = "{\n" +
audioRoot = new AudioRoot();
header = new com.mortals.xhx.common.pdu.Header();
header.setAppId(appid);
header.setStatus(status);
audioRoot.setHeader(header);
payload = new com.mortals.xhx.common.pdu.Payload();
audio = new Audio();
audio.setEncoding(encoding);
audio.setSampleRate(16000);
audio.setChannels(1);
audio.setBitDepth(16);
audio.setSeq(seq);
audio.setStatus(status);
audio.setAudio(Base64.getEncoder().encodeToString(Arrays.copyOf(buffer, len)));
payload.setAudio(audio);
audioRoot.setPayload(payload);
json = JSON.toJSONString(audioRoot);
/* json = "{\n" +
" \"header\": {\n" +
" \"app_id\": \"" + appid + "\",\n" +
" \"status\": 1\n" +
" },\n" +
" \"payload\": {\n" +
" \"audio\": {\n" +
" \"encoding\": \"raw\",\n" +
" \"encoding\": \"raw\",\n" +
" \"sample_rate\": 16000,\n" +
" \"channels\": 1,\n" +
" \"bit_depth\": 16,\n" +
......@@ -135,18 +189,39 @@ public class IatModelMulUtil extends WebSocketListener {
" \"audio\": \"" + Base64.getEncoder().encodeToString(Arrays.copyOf(buffer, len)) + "\"\n" +
" }\n" +
" }\n" +
"}";
"}";*/
// log.info("mid json==>" + json);
webSocket.send(json);
break;
case StatusLastFrame: // 最后一帧音频status = 2 ,标志音频发送结束
json = "{\n" +
audioRoot = new AudioRoot();
header = new com.mortals.xhx.common.pdu.Header();
header.setAppId(appid);
header.setStatus(status);
audioRoot.setHeader(header);
payload = new com.mortals.xhx.common.pdu.Payload();
audio = new Audio();
audio.setEncoding(encoding);
audio.setSampleRate(16000);
audio.setChannels(1);
audio.setBitDepth(16);
audio.setSeq(seq);
audio.setStatus(status);
audio.setAudio("");
payload.setAudio(audio);
audioRoot.setPayload(payload);
json = JSON.toJSONString(audioRoot);
/* json = "{\n" +
" \"header\": {\n" +
" \"app_id\": \"" + appid + "\",\n" +
" \"status\": 2\n" +
" },\n" +
" \"payload\": {\n" +
" \"audio\": {\n" +
" \"encoding\": \"raw\",\n" +
" \"encoding\": \"raw\",\n" +
" \"sample_rate\": 16000,\n" +
" \"channels\": 1,\n" +
" \"bit_depth\": 16,\n" +
......@@ -155,7 +230,8 @@ public class IatModelMulUtil extends WebSocketListener {
" \"audio\": \"\"\n" +
" }\n" +
" }\n" +
"}";
"}";*/
// log.info("last json==>" + json);
webSocket.send(json);
break end;
}
......@@ -170,6 +246,7 @@ public class IatModelMulUtil extends WebSocketListener {
@Override
public void onMessage(WebSocket webSocket, String text) {
super.onMessage(webSocket, text);
log.info("onMessage==>" + text);
JsonParse jsonParse = gson.fromJson(text, JsonParse.class);
if (jsonParse != null) {
if (jsonParse.header.code != 0) {
......@@ -183,11 +260,11 @@ public class IatModelMulUtil extends WebSocketListener {
JsonParseText jsonParseText = gson.fromJson(decodeRes, JsonParseText.class);
List<Ws> wsList = jsonParseText.ws;
// System.out.print("中间识别结果==》");
// log.info("中间识别结果==》");
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 {
......@@ -200,7 +277,7 @@ public class IatModelMulUtil extends WebSocketListener {
}
}
if (jsonParse.payload.result.status == 2) { // 最终结果 说明数据全部返回完毕,可以关闭连接,释放资源
System.out.println("session end ");
log.info("session end ");
dateEnd = new Date();
log.info(sdf.format(dateBegin) + "开始");
......@@ -208,7 +285,7 @@ public class IatModelMulUtil extends WebSocketListener {
log.info("耗时:" + (dateEnd.getTime() - dateBegin.getTime()) + "ms");
// System.out.println("最终识别结果 ==》" + decodeRes); // 按照规则替换与追加出最终识别结果
log.info("本次识别sid ==》" + jsonParse.header.sid);
// emitter.complete();
// emitter.complete();
future.complete(responseBuilder.toString());
log.info("emitter 关闭");
......
......@@ -38,10 +38,20 @@ Content-Disposition: form-data; name="file"; filename="file.zip"
POST {{baseUrl}}/audio/upload
Content-Type: multipart/form-data; boundary=WebAppBoundary
--WebAppBoundary
Content-Disposition: form-data; name="file"; filename="test.mp3"
< ./test.mp3
--WebAppBoundary--
###上传音频文件1
POST {{baseUrl}}/audio/upload
Content-Type: multipart/form-data; boundary=WebAppBoundary
--WebAppBoundary
Content-Disposition: form-data; name="file"; filename="test.pcm"
< ./test.pcm
< ./test1.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