Somewhat fancy voice command recognition software
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

mpris.py 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. print('No media player found')
  48. return None
  49. def _next(self):
  50. p = self._mpris_proxy()
  51. if p is not None:
  52. p.Next()
  53. def _pause(self):
  54. p = self._mpris_proxy()
  55. if p is not None:
  56. p.Pause()
  57. def _play(self):
  58. p = self._mpris_proxy()
  59. if p is not None:
  60. p.Play()
  61. def _previous(self):
  62. p = self._mpris_proxy()
  63. if p is not None:
  64. p.Previous()
  65. def recognizer_finished(self, recognizer, text):
  66. """Print and speak the phrase when it is heard"""
  67. # Is there a matching command?
  68. if text in self.corpus_strings:
  69. self.emit('processed', text)
  70. self.commands[text]()
  71. return True
  72. else:
  73. return False