我已使用以下代码(基于在此站点上找到的代码:http://www.labbookpages.co.uk/audio/javaWavFiles.html)读取wav文件并创建双精度值缓冲区(音频帧):
// Open the wav file specified as the first argument
WavFile wavFile = WavFile.openWavFile(new File("recording_1536059009.wav"));
sampleRate = (int) wavFile.getSampleRate();
System.out.println("SR " + sampleRate);
// Display information about the wav file
wavFile.display();
System.out.println("");
// Create a variable that stores number of frames in file.
int numFrames = (int) wavFile.getNumFrames();
double duration = numFrames/sampleRate;
System.out.println("duration is " + duration);
// Get the number of audio channels in the wav file
int numChannels = wavFile.getNumChannels();
// Create a buffer of frames
double[] buffer = new double[numFrames * numChannels];
int framesRead;
do {
// Read frames into buffer
framesRead = wavFile.readFrames(buffer, numFrames);
} while (framesRead != 0);
然后我根据阈值将缓冲区分为高能部分:(块是连续帧(双值)的组,高于该阈值,它们看起来会像这样)。
3.0517578125E-5
9.1552734375E-5
3.0517578125E-5
9.1552734375E-5
这是我用来将双精度值的缓冲区划分为构成重要事件的这些“块”的代码。
ArrayList<Double> significantEvent = new ArrayList<Double>();
ArrayList<ArrayList<Double>> significantEventCollect = new ArrayList<ArrayList<Double>>();
for (int loop = 0; loop < buffer.length; loop++) {
if (buffer[loop] >= threshold) {
significantEvent.add(buffer[loop]);
System.out.println(buffer[loop]);
} else if (buffer[loop] < threshold && significantEvent.size() > 0) {
System.out.println("");
significantEventCollect.add(significantEvent);
significantEvent = new ArrayList<Double>();
我想将这些块转换回它们自己的wav文件,以便可以播放它们。
到目前为止,我还没有发现仅凭这些数据即可实现这一目标。
任何有关我下一步操作的指南都将非常有用。