当前位置: 首页>>代码示例>>Python>>正文


Python PyAudio.get_default_input_device_info方法代码示例

本文整理汇总了Python中pyaudio.PyAudio.get_default_input_device_info方法的典型用法代码示例。如果您正苦于以下问题:Python PyAudio.get_default_input_device_info方法的具体用法?Python PyAudio.get_default_input_device_info怎么用?Python PyAudio.get_default_input_device_info使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pyaudio.PyAudio的用法示例。


在下文中一共展示了PyAudio.get_default_input_device_info方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: AudioStream

# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import get_default_input_device_info [as 别名]
class AudioStream(object):

    def __init__(self, sample_rate=44100, channels=1, width=2, chunk=1024,
                 input_device_index=None):
        self.sample_rate = sample_rate
        self.channels = channels
        self.width = width
        self.chunk = chunk
        self.input_device_index = input_device_index

    def __enter__(self):
        self._pa = PyAudio()
        if self.input_device_index is None:
            self.input_device_index = \
                self._pa.get_default_input_device_info()['index']
        self._stream = self._pa.open(
            format=self._pa.get_format_from_width(self.width),
            channels=self.channels,
            rate=self.sample_rate,
            input=True,
            frames_per_buffer=self.chunk,
            input_device_index=self.input_device_index)
        self._stream.start_stream()
        return self

    def read(self):
        ''' On a buffer overflow this returns 0 bytes. '''
        try:
            return self._stream.read(self.chunk)
        except IOError:
            return ''
        except AttributeError:
            raise Exception('Must be used as a context manager.')

    def stream(self):
        try:
            while True:
                bytes = self.read()
                if bytes:
                    self.handle(bytes)
        except (KeyboardInterrupt, SystemExit):
            pass

    def __exit__(self, type, value, traceback):
        self._stream.stop_stream()
        self._stream.close()
        self._pa.terminate()

    def handle(self, bytes):
        pass
开发者ID:shomchak,项目名称:lissajous,代码行数:52,代码来源:__init__.py

示例2: AudioDevice

# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import get_default_input_device_info [as 别名]
class AudioDevice(QtCore.QObject):
    def __init__(self, logger):
        QtCore.QObject.__init__(self)
        self.logger = logger
        self.duo_input = False
        self.logger.push("Initializing PyAudio")
        self.pa = PyAudio()

        # look for devices
        self.input_devices = self.get_input_devices()
        self.output_devices = self.get_output_devices()

        for device in self.input_devices:
            self.logger.push("Opening the stream")
            self.stream = self.open_stream(device)
            self.device = device

            self.logger.push("Trying to read from input device %d" % device)
            if self.try_input_stream(self.stream):
                self.logger.push("Success")
                break
            else:
                self.logger.push("Fail")

        self.first_channel = 0
        nchannels = self.get_current_device_nchannels()
        if nchannels == 1:
            self.second_channel = 0
        else:
            self.second_channel = 1

        # counter for the number of input buffer overflows
        self.xruns = 0

            # method

    def get_readable_devices_list(self):
        devices_list = []

        default_device_index = self.get_default_input_device()

        for device in self.input_devices:
            dev_info = self.pa.get_device_info_by_index(device)
            api = self.pa.get_host_api_info_by_index(dev_info
                                                     ['hostApi'])['name']

            if device is default_device_index:
                extra_info = ' (system default)'
            else:
                extra_info = ''

            nchannels = self.pa.get_device_info_by_index(device)[
                                                    'maxInputChannels']

            desc = "%s (%d channels) (%s) %s" % (dev_info['name'],
                                                nchannels, api, extra_info)

            devices_list += [desc]

        return devices_list

    # method
    def get_readable_output_devices_list(self):
        devices_list = []

        default_device_index = self.get_default_output_device()

        for device in self.output_devices:
            dev_info = self.pa.get_device_info_by_index(device)
            api = self.pa.get_host_api_info_by_index(dev_info['hostApi']
                                                     )['name']

            if device is default_device_index:
                extra_info = ' (system default)'
            else:
                extra_info = ''

            nchannels = self.pa.get_device_info_by_index(device)[
                                                    'maxOutputChannels']

            desc = "%s (%d channels) (%s) %s" % (dev_info['name'], nchannels,
                                                 api, extra_info)

            devices_list += [desc]

        return devices_list

    # method
    def get_default_input_device(self):
        return self.pa.get_default_input_device_info()['index']

    # method
    def get_default_output_device(self):
        return self.pa.get_default_output_device_info()['index']

    # method
    def get_device_count(self):
        # FIXME only input devices should be chosen, not all of them !
        return self.pa.get_device_count()

#.........这里部分代码省略.........
开发者ID:benni3456,项目名称:audio_analyser,代码行数:103,代码来源:audio_device.py

示例3: MainWindow

# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import get_default_input_device_info [as 别名]
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)
开发者ID:andimarafioti,项目名称:python-oscillator,代码行数:95,代码来源:oscillator.py

示例4: AudioBackend

# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import get_default_input_device_info [as 别名]

#.........这里部分代码省略.........

			desc = "%s (%d channels) (%s) %s" %(dev_info['name'], nchannels, api, extra_info)
			
			devices_list += [desc]

		return devices_list

	# method
	def get_readable_output_devices_list(self):
		devices_list = []
		
		default_device_index = self.get_default_output_device()
		
		for device in self.output_devices:
			dev_info = self.pa.get_device_info_by_index(device)
			api = self.pa.get_host_api_info_by_index(dev_info['hostApi'])['name']

			if device is default_device_index:
				extra_info = ' (system default)'
			else:
				extra_info = ''
			
			nchannels = self.pa.get_device_info_by_index(device)['maxOutputChannels']

			desc = "%s (%d channels) (%s) %s" %(dev_info['name'], nchannels, api, extra_info)
			
			devices_list += [desc]

		return devices_list

	# method
	def get_default_input_device(self):
		try:
			index = self.pa.get_default_input_device_info()['index']
		except IOError:
			index = None

		return index

	# method
	def get_default_output_device(self):
		try:
			index = self.pa.get_default_output_device_info()['index']
		except IOError:
			index = None
		return

	# method
	def get_device_count(self):
		# FIXME only input devices should be chosen, not all of them !
		return self.pa.get_device_count()

	# method
	# returns a list of input devices index, starting with the system default
	def get_input_devices(self):
		device_count = self.get_device_count()
		device_range = list(range(0, device_count))

		default_input_device = self.get_default_input_device()

		if default_input_device	is not None:
			# start by the default input device
			device_range.remove(default_input_device)
			device_range = [default_input_device] + device_range

		# select only the input devices by looking at the number of input channels
开发者ID:claypipkin,项目名称:friture,代码行数:70,代码来源:audiobackend.py


注:本文中的pyaudio.PyAudio.get_default_input_device_info方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。