Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python2.7
- # In order to run this you will need python-numpy and alsa-utils to be installed
- # I'm sorry for this being quick and dirty
- from sys import stdin, argv
- from struct import unpack, pack, Struct
- import subprocess as sp
- import numpy as npy
- import time
- rate = 16000
- nsamples = 100
- nchars = 75
- block = 2000
- ssize = 2
- sformat = "S16_LE"
- sendian = "<"
- schar = "h"
- ssingle = sendian + schar
- maxval = (2**14-1)*2+1
- minval = -maxval
- blockformat = sendian + schar * block
- packer = Struct(blockformat)
- up = packer.unpack
- pa = packer.pack
- spack = Struct(sendian+schar)
- zeroblock = spack.pack(0)*(block*ssize)
- tickblock = (
- spack.pack(0) +
- spack.pack(minval) +
- spack.pack(0) +
- spack.pack(maxval) +
- spack.pack(0)*((block-2)*2))
- params = [
- "-f", sformat,
- "-r", str(rate),
- "-t", "raw",
- ]
- pin = sp.Popen(
- ["arecord", "-c", "1"] + params,
- stdout=sp.PIPE
- )
- pout = sp.Popen(
- ["aplay", "-c", "2"] + params,
- stdin=sp.PIPE
- )
- calib = None
- while True:
- pout.stdin.write(tickblock)
- inp = pin.stdout.read(block*ssize)
- if (len(inp) < block*ssize):
- print "Block discarded, too short"
- break
- inpa = npy.array(packer.unpack(inp))
- inpa = inpa[:-1]-inpa[1:]
- peakt = npy.argmax(inpa)
- if peakt > 100:
- pin.stdout.read(ssize*((peakt-100)/100+1))
- elif peakt < 100:
- pout.stdin.write(zeroblock[:ssize*2])
- part = inpa[peakt:peakt+nsamples]
- if len(part) < nsamples:
- continue
- if calib == None:
- calib = part
- continue
- swap = part
- part = abs(part-calib)
- tmp = [" "]*nchars
- detected = False
- for i in xrange(len(part)):
- threshold = 10+((nsamples-i)*130/nsamples)
- if part[i] > threshold:
- if i > 2:
- detected = True
- tmp[(i*nchars)/nsamples] = "#"
- elif detected:
- tmp[(i*nchars)/nsamples] = "-"
- if detected:
- print "".join(tmp)
- calib = swap
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement