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


Python PyAudio.get_device_count方法代码示例

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


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

示例1: __init__

# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import get_device_count [as 别名]
 def __init__(self, file="audio"):
     self.format = paInt16
     audio = PyAudio()
     if hasattr(settings, 'AUDIO_DEVICE_INDEX'):
         self.device_index = settings.AUDIO_DEVICE_INDEX
     elif hasattr(settings, 'AUDIO_DEVICE'):
         for i in range(audio.get_device_count()):
             curr_device = audio.get_device_info_by_index(i)
             print 'Found device: %s' % curr_device['name']
             if curr_device['name'] == settings.AUDIO_DEVICE:
                 print 'Assigning %s (Index: %s)' % (
                     settings.AUDIO_DEVICE, i
                 )
                 self.device_index = i
     elif not hasattr(self, 'device_index'):
         print 'No Audio device specified. Discovering...'
         for i in range(audio.get_device_count()):
             curr_device = audio.get_device_info_by_index(i)
             print 'Found device: %s' % curr_device['name']
             if curr_device['maxInputChannels'] > 0:
                 self.device_index = curr_device['index']
                 print 'Using device: %s' % curr_device['name']
                 break
     print audio.get_device_info_by_index(self.device_index)
     try:
         device = audio.get_device_info_by_index(self.device_index)
         calc_rate = device['defaultSampleRate']
         print 'Discovered Sample Rate: %s' % calc_rate
         self.rate = int(calc_rate)
     except:
         print 'Guessing sample rate of 44100'
         self.rate = 44100
     self.channel = 1
     self.chunk = 1024
     self.file = file
开发者ID:AnanthaRajuC,项目名称:homecontrol,代码行数:37,代码来源:pygsr.py

示例2: ears_setup

# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import get_device_count [as 别名]
def ears_setup():
    p = PyAudio()
    count = p.get_device_count()
    device = [i for i in range(count) if "Logitech" in p.get_device_info_by_index(i)["name"]][0]

    source = Microphone(device_index=device)
    # yup, I'm playing with the internals of this class.
    source.CHUNK = 512
    source.RATE = 8000
    source.CHANNELS = 1
    try:
        source.__enter__()
        source.stream.stop_stream()
    except:
        vprint(1, "Microphone initialization failed.")
        source.__exit__()

    return source
开发者ID:fangfx,项目名称:JarvisTuring,代码行数:20,代码来源:ears.py

示例3: AudioDevice

# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import get_device_count [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

示例4: AudioBackend

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

#.........这里部分代码省略.........
			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
		input_devices = []
		for device in device_range:
			n_input_channels = self.pa.get_device_info_by_index(device)['maxInputChannels']
			if n_input_channels > 0:
				input_devices += [device]

		return input_devices

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

		default_output_device = self.get_default_output_device()
开发者ID:claypipkin,项目名称:friture,代码行数:70,代码来源:audiobackend.py


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