本文整理汇总了Python中pyaudio.PyAudio.get_device_info_by_index方法的典型用法代码示例。如果您正苦于以下问题:Python PyAudio.get_device_info_by_index方法的具体用法?Python PyAudio.get_device_info_by_index怎么用?Python PyAudio.get_device_info_by_index使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyaudio.PyAudio
的用法示例。
在下文中一共展示了PyAudio.get_device_info_by_index方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import get_device_info_by_index [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
示例2: __init__
# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import get_device_info_by_index [as 别名]
def __init__(self):
super(VCGame, self).__init__(255, 255, 255, 255, 800, 600)
# 初始化参数
# frames_per_buffer
self.numSamples = 1000
# 声控条
self.vbar = Sprite('black.png')
self.vbar.position = 20, 450
self.vbar.scale_y = 0.1
self.vbar.image_anchor = 0, 0
self.add(self.vbar)
# 皮卡丘类
self.pikachu = Pikachu()
self.add(self.pikachu)
# cocosnode精灵类
self.floor = cocos.cocosnode.CocosNode()
self.add(self.floor)
position = 0, 100
for i in range(120):
b = Block(position)
self.floor.add(b)
position = b.x + b.width, b.height
# 声音输入
audio = PyAudio()
SampleRate = int(audio.get_device_info_by_index(0)['defaultSampleRate'])
self.stream = audio.open(format=paInt16,
channels=1,
rate=SampleRate,
input=True,
frames_per_buffer=self.numSamples)
self.schedule(self.update)
示例3: record
# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import get_device_info_by_index [as 别名]
def record(self, time, device_i=None):
audio = PyAudio()
print audio.get_device_info_by_index(1)
stream = audio.open(input_device_index=device_i,output_device_index=device_i,format=self.format, channels=self.channel,
rate=self.rate, input=True,
frames_per_buffer=self.chunk)
print "REC: "
frames = []
for i in range(0, self.rate / self.chunk * time):
data = stream.read(self.chunk)
frames.append(data)
stream.stop_stream()
print "END"
stream.close()
audio.terminate()
write_frames = open_audio(self.file, 'wb')
write_frames.setnchannels(self.channel)
write_frames.setsampwidth(audio.get_sample_size(self.format))
write_frames.setframerate(self.rate)
write_frames.writeframes(''.join(frames))
write_frames.close()
self.convert()
示例4: ears_setup
# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import get_device_info_by_index [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
示例5: RecordWave
# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import get_device_info_by_index [as 别名]
def RecordWave():
pa = PyAudio()
devinfo = pa.get_device_info_by_index(1)
'''
if pa.is_format_supported(8000, input_device=devinfo['index'],
input_channels=devinfo['maxInputChannels'],
input_format=paInt16):
print 'Yes'
'''
stream = pa.open(format=paInt16, channels=1, rate=framerate, input=True,
frames_per_buffer = NUM_SAMPLES)
saveBuffer = []
count = 0
print 'please say anything'
while count < TIME*2:
stringAudioData = stream.read(NUM_SAMPLES)
saveBuffer.append(stringAudioData)
count += 1
filename = datetime.now().strftime("2")+".wav"
SaveWaveFile(filename, saveBuffer)
print filename, "saved"
示例6: AudioDevice
# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import get_device_info_by_index [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()
#.........这里部分代码省略.........
示例7: AudioBackend
# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import get_device_info_by_index [as 别名]
class AudioBackend(QtCore.QObject):
underflow = QtCore.pyqtSignal()
new_data_available_from_callback = QtCore.pyqtSignal(bytes, int, float, int)
new_data_available = QtCore.pyqtSignal(ndarray, float, int)
def callback(self, in_data, frame_count, time_info, status):
#do the minimum from here to prevent overflows, just pass the data to the main thread
input_time = time_info['input_buffer_adc_time']
# some API drivers in PortAudio do not return a valid time, so fallback to the current stream time
if input_time == 0.:
input_time = time_info['current_time']
if input_time == 0.:
input_time = self.stream.get_time()
self.new_data_available_from_callback.emit(in_data, frame_count, input_time, status)
return (None, 0)
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()
self.device = None
self.first_channel = None
self.second_channel = None
# we will try to open all the input devices until one
# works, starting by the default input device
for device in self.input_devices:
self.logger.push("Opening the stream")
try:
self.stream = self.open_stream(device)
self.stream.start_stream()
self.device = device
self.logger.push("Success")
break
except:
self.logger.push("Fail")
if self.device is not None:
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
self.chunk_number = 0
self.new_data_available_from_callback.connect(self.handle_new_data)
def close(self):
self.stream.stop_stream()
self.stream.close()
self.stream = None
# 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()
#.........这里部分代码省略.........