word_extractor.py
Go to the documentation of this file.
1 # Copyright 2017 Mycroft AI Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15 from speech_recognition import AudioData
16 
17 
19  SILENCE_SECS = 0.1
20  PRECISION_RATE = 0.01
21 
22  def __init__(self, audio, recognizer, metrics):
23  self.audio = audio
24  self.recognizer = recognizer
25  self.audio_size = len(self.audio.frame_data)
26  self.delta = int(self.audio_size / 2)
27  self.begin = 0
28  self.end = self.audio_size
29  self.precision = int(self.audio_size * self.PRECISION_RATE)
31  self.audio.sample_rate,
32  self.audio.sample_width)
33  self.metrics = metrics
34 
35  def __add(self, is_begin, value):
36  if is_begin:
37  self.begin += value
38  else:
39  self.end += value
40 
41  def __calculate_marker(self, is_begin):
42  dt = self.delta
43  sign = 1 if is_begin else -1
44 
45  while dt > self.precision:
46  self.__add(is_begin, dt * sign)
47  segment = self.audio.frame_data[self.begin:self.end]
48  found = self.recognizer.is_recognized(segment, self.metrics)
49  if not found:
50  self.__add(is_begin, dt * -sign)
51  dt = int(dt / 2)
52 
53  def calculate_range(self):
54  self.__calculate_marker(False)
55  self.__calculate_marker(True)
56 
57  @staticmethod
58  def create_silence(seconds, sample_rate, sample_width):
59  return '\0' * int(seconds * sample_rate * sample_width)
60 
62  byte_data = self.audio.frame_data[0:self.begin] + self.silence_data
63  return AudioData(byte_data, self.audio.sample_rate,
64  self.audio.sample_width)
65 
67  byte_data = self.silence_data + self.audio.frame_data[self.end:
68  self.audio_size]
69  return AudioData(byte_data, self.audio.sample_rate,
70  self.audio.sample_width)
def __init__(self, audio, recognizer, metrics)
def create_silence(seconds, sample_rate, sample_width)


mycroft_ros
Author(s):
autogenerated on Mon Apr 26 2021 02:35:40