|
@@ -8,6 +8,7 @@ import android.text.TextUtils;
|
|
|
|
|
|
public class VoiceModel {
|
|
|
|
|
|
+ //C系列控制卡
|
|
|
//帧头4字节:0x55,0xaa,0x00,0x00
|
|
|
//地址1字节:0x01 --485通讯屏号,默认1
|
|
|
//标识1字节:0x01
|
|
@@ -18,12 +19,28 @@ public class VoiceModel {
|
|
|
//帧长度2字节:0x21,0x00
|
|
|
//语音数据最多512字节:帧头[0xfd],长度[0x00,0x1e],命令字[0x01],文字编码格式[0x00],待合成语音文本[...]
|
|
|
//帧尾4字节:0x00,0x00,0x0d,0x0a
|
|
|
+ //sample C2M: 55AA0000010111D5000000000000120000001200FD000F01003132333435D3EFD2F4CEC4B1BE00000D0A
|
|
|
|
|
|
- private static byte[] head = {(byte)0x55, (byte)0xaa, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x01, (byte)0x11, (byte)0xd5, (byte)0x00, (byte)0x00,
|
|
|
+ //T系列控制卡:
|
|
|
+ //帧头:55 AA 00 00
|
|
|
+ //地址:01(可配置)
|
|
|
+ //标志:01
|
|
|
+ //操作码:00 D9
|
|
|
+ //保留:00 00
|
|
|
+ //帧序号:00 00 00 00
|
|
|
+ //总长:“数据”字段的长度。(可不给长度,由控制卡自动计算)
|
|
|
+ //帧长:“数据”字段的长度。(同总长,可不给长度,由控制卡自动计算)
|
|
|
+ //数据:有效数据,格式详细参见 下一节“数据字段格式”。
|
|
|
+ //帧尾:00 00 0D 0A
|
|
|
+ //sample T4: 55AA0000010100D90000000000000000000000002474656C6C3A3132333435D3EFD2F4CEC4B1BE00000D0A
|
|
|
+
|
|
|
+ private static final byte[] head_c = {(byte)0x55, (byte)0xaa, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x01, (byte)0x11, (byte)0xd5, (byte)0x00, (byte)0x00,
|
|
|
(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00};
|
|
|
- private static byte[] end = {(byte)0x00, (byte)0x00, (byte)0x0d, (byte)0x0a};
|
|
|
+ private static final byte[] head_t = {(byte)0x55, (byte)0xaa, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x01, (byte)0x00, (byte)0xd9, (byte)0x00, (byte)0x00,
|
|
|
+ (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00};
|
|
|
+ private static final byte[] end = {(byte)0x00, (byte)0x00, (byte)0x0d, (byte)0x0a};
|
|
|
|
|
|
- public static byte[] getVoiceData(String text) {
|
|
|
+ public static byte[] getVoiceDataC2M(String text) {
|
|
|
if (!TextUtils.isEmpty(text)) {
|
|
|
try {
|
|
|
//语音文本字节数组
|
|
@@ -46,7 +63,7 @@ public class VoiceModel {
|
|
|
//总长度
|
|
|
int dataLen = len + 24;
|
|
|
byte[] data = new byte[dataLen];
|
|
|
- System.arraycopy(head, 0, data, 0, 14 /*head.length*/);
|
|
|
+ System.arraycopy(head_c, 0, data, 0, 14 /*head.length*/);
|
|
|
System.arraycopy(len2, 0, data, 14 /*head.length*/, 4 /*len2.length*/);
|
|
|
System.arraycopy(len1, 0, data, 14+4 /*head.length+len2.length*/, 2 /*len1.length*/);
|
|
|
System.arraycopy(voiceData, 0, data, 14+4+2 /*head.length+len2.length+len1.length*/, len /*voiceData.length*/);
|
|
@@ -61,8 +78,31 @@ public class VoiceModel {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- public static byte[] getStopVoiceData() {
|
|
|
- byte[] data = {(byte)0x55, (byte)0xaa, (byte)0x00, (byte)0x00, //帧头
|
|
|
+ public static byte[] getVoiceDataT4(String text) {
|
|
|
+ if (!TextUtils.isEmpty(text)) {
|
|
|
+ try {
|
|
|
+ text = "$tell:" + text;
|
|
|
+ //语音文本字节数组
|
|
|
+ byte[] textBytes = text.getBytes("GBK");
|
|
|
+ //总长度
|
|
|
+ int len = textBytes.length;
|
|
|
+ int dataLen = len + 24;
|
|
|
+ byte[] data = new byte[dataLen];
|
|
|
+ System.arraycopy(head_t, 0, data, 0, 20 /*head.length*/);
|
|
|
+ System.arraycopy(textBytes, 0, data, 20 /*head.length*/, len /*voiceData.length*/);
|
|
|
+ System.arraycopy(end, 0, data, 20+len /*head.length+voiceData.length*/, 4 /*end.length*/);
|
|
|
+
|
|
|
+ return data;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static byte[] getStopVoiceDataC2M() {
|
|
|
+ return new byte[]{(byte)0x55, (byte)0xaa, (byte)0x00, (byte)0x00, //帧头
|
|
|
(byte)0x01, //地址
|
|
|
(byte)0x01, //标识
|
|
|
(byte)0x11, (byte)0xd5, //操作码
|
|
@@ -73,7 +113,27 @@ public class VoiceModel {
|
|
|
(byte)0xfd, (byte)0x00, (byte)0x01, (byte)0x02, //语音数据区域
|
|
|
(byte)0x00, (byte)0x00, (byte)0x0d, (byte)0x0a //帧尾
|
|
|
};
|
|
|
- return data;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static byte[] getStopVoiceDataT4() {
|
|
|
+ try {
|
|
|
+ String text = "$tell:";
|
|
|
+ //语音文本字节数组
|
|
|
+ byte[] textBytes = text.getBytes("GBK");
|
|
|
+ //总长度
|
|
|
+ int len = textBytes.length;
|
|
|
+ int dataLen = len + 24;
|
|
|
+ byte[] data = new byte[dataLen];
|
|
|
+ System.arraycopy(head_t, 0, data, 0, 20 /*head.length*/);
|
|
|
+ System.arraycopy(textBytes, 0, data, 20 /*head.length*/, len /*voiceData.length*/);
|
|
|
+ System.arraycopy(end, 0, data, 20+len /*head.length+voiceData.length*/, 4 /*end.length*/);
|
|
|
+
|
|
|
+ return data;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
//int转字节数组:低字节在前
|
|
@@ -118,4 +178,29 @@ public class VoiceModel {
|
|
|
}
|
|
|
return sb.toString();
|
|
|
}
|
|
|
+
|
|
|
+ public static byte[] hexStr2Bytes(String hex) {
|
|
|
+ int len = hex.length();
|
|
|
+ byte[] result;
|
|
|
+ if (len % 2 == 1){
|
|
|
+ //奇数
|
|
|
+ len++;
|
|
|
+ result = new byte[(len/2)];
|
|
|
+ hex="0"+hex;
|
|
|
+ }else {
|
|
|
+ //偶数
|
|
|
+ result = new byte[(len/2)];
|
|
|
+ }
|
|
|
+
|
|
|
+ int j=0;
|
|
|
+ for (int i = 0; i < len; i+=2){
|
|
|
+ result[j]=hexToByte(hex.substring(i,i+2));
|
|
|
+ j++;
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static byte hexToByte(String inHex){
|
|
|
+ return (byte)Integer.parseInt(inHex,16);
|
|
|
+ }
|
|
|
}
|