Skip to content

Commit d367df5

Browse files
kiok46KeyWeeUsr
kiok46
authored andcommitted
add listening to speech
1 parent 6768c2b commit d367df5

File tree

4 files changed

+137
-0
lines changed

4 files changed

+137
-0
lines changed

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Proximity X
5858
Screenshot X X X
5959
Sms (send messages) X X
6060
Spatial Orientation X X
61+
Speech to Text X
6162
Storage Path X X X X X
6263
Temperature X
6364
Text to speech X X X X X

examples/speech/main.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from kivy.app import App
2+
from kivy.uix.boxlayout import BoxLayout
3+
from plyer import stt
4+
from kivy.lang import Builder
5+
Builder.load_string('''
6+
<STTDemo>:
7+
Button:
8+
text: "Start listening"
9+
on_release: root.start()
10+
Button:
11+
text: "Stop listening"
12+
on_release: root.stop()
13+
14+
Button:
15+
text: "Set Commands"
16+
on_release: root.set_command()
17+
18+
Button:
19+
text: "commands title"
20+
on_release: root.commands_title()
21+
22+
Button:
23+
text: "commands"
24+
on_release: root.commands()
25+
26+
''')
27+
28+
29+
class STTDemo(BoxLayout):
30+
31+
def start(self):
32+
stt.start_listening()
33+
34+
def stop(self):
35+
stt.stop_listening()
36+
37+
def set_command(self):
38+
stt.set_commands()
39+
40+
def commands_title(self):
41+
print stt.display_commands_title()
42+
43+
def commands(self):
44+
print stt.display_commnds()
45+
46+
47+
class SpeechToTextApp(App):
48+
def build(self):
49+
return STTDemo()
50+
51+
if __name__ == '__main__':
52+
SpeechToTextApp().run()

plyer/facades/stt.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,21 @@ def exist(self):
177177

178178
return self._exist()
179179

180+
def set_commands(self):
181+
'''
182+
'''
183+
self._set_commands()
184+
185+
def display_commands_title(self):
186+
'''
187+
'''
188+
self._display_commands_title()
189+
190+
def display_commands(self):
191+
'''
192+
'''
193+
self._display_commands()
194+
180195
# private methods
181196
def _start(self):
182197
raise NotImplementedError
@@ -186,3 +201,12 @@ def _stop(self):
186201

187202
def _exist(self):
188203
raise NotImplementedError
204+
205+
def _set_commands(self, **kwargs):
206+
raise NotImplementedError()
207+
208+
def _display_commands_title(self, **kwargs):
209+
raise NotImplementedError()
210+
211+
def _display_commands(self, **kwargs):
212+
raise NotImplementedError()

plyer/platforms/macosx/stt.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from plyer.facades import STT
2+
from pyobjus import autoclass, objc_arr, objc_str, protocol, selector
3+
from pyobjus.dylib_manager import load_framework, INCLUDE
4+
5+
load_framework(INCLUDE.AppKit)
6+
7+
NSSpeechRecognizer = autoclass('NSSpeechRecognizer')
8+
NSString = autoclass('NSString')
9+
10+
11+
class SpeechToText(STT):
12+
13+
def _ns(self, x):
14+
NSString.alloc().initWithUTF8String_(x)
15+
16+
def _start_listening(self, **kwargs):
17+
self.obj = NSSpeechRecognizer.alloc()
18+
self.obj.init()
19+
# self.obj_delegate = NSSpeechRecognizerDelegate
20+
self.obj.commands = ["a", "b", "c"]
21+
self.obj.setDelegate_(self)
22+
self.obj.startListening()
23+
24+
# foo(NSSpeechRecognizerDelegate, "")
25+
26+
@protocol('NSSpeechRecognizerDelegate')
27+
def speechRecognizer_didRecognizeCommand_(self, sender, command):
28+
print command
29+
try:
30+
cnt = command.allObjects().count()
31+
for i in range(cnt):
32+
print command.allObjects().objectAtIndex_(i).UTF8String()
33+
except:
34+
pass
35+
36+
def _set_commands(self):
37+
self.obj.commands = ["a", "b", "c"]
38+
self.obj.setCommands_ = ["a", "b", "c"]
39+
40+
def _display_commands_title(self):
41+
return self.obj.delegate
42+
# return self.obj.displayedCommandsTitle
43+
44+
def _display_commnds(self):
45+
return self.obj.commands
46+
47+
def _stop_listening(self, **kwargs):
48+
self.obj.stopListening()
49+
print "Not Listening"
50+
51+
52+
def foo(name, string):
53+
matching = []
54+
matching = [s for s in dir(name) if "{}".format(string) in s]
55+
for m in matching:
56+
print m
57+
58+
59+
def instance():
60+
return SpeechToText()

0 commit comments

Comments
 (0)