diff --git a/README.rst b/README.rst index 9071312e5..19eee9419 100644 --- a/README.rst +++ b/README.rst @@ -26,6 +26,7 @@ Camera (taking picture) X X GPS X X Notifications X X X X Text to speech X X X X X +Speech to Text X Email (open mail client) X X X X X Vibrator X X Sms (send messages) X diff --git a/examples/speech/main.py b/examples/speech/main.py new file mode 100644 index 000000000..ad282db6f --- /dev/null +++ b/examples/speech/main.py @@ -0,0 +1,52 @@ +from kivy.app import App +from kivy.uix.boxlayout import BoxLayout +from plyer import stt +from kivy.lang import Builder +Builder.load_string(''' +: + Button: + text: "Start listening" + on_release: root.start() + Button: + text: "Stop listening" + on_release: root.stop() + + Button: + text: "Set Commands" + on_release: root.set_command() + + Button: + text: "commands title" + on_release: root.commands_title() + + Button: + text: "commands" + on_release: root.commands() + +''') + + +class STTDemo(BoxLayout): + + def start(self): + stt.start_listening() + + def stop(self): + stt.stop_listening() + + def set_command(self): + stt.set_commands() + + def commands_title(self): + print stt.display_commands_title() + + def commands(self): + print stt.display_commnds() + + +class SpeechToTextApp(App): + def build(self): + return STTDemo() + +if __name__ == '__main__': + SpeechToTextApp().run() diff --git a/plyer/__init__.py b/plyer/__init__.py index d80346dcb..b176d94d6 100644 --- a/plyer/__init__.py +++ b/plyer/__init__.py @@ -6,7 +6,8 @@ __all__ = ('accelerometer', 'audio', 'battery', 'call', 'camera', 'compass', 'email', 'filechooser', 'gps', 'gyroscope', 'irblaster', - 'orientation', 'notification', 'sms', 'tts', 'uniqueid', 'vibrator') + 'orientation', 'notification', 'sms', 'stt', 'tts', 'uniqueid', + 'vibrator') __version__ = '1.2.5dev' @@ -56,6 +57,9 @@ #: Sms proxy to :class:`plyer.facades.Sms` sms = Proxy('sms', facades.Sms) +#: STT proxy to :class:`plyer.facades.STT` +stt = Proxy('stt', facades.STT) + #: TTS proxy to :class:`plyer.facades.TTS` tts = Proxy('tts', facades.TTS) diff --git a/plyer/facades/__init__.py b/plyer/facades/__init__.py index 5db8fe7ed..125f847c2 100644 --- a/plyer/facades/__init__.py +++ b/plyer/facades/__init__.py @@ -8,8 +8,8 @@ __all__ = ('Accelerometer', 'Audio', 'Battery', 'Call', 'Camera', 'Compass', 'Email', 'FileChooser', 'GPS', 'Gyroscope', 'IrBlaster', - 'Orientation', 'Notification', 'Sms', 'TTS', 'UniqueID', 'Vibrator', - 'Flash') + 'Orientation', 'Notification', 'Sms', 'STT', 'TTS', 'UniqueID', + 'Vibrator', 'Flash') from plyer.facades.accelerometer import Accelerometer from plyer.facades.audio import Audio @@ -25,6 +25,7 @@ from plyer.facades.orientation import Orientation from plyer.facades.notification import Notification from plyer.facades.sms import Sms +from plyer.facades.stt import STT from plyer.facades.tts import TTS from plyer.facades.uniqueid import UniqueID from plyer.facades.vibrator import Vibrator diff --git a/plyer/facades/stt.py b/plyer/facades/stt.py new file mode 100644 index 000000000..7555003fc --- /dev/null +++ b/plyer/facades/stt.py @@ -0,0 +1,47 @@ +class STT(object): + '''SpeechToText facade. + ''' + + def start_listening(self): + ''' + Tells the speech recognition engine to begin listening for commands. + ''' + self._start_listening() + + def stop_listening(self): + ''' + Tells the speech recognition engine to suspend listening for commands. + ''' + self._stop_listening() + + def set_commands(self): + ''' + ''' + self._set_commands() + + def display_commands_title(self): + ''' + ''' + self._display_commands_title() + + def display_commnds(self): + ''' + ''' + self._display_commnds() + + # private + + def _start_listening(self, **kwargs): + raise NotImplementedError() + + def _stop_listening(self, **kwargs): + raise NotImplementedError() + + def _set_commands(self, **kwargs): + raise NotImplementedError() + + def _display_commands_title(self, **kwargs): + raise NotImplementedError() + + def _display_commands(self, **kwargs): + raise NotImplementedError() diff --git a/plyer/platforms/macosx/stt.py b/plyer/platforms/macosx/stt.py new file mode 100644 index 000000000..e695cdb1e --- /dev/null +++ b/plyer/platforms/macosx/stt.py @@ -0,0 +1,60 @@ +from plyer.facades import STT +from pyobjus import autoclass, objc_arr, objc_str, protocol, selector +from pyobjus.dylib_manager import load_framework, INCLUDE + +load_framework(INCLUDE.AppKit) + +NSSpeechRecognizer = autoclass('NSSpeechRecognizer') +NSString = autoclass('NSString') + + +class SpeechToText(STT): + + def _ns(self, x): + NSString.alloc().initWithUTF8String_(x) + + def _start_listening(self, **kwargs): + self.obj = NSSpeechRecognizer.alloc() + self.obj.init() + # self.obj_delegate = NSSpeechRecognizerDelegate + self.obj.commands = ["a", "b", "c"] + self.obj.setDelegate_(self) + self.obj.startListening() + + # foo(NSSpeechRecognizerDelegate, "") + + @protocol('NSSpeechRecognizerDelegate') + def speechRecognizer_didRecognizeCommand_(self, sender, command): + print command + try: + cnt = command.allObjects().count() + for i in range(cnt): + print command.allObjects().objectAtIndex_(i).UTF8String() + except: + pass + + def _set_commands(self): + self.obj.commands = ["a", "b", "c"] + self.obj.setCommands_ = ["a", "b", "c"] + + def _display_commands_title(self): + return self.obj.delegate + # return self.obj.displayedCommandsTitle + + def _display_commnds(self): + return self.obj.commands + + def _stop_listening(self, **kwargs): + self.obj.stopListening() + print "Not Listening" + + +def foo(name, string): + matching = [] + matching = [s for s in dir(name) if "{}".format(string) in s] + for m in matching: + print m + + +def instance(): + return SpeechToText()