Skip to content

Instantly share code, notes, and snippets.

@nobonobo
Created March 23, 2012 01:08
Show Gist options
  • Save nobonobo/2165943 to your computer and use it in GitHub Desktop.
Save nobonobo/2165943 to your computer and use it in GitHub Desktop.
トーン検出してあそんでみた。
# encoding: utf-8
import sys
import time
import shlex
from subprocess import Popen, PIPE
import atexit
import wave
from numpy import *
range = xrange
rate = 16000
detect_min = 100
detect_max = 1000
flen = int(rate/detect_min)
begin = int(rate/detect_max)
def read_wav(fname):
wav = wave.open(fname)
return fromstring(wav.readframes(wav.getnframes()), 'h')
def record_wav():
cmds = []
if sys.platform=='linux2':
# kill pipe-buffer
cmds.extend(shlex.split('stdbuf -o0'))
# main sox command-line
cmds.extend(shlex.split('sox -q'))
# input stream
cmds.extend(shlex.split('-d'))
#cmds.extend(shlex.split('sample.wav'))
# output stream
cmds.extend(shlex.split('-t raw -e signed-integer -b 16 -c 1 -r {0} -'.format(rate)))
proc = Popen(cmds, stdout=PIPE, bufsize=0)
atexit.register(proc.kill)
bytes = ''
while 1:
bytes = proc.stdout.read(flen*2)
if len(bytes)==flen*2:
yield fromstring(bytes, 'h')
def amdf(samples):
higher = 0
lower = 2**15-1
for i in range(flen-begin):
shift = begin+i
v = int(average(abs(samples[shift:shift+flen] - samples[0:flen])))
higher = max(higher, v)
if higher*0.95>v:
lower = min(lower, v)
if lower*1.05<v:
return rate/shift
return 0
"""
samples = read_wav('sample.wav')
for pick in range(0,len(samples)-frame*2, frame):
pitch = amdf(samples[pick:pick+frame*2])
print pick, pitch
"""
print 'frame-length:',flen
average_vol = 2**15-1
last_avr = 0.0
last_am = 0
gen = record_wav()
last_frame = gen.next()
for frame in gen:
vol = average(abs(frame-last_avr))
last_avr = last_avr*0.9 + average(frame)*0.1
average_vol = average_vol*0.995 + vol*0.005
res = 0
if vol > average_vol*1.5:
am = amdf(concatenate((last_frame,frame)))
res = am #if last_am==am else 0
last_am = am
print '\r{0:5d}, {1:5d}'.format(int(vol), res),
last_frame = frame
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment