作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我有一个包含以下内容的音频wav文件:
+-----------+----------------------------------------+
| meta data | 'Audio recognition sometimes is trick' |.wav
+-----------+----------------------------------------+
+-----------+-------------+
| meta data | 'sometimes' |.wav
+-----------+-------------+
//...
Path source = Paths.get("source.wav");
Path sample = Paths.get("sometimes.wav");
int index = compare(transform(source), transform(sample));
System.out.println("Shouldn't I be greater than -1!? " + (index > -1));
//...
private int compare(int[] source, int[] sample) throws IOException {
return Collections.indexOfSubList(Arrays.asList(source), Arrays.asList(sample));
}
private int[] transform(Path audio) throws IOException, UnsupportedAudioFileException {
try (AudioInputStream ais = AudioSystem.getAudioInputStream(
new ByteArrayInputStream(Files.readAllBytes(audio)))) {
AudioFormat format = ais.getFormat();
byte[] audioBytes = new byte[(int) (ais.getFrameLength() * format.getFrameSize())];
int nlengthInSamples = audioBytes.length / 2;
int[] audioData = new int[nlengthInSamples];
for (int i = 0; i < nlengthInSamples; i++) {
int LSB = audioBytes[2*i]; /* First byte is LSB (low order) */
int MSB = audioBytes[2*i+1]; /* Second byte is MSB (high order) */
audioData[i] = (MSB << 8) | (255 & LSB);
}
return audioData;
}
}
new String(source).contains(new String(sample));
最佳答案
@菲尔,你就是那个家伙!您的提示使我找到了解决方案!
private int compare(byte[] captchaData, byte[] sampleData) throws IOException {
return new String(captchaData).indexOf(new String(sampleData));
}
private byte[] transform(Path audio) throws IOException, UnsupportedAudioFileException {
AudioInputStream ais = AudioSystem.getAudioInputStream(audio.toFile());
AudioFormat format = ais.getFormat();
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
int nBufferSize = 1024 * format.getFrameSize();
byte[] abBuffer = new byte[nBufferSize];
int nBytesRead;
while ((nBytesRead = ais.read(abBuffer)) > -1) {
baos.write(abBuffer, 0, nBytesRead);
}
return baos.toByteArray();
}
}
private List<byte[]> split(byte[] audioData) {
System.out.println(audioData.length);
List<byte[]> byteList = new ArrayList<>();
int zeroCounter = 0;
int lastPos = 0;
for (int i = 0; i < audioData.length; i++) {
if (audioData[i] >= -1 && audioData[i] <= 1) {
zeroCounter++; //too many leading 'zeros' could indicate silence or very low noise...
} else if (zeroCounter > 0) {
if (zeroCounter > 2000) {
int from = lastPos;
int to = i - (zeroCounter/2);
byteList.add(
Arrays.copyOfRange(
audioData,
from,
to));
System.out.println("split from: " + from + " to: " + to);
lastPos = to;
}
zeroCounter = 0;
}
}
return byteList;
}
关于java - 比较字节时,提取的音频样本是否应包含在其原始源中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27695374/
我是一名优秀的程序员,十分优秀!