Beispiel #1
0
 def get_input(self):
     p = PyAudio()
     info = p.get_host_api_info_by_index(0)
     numdevices = info.get('deviceCount')
     devices = []
     for i in range(0, numdevices):
         devices.append(p.get_device_info_by_host_api_device_index(0, i))
         print("Input Device id ", i, " - ", p.get_device_info_by_host_api_device_index(0, i).get('name'))
     return devices[1]
Beispiel #2
0
def _config_pyaudio_mic(audio: pyaudio.PyAudio):
    """Do one-time setup of PyAudio to work with the system mic."""
    print('----------------------record device list---------------------')
    info = audio.get_host_api_info_by_index(0)
    numdevices = info.get('deviceCount')
    for i in range(0, numdevices):
        if (audio.get_device_info_by_host_api_device_index(
                0, i).get('maxInputChannels')) > 0:
            print(
                'Input Device id ', i, ' - ',
                audio.get_device_info_by_host_api_device_index(0,
                                                               i).get('name'))

    print('-------------------------------------------------------------')
    cfg['portaudio_device_index'] = int(input())
    print('recording via index ' + str(cfg['portaudio_device_index']))
def get_pulse_device():
    """
    Detects pulse device

    :return: id of the pulse device
    :rtype: int
    """
    # print("The following sound devices are available.")
    p = PyAudio()
    info = p.get_host_api_info_by_index(0)
    numdevices = info.get("deviceCount")
    audio_device = 0
    for i in range(0, numdevices):
        if (p.get_device_info_by_host_api_device_index(
                0, i).get("maxOutputChannels")) > 0:
            device_name = p.get_device_info_by_host_api_device_index(
                0, i).get("name")
            # print("Input Device id ", i, " - ", device_name)
            if device_name.find("pulse") != -1:
                audio_device = i
    return audio_device
Beispiel #4
0
def check_devices():
    """Imprime los puertos de entrada y salida con nombre e índice.
    
    Basado en: https://stackoverflow.com/questions/36894315/how-to-select-a-specific-input-device-with-pyaudio
    """

    from pyaudio import PyAudio

    p = PyAudio()

    info = p.get_host_api_info_by_index(0)

    numdevices = info.get('deviceCount')

    for i in range(0, numdevices):

        if (p.get_device_info_by_host_api_device_index(
                0, i).get('maxInputChannels')) > 0:
            print("Input Device id ", i, " - ",
                  p.get_device_info_by_host_api_device_index(0, i).get('name'))

        else:
            print("Output Device id ", i, " - ",
                  p.get_device_info_by_host_api_device_index(0, i).get('name'))
Beispiel #5
0
 def __init__(self,
              pa: pyaudio.PyAudio,
              rate: int,
              channels: int,
              format: int,
              *args,
              input_device_index: Optional[int] = None,
              output_device_index: Optional[int] = None,
              **kwargs):
     self.indi: Optional[int] = input_device_index
     self.indo: Optional[int] = output_device_index
     self.name: str = pa.get_device_info_by_host_api_device_index(
         0, self.indi if self.indi is not None else self.indo)['name']
     super(Device, self).__init__(pa,
                                  *args,
                                  rate=rate,
                                  channels=channels,
                                  format=format,
                                  *args,
                                  input_device_index=input_device_index,
                                  output_device_index=output_device_index,
                                  **kwargs)
     pa._streams.add(self)
