gpt4 book ai didi

java - 比较字节时,提取的音频样本是否应包含在其原始源中?

转载 作者:行者123 更新时间:2023-12-02 22:40:14 24 4
gpt4 key购买 nike

假设我有一个包含以下内容的音频wav文件:

+-----------+----------------------------------------+
| meta data | 'Audio recognition sometimes is trick' |.wav
+-----------+----------------------------------------+

现在,考虑以Audacity打开此音频,并根据波形图提取并有时将“有时”一词保存在另一个文件中。
+-----------+-------------+
| meta data | 'sometimes' |.wav
+-----------+-------------+

然后,我使用此Java代码仅从两个文件中获取音频数据:
    //...
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));

有人可以指出我在这里想念的东西吗?

最佳答案

@菲尔,你就是那个家伙!您的提示使我找到了解决方案!

  • Audacity样本音频提取以某种不同的方式对样本字节进行编码。
  • 我编写了一个Java程序来识别源音频中的静音,然后拆分了一些
    逐字取样;
  • 比较源和匹配的新非听觉采样!

  • 这是新的转换和比较:
    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/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com