package com.iflytek.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.iflytek.cloud.speech.RecognizerListener;
import com.iflytek.cloud.speech.RecognizerResult;
import com.iflytek.cloud.speech.Setting;
import com.iflytek.cloud.speech.SpeechConstant;
import com.iflytek.cloud.speech.SpeechError;
import com.iflytek.cloud.speech.SpeechRecognizer;
import com.iflytek.cloud.speech.SpeechUtility;
/**
* 音频文件识别
*
* @author zxh
*
*/
public class MscRecognition {
private static final Log logger = LogFactory.getLog(MscRecognition.class);
// 账号ID
private static final String APPID = "5a290e4a";
private StringBuffer mResult = new StringBuffer();
/** 最大等待时间, 单位ms */
private int maxWaitTime = 600;
/** 每次等待时间 */
private int perWaitTime = 100;
/** 音频文件 */
private String fileName = "";
static {
Setting.setShowLog(false);
SpeechUtility.createUtility("appid=" + APPID);
}
public String voiceTowords(String fileName) {
return initVoiceTowords(fileName, true);
}
/**
*
* @param fileName
* @param init
* 初始化最大等待时间
* @return
*/
public String initVoiceTowords(String fileName, boolean init) {
if (init) {
maxWaitTime = 6000;
}
this.fileName = fileName;
return recognize();
}
// *************************************音频流听写*************************************
/**
* 听写
*
* @return
*/
private String recognize() {
if (SpeechRecognizer.getRecognizer() == null)
SpeechRecognizer.createRecognizer();
return RecognizePcmfileByte();
}
/**
* 自动化测试注意要点 如果直接从音频文件识别,需要模拟真实的音速,防止音频队列的堵塞
*
*/
private String RecognizePcmfileByte() {
// 1、读取音频文件
FileInputStream fis = null;
byte[] voiceBuffer = null;
try {
fis = new FileInputStream(new File(fileName));
voiceBuffer = new byte[fis.available()];
fis.read(voiceBuffer);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != fis) {
fis.close();
fis = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
// 2、音频流听写
if (0 == voiceBuffer.length) {
mResult.append("no audio avaible!");
} else {
// 解析之前将存出结果置为空
mResult.setLength(0);
SpeechRecognizer recognizer = SpeechRecognizer.getRecognizer();
// 设置参数,详见《MSC Reference Manual》SpeechConstant类
// 应用领域用于听写和语音语义服务
recognizer.setParameter(SpeechConstant.DOMAIN, "iat");
// 语言区域
recognizer.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
// 方言 每一种语言区域
recognizer.setParameter(SpeechConstant.ACCENT, "mandarin");
// 音频源
recognizer.setParameter(SpeechConstant.AUDIO_SOURCE, "-1");
// 音频采样率
recognizer.setParameter(SpeechConstant.SAMPLE_RATE, "8000");
// 返回格式
recognizer.setParameter(SpeechConstant.RESULT_TYPE, "plain");
// 开始听写
recognizer.startListening(recListener);
// voiceBuffer为音频数据流,splitBuffer为自定义分割接口,将其以4.8k字节分割成数组
ArrayList<byte[]> buffers = splitBuffer(voiceBuffer, voiceBuffer.length, 4800);
for (int i = 0; i < buffers.size(); i++) {
// 每次写入msc数据4.8K,相当150ms录音数据
recognizer.writeAudio(buffers.get(i), 0, buffers.get(i).length);
}
recognizer.stopListening();
// 在原有的代码基础上主要添加了这个while代码等待音频解析完成,recognizer.isListening()返回true,说明解析工作还在进行
while (recognizer.isListening()) {
try {
Thread.sleep(perWaitTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
maxWaitTime -= perWaitTime;
}
}
return mResult.toString();
}
/**
* 将字节缓冲区按照固定大小进行分割成数组
*
* @param buffer
* 缓冲区
* @param length
* 缓冲区大小
* @param spsize
* 切割块大小
* @return
*/
private ArrayList<byte[]> splitBuffer(byte[] buffer, int length, int spsize) {
ArrayList<byte[]> array = new ArrayList<byte[]>();
if (spsize <= 0 || length <= 0 || buffer == null || buffer.length < length)
return array;
int size = 0;
// 截断识别5秒得数据
/*int fiveMsec = 93946;
if (length >= fiveMsec) {
length = fiveMsec;
}
*/
while (size < length) {
int left = length - size;
if (spsize < left) {
byte[] sdata = new byte[spsize];
System.arraycopy(buffer, size, sdata, 0, spsize);
array.add(sdata);
size += spsize;
} else {
byte[] sdata = new byte[left];
System.arraycopy(buffer, size, sdata, 0, left);
array.add(sdata);
size += left;
}
}
return array;
}
/**
* 听写监听器
*/
private RecognizerListener recListener = new RecognizerListener() {
// 开始录音
public void onBeginOfSpeech() {
}
// 结束录音
public void onEndOfSpeech() {
}
// 音量值0~30
public void onVolumeChanged(int volume) {
}
public void onResult(RecognizerResult result, boolean islast) {
mResult.append(result.getResultString());
}
// 会话发生错误回调接口
public void onError(SpeechError error) {
System.out.println("---------------error");
error.getErrorDescription(true);// 获取错误码描述
logger.debug(fileName + "-xf错误码:-" + error.getErrorCode());
}
// 扩展用接口
public void onEvent(int eventType, int arg1, int agr2, String msg) {
}
};
}
没有合适的资源?快使用搜索试试~ 我知道了~
科大讯飞语音识别音频转文字

共4个文件
java:4个

需积分: 50 131 下载量 157 浏览量
2017-12-19
08:29:12
上传
评论 9
收藏 5KB ZIP 举报
温馨提示
科大讯飞语音识别自己写的demo,已经过自测没有问题; 1.使用前建议下载看科大讯飞的接口文档 http://doc.xfyun.cn/msc_java/299249; MSC Develop Manual for Java.pdf 2.必须添加语音识别的jar包
资源推荐
资源详情
资源评论















收起资源包目录






共 4 条
- 1
资源评论

- #完美解决问题
- #运行顺畅
- #内容详尽
- #全网独家
- #注释完整
- 妙趣横生的大白菜2021-09-12无语了,都不全代码

sunpx3
- 粉丝: 13
- 资源: 17
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- VMWARE虚拟机可以直接安装使用
- FLAC3D 6.0-7.0版塑形区体积输出及剪切、张拉破坏区域体积可视化展示,FLAC3D 6.0-7.0版体积输出:塑形区、剪切破坏区及张拉破坏区体积分析图示,FLAC3D输出塑形区体积,适用于6
- STM32 VS Code Extension
- oracle维护手册,使运维工作简单化
- 007springboot大学生租房平台的设计与实现.zip
- localization-zh-243.jar
- 圆环进度条示例代码程序
- 基于遗传算法优化XGBoost模型参数的时间序列预测方法研究:迭代次数、最大深度和学习率的交叉验证优化策略,基于遗传算法优化XGBoost模型参数的时间序列预测算法:采用交叉验证抑制过拟合问题并优化迭
- 基于Visual Studio 2022开发,支持C++开发语言对接的SDK(Linux 32bit),主要支持实时视频解码播放、获取实时视频码流、录像码流解码播放、获取录像码流以及录像下载,支持获取
- 网络中心运维工程师面试全册总结
- Title Lorem Ipsum.pptx
- 成绩查询系统源码下载!
- AI大模型学习与使用.docx
- 电力系统融合终端消缺运维手册及操作步骤
- 华为USG系列防火墙配置案例手册完整版
- 大学生实验报告撰写基础教程
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制
