I have a wav format mono audio file and I have a second file which contains a small clip from that file (so the second file is a much smaller wav file). All I need to know is the approximate start points of the clip within the larger audio file (the clip may be repeated --I would like the byte position of each occurrence.) I'm looking for particular sounds in a speach file

My natural thought was to do a regexp in Perl. I'm running activestate Perl 5.8 on Windows, so I know I have to use binmode to deal with binary files. Some trial and error revealed that to do anything with a regexp with a binary search I have to escape all of the interesting characters in the clip I'm searching for. Here's some code just to try to get the basic concept working:

#!/usr/bin/perl -w use strict; my $wordsound = 'balks.wav'; my $lettersound = 'a.wav'; my $letter; open (WORDSOUND, $wordsound) or die "can't open $wordsound: $!"; binmode (WORDSOUND); open (LETTERSOUND, $lettersound) or die "can't open $lettersound: $!"; binmode (LETTERSOUND); #actual data starts on byte 4097 seek (LETTERSOUND, 4097,0); #100 bytes ought to be OK for identifying the clip my $error = read (LETTERSOUND, $letter, 100); my $word = <WORDSOUND>; #escape all potentially nasty characters with \'s #I'm not sure if I'm escaping everything I need to (?) $letter =~ s/(\\|\||\(|\)|\[|\{|\^|\$|\*|\+|\?|\.)/\\$&/gsm; $word =~ /$letter/gms; print pos($word);

All I get is
"Use of uninitialized value in print at parsesound.pl line 25, <WORDSOUND> line 1."

If anyone has ever looked for a binary string within a binary file and has any code, that'd be great. I would have thought it would be a pretty routine thing, but googling didn't yield much. I didn't see it in the Perl Monks search either. Hmmmm.


In reply to searching for a binary string in a binary file by rochlin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.