lundi 29 décembre 2014

Should an extracted audio sample be contained inside its original source when comparing bytes?

Let's say that I have an audio wav file with the sentence:



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


Now consider opening this audio in Audacity and extracting and saving the word 'sometimes' in another file based on its wave draw.



+-----------+-------------+
| meta data | 'sometimes' |.wav
+-----------+-------------+


Then I used this Java code to get the audio data only from both files:



//...
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;
}
}


Now comes my question again.


Shouldn't this code be able to find 'sometimes' audio data bytes inside the original audio file considering the extraction mentioned before?


I tried comparing contents as String but no lucky at all:



new String(source).contains(new String(sample));


Can someone point what I missing here?


Aucun commentaire:

Enregistrer un commentaire