如何在JavaFX中创建音频波形?
我想使用JavaFX中的图表区域获得音频波形图。不幸的是,我不清楚该怎么做,价值观是什么要从指定给x轴和y轴的声音中提取? 我试图阅读其他帖子,但在javafx上什么也没找到。 你能帮我吗如何在JavaFX中创建音频波形?,java,javafx,Java,Javafx,我想使用JavaFX中的图表区域获得音频波形图。不幸的是,我不清楚该怎么做,价值观是什么要从指定给x轴和y轴的声音中提取? 我试图阅读其他帖子,但在javafx上什么也没找到。 你能帮我吗 示例图像: 下面是提取波形的代码 我正在为我的范围提取正确的参数 如何使用JavaFX打印图形 public class SimpleWaveformExtractor implements WaveformExtractor { private static final int DEFA
示例图像:
下面是提取波形的代码 我正在为我的范围提取正确的参数 如何使用JavaFX打印图形
public class SimpleWaveformExtractor implements WaveformExtractor {
private static final int DEFAULT_BUFFER_SIZE = 32768;
@Override
public double[] extract(File inputFile) {
AudioInputStream in = null;
try {
in = AudioSystem.getAudioInputStream(inputFile);
} catch (Exception e) {
System.out.println("Cannot read audio file");
return new double[0];
}
AudioFormat format = in.getFormat();
byte[] audioBytes = readBytes(in);
int[] result = null;
if (format.getSampleSizeInBits() == 16) {
int samplesLength = audioBytes.length / 2;
result = new int[samplesLength];
if (format.isBigEndian()) {
for (int i = 0; i < samplesLength; ++i) {
byte MSB = audioBytes[i * 2];
byte LSB = audioBytes[i * 2 + 1];
result[i] = MSB << 8 | (255 & LSB);
}
} else {
for (int i = 0; i < samplesLength; i += 2) {
byte LSB = audioBytes[i * 2];
byte MSB = audioBytes[i * 2 + 1];
result[i / 2] = MSB << 8 | (255 & LSB);
}
}
} else {
int samplesLength = audioBytes.length;
result = new int[samplesLength];
if (format.getEncoding().toString().startsWith("PCM_SIGN")) {
for (int i = 0; i < samplesLength; ++i) {
result[i] = audioBytes[i];
}
} else {
for (int i = 0; i < samplesLength; ++i) {
result[i] = audioBytes[i] - 128;
}
}
}
return ArraysHelper.normalize(result);
}
private byte[] readBytes(AudioInputStream in) {
byte[] result = new byte[0];
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
try {
int bytesRead = 0;
do {
bytesRead = in.read(buffer);
result = ArrayUtils.addAll(result, buffer);
} while (bytesRead != -1);
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}
这是返回double数组的代码:
private double[] extractWaveform(File file) throws IOException, UnsupportedAudioFileException {
return new WavFileExtractor().extract(file);
}
这似乎是一个关于“如何绘制声音文件”的一般性问题,没有关于JavaFX的任何具体内容。你能告诉我一些链接吗?这应该可以帮助你@Bo Halim nice link!好的,玛格丽塔,我会在youtube上制作一些关于这个的教程,因为很多人都需要:)。敬请期待。
private double[] extractWaveform(File file) throws IOException, UnsupportedAudioFileException {
return new WavFileExtractor().extract(file);
}