AudioMixer.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include "AudioMixer.h"
  2. #include <mutex> // for scoped_lock
  3. using namespace bell;
  4. AudioMixer::AudioMixer() {}
  5. std::unique_ptr<StreamInfo> AudioMixer::process(
  6. std::unique_ptr<StreamInfo> info) {
  7. std::scoped_lock lock(this->accessMutex);
  8. if (info->numChannels != from) {
  9. throw std::runtime_error(
  10. "AudioMixer: Input channel count does not match configuration");
  11. }
  12. info->numChannels = to;
  13. for (auto& singleConf : mixerConfig) {
  14. if (singleConf.source.size() == 1) {
  15. if (singleConf.source[0] == singleConf.destination) {
  16. continue;
  17. }
  18. // Copy channel
  19. for (int i = 0; i < info->numSamples; i++) {
  20. info->data[singleConf.destination][i] =
  21. info->data[singleConf.source[0]][i];
  22. }
  23. } else {
  24. // Mix channels
  25. float sample = 0.0f;
  26. for (int i = 0; i < info->numSamples; i++) {
  27. sample = 0.0;
  28. for (auto& source : singleConf.source) {
  29. sample += info->data[source][i];
  30. }
  31. info->data[singleConf.destination][i] =
  32. sample / (float)singleConf.source.size();
  33. }
  34. }
  35. }
  36. return info;
  37. }