Pyaudio设置输出=变量

时间:2015-01-20 23:15:10

标签: multithreading python-2.7 pyaudio

如何设置一个等于'O'或' - '的变量,然后将其放在if语句中,如下所示:

if variable == 'O':
    print 'hi'

你怎么能这样做:

import threading
from array import array
from Queue import Queue, Full
import pyaudio


CHUNK_SIZE = 1024
MIN_VOLUME = 500
BUF_MAX_SIZE = CHUNK_SIZE * 10


def main():
    stopped = threading.Event()
    q = Queue(maxsize=int(round(BUF_MAX_SIZE / CHUNK_SIZE)))

    listen_t = threading.Thread(target=listen, args=(stopped, q))
    listen_t.start()
    record_t = threading.Thread(target=record, args=(stopped, q))
    record_t.start()

    try:
        while True:
        listen_t.join(0.1)
        record_t.join(0.1)
    except KeyboardInterrupt:
        stopped.set()

    listen_t.join()
    record_t.join()


def record(stopped, q):
    while True:
        if stopped.wait(timeout=0):
            break
        chunk = q.get()
        vol = max(chunk)
        if vol >= MIN_VOLUME:
        # TODO: write to file
            print "O",
        else:
            print "-",


def listen(stopped, q):
    stream = pyaudio.PyAudio().open(
        format=pyaudio.paInt16,
        channels=2,
        rate=44100,
        input=True,
        frames_per_buffer=1024,
    )

    while True:
        if stopped.wait(timeout=0):
            break
        try:
            q.put(array('h', stream.read(CHUNK_SIZE)))
        except Full:
            pass  # discard


if __name__ == '__main__':
    main()

你可以使用它,如果输出是'O'那么打印你好吗?有人会为我编写代码,因为我一直在尝试编写这段代码,但我仍然无法让代码对我有用。谢谢。

1 个答案:

答案 0 :(得分:1)

为了在Python中使用if then语句,首先我们需要根据需求和类型使用适当的语法声明变量值。

s = "O"
if s == 'O':
    print 'hi'