Russ Nelson's blog

[ Home | RSS 2.0 | ATOM 1.0 ]

Sat, 01 Nov 2008

findwhistle

I've experimented with keeping an audio recording in addition to a GPS track of my bicycle rides. The trouble with a continuous audio recording is that 1) it's long, 2) it's boring, and 3) the interesting things are hard to seek to. If you could do reliable speech recognition, you could say a word like "mark" or somesuch. However, in my experience, street noise is going to kill you.

Better than that, you detect a whistle. The code below will print the duration of the whistle, the time from the beginning of the audio recording, and the pitch of the whistle. The purpose of this is to be able to do continuous audio recording, and yet be able to take a waypoint with an audio annotation.

#!/usr/bin/python

import sys
import wave
import struct

def findwhistle(inwave):
    """given an open wave file, return an array which consists of the times
    whenever a whistle was found."""
    framecount = 0
    zerocross = 0
    lastzerocross = 0
    zerocrosssum = 0
    zerocrosscount = 0
    sign = 1
    while True:
        frames = inwave.readframes(100)
        if len(frames) == 0: break
        frames = struct.unpack("<100h", frames)
        for i, sample in enumerate(frames):
            if sign * sample > 0:
                zerocross += 1
            else:
                if abs(zerocross - lastzerocross) <= 1:
                    zerocrosssum += zerocross
                    zerocrosscount += 1
                else:
                    if zerocrosscount > 100:
                         print '! %4.2f %4.2f %5.0f' % ( zerocrosssum / 8000.0, (framecount + i - zerocrosssum) / 8000.0,  zerocrosscount / (zerocrosssum / 8000.0))
                    zerocrosssum = 0
                    zerocrosscount = 0
                #print zerocross
                sign = -sign
                lastzerocross = zerocross
                zerocross = 1
        framecount += len(frames)
    return framecount / 8000.0

def main():
    f = wave.open(sys.argv[1], "r")
    print f.getparams()
    print findwhistle(f)

if __name__ == "__main__":
    main()

posted at: 05:46 | path: /opensource | permanent link to this entry

Made with Pyblosxom