aboutsummaryrefslogtreecommitdiff
path: root/voctocore/lib/audiomix.py
blob: 17bc1fbb35fe74cee0fe27a704888f05317b9953 (plain)
  1. #!/usr/bin/python3
  2. import logging
  3. from gi.repository import Gst
  4. from enum import Enum
  5. from lib.config import Config
  6. class AudioMix(object):
  7. def __init__(self):
  8. self.log = logging.getLogger('AudioMix')
  9. self.selectedSource = 0
  10. self.caps = Config.get('mix', 'audiocaps')
  11. self.names = Config.getlist('mix', 'sources')
  12. self.log.info('Configuring Mixer for %u Sources', len(self.names))
  13. pipeline = """
  14. audiomixer name=mix !
  15. {caps} !
  16. queue !
  17. tee name=tee
  18. tee. ! queue ! interaudiosink channel=audio_mix_out
  19. """.format(
  20. caps=self.caps
  21. )
  22. if Config.getboolean('previews', 'enabled'):
  23. pipeline += """
  24. tee. ! queue ! interaudiosink channel=audio_mix_preview
  25. """
  26. if Config.getboolean('stream-blanker', 'enabled'):
  27. pipeline += """
  28. tee. ! queue ! interaudiosink channel=audio_mix_streamblanker
  29. """
  30. for idx, name in enumerate(self.names):
  31. pipeline += """
  32. interaudiosrc channel=audio_{name}_mixer !
  33. {caps} !
  34. mix.
  35. """.format(
  36. name=name,
  37. caps=self.caps
  38. )
  39. self.log.debug('Creating Mixing-Pipeline:\n%s', pipeline)
  40. self.mixingPipeline = Gst.parse_launch(pipeline)
  41. self.log.debug('Binding Error & End-of-Stream-Signal on Mixing-Pipeline')
  42. self.mixingPipeline.bus.add_signal_watch()
  43. self.mixingPipeline.bus.connect("message::eos", self.on_eos)
  44. self.mixingPipeline.bus.connect("message::error", self.on_error)
  45. self.log.debug('Initializing Mixer-State')
  46. self.updateMixerState()
  47. self.log.debug('Launching Mixing-Pipeline')
  48. self.mixingPipeline.set_state(Gst.State.PLAYING)
  49. def updateMixerState(self):
  50. self.log.info('Updating Mixer-State')
  51. for idx, name in enumerate(self.names):
  52. volume = int(idx == self.selectedSource)
  53. self.log.debug('Setting Mixerpad %u to volume=%0.2f', idx, volume)
  54. mixerpad = self.mixingPipeline.get_by_name('mix').get_static_pad('sink_%u' % idx)
  55. mixerpad.set_property('volume', volume)
  56. def setAudioSource(self, source):
  57. self.selectedSource = source
  58. self.updateMixerState()
  59. def getAudioSource(self):
  60. return self.selectedSource
  61. def on_eos(self, bus, message):
  62. self.log.debug('Received End-of-Stream-Signal on Mixing-Pipeline')
  63. def on_error(self, bus, message):
  64. self.log.debug('Received Error-Signal on Mixing-Pipeline')
  65. (error, debug) = message.parse_error()
  66. self.log.debug('Error-Details: #%u: %s', error.code, debug)