AudioMixer.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #pragma once
  2. #include <cJSON.h> // for cJSON_GetObjectItem, cJSON, cJSON_IsArray
  3. #include <stddef.h> // for NULL
  4. #include <algorithm> // for find
  5. #include <cstdint> // for uint8_t
  6. #include <memory> // for unique_ptr
  7. #include <stdexcept> // for invalid_argument
  8. #include <vector> // for vector
  9. #include "AudioTransform.h" // for AudioTransform
  10. #include "StreamInfo.h" // for StreamInfo
  11. namespace bell {
  12. class AudioMixer : public bell::AudioTransform {
  13. public:
  14. enum DownmixMode { DEFAULT };
  15. struct MixerConfig {
  16. std::vector<int> source;
  17. int destination;
  18. };
  19. AudioMixer();
  20. ~AudioMixer(){};
  21. // Amount of channels in the input
  22. int from;
  23. // Amount of channels in the output
  24. int to;
  25. // Configuration of each channels in the mixer
  26. std::vector<MixerConfig> mixerConfig;
  27. std::unique_ptr<StreamInfo> process(
  28. std::unique_ptr<StreamInfo> data) override;
  29. void reconfigure() override {}
  30. void fromJSON(cJSON* json) {
  31. cJSON* mappedChannels = cJSON_GetObjectItem(json, "mapped_channels");
  32. if (mappedChannels == NULL || !cJSON_IsArray(mappedChannels)) {
  33. throw std::invalid_argument("Mixer configuration invalid");
  34. }
  35. this->mixerConfig = std::vector<MixerConfig>();
  36. cJSON* iterator = NULL;
  37. cJSON_ArrayForEach(iterator, mappedChannels) {
  38. std::vector<int> sources(0);
  39. cJSON* iteratorNested = NULL;
  40. cJSON_ArrayForEach(iteratorNested,
  41. cJSON_GetObjectItem(iterator, "source")) {
  42. sources.push_back(iteratorNested->valueint);
  43. }
  44. int destination = cJSON_GetObjectItem(iterator, "destination")->valueint;
  45. this->mixerConfig.push_back(
  46. MixerConfig{.source = sources, .destination = destination});
  47. }
  48. std::vector<uint8_t> sources(0);
  49. for (auto& config : mixerConfig) {
  50. for (auto& source : config.source) {
  51. if (std::find(sources.begin(), sources.end(), source) ==
  52. sources.end()) {
  53. sources.push_back(source);
  54. }
  55. }
  56. }
  57. this->from = sources.size();
  58. this->to = mixerConfig.size();
  59. }
  60. };
  61. } // namespace bell