class MainWindow(QtGui.QMainWindow):
    """ A Qt QMainWindow that is home to a matplotlib figure and two combo
    boxes. The combo boxes allow the selection of a sound card by API and
    name. The figure will show the waveform of the audio input of that sound
    card.
    """
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        # Monkey patch missing methods into PyAudio.
        PyAudio.device_index_to_host_api_device_index = (
            device_index_to_host_api_device_index)

        self.pyaudio = PyAudio()

        # Create the UI widgets.
        central_widget = QtGui.QWidget(self)
        self.setCentralWidget(central_widget)
        main_layout = QtGui.QVBoxLayout(central_widget)
        self.figure = FigureWidget()
        main_layout.addWidget(self.figure)
        horizontal_layout = QtGui.QHBoxLayout()
        main_layout.addLayout(horizontal_layout)
        api_list = QtGui.QComboBox()
        api_list.setModel(APIListModel(self.pyaudio))
        horizontal_layout.addWidget(api_list)
        device_list = QtGui.QComboBox()
        device_list_model = DeviceListModel(self.pyaudio)
        device_list.setModel(device_list_model)
        horizontal_layout.addWidget(device_list)

        # Connect the moving parts
        api_list.currentIndexChanged.connect(device_list_model.set_api_index)
        api_list.currentIndexChanged.connect(self.change_api_index)
        device_list.currentIndexChanged.connect(self.change_device_index)

        # Tell all widgets to use the default audio device.
        default_api_index = (
            self.pyaudio.get_default_input_device_info()["hostApi"])
        default_device_index = (
            self.pyaudio.device_index_to_host_api_device_index(
                self.pyaudio.get_default_host_api_info()["defaultInputDevice"],
                default_api_index))
        self.api_index = default_api_index
        self.device_index = default_device_index
        self.stream = None
        api_list.setCurrentIndex(default_api_index)
        device_list_model.set_api_index(default_api_index)
        device_list.setCurrentIndex(default_device_index)

    def closeEvent(self, event):
        """ Called by Qt when the program quits. Stops audio processing. """
        self.stream.close()
        # wait for audio processing to clear its buffers
        time.sleep(0.1)

    def change_api_index(self, api_index):
        """ Restarts audio processing with new index. """
        self.api_index = api_index
        self.restart_audio()

    def change_device_index(self, device_index):
        """ Restarts audio processing with new index. """
        self.device_index = device_index
        self.restart_audio()

    def restart_audio(self):
        """ Restarts audio processing with current API and device indices. """
        device_info = (self.pyaudio.get_device_info_by_host_api_device_index(
            self.api_index, self.device_index))
        self.num_channels = device_info['maxInputChannels']

        if self.stream:
            self.stream.close()
        self.stream = self.pyaudio.open(
            rate=int(device_info['defaultSampleRate']),
            channels=self.num_channels,
            input_device_index=device_info['index'],
            format=paFloat32,
            input=True,
            stream_callback=self.audio_callback)
        self.figure.create_plots(self.num_channels)

    def audio_callback(self, in_data, frame_count, time_info, status_flags):
        """ Called by pyaudio whenever audio data is available.
        Updates the matplotlib figure.
        """
        data = numpy.fromstring(in_data, dtype=numpy.float32)
        data = numpy.reshape(
            data, (len(data) / self.num_channels, self.num_channels))
        self.figure.draw(data)
        return (None, paContinue)
Beispiel #7
0
from pyaudio import PyAudio

p = PyAudio()
info = p.get_host_api_info_by_index(0)
numdevices = info.get('deviceCount')
for i in range(0, numdevices):
    if (p.get_device_info_by_host_api_device_index(0, i).get('maxInputChannels')) > 0:
        print("Input Device id ", i, " - ", p.get_device_info_by_host_api_device_index(0, i).get('name'))
class MainWindow(QtGui.QMainWindow):
    """ A Qt QMainWindow that is home to a matplotlib figure and two combo
    boxes. The combo boxes allow the selection of a sound card by API and
    name. The figure will show the waveform of the audio input of that sound
    card.
    """

    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        # Monkey patch missing methods into PyAudio.
        PyAudio.device_index_to_host_api_device_index = (
            device_index_to_host_api_device_index)

        self.pyaudio = PyAudio()

        # Create the UI widgets.
        central_widget = QtGui.QWidget(self)
        self.setCentralWidget(central_widget)
        main_layout = QtGui.QVBoxLayout(central_widget)
        self.figure = FigureWidget()
        main_layout.addWidget(self.figure)
        horizontal_layout = QtGui.QHBoxLayout()
        main_layout.addLayout(horizontal_layout)
        api_list = QtGui.QComboBox()
        api_list.setModel(APIListModel(self.pyaudio))
        horizontal_layout.addWidget(api_list)
        device_list = QtGui.QComboBox()
        device_list_model = DeviceListModel(self.pyaudio)
        device_list.setModel(device_list_model)
        horizontal_layout.addWidget(device_list)

        # Connect the moving parts
        api_list.currentIndexChanged.connect(device_list_model.set_api_index)
        api_list.currentIndexChanged.connect(self.change_api_index)
        device_list.currentIndexChanged.connect(self.change_device_index)

        # Tell all widgets to use the default audio device.
        default_api_index = (
            self.pyaudio.get_default_input_device_info()["hostApi"])
        default_device_index = (
            self.pyaudio.device_index_to_host_api_device_index(
                self.pyaudio.get_default_host_api_info()["defaultInputDevice"],
                default_api_index))
        self.api_index = default_api_index
        self.device_index = default_device_index
        self.stream = None
        api_list.setCurrentIndex(default_api_index)
        device_list_model.set_api_index(default_api_index)
        device_list.setCurrentIndex(default_device_index)

    def closeEvent(self, event):
        """ Called by Qt when the program quits. Stops audio processing. """
        self.stream.close()
        # wait for audio processing to clear its buffers
        time.sleep(0.1)

    def change_api_index(self, api_index):
        """ Restarts audio processing with new index. """
        self.api_index = api_index
        self.restart_audio()

    def change_device_index(self, device_index):
        """ Restarts audio processing with new index. """
        self.device_index = device_index
        self.restart_audio()

    def restart_audio(self):
        """ Restarts audio processing with current API and device indices. """
        device_info = (
            self.pyaudio.get_device_info_by_host_api_device_index(self.api_index,
                                                                  self.device_index))
        self.num_channels = device_info['maxInputChannels']

        if self.stream:
            self.stream.close()
        self.stream = self.pyaudio.open(
            rate=int(device_info['defaultSampleRate']),
            channels=self.num_channels,
            input_device_index=device_info['index'],
            format=paFloat32,
            input=True,
            stream_callback=self.audio_callback)
        self.figure.create_plots(self.num_channels)

    def audio_callback(self, in_data, frame_count, time_info, status_flags):
        """ Called by pyaudio whenever audio data is available.
        Updates the matplotlib figure.
        """
        data = numpy.fromstring(in_data, dtype=numpy.float32)
        data = numpy.reshape(data, (len(data)/self.num_channels,self.num_channels))
        self.figure.draw(data)
        return (None, paContinue)
