Skip to content

Commit 6e145bb

Browse files
kiok46KeyWeeUsr
kiok46
authored andcommitted
add listening to speech
1 parent 9b9c523 commit 6e145bb

File tree

6 files changed

+166
-2
lines changed

6 files changed

+166
-2
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/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
__all__ = ('accelerometer', 'audio', 'barometer', 'battery', 'call', 'camera',
99
'compass', 'email', 'filechooser', 'flash', 'gps', 'gravity',
1010
'gyroscope', 'irblaster', 'light', 'orientation', 'notification',
11-
'proximity', 'sms', 'tts', 'uniqueid', 'vibrator', 'wifi',
11+
'proximity', 'sms', 'stt', 'tts', 'uniqueid', 'vibrator', 'wifi',
1212
'temperature', 'humidity', 'spatialorientation', 'brightness',
1313
'storagepath', 'processors', 'bluetooth', 'screenshot')
1414

@@ -76,6 +76,9 @@
7676
#: Sms proxy to :class:`plyer.facades.Sms`
7777
sms = Proxy('sms', facades.Sms)
7878

79+
#: STT proxy to :class:`plyer.facades.STT`
80+
stt = Proxy('stt', facades.STT)
81+
7982
#: TTS proxy to :class:`plyer.facades.TTS`
8083
tts = Proxy('tts', facades.TTS)
8184

plyer/facades/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
__all__ = ('Accelerometer', 'Audio', 'Barometer', 'Battery', 'Call', 'Camera',
1010
'Compass', 'Email', 'FileChooser', 'GPS', 'Gravity', 'Gyroscope',
1111
'IrBlaster', 'Light', 'Orientation', 'Notification', 'Proximity',
12-
'Sms', 'TTS', 'UniqueID', 'Vibrator', 'Wifi', 'Flash',
12+
'Sms', 'STT', 'TTS', 'UniqueID', 'Vibrator', 'Wifi', 'Flash',
1313
'Temperature', 'Humidity', 'SpatialOrientation', 'Brightness',
1414
'Processors', 'StoragePath', 'keystore', 'Bluetooth', 'Screenshot')
1515

@@ -32,6 +32,7 @@
3232
from plyer.facades.orientation import Orientation
3333
from plyer.facades.notification import Notification
3434
from plyer.facades.sms import Sms
35+
from plyer.facades.stt import STT
3536
from plyer.facades.tts import TTS
3637
from plyer.facades.uniqueid import UniqueID
3738
from plyer.facades.vibrator import Vibrator

plyer/facades/stt.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class STT(object):
2+
'''SpeechToText facade.
3+
'''
4+
5+
def start_listening(self):
6+
'''
7+
Tells the speech recognition engine to begin listening for commands.
8+
'''
9+
self._start_listening()
10+
11+
def stop_listening(self):
12+
'''
13+
Tells the speech recognition engine to suspend listening for commands.
14+
'''
15+
self._stop_listening()
16+
17+
def set_commands(self):
18+
'''
19+
'''
20+
self._set_commands()
21+
22+
def display_commands_title(self):
23+
'''
24+
'''
25+
self._display_commands_title()
26+
27+
def display_commnds(self):
28+
'''
29+
'''
30+
self._display_commnds()
31+
32+
# private
33+
34+
def _start_listening(self, **kwargs):
35+
raise NotImplementedError()
36+
37+
def _stop_listening(self, **kwargs):
38+
raise NotImplementedError()
39+
40+
def _set_commands(self, **kwargs):
41+
raise NotImplementedError()
42+
43+
def _display_commands_title(self, **kwargs):
44+
raise NotImplementedError()
45+
46+
def _display_commands(self, **kwargs):
47+
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)