本文整理汇总了Python中pyaudio.get_sample_size函数的典型用法代码示例。如果您正苦于以下问题:Python get_sample_size函数的具体用法?Python get_sample_size怎么用?Python get_sample_size使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_sample_size函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self, device_index = None):
self.format = pyaudio.paInt16
self.SAMPLE_WIDTH = pyaudio.get_sample_size(self.format)
self.CHUNK = 1024
self.audio = None
self.stream = None
示例2: __init__
def __init__(self, byte_data, sample_rate, bit_width, channels, dtype = None):
"""
byte_data: A byte string containing the raw data.
BIT_WIDTH: bit width in bytes.
"""
assert isinstance(bit_width, (int, long)) and bit_width > 0, \
"`bit_width` must be positive integer."
bit_width = pyaudio.get_sample_size(pyaudio.get_format_from_width(bit_width))
assert isinstance(channels, int) and channels in [1, 2], \
"`channels` can be either 1(mono) or 2(stereo)."
assert channels in (1, 2), \
"`channels` can be either 1(mono) or 2(stereo) only."
assert sample_rate > 0, "`sample_rate` must be positive."
self.__bit_width = bit_width
self.__channels = channels
self.__sample_rate = sample_rate
self.__byte_data = byte_data # a byte string
if dtype is None:
dtype = self._get_dtype_by_bit_width()
if not self._validate_dtype(dtype):
raise ValueError("`dtype` is not compatible with the `bit_width`.")
self.__dtype = dtype
self.format = pyaudio.get_format_from_width(self.BIT_WIDTH)
示例3: pyaudio_scb
def pyaudio_scb(self, rate, fmt=pyaudio.paInt16):
samp_size = pyaudio.get_sample_size(fmt)
maxint = (1 << (8*samp_size)) - 1
dtype = ['!', 'h', 'i', '!', 'l', '!', '!', '!', 'q'][samp_size]
def __callback(data, frames, time, status, self=self, rate=rate, maxint=maxint, dtype=dtype):
return struct.pack(dtype*frames, *[maxint*int(i) for i in self.data(frames, self.freq, rate)])
return __callback
示例4: __init__
def __init__(self, recognizer, agent):
super(Listener, self).__init__()
self.recognizer = recognizer
self.agent = agent
self.queue = Queue.Queue()
self.running = True
self.sample_rate = 16000
self.sample_width = pyaudio.get_sample_size(pyaudio.paInt16) * 1
self.channels = 1
config = Decoder.default_config()
config.set_string('-hmm', path.join(PS_MODEL_DIR, 'en-us/en-us'))
config.set_string('-lm', path.join(PS_MODEL_DIR, 'en-us/en-us.lm.dmp'))
config.set_string('-dict', path.join(DATA_DIR, 'pocketsphinx/model/en-us/victoria-en-us.dict'))
config.set_string('-logfn', 'NUL')
config.set_string('-keyphrase', KEY_PHRASE)
config.set_float('-samprate', self.sample_rate)
config.set_float('-kws_threshold', 1e-40)
self.decoder = Decoder(config)
self.decoder.start_utt()
self.frames = collections.deque()
self.listening = -1
self.silence_start = -1
self.last_logged_hyp = None
示例5: activeListenToAllOptions
def activeListenToAllOptions(self, THRESHOLD=None, LISTEN=True, MUSIC=False):
"""
Records until a second of silence or times out after 12 seconds
Returns a list of the matching options or None
"""
RATE = 16000
CHUNK = 1024
LISTEN_TIME = 12
# check if no threshold provided
if THRESHOLD == None:
THRESHOLD = self.fetchThreshold()
self.speaker.play(jasperpath.data('audio', 'beep_hi.wav'))
# prepare recording stream
stream = self._audio.open(format=pyaudio.paInt16,
channels=1,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
frames = []
# increasing the range # results in longer pause after command generation
lastN = [THRESHOLD * 1.2 for i in range(30)]
for i in range(0, RATE / CHUNK * LISTEN_TIME):
data = stream.read(CHUNK)
frames.append(data)
score = self.getScore(data)
lastN.pop(0)
lastN.append(score)
average = sum(lastN) / float(len(lastN))
# TODO: 0.8 should not be a MAGIC NUMBER!
if average < THRESHOLD * 0.8:
break
self.speaker.play(jasperpath.data('audio', 'beep_lo.wav'))
# save the audio data
stream.stop_stream()
stream.close()
with tempfile.SpooledTemporaryFile(mode='w+b') as f:
wav_fp = wave.open(f, 'wb')
wav_fp.setnchannels(1)
wav_fp.setsampwidth(pyaudio.get_sample_size(pyaudio.paInt16))
wav_fp.setframerate(RATE)
wav_fp.writeframes(''.join(frames))
wav_fp.close()
f.seek(0)
mode = TranscriptionMode.MUSIC if MUSIC else TranscriptionMode.NORMAL
transcribed = self.active_stt_engine.transcribe(f, mode=mode)
return transcribed
示例6: __init__
def __init__(self):
self.CHUNK = 1024
self.RATE = 16000
self.FORMAT = pyaudio.paInt16
self.DTYPE='Int16'
self.CHANNELS = 1
self.RUN_SECONDS = 1000
self.sampwidth = pyaudio.get_sample_size(self.FORMAT)
self.mic = False
self.wf = None
self.deque_time = deque(maxlen=20)
self.deque_mean = deque(maxlen=3)
self.deque_freq = deque(maxlen=3)
self.posneg_frequencies = np.fft.fftfreq(self.CHUNK, 1.0/self.RATE)
self.freqs_indices = np.where(self.posneg_frequencies >= 0)
self.freqs = self.posneg_frequencies[np.where(self.posneg_frequencies >= 0)]
self.t0 = time.time()
self.ichunk = 0
# plt.ion()
# plt.axis([0,100,0,800])
# fig=plt.figure()
if RPI:
self.led = Lights()
self.led.start()
示例7: __init__
def __init__(self, wrapped_stream, format, muted=False):
assert wrapped_stream is not None
self.wrapped_stream = wrapped_stream
self.muted = muted
self.SAMPLE_WIDTH = pyaudio.get_sample_size(format)
self.muted_buffer = b''.join([b'\x00' * self.SAMPLE_WIDTH])
示例8: __init__
def __init__(self, device_index, sample_rate, bit_width, chunk_size = 8092, channels = 1):
audio = pyaudio.PyAudio()
## Checking the device_index is valid or not.
assert isinstance(device_index, (int, long)), "Device index must be an integer."
device_count = audio.get_device_count()
assert 0 <= device_index < device_count, "`device_index` out of range: {} out of {}".format(device_index, count)
audio.terminate()
self.__device_index = device_index
if not self.device_info["maxInputChannels"] > 0:
raise DeviceTypeError("Can not source from a non-input device.")
self.__format = pyaudio.get_format_from_width(bit_width)
self.__bit_width = pyaudio.get_sample_size(self.FORMAT)
assert isinstance(sample_rate, (int, long)), "`sample_rate` must be integer."
max_sample_rate = self.device_info["defaultSampleRate"]
assert 0 < sample_rate <= max_sample_rate, "`sample_rate` out of range: {} out of {}".format(sample_rate, max_sample_rate)
self.__sample_rate = sample_rate
assert isinstance(chunk_size, (int, long)), "`chunk_size` must be integer."
self.__chunk_size = chunk_size
assert channels in [1, 2], '`channels` can be either 1 or 2. 1 for mono audio, 2 for stereo.'
self.__channels = channels
# audio resource and streams.
self.__audio = None
self.__input_stream = None
示例9: save_audio
def save_audio(self, fp, frames):
sample_width = pyaudio.get_sample_size(self.format)
f = open_audio(fp, 'wb')
f.setsampwidth(sample_width)
f.setframerate(self.rate)
f.setnchannels(1)
f.writeframes(''.join(frames))
f.close()
示例10: write_chunks
def write_chunks(path, frames, rate=44100, channels=2, format=pyaudio.paInt16):
""" write audio to hard disk """
wf = wave.open(path, "wb")
wf.setnchannels(channels)
wf.setsampwidth(pyaudio.get_sample_size(format))
wf.setframerate(rate)
wf.writeframes(b"".join(frames))
wf.close()
示例11: __init__
def __init__(self, device_index = None):
self.device_index = device_index
self.format = pyaudio.paInt16 # 16-bit int sampling
self.SAMPLE_WIDTH = pyaudio.get_sample_size(self.format)
self.RATE = 16000 # sampling rate in Hertz
self.CHANNELS = 1 # mono audio
self.CHUNK = 1024 # number of frames stored in each buffer
self.audio = None
self.stream = None
示例12: write_wave
def write_wave(arrs, filepath,
sample_width=pyaudio.get_sample_size(PYAUDIO_FORMAT),
channels=CHANNELS, sample_rate=SAMPLE_RATE):
f = wave.open(filepath, 'w')
f.setnchannels(channels)
f.setsampwidth(sample_width)
f.setframerate(sample_rate)
for arr in _structpack(arrs):
f.writeframes(arr)
f.close()
示例13: __init__
def __init__(self, device_index = None):
self.device_index = device_index
self.format = pyaudio.paInt16
self.SAMPLE_WIDTH = pyaudio.get_sample_size(self.format)
self.RATE = 44100
self.CHANNELS = 1
self.CHUNK = 2205
self.audio = None
self.stream = None
示例14: passiveListen
def passiveListen(self, persona):
rate = self.PASSIVE_RATE
chunk = self.PASSIVE_CHUNK
LISTEN_TIME = 10
THRESHOLD = self.THRESHOLD_MULTIPLIER * self.fetchThreshold()
stream = self._audio.open(format=pyaudio.paInt16,
channels=1,
rate=rate,
input=True,
frames_per_buffer=chunk)
frames = []
didDetect = False
for i in range(0, rate / chunk * LISTEN_TIME):
data = stream.read(chunk)
frames.append(data)
score = self.getScore(data)
if score > THRESHOLD:
didDetect = True
break
# no use continuing if no flag raised
if not didDetect:
print "No disturbance detected"
stream.stop_stream()
stream.close()
return (None, None)
# cutoff any recording before this disturbance was detected
frames = frames[-20:]
# otherwise, let's keep recording for few seconds and save the file
DELAY_MULTIPLIER = 1.5
for i in range(0, rate / chunk * DELAY_MULTIPLIER):
data = stream.read(chunk)
frames.append(data)
with tempfile.NamedTemporaryFile(mode='w+b') as f:
wav_fp = wave.open(f, 'wb')
wav_fp.setnchannels(1)
wav_fp.setsampwidth(pyaudio.get_sample_size(pyaudio.paInt16))
wav_fp.setframerate(rate)
wav_fp.writeframes(''.join(frames))
wav_fp.close()
f.seek(0)
# check if PERSONA was said
transcribed = self.passive_transcribe(f)
if any(persona in phrase for phrase in transcribed):
return (THRESHOLD, persona)
return (False, transcribed)
示例15: __init__
def __init__(self, hmm_type=1, vad_threshold=3.5, pl_window=10, wip=1e-4,
silprob=0.3, bestpath=True, remove_dc=True, do_keyphrase=False, keyphrase="NAVSA", kws_threshold=1e-4):
self.CHUNK = 1024
self.RATE = 16000
self.FORMAT = pyaudio.paInt16
self.CHANNELS = 1
self.RUN_SECONDS = 1000
self.sampwidth = pyaudio.get_sample_size(self.FORMAT)
self.do_trigger = False
# Create a decoder with certain model
self.config = Decoder.default_config()
if hmm_type == 0:
self.config.set_string('-hmm', '/usr/local/share/pocketsphinx/model/en-us/en-us')
elif hmm_type == 1:
self.config.set_string('-hmm', 'model/cmusphinx-en-us-5.2')
self.config.set_string('-dict', 'model/7705.dic')
if do_keyphrase:
self.config.set_string('-keyphrase', keyphrase)
self.config.set_float('-kws_threshold', kws_threshold)
else:
self.config.set_string('-lm', 'model/7705.lm')
self.config.set_string('-logfn', '/dev/null')
self.config.set_string('-debug', '1')
# http://cmusphinx.sourceforge.net/wiki/pocketsphinxhandhelds
self.config.set_boolean('-bestpath', bestpath) # default is true
self.config.set_float('-vad_threshold', vad_threshold) # default is 2
self.config.set_float("-pl_window", pl_window) # default is 5, range is 0 to 10
self.config.set_float('-wip', wip) # 0.005 Silence word transition probability
self.config.set_float('-silprob', silprob) # 0.65 Word insertion penalty
self.config.set_string('-remove_dc', 'yes' if remove_dc else 'no')
self.decoder = Decoder(self.config)
self.deque_time = deque(maxlen=20)
self.deque_mean = deque(maxlen=50)
self.mic = False
self.wf = None
self.vad = False
self.rec_trigger = False
self.rec_frames = []
self.sec_since_kw = 999.9
self.sec_since_vad = 999.9