java离线语⾳_Java通过JNA麦克风调离线语⾳唤醒package com.day.iFlyInterfacemonUtil.dll.ivw;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.LineUnavailableException;
import com.day.iFlyInterfacemonUtil.dll.ivw.OfflineWindowsIvw.MyDllInterface;
import com.sun.jna.Memory;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
public class IvwThread extends Thread{
OfflineWindowsIvw offlineWindowsIvw=null;
IvwThread(OfflineWindowsIvw offlineWindowsIvw){
this.offlineWindowsIvw=offlineWindowsIvw;
}
public void run(){
//登录参数
String lgi_param = "appid = 填写您⾃⼰的, work_dir = ./dllLib/ivw";//注意路径放置
String ssb_param = "ivw_threshold=0:1450,sst=wakeup,ivw_shot_word=1,ivw_res_path =fo|res/ivw/wakeupresource.jet";
//登录成功标志ret为0  MSP_SUCCESS = 0,
int ret=MyDllInterface.INSTANCE.MSPLogin(null, null, lgi_param);
if (ret!=0){
System.out.println("登录失败...请检查");
}else{
System.out.println("请注意,唤醒语⾳需要根据唤醒词内容⾃⾏录制并重命名为宏IVW_AUDIO_FILE_NAME所指定名称");
}
IntByReference intByReference = new IntByReference(-1);
String session_id=MyDllInterface.INSTANCE.QIVWSessionBegin(null,ssb_param,intByReference);
System.out.println("本次会话id为:"+session_id);
//指定唤醒⾳频⽂件读取
File file=new File("./zMusic/pcm/Ivw/awake.pcm");
int frameSize = 10*640; //每次发送⾳频⼤⼩
int intervel = 200;
int MSP_AUDIO_SAMPLE_INIT = 0x00;//初始化帧
int SP_AUDIO_SAMPLE_FIRST = 0x01;//第⼀帧
int MSP_AUDIO_SAMPLE_CONTINUE  = 0x02;//中间帧
int MSP_AUDIO_SAMPLE_LAST  = 0x04;//最后帧
int sendStage=0x01;
Ivw_ntf_handler msgProcCb = new Ivw_ntf_handlerImpl();//回调函数实例
int err_code = MyDllInterface.INSTANCE.QIVWRegisterNotify(session_id,msgProcCb,null);
System.out.println("注册函数返回的错误码"+err_code);
System.out.println("csid="+session_id+",错误码"+err_code);
Pointer audioDataPointer = new Memory(9600000);
try (FileInputStream fs = new FileInputStream(file)) {
while(true){
//println(fs.available());
byte[] byteArrayAudioData = new byte[frameSize];
offlineWindowsIvw.targetDataLine.open(offlineWindowsIvw.audioFormat);
offlineWindowsIvw.targetDataLine.start();
int len = new AudioInputStream(offlineWindowsIvw.targetDataLine).read(byteArrayAudioData);
if(len!=-1){
/*String Encoder().pyOf(buffer, len)); audioDataPointer.setString(indexFlag, sendData);
//byte[] pyOf(buffer, len);
/*for(long z=0;z
audioDataPointer.setByte(z, buffer[(int) z]);
}*/
//System.out.print(sendStage);
//直接写byte[] 数组数据也可以的
err_code = MyDllInterface.INSTANCE.QIVWAudioWrite(session_id,byteArrayAudioData,len,sendStage); //println(sendData);
//System.out.println("csid="+session_id+",错误码"+err_code+",长度"+len+",aus="+sendStage);
}
//调⽤麦克风时候,这段将不会被执⾏...
java jnasendStage=0x02;
if (len == -1) {
//println("空帧....");
byte b='b';
sendStage=0x04;
audioDataPointer=new Memory(100000);
audioDataPointer.setByte(0, b);
err_code = MyDllInterface.INSTANCE.QIVWAudioWrite(session_id,"".getBytes(),0,sendStage); //System.out.println("csid="+session_id+",错误码"+err_code+",长度"+len+",aus="+sendStage); System.out.println("最后⼀帧返回的错误码:"+err_code+",即将执⾏退出...");
break;  //⽂件读完,跳出循环
}
if(err_code!=0){
System.out.println("出错了...");
}
Thread.sleep(200); //模拟⼈说话时间间隙,10帧的⾳频时长为200ms
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
}
//Thread.sleep(500);
MyDllInterface.INSTANCE.QIVWSessionEnd(session_id, "正常终⽌");
int tempCode=MyDllInterface.INSTANCE.MSPLogout();
System.out.println("最终的执⾏退出,返回的错误码:"+tempCode);
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。