summaryrefslogtreecommitdiff
path: root/voctocore/lib/audiomix.py
blob: 6c1768ae006be9a395152216287247b77d340a91 (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 on Mixing-Pipeline')
  43. self.mixingPipeline.bus.add_signal_watch()
  44. self.mixingPipeline.bus.connect("message::eos", self.on_eos)
  45. self.mixingPipeline.bus.connect("message::error", self.on_error)
  46. self.log.debug('Initializing Mixer-State')
  47. self.updateMixerState()
  48. self.log.debug('Launching Mixing-Pipeline')
  49. self.mixingPipeline.set_state(Gst.State.PLAYING)
  50. def updateMixerState(self):
  51. self.log.info('Updating Mixer-State')
  52. for idx, name in enumerate(self.names):
  53. volume = int(idx == self.selectedSource)
  54. self.log.debug('Setting Mixerpad %u to volume=%0.2f', idx, volume)
  55. mixerpad = self.mixingPipeline.get_by_name('mix').get_static_pad('sink_%u' % idx)
  56. mixerpad.set_property('volume', volume)
  57. def setAudioSource(self, source):
  58. self.selectedSource = source
  59. self.updateMixerState()
  60. def getAudioSource(self):
  61. return self.selectedSource
  62. def on_eos(self, bus, message):
  63. self.log.debug('Received End-of-Stream-Signal on Mixing-Pipeline')
  64. def on_error(self, bus, message):
  65. self.log.debug('Received Error-Signal on Mixing-Pipeline')
  66. (error, debug) = message.parse_error()
  67. self.log.debug('Error-Details: #%u: %s', error.code, debug)