AudioMixer.h 2.3 KB

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