java 语音采集组件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 语音采集组件相关的知识,希望对你有一定的参考价值。

class Capture implements Runnable{

TargetDateLine line;//可以从中读取音频数据的某种类型的 DataLine

Thread thread;

Socket socket;

BufferedOutputStream captrueOutputStream;

Captrue(Socket socket){//构造函数,取得socket

?? this.socket=socket;

}

public void start(){//启动线程

?? thread=new Thread(this);

?? thread.setName(“Capture”);

?? thread.start();

}

public void stop(){//停止线程

?? thread=null;

}

public void run(){

?? try{

????? captrueOutputStream=new BufferedOutputStream(s.getOutputStream())//建立输出流

}catch(IOException e){

?? return;

}

AudioFormat format =new AudioFormat(8000,16,2,true,true);//AudioFormat(float sampleRate, int sampleSizeInBits, int channels, boolean signed, boolean bigEndian)
??????? DataLine.Info info = new DataLine.Info(TargetDataLine.class,format);

??????? try {
??????????? line = (TargetDataLine) Audiosystem.getLine(info);
??????????? line.open(format, line.getBufferSize());
??????? } catch (Exception ex) {
??????????? return;
??????? }

?? byte[] data = new byte[1024];//此处的1024可以情况进行调整,应跟下面的1024应保持一致
?????????? int numBytesRead=0;
?????????? line.start();

?????????? while (thread != null) {
?????????????? numBytesRead = line.read(data, 0,128);//取数据(1024)的大小直接关系到传输的速度,一般越小越快,
?????????????? try {
???????????????? captrueOutputStream.write(data, 0, numBytesRead);//写入网络流
?????????????? }
?????????????? catch (Exception ex) {
?????????????????? break;
?????????????? }
?????????? }

?????????? line.stop();
?????????? line.close();
?????????? line = null;

?????????? try {
?????????????? captrueOutputStream.flush();
?????????????? captrueOutputStream.close();
?????????? } catch (IOException ex) {
?????????????? ex.printStackTrace();
?????????? }
?????? }
????
}

以上是关于java 语音采集组件的主要内容,如果未能解决你的问题,请参考以下文章

语音分离基于matlab PCA+ICA语音信号采集+混合+分离含Matlab源码 1592期

基于matlab的声音信号采集与处理

android开发实现语音数据实时采集/播放

语音数据采集-实时语音数据可视化

基于kaldi的iOS实时语音识别(本地)+03+音频采集传输

语音采集回放系统-硬件总结