美文网首页
科大讯飞wav文件转文字

科大讯飞wav文件转文字

作者: 辉_2017 | 来源:发表于2017-12-13 17:05 被阅读0次

    package com.iflytek.test;

    public class TestMscRecognition {

    private static String fileName = "G:/留言测试wav/201712121458598267.wav";

    // private static String fileName = "E:/amr文件/11.wav";

    public static void main(String[] args) {

    MscRecognition mObject = new MscRecognition();

    String result = null;

    result = mObject.voiceTowords(fileName);

    if (result.length() > 0) {

    System.out.println("识别的文字:" + result);

    } else {

    System.out.println("空语音");

    }

    }

    }

    package com.iflytek.test;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.util.ArrayList;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 {// 账号IDprivate static final String APPID = "5a290e4a";private StringBuffer mResult = new StringBuffer();/** 最大等待时间, 单位ms */private int maxWaitTime = 500;/** 每次等待时间 */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字节分割成数组ArrayListbuffers = 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();System.out.println(recognizer.isListening());// 在原有的代码基础上主要添加了这个while代码等待音频解析完成,recognizer.isListening()返回true,说明解析工作还在进行while (recognizer.isListening()) {if (maxWaitTime < 0) {mResult.setLength(0);mResult.append("解析超时!");break;}try {Thread.sleep(perWaitTime);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}maxWaitTime -= perWaitTime;}}return mResult.toString();}/** * 将字节缓冲区按照固定大小进行分割成数组 * * @param buffer * 缓冲区 * @param length * 缓冲区大小 * @param spsize * 切割块大小 * @return */private ArrayListsplitBuffer(byte[] buffer, int length, int spsize) {ArrayListarray = new ArrayList();

    if (spsize <= 0 || length <= 0 || buffer == null || buffer.length < length)

    return array;

    int size = 0;

    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);// 获取错误码描述

    System.out.println("错误码:" + error.getErrorCode());

    }

    // 扩展用接口

    public void onEvent(int eventType, int arg1, int agr2, String msg) {

    }

    };

    }

    相关文章

      网友评论

          本文标题:科大讯飞wav文件转文字

          本文链接:https://www.haomeiwen.com/subject/aolzixtx.html