pyaudio - “侦听”直到检测到语音,然后记录到.wav文件

时间:2013-09-28 18:28:19

标签: python multithreading audio pyaudio

我遇到了一些问题,似乎无法理解这个概念。

我想做的是:

让麦克风“收听”浊音(高于特定阈值)然后开始录制到.wav文件,直到此人停止说话/信号不再存在。例如:

begin:
   listen() -> nothing is being said
   listen() -> nothing is being said
   listen() -> VOICED - _BEGIN RECORDING_
   listen() -> VOICED - _BEGIN RECORDING_
   listen() -> UNVOICED - _END RECORDING_
end

我想使用“线程”也这样做,这样就可以创建一个不断“监听”文件的线程,并且当有浊音数据时,另一个线程会开始。但是,我不能为我的生活弄清楚我应该怎么做..到目前为止,这是我的代码:

import wave
import sys
import threading
from array import array
from sys import byteorder

try:
    import pyaudio
    CHECK_PYLIB = True
except ImportError:
    CHECK_PYLIB = False

class Audio:
    _chunk = 0.0
    _format = 0.0
    _channels = 0.0
    _rate = 0.0
    record_for = 0.0
    stream = None

    p = None

    sample_width = None
    THRESHOLD = 500

    # initial constructor to accept params
    def __init__(self, chunk, format, channels, rate):
        #### set data-types

        self._chunk = chunk
        self.format = pyaudio.paInt16,
        self.channels = channels
        self.rate = rate

        self.p = pyaudio.PyAudio();

   def open(self):
       # print "opened"
       self.stream = self.p.open(format=pyaudio.paInt16,
                                 channels=2,
                                 rate=44100,
                                 input=True,
                                 frames_per_buffer=1024);
       return True

   def record(self):
       # create a new instance/thread to record the sound
       threading.Thread(target=self.listen).start();

   def is_silence(snd_data):
       return max(snd_data) < THRESHOLD

   def listen(self):
       r = array('h')

       while True:
           snd_data = array('h', self.stream.read(self._chunk))

           if byteorder == 'big':
               snd_data.byteswap()
           r.extend(snd_data)

       return sample_width, r

我猜我可以记录“5”秒块,然后,如果该块被视为“浊音”,则应该启动该线程,直到捕获到所有语音数据。但是,因为当前它位于while True:,所以我不希望在有浊音命令之前捕获所有音频,例如“没有声音”,“没有声音”,“声音”,“声音”,“没有声音”,“没声音”我只想要wav文件中的“声音”..任何人都有任何建议吗?

谢谢

编辑:

import wave
import sys
import time 
import threading 
from array import array
from sys import byteorder
from Queue import Queue, Full

import pyaudio 

CHUNK_SIZE = 1024
MIN_VOLUME = 500

BUF_MAX_SIZE = 1024 * 10 

process_g = 0 

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()

process_g = threading.Thread(target=process, args=(stopped, q))

process_g.start()

try:

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

listen_t.join()
process_g.join()

  def process(stopped, q):

  while True:
    if stopped.wait(timeout = 0):
        break
    print "I'm processing.."
    time.sleep(300)

   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 and stopped.wait(timeout=0):
          break
      try:
        print process_g
        for i in range(0, int(44100 / 1024 * 5)):
            data_chunk = array('h', stream.read(CHUNK_SIZE))
            vol = max(data_chunk)
            if(vol >= MIN_VOLUME):
                print "WORDS.."
            else:
                print "Nothing.."

        except Full:
                pass 

    if __name__ == '__main__':
    main()

现在,每5秒钟后,我需要执行“进程”功能,然后处理数据(time.delay(10),同时执行此操作,然后重新开始录制..

2 个答案:

答案 0 :(得分:5)

花了一些时间,我已经提出了以下代码,似乎正在做你需要的。它当然没有做的是将音频数据写入.wav(或等效的)文件,但您可以自己实现:

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

import pyaudio


CHUNK_SIZE = 1024
MIN_VOLUME = 500
# if the recording thread can't consume fast enough, the listener will start discarding
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()

答案 1 :(得分:5)

看这里:

https://github.com/jeysonmc/python-google-speech-scripts/blob/master/stt_google.py

它甚至将Wav转换为flac并将其发送到google Speech api,如果您不需要它,只需删除stt_google_wav函数;)