本文整理汇总了Python中pyaudio.paInt32方法的典型用法代码示例。如果您正苦于以下问题:Python pyaudio.paInt32方法的具体用法?Python pyaudio.paInt32怎么用?Python pyaudio.paInt32使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyaudio
的用法示例。
在下文中一共展示了pyaudio.paInt32方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import pyaudio [as 别名]
# 或者: from pyaudio import paInt32 [as 别名]
def __init__(self,
audio_device=0,
audio_format=pyaudio.paInt32,
rate=44100,
chunk_size=4096,
pause_threshold_seconds=1.0,
include_before_seconds=0.5,
include_after_seconds=0.5,
add_silence_seconds=0.5,
init_energy_threshold=1000000,
energy_damping=0.7):
self.audio_device = audio_device
self.audio_format = audio_format
self.rate = rate
self.chunk_size = chunk_size
self.session = pyaudio.PyAudio()
self.sample_width = self.session.get_sample_size(audio_format)
self.add_silence_seconds = add_silence_seconds
self.seconds_per_buffer = float(chunk_size)/rate
self.include_before = include_before_seconds/self.seconds_per_buffer
self.include_before = int(math.ceil(self.include_before))
self.include_after = include_after_seconds/self.seconds_per_buffer
self.include_after = int(math.ceil(self.include_after))
self.pause_threshold = pause_threshold_seconds/self.seconds_per_buffer
self.pause_threshold = int(math.ceil(self.pause_threshold))
self.energy_threshold = init_energy_threshold
self.energy_damping = energy_damping
示例2: __init__
# 需要导入模块: import pyaudio [as 别名]
# 或者: from pyaudio import paInt32 [as 别名]
def __init__(self, samplerate: int = 0, samplewidth: int = 0, nchannels: int = 0, frames_per_chunk: int = 0) -> None:
super().__init__(samplerate, samplewidth, nchannels, frames_per_chunk, 0)
self.initialize()
thread_ready = threading.Event()
def audio_thread() -> None:
audio = pyaudio.PyAudio() # type: ignore
try:
mixed_chunks = self.mixer.chunks()
audio_format = audio.get_format_from_width(self.samplewidth) \
if self.samplewidth != 4 else pyaudio.paInt32 # type: ignore
output_device = None if playback.default_audio_device < 0 else playback.default_audio_device
stream = audio.open(format=audio_format, channels=self.nchannels, rate=self.samplerate, output=True,
output_device_index=output_device, input_device_index=output_device)
thread_ready.set()
try:
silence = b"\0" * self.chunksize
while True:
data = next(mixed_chunks) or silence
if isinstance(data, memoryview):
data = data.tobytes() # PyAudio stream can't deal with memoryview
stream.write(data)
if len(data) < self.chunksize:
stream.write(silence[len(data):])
if self.playing_callback:
sample = Sample.from_raw_frames(data, self.samplewidth, self.samplerate, self.nchannels)
self.playing_callback(sample)
except StopIteration:
pass
finally:
stream.close()
finally:
audio.terminate()
self.output_thread = threading.Thread(target=audio_thread, name="audio-pyaudio", daemon=True)
self.output_thread.start()
thread_ready.wait()
示例3: _apiai_stt
# 需要导入模块: import pyaudio [as 别名]
# 或者: from pyaudio import paInt32 [as 别名]
def _apiai_stt(self):
from math import log
import audioop
import pyaudio
import time
resampler = apiai.Resampler(source_samplerate=settings['RATE'])
request = self.ai.voice_request()
vad = apiai.VAD()
def callback(in_data, frame_count):
frames, data = resampler.resample(in_data, frame_count)
if settings.show_decibels:
decibel = 20 * log(audioop.rms(data, 2) + 1, 10)
click.echo(decibel)
state = vad.processFrame(frames)
request.send(data)
state_signal = pyaudio.paContinue if state == 1 else pyaudio.paComplete
return in_data, state_signal
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt32, input=True, output=False, stream_callback=callback,
channels=settings['CHANNELS'], rate=settings['RATE'], frames_per_buffer=settings['CHUNK'])
stream.start_stream()
click.echo("Speak!")
while stream.is_active():
time.sleep(0.1)
stream.stop_stream()
stream.close()
p.terminate()
示例4: __init__
# 需要导入模块: import pyaudio [as 别名]
# 或者: from pyaudio import paInt32 [as 别名]
def __init__(self, rate=16000, frames_size=None, channels=None, device_name=None, bits_per_sample=16):
"""Setup a pyaudio callback stream to record audio
Args:
rate: sample rate
frames_size: number of each channel's frames per chunk
channels: channels' number
device_name: device name to search
bits_per_sample: sample width - 8, 16, 24 or 32
"""
super(Source, self).__init__()
self.rate = rate
self.frames_size = frames_size if frames_size else rate / 100
self.channels = channels if channels else 1
self.pyaudio_instance = pyaudio.PyAudio()
formats = [pyaudio.paInt8, pyaudio.paInt16, pyaudio.paInt24, pyaudio.paInt32]
width = formats[bits_per_sample / 8 - 1]
# Search device by name
if device_name:
for i in range(self.pyaudio_instance.get_device_count()):
dev = self.pyaudio_instance.get_device_info_by_index(i)
name = dev['name'].encode('utf-8')
logger.info('{}:{} with {} input channels'.format(i, name, dev['maxInputChannels']))
if name.find(device_name) >= 0:
logger.info('Use {}'.format(name))
device_index = i
break
else:
device_index = self.pyaudio_instance.get_default_input_device_info()['index']
if device_index is None:
raise ValueError('Can not find an input device {}'.format(device_name))
self.stream = self.pyaudio_instance.open(
start=False,
format=width,
input_device_index=device_index,
channels=self.channels,
rate=int(self.rate),
frames_per_buffer=int(self.frames_size),
stream_callback=self._callback,
input=True
)