fishing.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:wow-fishipy 作者: kioltk 项目源码 文件源码
def listen():
    print 'Well, now we are listening for loud sounds...'
    CHUNK = 1024  # CHUNKS of bytes to read each time from mic
    FORMAT = pyaudio.paInt16
    CHANNELS = 2
    RATE = 18000
    THRESHOLD = 1200  # The threshold intensity that defines silence
                      # and noise signal (an int. lower than THRESHOLD is silence).
    SILENCE_LIMIT = 1  # Silence limit in seconds. The max ammount of seconds where
                       # only silence is recorded. When this time passes the
                       # recording finishes and the file is delivered.
    #Open stream
    p = pyaudio.PyAudio()

    stream = p.open(format=FORMAT,
                    channels=CHANNELS,
                    rate=RATE,
                    input=True,
                    frames_per_buffer=CHUNK)
    cur_data = ''  # current chunk  of audio data
    rel = RATE/CHUNK
    slid_win = deque(maxlen=SILENCE_LIMIT * rel)


    success = False
    listening_start_time = time.time()
    while True:
        try:
            cur_data = stream.read(CHUNK)
            slid_win.append(math.sqrt(abs(audioop.avg(cur_data, 4))))
            if(sum([x > THRESHOLD for x in slid_win]) > 0):
                print 'I heart something!'
                success = True
                break
            if time.time() - listening_start_time > 20:
                print 'I don\'t hear anything already 20 seconds!'
                break
        except IOError:
            break

    # print "* Done recording: " + str(time.time() - start)
    stream.close()
    p.terminate()
    return success
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号