在用户定义的类中使用Pyaudio回调方法

时间:2015-09-21 22:30:23

标签: python-2.7 pyaudio

我认为这很可能只是我很愚蠢,但我试图创建一个用户定义的类,它具有用户定义的回调函数以与pyaudio一起使用。这就是我所拥有的:

class asoa_io:        
  def write_callback(self):
    def _write_callback(in_data, frame_count, time_info, status):
        data = self.template_wave[template_idx*template_framesize:(template_idx+1)*template_framesize - 1]
        self.template_idx += 1
        return (data, pyaudio.paContinue)
    return _write_callback

  def read_callback(self):
    def _read_callback(in_data, frame_count, time_info, status):
        self.signal += in_data  # This has the incoming signal as a list of frames.
        return (in_data, pyaudio.paContinue)
    return _read_callback

  """ Synthesize the speech and do any preprocessing.
  Note that we want to use the kal_diphone voice for synthesis.
  """
  def __init__(self, text_string):
    temp_fd, template_file = tempfile.mkstemp()
    string_file = tempfile.NamedTemporaryFile()


    string_file.write(text_string)
    string_file.flush()


    if (subprocess.call([PATH_TO_TEXT2WAVE, string_file.name, "-o", template_file]) > 0):
        print "Error running text2wave"

    string_file.close()


    self.template_rate, self.template_wave = scipy.io.wavfile.read(template_file)
    self.template_length = len(self.template_wave)

    self.pa = pyaudio.PyAudio()
    self.input_stream = self.pa.open(format=pyaudio.paInt16,
                                channels=1, rate = self.template_rate, input=True,
                                stream_callback = self.read_callback)
    self.output_stream = self.pa.open(format=pyaudio.paInt16,
                                 channels=1, rate = self.template_rate, output=True,
                                 stream_callback = self.write_callback)

    self.template_idx = 0
    self.signal = []


  def start_speech(self):
    self.output_stream.start_stream()
    self.input_stream.start_stream()

调用功能部分虽然没有正常工作 - 我收到了错误

  

write_callback()只需1个参数(给定5个)

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

好吧,我似乎是对的,我 只是愚蠢。

如果使read_callback成为5个变量的函数,则有效。

  

def read_callback(self,in_data,frame_count,time_info,status):

与write_callback相似