aboutsummaryrefslogtreecommitdiff
path: root/voctocore/lib/audiomix.py
blob: 37376fdecbd106ab219bfd4104838a927f2064f1 (plain)
  1. import logging
  2. from gi.repository import Gst
  3. from enum import Enum
  4. from lib.config import Config
  5. from lib.clock import Clock
  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.mixingPipeline.use_clock(Clock)
  42. self.log.debug('Binding Error & End-of-Stream-Signal '
  43. 'on Mixing-Pipeline')
  44. self.mixingPipeline.bus.add_signal_watch()
  45. self.mixingPipeline.bus.connect("message::eos", self.on_eos)
  46. self.mixingPipeline.bus.connect("message::error", self.on_error)
  47. self.log.debug('Initializing Mixer-State')
  48. self.updateMixerState()
  49. self.log.debug('Launching Mixing-Pipeline')
  50. self.mixingPipeline.set_state(Gst.State.PLAYING)
  51. def updateMixerState(self):
  52. self.log.info('Updating Mixer-State')
  53. for idx, name in enumerate(self.names):
  54. volume = int(idx == self.selectedSource)
  55. self.log.debug('Setting Mixerpad %u to volume=%0.2f', idx, volume)
  56. mixerpad = (self.mixingPipeline.get_by_name('mix')
  57. .get_static_pad('sink_%u' % idx))
  58. mixerpad.set_property('volume', volume)
  59. def setAudioSource(self, source):
  60. self.selectedSource = source
  61. self.updateMixerState()
  62. def getAudioSource(self):
  63. return self.selectedSource
  64. def on_eos(self, bus, message):
  65. self.log.debug('Received End-of-Stream-Signal on Mixing-Pipeline')
  66. def on_error(self, bus, message):
  67. self.log.debug('Received Error-Signal on Mixing-Pipeline')
  68. (error, debug) = message.parse_error()
  69. self.log.debug('Error-Details: #%u: %s', error.code, debug)