summaryrefslogtreecommitdiff
path: root/voctocore/lib/audiomix.py
blob: c37d2157fb921496a9c5acc622f5f46a2e7f6af1 (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. for idx, name in enumerate(self.names):
  27. pipeline += """
  28. interaudiosrc channel=audio_{name}_mixer !
  29. {caps} !
  30. mix.
  31. """.format(
  32. name=name,
  33. caps=self.caps
  34. )
  35. self.log.debug('Creating Mixing-Pipeline:\n%s', pipeline)
  36. self.mixingPipeline = Gst.parse_launch(pipeline)
  37. self.log.debug('Binding Error & End-of-Stream-Signal on Mixing-Pipeline')
  38. self.mixingPipeline.bus.add_signal_watch()
  39. self.mixingPipeline.bus.connect("message::eos", self.on_eos)
  40. self.mixingPipeline.bus.connect("message::error", self.on_error)
  41. self.log.debug('Initializing Mixer-State')
  42. self.updateMixerState()
  43. self.log.debug('Launching Mixing-Pipeline')
  44. self.mixingPipeline.set_state(Gst.State.PLAYING)
  45. def updateMixerState(self):
  46. self.log.info('Updating Mixer-State')
  47. for idx, name in enumerate(self.names):
  48. volume = int(idx == self.selectedSource)
  49. self.log.debug('Setting Mixerpad %u to volume=%0.2f', idx, volume)
  50. mixerpad = self.mixingPipeline.get_by_name('mix').get_static_pad('sink_%u' % idx)
  51. mixerpad.set_property('volume', volume)
  52. def setAudioSource(self, source):
  53. self.selectedSource = source
  54. self.updateMixerState()
  55. def on_eos(self, bus, message):
  56. self.log.debug('Received End-of-Stream-Signal on Mixing-Pipeline')
  57. def on_error(self, bus, message):
  58. self.log.debug('Received Error-Signal on Mixing-Pipeline')
  59. (error, debug) = message.parse_error()
  60. self.log.debug('Error-Details: #%u: %s', error.code, debug)