Somewhat fancy voice command recognition software
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mpris.py 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # This is part of Kaylee
  2. # -- this code is licensed GPLv3
  3. # Copyright 2015-2017 Clayton G. Hobbs
  4. # Portions Copyright 2013 Jezra
  5. """MPRIS media player control plugin
  6. This plugin allows control of a media player supporting the Media
  7. Player Remote Interfacing Specification (MPRIS) D-Bus interface. To
  8. use this plugin, you must first install `pydbus
  9. <https://github.com/LEW21/pydbus>`__ for Python 3. This plugin takes
  10. no configuration, so load it as follows::
  11. ".mpris": {}
  12. Note: if your microphone can hear the audio produced by your speakers,
  13. it is recommended to load PulseAudio's module-echo-cancel when using
  14. this plugin. Recognition while music is playing may still be mediocre,
  15. but without module-echo-cancel it has almost no chance of working.
  16. """
  17. from pydbus import SessionBus
  18. from .pluginbase import PluginBase
  19. class Plugin(PluginBase):
  20. """Main class for the MPRIS plugin"""
  21. def __init__(self, config, name):
  22. """Initialize the MPRIS plugin"""
  23. super().__init__(config, name)
  24. # Get the D-Bus proxy object
  25. self._bus = SessionBus()
  26. self._dbus_proxy = self._bus.get('.DBus')
  27. self.commands = {
  28. 'next song': self._next,
  29. 'next video': self._next,
  30. 'pause music': self._pause,
  31. 'pause video': self._pause,
  32. 'play music': self._play,
  33. 'play video': self._play,
  34. 'previous song': self._previous,
  35. 'previous video': self._previous
  36. }
  37. self.corpus_strings.update(self.commands)
  38. def _mpris_proxy(self):
  39. """Get a proxy for the first MPRIS media player, or None"""
  40. try:
  41. # Get the bus name of the first MPRIS media player
  42. bus_name = [name for name in self._dbus_proxy.ListNames()
  43. if name.startswith('org.mpris.MediaPlayer2')][0]
  44. # Get the proxy
  45. return self._bus.get(bus_name, '/org/mpris/MediaPlayer2')
  46. except IndexError:
  47. return None
  48. def _next(self):
  49. p = self._mpris_proxy()
  50. if p is not None:
  51. p.Next()
  52. def _pause(self):
  53. p = self._mpris_proxy()
  54. if p is not None:
  55. p.Pause()
  56. def _play(self):
  57. p = self._mpris_proxy()
  58. if p is not None:
  59. p.Play()
  60. def _previous(self):
  61. p = self._mpris_proxy()
  62. if p is not None:
  63. p.Previous()
  64. def confidence(self, text):
  65. """Return whether or not the command can be handled"""
  66. if text not in self.corpus_strings:
  67. return 0
  68. if self._mpris_proxy() is None:
  69. return 0
  70. return 1
  71. def handle(self, text):
  72. """Print and speak the phrase when it is heard"""
  73. # Is there a matching command?
  74. if self.confidence(text):
  75. self.commands[text]()
  76. return True
  77. else:
  78. return False