Python pyaudio.paContinue() Examples

The following are 27 code examples of pyaudio.paContinue(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pyaudio , or try the search function .
Example #1
Source File: play_callback.py    From learning-python with MIT License 6 votes vote down vote up
def callback(in_data, frame_count, time_info, status):
    data = wf.readframes(frame_count)
    return (data, pyaudio.paContinue) 
Example #2
Source File: demo_client.py    From ZASR_tensorflow with Apache License 2.0 6 votes vote down vote up
def callback(in_data, frame_count, time_info, status):
    """Audio recorder's stream callback function."""
    global data_list, is_recording, enable_trigger_record
    if is_recording:
        data_list.append(in_data)
        enable_trigger_record = False
    elif len(data_list) > 0:
        # Connect to server and send data
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.connect((args.host_ip, args.host_port))
        sent = ''.join(data_list)
        sock.sendall(struct.pack('>i', len(sent)) + sent)
        print('Speech[length=%d] Sent.' % len(sent))
        # Receive data from the server and shut down
        received = sock.recv(1024)
        print "Recognition Results: {}".format(received)
        sock.close()
        data_list = []
    enable_trigger_record = True
    return (in_data, pyaudio.paContinue) 
Example #3
Source File: client.py    From deepspeech-websocket-server with Mozilla Public License 2.0 6 votes vote down vote up
def __init__(self, callback=None, buffer_s=0, flush_queue=True):
        def proxy_callback(in_data, frame_count, time_info, status):
            callback(in_data)
            return (None, pyaudio.paContinue)
        if callback is None: callback = lambda in_data: self.buffer_queue.put(in_data, block=False)
        self.sample_rate = self.RATE
        self.flush_queue = flush_queue
        self.buffer_queue = queue.Queue(maxsize=(buffer_s * 1000 // self.block_duration_ms))
        self.pa = pyaudio.PyAudio()
        self.stream = self.pa.open(format=self.FORMAT,
                                   channels=self.CHANNELS,
                                   rate=self.sample_rate,
                                   input=True,
                                   frames_per_buffer=self.block_size,
                                   stream_callback=proxy_callback)
        self.stream.start_stream()
        self.active = True 
Example #4
Source File: audio.py    From Oe2sSLE with GNU General Public License v2.0 6 votes vote down vote up
def __init__(self, data, fmt):

        if fmt.formatTag != RIFF.WAVE_fmt_.WAVE_FORMAT_PCM:
            raise Exception()

        if fmt.samplesPerSec < 1000 or fmt.samplesPerSec > 192000:
            data, fmt = wav_tools.wav_resample_preview(data, fmt, 1000, 192000)
        self.data = data
        self.fmt = fmt
        self._offset = 0

        def callback(indata, frames, time, status):
            n_bytes=frames*fmt.blockAlign
            to_read=min(n_bytes, len(self.data) - self._offset)
            outdata = self.data[self._offset:self._offset+to_read]
            self._offset += to_read
            return (outdata,pa.paContinue)

        self.stream = audio.open(format=pa.paInt16, channels=fmt.channels, rate=fmt.samplesPerSec, output=True, stream_callback=callback) 
Example #5
Source File: speech_demo_tk.py    From honk with MIT License 6 votes vote down vote up
def _on_audio(self, in_data, frame_count, time_info, status):
        data_ok = (in_data, pyaudio.paContinue)
        self.last_data = in_data
        self._audio_buf.append(in_data)
        if len(self._audio_buf) != 16:
            return data_ok
        audio_data = base64.b64encode(zlib.compress(b"".join(self._audio_buf)))
        self._audio_buf = []
        response = requests.post("{}/listen".format(self.endpoint), json=dict(wav_data=audio_data.decode(), method="all_label"))
        response = json.loads(response.content.decode())
        if not response:
            return data_ok
        max_key = max(response.items(), key=lambda x: x[1])[0]
        for key in response:
            p = response[key]
            if p < 0.5 and key != "__unknown__":
                continue
            key = key.replace("_", "")
            print(key)
            self.words += [ key ]
        return data_ok 
Example #6
Source File: speech_demo.py    From honk with MIT License 6 votes vote down vote up
def _on_audio(self, in_data, frame_count, time_info, status):
        data_ok = (in_data, pyaudio.paContinue)
        self.last_data = in_data
        self._audio_buf.append(in_data)
        if len(self._audio_buf) != 16:
            return data_ok
        audio_data = base64.b64encode(zlib.compress(b"".join(self._audio_buf)))
        self._audio_buf = []
        response = requests.post("{}/listen".format(self.endpoint), json=dict(wav_data=audio_data.decode(), method="all_label"))
        response = json.loads(response.content.decode())
        if not response:
            return data_ok
        max_key = max(response.items(), key=lambda x: x[1])[0]
        for key in response:
            p = response[key]
            if p < 0.5 and key != "__unknown__":
                continue
            key = key.replace("_", "")
            try:
                Indicator.indicators[labels.index(key)].highlight(1.)
            except ValueError:
                continue
        return data_ok 
Example #7
Source File: audioProcessor.py    From gcc-nmf with MIT License 5 votes vote down vote up
def createAudioStream(self):
        import wave        
        import pyaudio
        
        if self.pyaudio is None:
            self.pyaudio = pyaudio.PyAudio()
        
        self.paContinue = pyaudio.paContinue
        
        waveFile = wave.open(self.fileName, 'rb')
        self.numFrames = waveFile.getnframes()
        self.samples = waveFile.readframes(self.numFrames)
        self.sampleRate = waveFile.getframerate()
        self.bytesPerFrame = waveFile.getsampwidth()
        self.bytesPerFrameAllChannels = self.bytesPerFrame * self.numChannels
        self.format = self.pyaudio.get_format_from_width(self.bytesPerFrame)
        waveFile.close()
        self.waveFile = wave.open(self.fileName, 'rb')
            
        self.sampleIndex = 0
        self.audioStream = self.pyaudio.open(format=self.format,
                                             channels=self.numChannels,
                                             rate=self.sampleRate,
                                             frames_per_buffer=self.blockSize,
                                             output=True,
                                             stream_callback=self.filePlayerCallback) 
Example #8
Source File: friday.py    From Friday with MIT License 5 votes vote down vote up
def _apiai_stt(self):
        from math import log
        import audioop
        import pyaudio
        import time
        resampler = apiai.Resampler(source_samplerate=settings['RATE'])
        request = self.ai.voice_request()
        vad = apiai.VAD()

        def callback(in_data, frame_count):
            frames, data = resampler.resample(in_data, frame_count)
            if settings.show_decibels:
                decibel = 20 * log(audioop.rms(data, 2) + 1, 10)
                click.echo(decibel)
            state = vad.processFrame(frames)
            request.send(data)
            state_signal = pyaudio.paContinue if state == 1 else pyaudio.paComplete
            return in_data, state_signal

        p = pyaudio.PyAudio()
        stream = p.open(format=pyaudio.paInt32, input=True, output=False, stream_callback=callback,
                        channels=settings['CHANNELS'], rate=settings['RATE'], frames_per_buffer=settings['CHUNK'])
        stream.start_stream()
        click.echo("Speak!")
        while stream.is_active():
            time.sleep(0.1)
        stream.stop_stream()
        stream.close()
        p.terminate() 
Example #9
Source File: audio_recorder.py    From project-keyword-spotter with Apache License 2.0 5 votes vote down vote up
def _enqueue_raw_audio(self, in_data, *_):  # unused args to match expected
    try:
      self._raw_audio_queue.put((in_data, time.time()), block=False)
      return None, pyaudio.paContinue
    except queue.Full:
      error_message = "Raw audio buffer full."
      logger.critical(error_message)
      raise TimeoutError(error_message) 
Example #10
Source File: record.py    From robust_audio_ae with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def callback(in_data, frame_count, time_info, status):
    global frame, indata, outdata
    indata.append(in_data)
    frame += 1
    return (outdata[frame * nchannel * chunk: (frame + 1) * nchannel * chunk], pyaudio.paContinue) 
Example #11
Source File: player.py    From Speaker-Diarization with Apache License 2.0 5 votes vote down vote up
def callback(self, in_data, frame_count, time_info, status):
        data = self.wf.readframes(frame_count)
        self.pos += frame_count
        return (data, pyaudio.paContinue) 
Example #12
Source File: audio.py    From Oe2sSLE with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, data, fmt, esli):
        
        if fmt.formatTag != RIFF.WAVE_fmt_.WAVE_FORMAT_PCM:
            raise Exception()

        if fmt.samplesPerSec < 1000 or fmt.samplesPerSec > 192000:
            data, fmt = wav_tools.wav_resample_preview(data, fmt, 1000, 192000)
        self.data = data
        self.fmt = fmt
        self.esli = esli
        
        self._total_offset = 0
        self._offset = esli.OSC_StartPoint_address
        self._duration = 0

        """
        TODO: use esli.playVolume and esli.playLogScale
        """
        def callback(indata, frames, time, status):
            n_bytes=frames*fmt.blockAlign
            n_read=0
            data=bytearray(n_bytes)
            end=self.esli.OSC_StartPoint_address + self.esli.OSC_EndPoint_offset#+self.fmt.blockAlign
            while n_read < n_bytes:
                to_read =min(n_bytes-n_read, end - self._offset)
                data[n_read:n_read+to_read] = self.data[self._offset:self._offset+to_read]
                n_read += to_read
                self._offset += to_read
                if self._offset == end:
                    if self.esli.OSC_LoopStartPoint_offset < self.esli.OSC_EndPoint_offset:
                        self._offset = self.esli.OSC_StartPoint_address + self.esli.OSC_LoopStartPoint_offset
                    else:
                        break

            if not n_read:
                return (bytes(data),pa.paComplete)
         
            self._total_offset += n_read
            return (bytes(data),pa.paContinue)

        self.stream = audio.open(format=pa.paInt16, channels=fmt.channels, rate=fmt.samplesPerSec, output=True, stream_callback=callback) 
Example #13
Source File: audiostream.py    From rtmonoaudio2midi with GNU General Public License v3.0 5 votes vote down vote up
def _process_frame(self, data, frame_count, time_info, status_flag):
        data_array = np.fromstring(data, dtype=np.int16)
        freq0 = self._spectral_analyser.process_data(data_array)
        if freq0:
            # Onset detected
            print("Note detected; fundamental frequency: ", freq0)
            midi_note_value = int(hz_to_midi(freq0)[0])
            print("Midi note value: ", midi_note_value)
            note = RTNote(midi_note_value, 100, 0.5)
            self._synth.play_note(note)
        return (data, paContinue) 
Example #14
Source File: audio.py    From LedFx with MIT License 5 votes vote down vote up
def _audio_sample_callback(self, in_data, frame_count, time_info, status):
        """Callback for when a new audio sample is acquired"""
        self._raw_audio_sample = np.fromstring(in_data, dtype=np.float32)

        self.pre_process_audio()
        self._invalidate_caches()
        self._invoke_callbacks()

        return (self._raw_audio_sample, pyaudio.paContinue) 
Example #15
Source File: microphone-speech-to-text.py    From python-sdk with Apache License 2.0 5 votes vote down vote up
def pyaudio_callback(in_data, frame_count, time_info, status):
    try:
        q.put(in_data)
    except Full:
        pass # discard
    return (None, pyaudio.paContinue)

# instantiate pyaudio 
Example #16
Source File: demo_streaming_asr.py    From OpenSeq2Seq with Apache License 2.0 5 votes vote down vote up
def callback(in_data, frame_count, time_info, status):
    signal = np.frombuffer(in_data, dtype=np.int16)
    pred = asr.transcribe(signal)
    if len(pred.strip()):
        print('"{}"'.format(pred))
    return (in_data, pa.paContinue) 
Example #17
Source File: pyaudio-wire-callback.py    From Cerebrum with MIT License 5 votes vote down vote up
def callback(in_data, frame_count, time_info, status):
    return (in_data, pyaudio.paContinue) 
Example #18
Source File: listener.py    From pineal with GNU Affero General Public License v3.0 5 votes vote down vote up
def listen(device='default', channels=2, frames_per_buffer=1024, rate=44100):
    global audio_data
    audio_data = np.zeros([channels, frames_per_buffer])

    pa = pyaudio.PyAudio()
    devices = get_devices(pa)
    # print(devices)
    input_device = devices[device]

    def stream_cb(in_data, frame_count, time_info, status):
        data = np.fromstring(in_data, dtype=np.float32)
        data = np.reshape(data, (frame_count, channels))
        data = [data[:, i]
                for i in range(channels)]

        audio_data[:, :] = data

        return (in_data, pyaudio.paContinue)

    stream = pa.open(format=pyaudio.paFloat32,
                     channels=channels,
                     rate=rate,
                     frames_per_buffer=frames_per_buffer,
                     input=True,
                     input_device_index=input_device,
                     stream_callback=stream_cb)

    def idle():
        stream.start_stream()

        try:
            while True:
                sleep(1)
        finally:
            stream.stop_stream()
            stream.close()
            pa.terminate()

    t = Thread(target=idle)
    t.daemon = True
    t.start() 
Example #19
Source File: audio_recorder.py    From magenta with Apache License 2.0 5 votes vote down vote up
def _enqueue_raw_audio(self, in_data, *_):  # unused args to match expected
    try:
      self._raw_audio_queue.put((in_data, time.time()), block=False)
      return None, pyaudio.paContinue
    except queue.Full:
      error_message = 'Raw audio buffer full.'
      logging.error(error_message)
      raise AudioTimeoutError(error_message) 
Example #20
Source File: wire_callback.py    From learning-python with MIT License 5 votes vote down vote up
def callback(in_data, frame_count, time_info, status):
    return (in_data, pyaudio.paContinue) 
Example #21
Source File: snowboydecoder.py    From Melissa-Core with MIT License 4 votes vote down vote up
def __init__(self, decoder_model,
                 resource=RESOURCE_FILE,
                 sensitivity=None,
                 audio_gain=1):
        if sensitivity is None:
            sensitivity = []

        def audio_callback(in_data, frame_count, time_info, status):
            self.ring_buffer.extend(in_data)
            play_data = chr(0) * len(in_data)
            return play_data, pyaudio.paContinue

        tm = type(decoder_model)
        ts = type(sensitivity)
        if tm is not list:
            decoder_model = [decoder_model]
        if ts is not list:
            sensitivity = [sensitivity]
        model_str = ",".join(decoder_model)

        self.detector = snowboydetect.SnowboyDetect(
            resource_filename=resource.encode(), model_str=model_str.encode())
        self.detector.SetAudioGain(audio_gain)
        self.num_hotwords = self.detector.NumHotwords()

        if len(decoder_model) > 1 and len(sensitivity) == 1:
            sensitivity = sensitivity*self.num_hotwords
        if len(sensitivity) != 0:
            assert self.num_hotwords == len(sensitivity), \
                "number of hotwords in decoder_model (%d) and sensitivity " \
                "(%d) does not match" % (self.num_hotwords, len(sensitivity))
        sensitivity_str = ",".join([str(t) for t in sensitivity])
        if len(sensitivity) != 0:
            self.detector.SetSensitivity(sensitivity_str.encode())

        self.ring_buffer = RingBuffer(
            self.detector.NumChannels() * self.detector.SampleRate() * 5)
        self.audio = pyaudio.PyAudio()
        self.stream_in = self.audio.open(
            input=True, output=False,
            format=self.audio.get_format_from_width(
                self.detector.BitsPerSample() / 8),
            channels=self.detector.NumChannels(),
            rate=self.detector.SampleRate(),
            frames_per_buffer=2048,
            stream_callback=audio_callback) 
Example #22
Source File: streaming_microphone.py    From alexa-voice-service-client with MIT License 4 votes vote down vote up
def main(client_id, secret, refresh_token):
    alexa_client = AlexaClient(
        client_id=client_id,
        secret=secret,
        refresh_token=refresh_token,
    )

    p = pyaudio.PyAudio()

    def callback(in_data, frame_count, time_info, status):
        input_buffer.write(in_data)
        return (in_data, pyaudio.paContinue)

    stream = p.open(
        rate=16000,
        channels=1,
        format=pyaudio.paInt16,
        input=True,
        stream_callback=callback,
        frames_per_buffer=128,
        start=False
    )

    dialog_request_id = None

    try:
        print('listening. Press CTRL + C to exit.')
        input_buffer = io.BytesIO()
        stream.start_stream()
        print('Say something to Alexa.')
        alexa_client.connect()
        while True:
            directives = alexa_client.send_audio_file(
                input_buffer,
                dialog_request_id=dialog_request_id
            )
            stream.stop_stream()
            if directives:
                dialog_request_id = None
                print('Alexa\'s turn.')
                for directive in directives:
                    if directive.name == 'ExpectSpeech':
                        dialog_request_id = directive.dialog_request_id
                    if directive.name in ['Speak', 'Play']:
                        output_buffer = io.BytesIO(directive.audio_attachment)
                        track = AudioSegment.from_mp3(output_buffer)
                        play(track)
                input_buffer = io.BytesIO()
            stream.start_stream()
            print('Your turn. Say something.')
            time.sleep(1)
    finally:
        stream.stop_stream()
        stream.close()
        p.terminate() 
Example #23
Source File: snowboydecoder.py    From kalliope with GNU General Public License v3.0 4 votes vote down vote up
def __init__(self, decoder_model, resource=RESOURCE_FILE, sensitivity=[], audio_gain=1, detected_callback=None,
                 interrupt_check=lambda: False, sleep_time=0.03):

        super(HotwordDetector, self).__init__()
        self.detected_callback = detected_callback
        self.interrupt_check = interrupt_check
        self.sleep_time = sleep_time
        self.kill_received = False
        self.paused = False

        def audio_callback(in_data, frame_count, time_info, status):
            self.ring_buffer.extend(in_data)
            play_data = chr(0) * len(in_data)
            return play_data, pyaudio.paContinue

        tm = type(decoder_model)
        ts = type(sensitivity)
        if tm is not list:
            decoder_model = [decoder_model]
        if ts is not list:
            sensitivity = [sensitivity]
        model_str = ",".join(decoder_model)

        self.detector = snowboydetect.SnowboyDetect(
            resource_filename=resource.encode(), model_str=model_str.encode())
        self.detector.SetAudioGain(audio_gain)
        self.num_hotwords = self.detector.NumHotwords()

        if len(decoder_model) > 1 and len(sensitivity) == 1:
            sensitivity = sensitivity*self.num_hotwords
        if len(sensitivity) != 0:
            assert self.num_hotwords == len(sensitivity), \
                "number of hotwords in decoder_model (%d) and sensitivity " \
                "(%d) does not match" % (self.num_hotwords, len(sensitivity))
        sensitivity_str = ",".join([str(t) for t in sensitivity])
        if len(sensitivity) != 0:
            self.detector.SetSensitivity(sensitivity_str.encode())

        self.ring_buffer = RingBuffer(
            self.detector.NumChannels() * self.detector.SampleRate() * 5)
        self.audio = pyaudio.PyAudio()
        self.open_audio(audio_callback) 
Example #24
Source File: snowboydecoder.py    From snips-custom-hotword with GNU General Public License v3.0 4 votes vote down vote up
def __init__(self, decoder_model,
                 resource=RESOURCE_FILE,
                 sensitivity=[],
                 audio_gain=1):

        def audio_callback(in_data, frame_count, time_info, status):
            self.ring_buffer.extend(in_data)
            play_data = chr(0) * len(in_data)
            return play_data, pyaudio.paContinue

        tm = type(decoder_model)
        ts = type(sensitivity)
        if tm is not list:
            decoder_model = [decoder_model]
        if ts is not list:
            sensitivity = [sensitivity]
        model_str = ",".join(decoder_model)

        self.detector = snowboydetect.SnowboyDetect(
            resource_filename=resource.encode(), model_str=model_str.encode())
        self.detector.SetAudioGain(audio_gain)
        #this code is used when test universal model
        #self.detector.ApplyFrontend(True)
        self.num_hotwords = self.detector.NumHotwords()

        if len(decoder_model) > 1 and len(sensitivity) == 1:
            sensitivity = sensitivity*self.num_hotwords
        if len(sensitivity) != 0:
            assert self.num_hotwords == len(sensitivity), \
                "number of hotwords in decoder_model (%d) and sensitivity " \
                "(%d) does not match" % (self.num_hotwords, len(sensitivity))
        sensitivity_str = ",".join([str(t) for t in sensitivity])
        if len(sensitivity) != 0:
            self.detector.SetSensitivity(sensitivity_str.encode())

        self.ring_buffer = RingBuffer(
            self.detector.NumChannels() * self.detector.SampleRate() * 5)
        self.audio = pyaudio.PyAudio()
        self.stream_in = self.audio.open(
            input=True, output=False,
            format=self.audio.get_format_from_width(
                self.detector.BitsPerSample() / 8),
            channels=self.detector.NumChannels(),
            rate=self.detector.SampleRate(),
            frames_per_buffer=2048,
            stream_callback=audio_callback) 
Example #25
Source File: snowboydecoder.py    From GassistPi with GNU General Public License v3.0 4 votes vote down vote up
def __init__(self, decoder_model,
                 resource=RESOURCE_FILE,
                 sensitivity=[],
                 audio_gain=1):

        def audio_callback(in_data, frame_count, time_info, status):
            self.ring_buffer.extend(in_data)
            play_data = chr(0) * len(in_data)
            return play_data, pyaudio.paContinue

        tm = type(decoder_model)
        ts = type(sensitivity)
        if tm is not list:
            decoder_model = [decoder_model]
        if ts is not list:
            sensitivity = [sensitivity]
        model_str = ",".join(decoder_model)

        self.detector = snowboydetect.SnowboyDetect(
            resource_filename=resource.encode(), model_str=model_str.encode())
        self.detector.SetAudioGain(audio_gain)
        self.num_hotwords = self.detector.NumHotwords()

        if len(decoder_model) > 1 and len(sensitivity) == 1:
            sensitivity = sensitivity*self.num_hotwords
        if len(sensitivity) != 0:
            assert self.num_hotwords == len(sensitivity), \
                "number of hotwords in decoder_model (%d) and sensitivity " \
                "(%d) does not match" % (self.num_hotwords, len(sensitivity))
        sensitivity_str = ",".join([str(t) for t in sensitivity])
        if len(sensitivity) != 0:
            self.detector.SetSensitivity(sensitivity_str.encode())

        self.ring_buffer = RingBuffer(
            self.detector.NumChannels() * self.detector.SampleRate() * 5)
        self.audio = pyaudio.PyAudio()
        self.stream_in = self.audio.open(
            input=True, output=False,
            format=self.audio.get_format_from_width(
                self.detector.BitsPerSample() / 8),
            channels=self.detector.NumChannels(),
            rate=self.detector.SampleRate(),
            frames_per_buffer=2048,
            stream_callback=audio_callback) 
Example #26
Source File: streaming_transcribe.py    From rnnt-speech-recognition with MIT License 4 votes vote down vote up
def main(args):

    model_dir = os.path.dirname(os.path.realpath(args.checkpoint))

    hparams = model_utils.load_hparams(model_dir)

    _, tok_to_text, vocab_size = encoding.get_encoder(
        encoder_dir=model_dir,
        hparams=hparams)
    hparams[HP_VOCAB_SIZE.name] = vocab_size

    model = build_keras_model(hparams, stateful=True)
    model.load_weights(args.checkpoint)

    decoder_fn = decoding.greedy_decode_fn(model, hparams)

    p = pyaudio.PyAudio()

    def listen_callback(in_data, frame_count, time_info, status):
        global LAST_OUTPUT

        audio = tf.io.decode_raw(in_data, out_type=tf.float32)

        log_melspec = preprocessing.preprocess_audio(
            audio=audio,
            sample_rate=SAMPLE_RATE,
            hparams=hparams)
        log_melspec = tf.expand_dims(log_melspec, axis=0)

        decoded = decoder_fn(log_melspec)[0]

        transcription = LAST_OUTPUT + tok_to_text(decoded)\
            .numpy().decode('utf8')

        if transcription != LAST_OUTPUT:
            LAST_OUTPUT = transcription
            print(transcription)

        return in_data, pyaudio.paContinue

    stream = p.open(
        format=pyaudio.paFloat32,
        channels=NUM_CHANNELS,
        rate=SAMPLE_RATE,
        input=True,
        frames_per_buffer=CHUNK_SIZE,
        stream_callback=listen_callback)

    print('Listening...')

    stream.start_stream()

    while stream.is_active():
        time.sleep(0.1)

    stream.stop_stream()
    stream.close()

    p.terminate() 
Example #27
Source File: snowboydecoder.py    From dialogflow_ros with MIT License 4 votes vote down vote up
def __init__(self, decoder_model,
                 resource=RESOURCE_FILE,
                 sensitivity=[],
                 audio_gain=1):

        def audio_callback(in_data, frame_count, time_info, status):
            self.ring_buffer.extend(in_data)
            play_data = chr(0) * len(in_data)
            return play_data, pyaudio.paContinue

        tm = type(decoder_model)
        ts = type(sensitivity)
        if tm is not list:
            decoder_model = [decoder_model]
        if ts is not list:
            sensitivity = [sensitivity]
        model_str = ",".join(decoder_model)

        self.detector = snowboydetect.SnowboyDetect(
            resource_filename=resource.encode(), model_str=model_str.encode())
        self.detector.SetAudioGain(audio_gain)
        self.num_hotwords = self.detector.NumHotwords()

        if len(decoder_model) > 1 and len(sensitivity) == 1:
            sensitivity = sensitivity*self.num_hotwords
        if len(sensitivity) != 0:
            assert self.num_hotwords == len(sensitivity), \
                "number of hotwords in decoder_model (%d) and sensitivity " \
                "(%d) does not match" % (self.num_hotwords, len(sensitivity))
        sensitivity_str = ",".join([str(t) for t in sensitivity])
        if len(sensitivity) != 0:
            self.detector.SetSensitivity(sensitivity_str.encode())

        self.ring_buffer = RingBuffer(
            self.detector.NumChannels() * self.detector.SampleRate() * 5)
        self.audio = pyaudio.PyAudio()
        self.stream_in = self.audio.open(
            input=True, output=False,
            format=self.audio.get_format_from_width(
                self.detector.BitsPerSample() / 8),
            channels=self.detector.NumChannels(),
            rate=self.detector.SampleRate(),
            frames_per_buffer=2048,
            stream_callback=audio_callback)