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