我有一个 .wav
文件。我想从那个声音文件中获取 PCM 数据,这样我就可以从声音中获取各个数据 block 并对其进行处理。
但是我不知道该怎么做。谁能告诉我该怎么做? 到目前为止我已经这样做了:
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
从文件中获取AudioInputStream
。 - 查询
AudioFormat
的流。 - 创建一个
byte[]
来适应格式。例如。 8 位单声道是byte[1]
。 16 位立体声是byte[4]
。 - 以
byte[]
block 的形式读取流,它将逐帧包含声音。 - 继续进一步处理..
关于java - 如何从 wav 文件中获取 PCM 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12120399/