def rms(self):
return audioop.rms(self.__frames, self.samplewidth)
python类rms()的实例源码
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()
def check_silence(self, buf):
volume = audioop.rms(buf, 2)
if (volume >= config.THRESHOLD):
self.silence_timer = time.time()
if (self.append == False):
if (self.hatch.get('debug') == True):
print ('starting append mode')
self.timer = time.time()
for sbuf in self.silence_buffer:
self.prepare.prepare(sbuf, volume)
self.silence_buffer = [ ]
self.append = True
self.silence_counter = 0
else:
self.silence_counter += 1
self.silence_buffer.append(buf)
if (len(self.silence_buffer) > 3):
del self.silence_buffer[0]
if (self.out != None and self.out.closed != True):
self.out.write(buf)
if (self.append == True):
self.prepare.prepare(buf, volume)
if (self.append == True and self.silence_timer > 0
and self.silence_timer + config.MAX_SILENCE_AFTER_START < time.time()
and self.live == True):
self.stop("stop append mode because of silence")
if (self.append == True and self.timer + config.MAX_TIME < time.time()
and self.live == True):
self.stop("stop append mode because time is up")
def rms(audio_fragment, sample_width_in_bytes=2):
"""Given audio fragment, return the root mean square."""
# Equal to
# return int(np.sqrt(np.mean(np.square(
# np.absolute(audio_fragment.astype(np.float))))))
return audioop.rms(np.abs(audio_fragment), sample_width_in_bytes)
def record(self):
'''
time.sleep(1)
args = ['arecord', '-d', '5', self.voicefile]
proc = subprocess.Popen(args)
time.sleep(5)
'''
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
CHUNK = 1024
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = self.voicefile
audio = pyaudio.PyAudio()
# start Recording
stream = audio.open(format=FORMAT, channels=CHANNELS,
rate=RATE, input=True,
frames_per_buffer=CHUNK)
print "recording..."
frames = []
threshold = 1000
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
rms = audioop.rms(data,2)
if rms > threshold:
print "I am hearing you now"
frames.append(data)
print "finished recording"
# stop Recording
stream.stop_stream()
stream.close()
audio.terminate()
waveFile = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
waveFile.setnchannels(CHANNELS)
waveFile.setsampwidth(audio.get_sample_size(FORMAT))
waveFile.setframerate(RATE)
waveFile.writeframes(b''.join(frames))
waveFile.close()
def record(self):
'''
time.sleep(1)
args = ['arecord', '-d', '5', self.voicefile]
proc = subprocess.Popen(args)
time.sleep(5)
'''
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
CHUNK = 1024
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = self.voicefile
audio = pyaudio.PyAudio()
# start Recording
stream = audio.open(format=FORMAT, channels=CHANNELS,
rate=RATE, input=True,
frames_per_buffer=CHUNK)
print "recording..."
frames = []
threshold = 1000
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
rms = audioop.rms(data,2)
if rms > threshold:
print "I am hearing you now"
frames.append(data)
print "finished recording"
# stop Recording
stream.stop_stream()
stream.close()
audio.terminate()
waveFile = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
waveFile.setnchannels(CHANNELS)
waveFile.setsampwidth(audio.get_sample_size(FORMAT))
waveFile.setframerate(RATE)
waveFile.writeframes(b''.join(frames))
waveFile.close()
def wait_for_keyword(self, keyword=None):
if not keyword:
keyword = self._keyword
frame_queue = queue.Queue()
keyword_uttered = threading.Event()
# FIXME: not configurable yet
num_worker_threads = 2
for i in range(num_worker_threads):
t = threading.Thread(target=self.check_for_keyword,
args=(frame_queue, keyword_uttered, keyword))
t.daemon = True
t.start()
frames = collections.deque([], 30)
recording = False
recording_frames = []
self._logger.info("Waiting for keyword '%s'...", keyword)
for frame in self._input_device.record(self._input_chunksize,
self._input_bits,
self._input_channels,
self._input_rate):
if keyword_uttered.is_set():
self._logger.info("Keyword %s has been uttered", keyword)
return
frames.append(frame)
if not recording:
snr = self._snr([frame])
if snr >= 10: # 10dB
# Loudness is higher than normal, start recording and use
# the last 10 frames to start
self._logger.debug("Started recording on device '%s'",
self._input_device.slug)
self._logger.debug("Triggered on SNR of %sdB", snr)
recording = True
recording_frames = list(frames)[-10:]
elif len(frames) >= frames.maxlen:
# Threshold SNR not reached. Update threshold with
# background noise.
self._threshold = float(audioop.rms("".join(frames), 2))
else:
# We're recording
recording_frames.append(frame)
if len(recording_frames) > 20:
# If we recorded at least 20 frames, check if we're below
# threshold again
last_snr = self._snr(recording_frames[-10:])
self._logger.debug(
"Recording's SNR dB: %f", last_snr)
if last_snr <= 3 or len(recording_frames) >= 60:
# The loudness of the sound is not at least as high as
# the the threshold, or we've been waiting too long
# we'll stop recording now
recording = False
self._logger.debug("Recorded %d frames",
len(recording_frames))
frame_queue.put(tuple(recording_frames))
self._threshold = float(
audioop.rms(b"".join(frames), 2))