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