Commit e246480e authored by 赵啸非's avatar 赵啸非

修改音频识别结果

parent 01d4dea7
...@@ -97,17 +97,18 @@ public class ApiSendMsgController { ...@@ -97,17 +97,18 @@ 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())))
throw new AppException("只支持pcm!"); // throw new AppException("只支持pcm!");
//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(filePath, appid); // IatModelMulUtil iatModelMulUtil = new IatModelMulUtil(filePath, appid);
IatModelMulUtil iatModelMulUtil = new IatModelMulUtil(bytes, appid);
String authUrl = IatModelMulUtil.getAuthUrl(hostUrl, apiKey, apiSecret); String authUrl = IatModelMulUtil.getAuthUrl(hostUrl, apiKey, apiSecret);
//log.info("authUrl==>" + authUrl); //log.info("authUrl==>" + authUrl);
......
...@@ -17,6 +17,7 @@ import java.io.FileInputStream; ...@@ -17,6 +17,7 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
...@@ -77,184 +78,231 @@ public class IatModelMulUtil extends WebSocketListener { ...@@ -77,184 +78,231 @@ public class IatModelMulUtil extends WebSocketListener {
int frameSize = 8092; //每一帧音频的大小,建议每 40ms 发送 122B int frameSize = 8092; //每一帧音频的大小,建议每 40ms 发送 122B
int intervel = 40; int intervel = 40;
int status = 0; // 音频的状态 int status = 0; // 音频的状态
int totalLength = bytes.length;
boolean isFirst = true;
int seq = 0; //数据序号 int seq = 0; //数据序号
try (FileInputStream fs = new FileInputStream(file)) { int offset = 0;
// try{
byte[] buffer = new byte[frameSize]; while (offset < totalLength) {
// 发送音频 int length = Math.min(frameSize, bytes.length - offset);
end: byte[] buffer = new byte[length];
while (true) { System.arraycopy(bytes, offset, buffer, 0, length);
seq++; // 每次循环更新下seq
//int len = bytes.length; boolean isLast = (offset + length) >= totalLength;
if (isFirst) {
status = StatusFirstFrame; // 仅一帧
int len = fs.read(buffer); } else if (isLast) {
//int len = bytes.length; status = StatusLastFrame; // 尾帧
if (len == -1) { } else {
status = StatusLastFrame; //文件读完,改变status 为 2 status = StatusContinueFrame; // 中间帧
} }
switch (status) {
case StatusFirstFrame: // 第一帧音频status = 0 switch (status) {
AudioRoot audioRoot = new AudioRoot(); case StatusFirstFrame: // 第一帧音频status = 0
com.mortals.xhx.common.pdu.Header header = new com.mortals.xhx.common.pdu.Header(); AudioRoot audioRoot = new AudioRoot();
header.setAppId(appid); com.mortals.xhx.common.pdu.Header header = new com.mortals.xhx.common.pdu.Header();
header.setStatus(StatusFirstFrame); header.setAppId(appid);
audioRoot.setHeader(header); header.setStatus(StatusFirstFrame);
audioRoot.setHeader(header);
Parameter parameter = new Parameter();
Parameter parameter = new Parameter();
Iat iat = new Iat();
iat.setDomain("slm"); Iat iat = new Iat();
iat.setLanguage("zh_cn"); iat.setDomain("slm");
iat.setAccent("mulacc"); iat.setLanguage("zh_cn");
iat.setEos(6000); iat.setAccent("mulacc");
iat.setVinfo(1); iat.setEos(6000);
com.mortals.xhx.common.pdu.Result result = new com.mortals.xhx.common.pdu.Result(); iat.setVinfo(1);
result.setEncoding("utf8"); com.mortals.xhx.common.pdu.Result result = new com.mortals.xhx.common.pdu.Result();
result.setCompress("raw"); result.setEncoding("utf8");
result.setFormat("json"); result.setCompress("raw");
iat.setResult(result); result.setFormat("json");
parameter.setIat(iat); iat.setResult(result);
audioRoot.setParameter(parameter); parameter.setIat(iat);
com.mortals.xhx.common.pdu.Payload payload = new com.mortals.xhx.common.pdu.Payload(); 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 audio = new Audio();
audio.setSampleRate(16000); audio.setEncoding(encoding);
audio.setChannels(1); audio.setSampleRate(16000);
audio.setBitDepth(16); audio.setChannels(1);
audio.setSeq(seq); audio.setBitDepth(16);
audio.setStatus(status); audio.setSeq(seq);
audio.setAudio(Base64.getEncoder().encodeToString(Arrays.copyOf(buffer, len))); audio.setStatus(status);
payload.setAudio(audio); audio.setAudio(Base64.getEncoder().encodeToString(buffer));
audioRoot.setPayload(payload); payload.setAudio(audio);
audioRoot.setPayload(payload);
String json = JSON.toJSONString(audioRoot);
/* String json = JSON.toJSONString(audioRoot);
String json = "{\n" + log.info("first json==>" + json);
" \"header\": {\n" + webSocket.send(json);
" \"app_id\": \"" + appid + "\",\n" + status = StatusContinueFrame; // 发送完第一帧改变status 为 1
" \"status\": " + StatusFirstFrame + "\n" + break;
" },\n" + case StatusContinueFrame: //中间帧status = 1
" \"parameter\": {\n" + audioRoot = new AudioRoot();
" \"iat\": {\n" + header = new com.mortals.xhx.common.pdu.Header();
" \"domain\": \"slm\",\n" + header.setAppId(appid);
" \"language\": \"zh_cn\",\n" + header.setStatus(status);
" \"accent\": \"mulacc\",\n" + audioRoot.setHeader(header);
" \"eos\": 6000,\n" +
" \"vinfo\": 1,\n" + payload = new com.mortals.xhx.common.pdu.Payload();
" \"result\": {\n" + audio = new Audio();
" \"encoding\": \"utf8\",\n" + audio.setEncoding(encoding);
" \"compress\": \"raw\",\n" + audio.setSampleRate(16000);
" \"format\": \"json\"\n" + audio.setChannels(1);
" }\n" + audio.setBitDepth(16);
" }\n" + audio.setSeq(seq);
" },\n" + audio.setStatus(status);
" \"payload\": {\n" + audio.setAudio(Base64.getEncoder().encodeToString(buffer));
" \"audio\": {\n" + payload.setAudio(audio);
" \"encoding\": \"raw\",\n" + audioRoot.setPayload(payload);
" \"sample_rate\": 16000,\n" +
" \"channels\": 1,\n" + json = JSON.toJSONString(audioRoot);
" \"bit_depth\": 16,\n" +
" \"seq\": " + seq + ",\n" + webSocket.send(json);
" \"status\": 0,\n" + break;
" \"audio\": \"" + Base64.getEncoder().encodeToString(Arrays.copyOf(buffer, len)) + "\"\n" + case StatusLastFrame: // 最后一帧音频status = 2 ,标志音频发送结束
" }\n" + audioRoot = new AudioRoot();
" }\n" + header = new com.mortals.xhx.common.pdu.Header();
"}";*/ header.setAppId(appid);
log.info("first json==>" + json); header.setStatus(status);
webSocket.send(json); audioRoot.setHeader(header);
status = StatusContinueFrame; // 发送完第一帧改变status 为 1
break; payload = new com.mortals.xhx.common.pdu.Payload();
case StatusContinueFrame: //中间帧status = 1 audio = new Audio();
audioRoot = new AudioRoot(); audio.setEncoding(encoding);
header = new com.mortals.xhx.common.pdu.Header(); audio.setSampleRate(16000);
header.setAppId(appid); audio.setChannels(1);
header.setStatus(status); audio.setBitDepth(16);
audioRoot.setHeader(header); audio.setSeq(seq);
audio.setStatus(status);
payload = new com.mortals.xhx.common.pdu.Payload(); audio.setAudio("");
audio = new Audio(); payload.setAudio(audio);
audio.setEncoding(encoding); audioRoot.setPayload(payload);
audio.setSampleRate(16000); json = JSON.toJSONString(audioRoot);
audio.setChannels(1); // log.info("last json==>" + json);
audio.setBitDepth(16); webSocket.send(json);
audio.setSeq(seq);
audio.setStatus(status); }
audio.setAudio(Base64.getEncoder().encodeToString(Arrays.copyOf(buffer, len))); try {
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" +
" \"sample_rate\": 16000,\n" +
" \"channels\": 1,\n" +
" \"bit_depth\": 16,\n" +
" \"seq\": " + seq + ",\n" +
" \"status\": 1,\n" +
" \"audio\": \"" + Base64.getEncoder().encodeToString(Arrays.copyOf(buffer, len)) + "\"\n" +
" }\n" +
" }\n" +
"}";*/
// log.info("mid json==>" + json);
webSocket.send(json);
break;
case StatusLastFrame: // 最后一帧音频status = 2 ,标志音频发送结束
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" +
" \"sample_rate\": 16000,\n" +
" \"channels\": 1,\n" +
" \"bit_depth\": 16,\n" +
" \"seq\": " + seq + ",\n" +
" \"status\": 2,\n" +
" \"audio\": \"\"\n" +
" }\n" +
" }\n" +
"}";*/
// log.info("last json==>" + json);
webSocket.send(json);
break end;
}
Thread.sleep(intervel); //模拟音频采样延时 Thread.sleep(intervel); //模拟音频采样延时
} catch (InterruptedException e) {
} }
} catch (Exception e) { offset += length;
log.info("error==>" + e.getMessage()); isFirst = false;
} }
}).start(); }).start();
// // try (FileInputStream fs = new FileInputStream(file)) {
// try{
// // 发送音频
// end:
// while (true) {
// seq++; // 每次循环更新下seq
// //int len = bytes.length;
// byte[] buffer = new byte[frameSize];
// System.arraycopy(buffer, seq*frameSize, buffer, 0, frameSize);
//
// //int len = fs.read(buffer);
// int len = bytes.length;
// if (len == -1) {
// status = StatusLastFrame; //文件读完,改变status 为 2
// }
// 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);
// log.info("first json==>" + json);
// webSocket.send(json);
// status = StatusContinueFrame; // 发送完第一帧改变status 为 1
// break;
// case StatusContinueFrame: //中间帧status = 1
// 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);
//
// webSocket.send(json);
// break;
// case StatusLastFrame: // 最后一帧音频status = 2 ,标志音频发送结束
// 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);
// // log.info("last json==>" + json);
// webSocket.send(json);
// break end;
// }
// Thread.sleep(intervel); //模拟音频采样延时
// }
// } catch (Exception e) {
// log.info("error==>" + e.getMessage());
// }
// }).start();
} }
@Override @Override
......
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