如何从wav文件中获取PCM数据? [英] How to get PCM data from a wav file?
本文介绍了如何从wav文件中获取PCM数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有 .wav
文件。我想从该声音文件中获取PCM数据,以便我可以从声音中获取单个数据块并对其进行处理。
I have a .wav
file. I want to get the PCM data from that sound file, so that I can get the individual data chunks from the sound and process it.
但我不知道怎么做。谁能告诉我怎么做?
到目前为止我已经这样做了:
But I don't know how to do it. Can anyone tell me how to do it? I have done this so far:
public class test
{
static int frameSample;
static int timeofFrame;
static int N;
static int runTimes;
static int bps;
static int channels;
static double times;
static int bufSize;
static int frameSize;
static int frameRate;
static long length;
public static void main(String[] args)
{
try
{
AudioInputStream ais = AudioSystem.getAudioInputStream(new File("music/audio.wav"));
AudioInputStream a;
int numBytes = ais.available();
System.out.println("numbytes: "+numBytes);
byte[] buffer = new byte[numBytes];
byte[] buffer1=new byte[numBytes] ;
int k=0;
int count=0;
while(count!=-1){
count=ais.read(buffer, 0, numBytes);
}
long value = 0;
for (int i = 0; i < buffer.length; i++)
{
value = ((long) buffer[i] & 0xffL) << (8 * i);
System.out.println("value: "+value);
}
} catch(Exception e) {
}
}
}
推荐答案
这可以通过完成Java Sound API。
- 使用
AudioSystem
获取<来自文件的code> AudioInputStream 。 - 查询
AudioFormat
的流。 - 创建
byte []
以适应格式。例如。 8位单声道是字节[1]
。 16位立体声是byte [4]
。 - 读取
byte [] <的块中的流/ code>并且它将逐帧包含声音。
- 继续进行处理..
- Use the
AudioSystem
to get anAudioInputStream
from the file. - Query the stream for the
AudioFormat
. - Create a
byte[]
to suit the format. E.G. 8 bit mono is abyte[1]
. 16 bit stereo isbyte[4]
. - Read the stream in chunks of the
byte[]
and it will contain the sound, frame by frame. - Proceed with further processing..
这篇关于如何从wav文件中获取PCM数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文