This repository was archived by the owner on Nov 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
84 lines (68 loc) · 2.61 KB
/
main.py
File metadata and controls
84 lines (68 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import mainthread
from textwrap import fill
from gestures4kivy import CommonGestures
from android_permissions import AndroidPermissions
from speech_events import SpeechEvents
# Touch event layout
####################
class TouchBoxLayout(BoxLayout, CommonGestures):
def cgb_primary(self, touch, focus_x, focus_y):
App.get_running_app().start_listening()
def cgb_select(self, touch, focus_x, focus_y, long_press):
if long_press:
App.get_running_app().copy_text()
class MyApp(App):
# Layout
########
def build(self):
layout = TouchBoxLayout(orientation = 'vertical')
instructions = Label(
text= 'Tap anywhere to Listen.\nLong Press anywhere to Copy.',
size_hint = (1.0, 0.1))
self.status = Label(text = 'Status: Not Listening',
size_hint = (1.0, 0.05))
self.speech = Label(size_hint = (1.0, 0.85))
layout.add_widget(instructions)
layout.add_widget(self.status)
layout.add_widget(self.speech)
return layout
# Permissions
#############
def on_start(self):
self.unwrapped = ''
self.dont_gc = AndroidPermissions(self.create_recognizer)
# Speech Recognizer
###################
def create_recognizer(self):
self.dont_gc = None
self.speech_events = SpeechEvents()
self.speech_events.create_recognizer(self.recognizer_event_handler)
@mainthread
def recognizer_event_handler(self, key, value):
if key == 'onReadyForSpeech':
self.status.text = 'Status: Listening.'
elif key == 'onBeginningOfSpeech':
self.status.text = 'Status: Speaker Detected.'
elif key == 'onEndOfSpeech':
self.status.text = 'Status: Not Listening.'
elif key == 'onError':
self.status.text = 'Status: ' + value + ' Not Listening.'
elif key in ['onPartialResults', 'onResults']:
self.unwrapped = str(value)
self.speech.text = fill(value, 40)
elif key in ['onBufferReceived', 'onEvent','onRmsChanged']:
pass
# Touch event handlers
######################
def start_listening(self):
if self.speech_events:
self.speech.text = ''
self.unwrapped = ''
self.speech_events.start_listening()
def copy_text(self):
if self.unwrapped:
self.speech_events.share_text_with_clipboard(self.unwrapped)
MyApp().run()