main.py
Go to the documentation of this file.
1 #
2 # Copyright 2020 Picovoice Inc.
3 #
4 # You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE"
5 # file accompanying this source.
6 #
7 # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8 # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9 # specific language governing permissions and limitations under the License.
10 #
11 
12 import argparse
13 import struct
14 import tkinter as tk
15 from threading import Thread
16 
17 import pvporcupine
18 from pvrecorder import PvRecorder
19 
20 KEYWORDS = [
21  'alexa', 'americano', 'blueberry', 'bumblebee', 'computer', 'grapefruit', 'grasshopper', 'hey google', 'hey siri',
22  'jarvis', 'picovoice', 'porcupine'
23 ]
24 
25 
26 class PorcupineThread(Thread):
27  def __init__(self, access_key, device_index, keyword_var):
28  super().__init__()
29 
30  self._access_key = access_key
31  self._device_index = device_index
32  self._keyword_var = keyword_var
33 
34  self._is_ready = False
35  self._stop = False
36  self._is_stopped = False
37 
38  def run(self):
39  ppn = None
40  recorder = None
41 
42  try:
43  ppn = pvporcupine.create(access_key=self._access_key, keywords=KEYWORDS, sensitivities=[0.75] * len(KEYWORDS))
44 
45  recorder = PvRecorder(device_index=self._device_index, frame_length=ppn.frame_length)
46  recorder.start()
47 
48  self._is_ready = True
49 
50  while not self._stop:
51  pcm = recorder.read()
52  keyword_index = ppn.process(pcm)
53  if keyword_index >= 0:
54  self._keyword_var.set(KEYWORDS[keyword_index])
55  print(self._keyword_var.get())
56  finally:
57  if recorder is not None:
58  recorder.delete()
59 
60  if ppn is not None:
61  ppn.delete()
62 
63  self._is_stopped = True
64 
65  def is_ready(self):
66  return self._is_ready
67 
68  def stop(self):
69  self._stop = True
70 
71  def is_stopped(self):
72  return self._is_stopped
73 
74 
75 def main():
76  parser = argparse.ArgumentParser()
77 
78  parser.add_argument('--access_key',
79  help='AccessKey obtained from Picovoice Console (https://picovoice.ai/console/)',
80  required=True)
81 
82  parser.add_argument('--audio_device_index', help='Index of input audio device.', type=int, default=-1)
83 
84  args = parser.parse_args()
85 
86  window = tk.Tk()
87  window.title('Porcupine Demo')
88  window.minsize(width=300, height=400)
89 
90  keyword_var = tk.StringVar(window)
91 
92  for x in KEYWORDS:
93  tk.Radiobutton(window, text=x, variable=keyword_var, value=x, indicator=0).pack(fill=tk.X, ipady=5)
94 
95  porcupine_thread = PorcupineThread(access_key=args.access_key,
96  device_index=args.audio_device_index,
97  keyword_var=keyword_var)
98 
99  def on_close():
100  porcupine_thread.stop()
101  while not porcupine_thread.is_stopped():
102  pass
103  window.destroy()
104 
105  window.protocol('WM_DELETE_WINDOW', on_close)
106 
107  porcupine_thread.start()
108  while not porcupine_thread.is_ready():
109  pass
110 
111  window.mainloop()
112 
113 
114 if __name__ == '__main__':
115  main()
main.PorcupineThread._keyword_var
_keyword_var
Definition: main.py:32
main.PorcupineThread.stop
def stop(self)
Definition: main.py:68
main.PorcupineThread._stop
_stop
Definition: main.py:35
main.PorcupineThread._is_stopped
_is_stopped
Definition: main.py:36
main.PorcupineThread._is_ready
_is_ready
Definition: main.py:34
main.PorcupineThread.run
def run(self)
Definition: main.py:38
main.PorcupineThread.is_stopped
def is_stopped(self)
Definition: main.py:71
main.PorcupineThread._access_key
_access_key
Definition: main.py:30
main.PorcupineThread.is_ready
def is_ready(self)
Definition: main.py:65
main
Definition: main.py:1
main.PorcupineThread._device_index
_device_index
Definition: main.py:31
main.PorcupineThread.__init__
def __init__(self, access_key, device_index, keyword_var)
Definition: main.py:27
main.main
def main()
Definition: main.py:75
main.PorcupineThread
Definition: main.py:26


picovoice_driver
Author(s):
autogenerated on Fri Apr 1 2022 02:13:56