Beispiel #9
0
class PreciseRunner(object):
    """
    Wrapper to use Precise. Example:
    >>> def on_act():
    ...     print('Activation!')
    ...
    >>> p = PreciseRunner(PreciseEngine('./precise-engine'), on_activation=on_act)
    >>> p.start()
    >>> from time import sleep; sleep(10)
    >>> p.stop()

    Args:
        engine (Engine): Object containing info on the binary engine
        trigger_level (int): Number of chunk activations needed to trigger on_activation
                       Higher values add latency but reduce false positives
        sensitivity (float): From 0.0 to 1.0, relates to the network output level required
                             to consider a chunk "active"
        stream (BinaryIO): Binary audio stream to read 16000 Hz 1 channel int16
                           audio from. If not given, the microphone is used
        on_prediction (Callable): callback for every new prediction
        on_activation (Callable): callback for when the wake word is heard
    """
    def __init__(self,
                 engine,
                 trigger_level=3,
                 sensitivity=0.5,
                 stream=None,
                 on_prediction=lambda x: None,
                 on_activation=lambda: None):
        self.engine = engine
        self.trigger_level = trigger_level
        self.sensitivity = sensitivity
        self.stream = stream
        self.on_prediction = on_prediction
        self.on_activation = on_activation
        self.chunk_size = engine.chunk_size
        self.read_divisor = 1

        print('sensitivity: ', self.sensitivity)

        self.pa = None
        self.thread = None
        self.running = False
        self.is_paused = False
        self.detector = TriggerDetector(self.chunk_size, sensitivity,
                                        trigger_level)
        atexit.register(self.stop)

    def _calc_read_divisor(self):
        """
        pyaudio.Stream.read takes samples as n, not bytes
        so read(n) should be read(n // sample_depth
        """
        try:
            import pyaudio
            if isinstance(self.stream, pyaudio.Stream):
                return 2
        except ImportError:
            pass
        return 1

    def start(self):
        """Start listening from stream"""
        if self.stream is None:
            from pyaudio import PyAudio, paInt16
            self.pa = PyAudio()

            print("================================================")
            info = self.pa.get_host_api_info_by_index(0)
            numdevices = info.get('deviceCount')
            for i in range(0, numdevices):
                if (self.pa.get_device_info_by_host_api_device_index(
                        0, i).get('maxInputChannels')) > 0:
                    print(
                        "Input Device id ", i, " - ",
                        self.pa.get_device_info_by_host_api_device_index(
                            0, i).get('name'),
                        self.pa.get_device_info_by_index(i).get(
                            'defaultSampleRate'))
            print("================================================")

            self.stream = self.pa.open(rate=16000,
                                       channels=1,
                                       format=paInt16,
                                       input=True,
                                       frames_per_buffer=self.chunk_size,
                                       input_device_index=2)

        self.read_divisor = self._calc_read_divisor()

        self.engine.start()
        self.running = True
        self.is_paused = False
        self.thread = Thread(target=self._handle_predictions)
        self.thread.daemon = True
        self.thread.start()

    def stop(self):
        """Stop listening and close stream"""
        if self.thread:
            self.running = False
            if isinstance(self.stream, ReadWriteStream):
                self.stream.write(b'\0' * self.chunk_size)
            self.thread.join()
            self.thread = None

        self.engine.stop()

        if self.pa:
            self.pa.terminate()
            self.stream.stop_stream()
            self.stream = self.pa = None

    def pause(self):
        self.is_paused = True

    def play(self):
        self.is_paused = False

    def _handle_predictions(self):
        """Continuously check Precise process output"""
        while self.running:
            # print('chunk_size: ', self.chunk_size, ' read_divisor: ', self.read_divisor)
            chunk = self.stream.read(self.chunk_size // self.read_divisor,
                                     exception_on_overflow=False)

            if self.is_paused:
                continue

            prob = self.engine.get_prediction(chunk)
            self.on_prediction(prob)
            if self.detector.update(prob):
                self.on_activation()