java数据合成声波,来自TargetDataLine的声波

Currently I am trying to record a sound wave from a mic and display amplitude values in realtime in Java. I came across Targetdataline but I am having a bit of trouble understanding I get data from it.

Sample code from Oracle states:

line = (TargetDataLine) AudioSystem.getLine(info);

line.open(format, line.getBufferSize());

ByteArrayOutputStream out = new ByteArrayOutputStream();

int numBytesRead;

byte[] data = new byte[line.getBufferSize() / 5];

// Begin audio capture.

line.start();

// Here, stopped is a global boolean set by another thread.

while (!stopped) {

// Read the next chunk of data from the TargetDataLine.

numBytesRead = line.read(data, 0, data.length);

****ADDED CODE HERE*****

// Save this chunk of data.

out.write(data, 0, numBytesRead);

}

So I am currently trying to add code to get a input stream of amplitude values however I get a ton of bytes when I print what the variable data is at the added code line.

for (int j=0; j

System.out.format("%02X ", data[j]);

}

Does anyone who has used TargetDataLine before know how I can make use of it?

解决方案

For anyone who has trouble using TargetDataLine for sound extraction in the future, the class WaveData by Ganesh Tiwari contains a very helpful method that turns bytes into a float array (http://code.google.com/p/speech-recognition-java-hidden-markov-model-vq-mfcc/source/browse/trunk/SpeechRecognitionHMM/src/org/ioe/tprsa/audio/WaveData.java):

public float[] extractFloatDataFromAudioInputStream(AudioInputStream audioInputStream) {

format = audioInputStream.getFormat();

audioBytes = new byte[(int) (audioInputStream.getFrameLength() * format.getFrameSize())];

// calculate durationSec

float milliseconds = (long) ((audioInputStream.getFrameLength() * 1000) / audioInputStream.getFormat().getFrameRate());

durationSec = milliseconds / 1000.0;

// System.out.println("The current signal has duration "+durationSec+" Sec");

try {

audioInputStream.read(audioBytes);

} catch (IOException e) {

System.out.println("IOException during reading audioBytes");

e.printStackTrace();

}

return extractFloatDataFromAmplitudeByteArray(format, audioBytes);

}

Using this I can get sound amplitude data.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值